m_n_kappa.matrices.Matrix#

class m_n_kappa.matrices.Matrix(matrix)#

Bases: object

New in version 0.2.0.

implement basic functionality of matrix computation

Parameters:

matrix (list[list]) – Representation of the matrix. Each entry of the first outer list represents a row. Each entry of the inner list represents the value.

See also

Vector

class for vector-operations

Examples

A Matrix is initialized by passing a list of lists. The inner list represents a row, wheras the outer list represents the hole matrix.

>>> from m_n_kappa.matrices import Matrix
>>> A = Matrix([[0, 1, 2], [3, 4, 5]])
>>> A
Matrix([[0.0, 1.0, 2.0], [3.0, 4.0, 5.0]])

You can transpose the matrix A by using the transpose()- method.

>>> A.transpose()
Matrix([[0.0, 3.0], [1.0, 4.0], [2.0, 5.0]])

Several multiplication-operations are given using multiply_by(). For example multiplying a float.

>>> A.multiply_by(5)
Matrix([[0.0, 5.0, 10.0], [15.0, 20.0, 25.0]])

A Vector, what itself leads to a new Vector.

>>> A.multiply_by(Vector([1, 2, 3]))
Vector([8.0, 26.0])

Or another Matrix:

>>> B = Matrix([[1, 2], [0, 1], [4, 0]])
>>> A.multiply_by(B)
Matrix([[9.0, 1.0], [23.0, 10.0]])

Adding and subtracting are done by using the corresponding methods add() and subtract().

>>> A.add(Matrix([6, 7, 8], [9, 10, 11]))
Matrix([[6.0, 8.0, 10.0], [12.0, 14.0, 16.0]])
>>> A.subtract(Matrix([6, 7, 8], [9, 10, 11]))
Matrix([[-6.0, -6.0, -6.0], [-6.0, -6.0, -6.0]])

As an alternative you may of course use the corresponding operators (+ and -).

>>> A + Matrix([6, 7, 8], [9, 10, 11])
Matrix([[6.0, 8.0, 10.0], [12.0, 14.0, 16.0]])
>>> A - Matrix([6, 7, 8], [9, 10, 11])
Matrix([[-6.0, -6.0, -6.0], [-6.0, -6.0, -6.0]])

In case you want a specific row or a specific column given as a Vector you only have to use the row_vector()- or the column_vector()-method. The number passed to the method describes the wanted row or column.

>>> A.column_vector(1)
Vector([1.0, 4.0]]
>>> A.row_vector(1)
Vector([3.0, 4.0, 5.0])

Note

Please be aware that python starts counting at zero (0). Therefore, if you want the first row of A (=[0, 1, 2]) you need to pass 0 to row_vector(). Same applies to column_vector().

Furthermore, Matrix supports QR decomposition <https://en.wikipedia.org/wiki/QR_decomposition>, that determines from the given matrix an orthonormalized matrix and an upper triangular matrix using the Matrixorthonormal_triangular()- method.

>>> QR =  Matrix([[12, -51, 4], [6, 167, -68], [-4, 24, -41]])
>>> q, t = QR.orthonormal_triangular(algorithm='Modified Gram-Schmidt')

In this example q is the orthonormalized matrix.

>>> q
Matrix([[6 / 7, -69 / 175, -58 / 175],[3 / 7, 158 / 175, 6 / 175],[-2 / 7, 6 / 35, -33 / 35]])

and t the triangular matrix. >>> t Matrix([[14, 21, -14], [0, 175, -70], [0, 0, 35]])

Methods

add(matrix)

add this matrix to another matrix

append(vector)

Appends the given vector as column-vector to this vector

column(number)

give the column with the given number

column_vector(column_number)

vector of the given column in the matrix

diagonal()

build new matrix inheriting the diagonal from this matrix and put all other places to zero

entry(row_number, column_number)

get the of the matrix at the given position

multiply_by(multiplicant)

multiplies this matrix with the given multiplicant

orthonormal_triangular([algorithm])

Determine orthogonal Matrix \(Q\) and (upper) triangular Matrix \(R\) of this matrix

replace(row, column, value)

row(number)

give the row with the given number

row_vector(row_number)

vector of the given row in the matrix

subtract(matrix)

subtracts the given matrix form this matrix

transpose()

transposes the matrix and gives a new matrix

Attributes

column_number

gives number of columns of the matrix

matrix

shows the matrix in list-form

row_number

gives the number of rows of the matrix

add(matrix)#

add this matrix to another matrix

Parameters:

matrix (Matrix) – the Matrix this matrix is added to

Returns:

