m_n_kappa.matrices.Jacobian#

class m_n_kappa.matrices.Jacobian(f_i, x_i, f_xi=None, h=1e-08, differentiation_type='forward')#

Bases: Matrix

Jacobian Matrix

New in version 0.2.0.

computes numerically and stores the partial derivatives of the system of functions

Parameters:
  • f_i (list[Callable]) – functions taking the x_i as argument

  • x_i (Vector | list[float]) – point where the Jacobian-Matrix is to be determined

  • f_xi (Vector) – Formulas f_i computed for the case x_i (Default: None). In case f_xi = None it will be computed.

  • h (float) – interval length for the numerical differentiation (Default: 0.0000001)

  • differentiation_type (str) –

    Type of differentation. Possible values are:

    • 'forward'``|’fwd’`` - see forward_difference_quotient() (Default)

    • 'backward'``|’bwd’`` - see backward_difference_quotient()

Examples

A system of equations \(f\) with variables \(x\), \(y\), \(z\) may look as given as follows.

\[\begin{split}f(x, y, z) = \begin{pmatrix} x^2 + y^2 + z \cdot \sin(x) \\ z^2 + z \cdot \sin(y) \end{pmatrix}\end{split}\]

In python the system of equations may be arranged as follows

>>> import math
>>> def f_1(variables : list):
...    x, y, z = variables
...    return x**2 + y**2 + z * math.sin(x)
>>> def f_2(variables: list):
...    x, y, z = variables
...    return z**2 + z * math.sin(y)
>>> functions = [f_1, f_2]

The Jacobian Matrix may therefore be determined at following point \(x = 1\), \(y = 1\) and \(z = 1\). This point is given as a vector.

>>> point = Vector([1, 1, 1])

The Jacobian Matrix is computed by passing the functions and the point to Jacobian.

>>> from m_n_kappa.matrices import Jacobian
>>> round(Jacobian(functions, point), 7)
Matrix([[2.5403023, 2, 0.8414709], [0, 0.5403023, 2.8414709]])

Due to the finite precision of numerical computetions the above given example is rounded to the maximum precision of 7 places.

Methods

add(matrix)

add this matrix to another matrix

append(vector)

Appends the given vector as column-vector to this vector

backward_difference_quotient(function_index, ...)

computation of the backward difference quotient for the given functions f_i at the point x_i

center_difference_quotient(function_index, ...)

computation of the backward difference quotient for the given functions f_i at the point x_i

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

forward_difference_quotient(function_index, ...)

computation of the forward difference quotient for the given functions f_i at the point x_i

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

differentiation_type

type of differentiation

f_i

methods to be differentiated

f_xi

results-vector at \(x_{i}\)

h

interval-length for the numerical differentiation

matrix

shows the matrix in list-form

row_number

gives the number of rows of the matrix

x_i

vector of the point where the Jacobian-Matrix is to be determined

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

backward_difference_quotient(function_index, point_index)#

computation of the backward difference quotient for the given functions f_i at the point x_i

Parameters:
Returns:

difference-quotient at m_n_kappa.matrices.Jacobian.x_i

Return type:

float

Notes

The following Formula describes the forward difference quotient.

\[\frac{f(x) - f(x - h)}{h}\]

where \(h\) is the interval length, \(f(x)\) describes the function the difference quotient is to be determined.

The difference quotient assumes that the derivative of the function \(f(x)\) is computed when \(h\) is infinitive small.

\[f'(x) = \lim_{h \rightarrow 0} \frac{f(x) - f(x - h)}{h}\]

See also

m_n_kappa.matrices.Jacobian.forward_difference_quotient()

similar, but with a step forward

center_difference_quotient(function_index, point_index)#

computation of the backward difference quotient for the given functions f_i at the point x_i

Parameters:
Returns:

difference-quotient at m_n_kappa.matrices.Jacobian.x_i

Return type:

float

Notes

The following Formula describes the forward difference quotient.

\[\frac{f(x + h) - f(x - h)}{2h}\]

where \(h\) is the interval length, \(f(x)\) describes the function the difference quotient that is to be determined.

The difference quotient assumes that the derivative of the function \(f(x)\) is computed when \(h\) is infinitive small.

\[f'(x) = \lim_{h \rightarrow 0} \frac{f(x + h) - f(x - h)}{2h}\]

See also

m_n_kappa.matrices.Jacobian.forward_difference_quotient()

similar, but only with a step forward

m_n_kappa.matrices.Jacobian.forward_difference_quotient()

similar, but only with a step backward

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
forward_difference_quotient(function_index, point_index)#

computation of the forward difference quotient for the given functions f_i at the point x_i

Parameters:
Returns:

difference-quotient at m_n_kappa.matrices.Jacobian.x_i

Return type:

float

Notes

The following Formula describes the forward difference quotient.

\[\frac{f(x + h) - f(x)}{h}\]

where \(h\) is the interval length, \(f(x)\) describes the function the difference quotient is to be determined.

The difference quotient assumes that the derivative of the function \(f(x)\) is computed when \(h\) is infinitive small.

\[f'(x) = \lim_{h \rightarrow 0} \frac{f(x + h) - f(x)}{h}\]

See also

m_n_kappa.matrices.Jacobian.backward_difference_quotient()

similar, but with a step backward

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 differentiation_type: str#

type of differentiation

property f_i: list[Callable]#

methods to be differentiated

property f_xi: Vector#

results-vector at \(x_{i}\)

property h: float#

interval-length for the numerical differentiation

property matrix: list[list]#

shows the matrix in list-form

property row_number: int#

gives the number of rows of the matrix

property x_i: Vector#

vector of the point where the Jacobian-Matrix is to be determined