m_n_kappa.matrices.Vector#
- class m_n_kappa.matrices.Vector(entries)#
Bases:
object
New in version 0.2.0.
- Parameters:
entries (list) – entries of the vector
See also
Matrix
matrix with basic functionality of matrices-computation
Examples
An instance of
Vector
is easily initialized by passing a list offloat
>>> from m_n_kappa.matrices import Vector >>> x = Vector([0, 1]) >>> x Vector([0.0, 1.0])
Adding another
Vector
is done by using the plus (+
) operator. Please be aware that both vectors need to be of the same length.>>> y = Vector([2, 3]) >>> x + y Vector([2.0, 4.0])
Alternatively you may use the
add()
-method.>>> x.add(y) Vector([2.0, 4.0])
Subtracting an
Vector
is also done like in two ways. By the Minus-Operator (-
).>>> x - y Vector([-2.0, -2.0])
Or by using the
subtract()
-method.>>> x.subtract(y) Vector([-2.0, -2.0])
If you want to compute the scalar-product (also called dot-product, \(x^{T} y\)) you may use the
scalar_product()
-method.>>> x.scalar_product(y) 3.0
Multiplying a scalar is done using the
multiply_scalar()
-method.>>> x.multiply_scalar(2.0) Vector([0.0, 2.0])
The
tensor_product()
-method therefore creates aMatrix
.>>> x.tensor_product(y) Matrix([[0.0, 0.0], [2.0, 3.0]])
If you want to modify a value of your Vector you may use the
replace()
-methodinplace
.>>> x.replace(number=0, value=2.0, inplace=True) >>> x Vector([2.0, 2.0])
Or not
inplace
getting anotherVector
in return.>>> x.replace(number=0, value=2.0) Vector([2.0, 2.0])
Using a
Vector
in afor
-loop is easily done as follows.>>> for entry in x: ... print(entry) 0 2
Comparing two vectors is also easy.
>>> x == y False
Whereas
>>> x == x True
Methods
add
(vector)Add this vector to another
append
(vector)Append another vector to this vector
length
()sum of the entries of this vector
multiply_scalar
(scalar)multiplies a scalar to this vector
norm
()Euclidian norm of the vector
replace
(number, value[, inplace])replace the entry of the given
number
withvalue
scalar_product
(vector)compute the scalar-product of this vector with another vector
subtract
(vector)Subtract
vector
from this vectortensor_product
(vector)compute the tensor-product of this vector with another vector
Attributes
entries of the vector
- add(vector)#
Add this vector to another
- append(vector)#
Append another vector to this vector
Both vectors need to have the same length
- Parameters:
vector (Vector) – vector to append to this vector
- Returns:
matrix from this and the appended vector
- Return type:
Examples
>>> from m_n_kappa.matrices import Vector >>> vector_1 = Vector([0, 1]) >>> vector_2 = Vector([2, 3]) >>> vector_1.append(vector_2) Matrix([[0.0, 2.0], [1.0, 3.0]])
- length()#
sum of the entries of this vector
- Return type:
float
- multiply_scalar(scalar)#
multiplies a scalar to this vector
- Parameters:
scalar (float) – number to multiply with this vector
- Returns:
vector where each entry is multiplied with the given number / scalar
- Return type:
- norm()#
Euclidian norm of the vector
- Return type:
float
- replace(number, value, inplace=False)#
replace the entry of the given
number
withvalue
- Parameters:
number (int) – entry-number to replace
value (float) – replace-value
inplace (bool) – If
True
changes value in place and does not return a value. IfFalse
returns this Vector withvalue
at the desired position (number
, Default:False
).
- Return type:
None | Vector
- scalar_product(vector)#
compute the scalar-product of this vector with another vector
- Parameters:
vector (Vector) – other vector to compute the tensor-product with
- Returns:
scalar-product of this vector with another vector
- Return type:
float
Examples
>>> from m_n_kappa.matrices import Vector >>> vector_1 = Vector([0, 1]) >>> vector_2 = Vector([2, 3]) >>> vector_1.scalar_product(vector_2) 3.0
- subtract(vector)#
Subtract
vector
from this vector
- tensor_product(vector)#
compute the tensor-product of this vector with another vector
- property entries: list#
entries of the vector