new Matrix where each entry is added to the corresponding one in the other matrix

Return type:

Matrix

Raises:

TypeError – if matrix is not of type m_n_kappa.matrices.Matrix

Examples

>>> from m_n_kappa.matrices import Matrix
>>> this_matrix = Matrix([[1, -3, 2], [1, 2, 7]])
>>> other_matrix = Matrix([[0, 3, 5], [2, 1, -1]])
>>> this_matrix.add(other_matrix)
Matrix([[1.0, 0.0, 7.0], [3.0, 3.0, 6.0]])
append(vector)#

Appends the given vector as column-vector to this vector

Parameters:

vector (Vector) – column-vector to append to this matrix

Returns:

this matrix with appended vector as additional column-vector

Return type:

Matrix

column(number)#

give the column with the given number

Parameters:

number (int) – number of the needed column

Returns:

values of the given column in a list from top to bottom

Return type:

list

column_vector(column_number)#

vector of the given column in the matrix

Parameters:

column_number (int) – number of the column

Returns:

vector of the column given by the number

Return type:

Vector

diagonal()#

build new matrix inheriting the diagonal from this matrix and put all other places to zero

entry(row_number, column_number)#

get the of the matrix at the given position

Parameters:
  • row_number (int) – number of row where the entry is located

  • column_number (int) – number of the columne where the entry is located

Returns:

value given at the desired position

Return type:

float

Examples

>>> from m_n_kappa.matrices import Matrix
>>> a_matrix = Matrix([[0, 1, 2], [3, 4, 5]])
>>> a_matrix.entry(1, 1)
4.0
multiply_by(multiplicant)#

multiplies this matrix with the given multiplicant

assumes that this matrix is on the left and the multiplicant is on the right of the multiplication-sign

Parameters:

multiplicant (Matrix | Vector | float) – Multiplicant to multiply this matrix with

Returns:

depending on the input the multiplication results in Matrix or a Vector

Return type:

Matrix | Vector

Raises:

ValueError – in case multiplicant is either of type Matrix, nor Matrix nor float

Examples

>>> from m_n_kappa.matrices import Matrix
>>> this_matrix = Matrix([[3, 2, 1], [1, 0, 2]])

Multiply another Matrix:

>>> other_matrix = Matrix([[1, 2], [0, 1], [4, 0]])
>>> this_matrix.multiply_by(other_matrix)
Matrix([[7.0, 8.0], [9.0, 2.0]])

Multiply a vector:

>>> a_vector = Vector([1, 2, 3])
>>> this_matrix.multiply_by(a_vector)
Vector([1.0, 26.0])

Multiply a number (=scalar):

>>> this_matrix.multiply_by(5)
Matrix([[5.0, -15.0, 10.0], [5.0, 10.0, 35.0]])
orthonormal_triangular(algorithm='Givens-rotation')#

Determine orthogonal Matrix \(Q\) and (upper) triangular Matrix \(R\) of this matrix

Parameters:

algorithm (str) –

Algorithm for computation of \(Q\) and \(R\). Currently following algorithms are supported:

Returns:

First entry is the orthogonal matrix of this matrix. Second entry is the (upper) triangluar Matrix of this matrix.

Return type:

tuple[Matrix, Matrix]

row(number)#

give the row with the given number

Parameters:

number (int) – number of the needed row

Returns:

values of the needed row

Return type:

list

row_vector(row_number)#

vector of the given row in the matrix

Parameters:

row_number (int) – number of the row

Returns:

vector of the row given by the number

Return type:

Vector

subtract(matrix)#

subtracts the given matrix form this matrix

Parameters:

matrix (Matrix) – matrix that is subtracted from this one

Returns:

New Matrix where each value of the given matrix is subtracted from this one

Return type:

Matrix

Examples

>>> from m_n_kappa.matrices import Matrix
>>> this_matrix = Matrix([[1, -3, 2], [1, 2, 7]])
>>> other_matrix = Matrix([[0, 3, 5], [2, 1, -1]])
>>> this_matrix.subtract(other_matrix)
Matrix([[1.0, -6.0, -3.0], [-1.0, 1.0, 8.0]])
transpose()#

transposes the matrix and gives a new matrix

Examples

>>> from m_n_kappa.matrices import Matrix
>>> a_matrix = Matrix([[0, 1, 2], [3, 4, 5]])
>>> a_matrix.transpose()
Matrix([[0.0, 3.0], [1.0, 4.0], [2.0, 5.0]])
property column_number: int#

gives number of columns of the matrix

property matrix: list[list]#

shows the matrix in list-form

property row_number: int#

gives the number of rows of the matrix