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 of float

>>> 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 a Matrix.

>>> 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()-method inplace.

>>> x.replace(number=0, value=2.0, inplace=True)
>>> x
Vector([2.0, 2.0])

Or not inplace getting another Vector in return.

>>> x.replace(number=0, value=2.0)
Vector([2.0, 2.0])

Using a Vector in a for-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 with value

scalar_product(vector)

compute the scalar-product of this vector with another vector

subtract(vector)

Subtract vector from this vector

tensor_product(vector)

compute the tensor-product of this vector with another vector

Attributes

entries

entries of the vector

add(vector)#

Add this vector to another

Parameters:

vector (Vector) – other vector

Returns:

Sum of both vector (this one and the other one)

Return type:

Vector

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:

Matrix

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:

Vector

norm()#

Euclidian norm of the vector

Return type:

float

replace(number, value, inplace=False)#

replace the entry of the given number with value

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. If False returns this Vector with value 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

Parameters:

vector (Vector) – vector to subtract from this vector

Returns:

result of the subtraction

Return type:

Vector

tensor_product(vector)#

compute the tensor-product of this vector with another vector

Parameters:

vector (Vector) – other vector to compute the tensor-product with

Returns:

m x n - matrix. Where m is the number of rows that is equal to the number of entries. And n is the number of columns that is equal to the number of entries of the vector.

Return type:

Matrix

property entries: list#

entries of the vector