Matrix decompositions

Several matrix decompositions are supported. They are available in matrix.decompositions:

LL decomposition

class matrix.decompositions.LL_Decomposition(L=None, p=None)[source]

Bases: matrix.decompositions.DecompositionBase

A matrix decomposition where \(LL^H\) is the decomposed (permuted) matrix.

L is a lower triangle matrix with ones on the diagonal. This decomposition is also called Cholesky decomposition.

Parameters:
  • L (numpy.ndarray or scipy.sparse.spmatrix) – The matrix L of the decomposition. optional, If it is not set yet, it must be set later.
  • p (numpy.ndarray) – The permutation vector used for the decomposition. This decomposition is of A[p[:, np.newaxis], p[np.newaxis, :]] where A is a matrix. optional, default: no permutation
L

The matrix L of the decomposition.

Type:numpy.matrix or scipy.sparse.spmatrix
P

The permutation matrix. P @ A @ P.T is the matrix A permuted by the permutation of the decomposition

Type:scipy.sparse.dok_matrix
append_block_decomposition(dec)[source]

Makes a new decomposition where this decompsition and the passed one are appended.

Parameters:dec (DecompositionBase) – The decomposition that should be appended to this decomposition.
Returns:dec – A new decomposition where this decompsition and the passed one are appended. This desomposition represents the first block and the passed one the second block.
Return type:DecompositionBase
as_LDL_Decomposition()[source]
as_any_type(*type_strs, copy=False)

Converts this decomposition to any of the passed types.

Parameters:
  • *type_strs (str) – The decomposition types to any of them this this decomposition is converted.
  • copy (bool) – Whether the data of this decomposition should always be copied or only if needed.
Returns:

If the type of this decomposition is not in type_strs, a decomposition of type type_str[0] is returned which represents the same decomposed matrix as this decomposition. Otherwise this decomposition or a copy of it is returned, depending on copy.

Return type:

matrix.decompositions.DecompositionBase

as_same_type(dec, copy=False)

Converts the passed decompositions to the same type as this decomposition.

Parameters:
  • doc (DecompositionBase) – The decomposition that should be converted.
  • copy (bool) – Whether the data of the decomposition that sould be converted should always be copied or only if needed.
Returns:

If the type of the passed decomposition is not same type as this decomposition, a decomposition of this type is returned which represents the same decomposed matrix as the passed decomposition. Otherwise this decomposition or a copy of it is returned, depending on copy.

Return type:

matrix.decompositions.DecompositionBase

as_type(type_str, copy=False)[source]

Converts this decomposition to passed type.

Parameters:
  • type_str (str) – The decomposition type to which this decomposition is converted.
  • copy (bool) – Whether the data of this decomposition should always be copied or only if needed.
Returns:

If the type of this decomposition is not type_str, a decomposition of type type_str is returned which represents the same decomposed matrix as this decomposition. Otherwise this decomposition or a copy of it is returned, depending on copy.

Return type:

matrix.decompositions.DecompositionBase

check_finite(check_finite=True)

Check if this is a decomposition representing a finite matrix.

Parameters:check_finite (bool) – Whether to perform this check. default: True
Raises:matrix.errors.DecompositionNotFiniteError – If this is a decomposition representing a non-finite matrix.
check_invertible()

Check if this is a decomposition representing an invertible matrix.

Raises:matrix.errors.DecompositionSingularError – If this is a decomposition representing a singular matrix.
composed_matrix

The composed matrix represented by this decomposition.

Type:numpy.matrix or scipy.sparse.spmatrix
copy()

Copies this decomposition.

Returns:A copy of this decomposition.
Return type:matrix.decompositions.DecompositionBase
inverse_matrix_both_sides_multiplication(x, y=None, dtype=None)[source]

Calculates the both sides (matrix-matrix or matrix-vector) product y.H @ B @ x, where B is the mattrix inverse of the composed matrix represented by this decomposition.

Parameters:
  • x (numpy.ndarray or scipy.sparse.spmatrix) – Vector or matrix in the product y.H @ B @ x. It must hold self.n == x.shape[0].
  • y (numpy.ndarray or scipy.sparse.spmatrix) – Vector or matrix in the product y.H @ B @ x. It must hold self.n == y.shape[0]. optional, default: If y is not passed, x is used as y.
  • dtype (numpy.dtype) – Type to use in computation. optional, default: Determined by the method.
Returns:

The result of y.H @ A @ x.

Return type:

numpy.ndarray or scipy.sparse.spmatrix

Raises:

matrix.errors.DecompositionSingularError – If this is a decomposition representing a singular matrix.

inverse_matrix_right_side_multiplication(x, dtype=None)[source]

Calculates the right side (matrix-matrix or matrix-vector) product B @ x, where B is the matrix inverse of the composed matrix represented by this decomposition.

Parameters:
  • x (numpy.ndarray or scipy.sparse.spmatrix) – Vector or matrix in the product in the matrix-matrix or matrix-vector B @ x. It must hold self.n == x.shape[0].
  • dtype (numpy.dtype) – Type to use in computation. optional, default: Determined by the method.
Returns:

The result of B @ x.

Return type:

numpy.ndarray or scipy.sparse.spmatrix

Raises:

matrix.errors.DecompositionSingularError – If this is a decomposition representing a singular matrix.

is_almost_equal(other, rtol=0.0001, atol=1e-06)[source]

Whether this decomposition is close to passed decomposition.

Parameters:
  • other (str) – The decomposition which to compare to this decomposition.
  • rtol (float) – The relative tolerance parameter.
  • atol (float) – The absolute tolerance parameter.
Returns:

Whether this decomposition is close to passed decomposition.

Return type:

bool

is_equal(other)[source]

Whether this decomposition is equal to passed decomposition.

Parameters:other (str) – The decomposition which to compare to this decomposition.
Returns:Whether this decomposition is equal to passed decomposition.
Return type:bool
is_finite()[source]

Returns whether this is a decomposition representing a finite matrix.

Returns:Whether this is a decomposition representing a finite matrix.
Return type:bool
is_invertible()

Returns whether this is a decomposition representing an invertible matrix.

Returns:Whether this is a decomposition representing an invertible matrix.
Return type:bool
is_permuted

Whether this is a decompositon with permutation.

Type:bool
is_positive_definite()[source]

Returns whether this is a decomposition of a positive definite matrix.

Returns:Whether this is a decomposition of a positive definite matrix.
Return type:bool
is_positive_semidefinite()[source]

Returns whether this is a decomposition of a positive semi-definite matrix.

Returns:Whether this is a decomposition of a positive semi-definite matrix.
Return type:bool
is_singular()[source]

Returns whether this is a decomposition representing a singular matrix.

Returns:Whether this is a decomposition representing a singular matrix.
Return type:bool
is_sparse()[source]

Returns whether this is a decomposition of a sparse matrix.

Returns:Whether this is a decomposition of a sparse matrix.
Return type:bool
is_type(type_str)

Whether this decomposition is of the passed type.

Parameters:type_str (str) – The decomposition type according to which is checked.
Returns:Whether this is a decomposition of the passed type.
Return type:bool
load(filename)

Loads a decomposition of this type.

Parameters:

filename (str) – Where the decomposition is saved.

Raises:
  • FileNotFoundError – If the files are not found in the passed directory.
  • DecompositionInvalidDecompositionTypeFile – If the files contains another decomposition type.
matrix_both_sides_multiplication(x, y=None, dtype=None)[source]

Calculates the both sides (matrix-matrix or matrix-vector) product y.H @ A @ x, where A is the composed matrix represented by this decomposition.

Parameters:
  • x (numpy.ndarray or scipy.sparse.spmatrix) – Vector or matrix in the product y.H @ A @ x. It must hold self.n == x.shape[0].
  • y (numpy.ndarray or scipy.sparse.spmatrix) – Vector or matrix in the product y.H @ A @ x. It must hold self.n == y.shape[0]. optional, default: If y is not passed, x is used as y.
  • dtype (numpy.dtype) – Type to use in computation. optional, default: Determined by the method.
Returns:

The result of y.H @ A @ x.

Return type:

numpy.ndarray or scipy.sparse.spmatrix

matrix_right_side_multiplication(x, dtype=None)[source]

Calculates the right side (matrix-matrix or matrix-vector) product A @ x, where A is the composed matrix represented by this decomposition.

Parameters:
  • x (numpy.ndarray or scipy.sparse.spmatrix) – Vector or matrix in the product in the matrix-matrix or matrix-vector A @ x. It must hold self.n == x.shape[0].
  • dtype (numpy.dtype) – Type to use in computation. optional, default: Determined by the method.
Returns:

The result of A @ x.

Return type:

numpy.ndarray or scipy.sparse.spmatrix

n

The dimension of the squared decomposed matrix.

Type:int
p

The permutation vector. A[p[:, np.newaxis], p[np.newaxis, :]] is the matrix A permuted by the permutation of the decomposition

Type:numpy.ndarray
p_inverse

The permutation vector that undoes the permutation.

Type:numpy.ndarray
permute_matrix(A)

Permutes a matrix by the permutation of the decomposition.

Parameters:A (numpy.ndarray or scipy.sparse.spmatrix) – The matrix that should be permuted.
Returns:The matrix A permuted by the permutation of the decomposition.
Return type:numpy.ndarray or scipy.sparse.spmatrix
save(filename)

Saves this decomposition.

Parameters:filename (str) – Where this decomposition should be saved.
solve(b, dtype=None)

Solves the equation A x = b regarding x, where A is the composed matrix represented by this decomposition.

Parameters:
  • b (numpy.ndarray or scipy.sparse.spmatrix) – Right-hand side vector or matrix in equation A x = b. It must hold self.n == b.shape[0].
  • dtype (numpy.dtype) – Type to use in computation. optional, default: Determined by the method.
Returns:

An x so that A x = b. The shape of x matches the shape of b.

Return type:

numpy.ndarray or scipy.sparse.spmatrix

Raises:

matrix.errors.DecompositionSingularError – If this is a decomposition representing a singular matrix.

type_str = 'LL'

The type of this decomposition represented as string.

Type:str
unpermute_matrix(A)

Unpermutes a matrix permuted by the permutation of the decomposition.

Parameters:A (numpy.ndarray or scipy.sparse.spmatrix) – The matrix that should be unpermuted.
Returns:The matrix A unpermuted by the permutation of the decomposition.
Return type:numpy.ndarray or scipy.sparse.spmatrix

LDL decomposition

class matrix.decompositions.LDL_Decomposition(L=None, d=None, p=None)[source]

Bases: matrix.decompositions.DecompositionBase

A matrix decomposition where \(LDL^H\) is the decomposed (permuted) matrix.

L is a lower triangle matrix with ones on the diagonal. D is a diagonal matrix. Only the diagonal values of D are stored.

Parameters:
  • L (numpy.ndarray or scipy.sparse.spmatrix) – The matrix L of the decomposition. optional, If it is not set yet, it must be set later.
  • d (numpy.ndarray) – The vector of the diagonal components of D of the decompositon. optional, If it is not set yet, it must be set later.
  • p (numpy.ndarray) – The permutation vector used for the decomposition. This decomposition is of A[p[:, np.newaxis], p[np.newaxis, :]] where A is a matrix. optional, default: no permutation
D

The permutation matrix.

Type:scipy.sparse.dia_matrix
L

The matrix L of the decomposition.

Type:numpy.matrix or scipy.sparse.spmatrix
LD

A matrix whose diagonal values are the diagonal values of D and whose off-diagonal values are those of L.

Type:numpy.matrix or scipy.sparse.spmatrix
P

The permutation matrix. P @ A @ P.T is the matrix A permuted by the permutation of the decomposition

Type:scipy.sparse.dok_matrix
append_block_decomposition(dec)[source]

Makes a new decomposition where this decompsition and the passed one are appended.

Parameters:dec (DecompositionBase) – The decomposition that should be appended to this decomposition.
Returns:dec – A new decomposition where this decompsition and the passed one are appended. This desomposition represents the first block and the passed one the second block.
Return type:DecompositionBase
as_LDL_DecompositionCompressed()[source]
as_LL_Decomposition()[source]
as_any_type(*type_strs, copy=False)

Converts this decomposition to any of the passed types.

Parameters:
  • *type_strs (str) – The decomposition types to any of them this this decomposition is converted.
  • copy (bool) – Whether the data of this decomposition should always be copied or only if needed.
Returns:

If the type of this decomposition is not in type_strs, a decomposition of type type_str[0] is returned which represents the same decomposed matrix as this decomposition. Otherwise this decomposition or a copy of it is returned, depending on copy.

Return type:

matrix.decompositions.DecompositionBase

as_same_type(dec, copy=False)

Converts the passed decompositions to the same type as this decomposition.

Parameters:
  • doc (DecompositionBase) – The decomposition that should be converted.
  • copy (bool) – Whether the data of the decomposition that sould be converted should always be copied or only if needed.
Returns:

If the type of the passed decomposition is not same type as this decomposition, a decomposition of this type is returned which represents the same decomposed matrix as the passed decomposition. Otherwise this decomposition or a copy of it is returned, depending on copy.

Return type:

matrix.decompositions.DecompositionBase

as_type(type_str, copy=False)[source]

Converts this decomposition to passed type.

Parameters:
  • type_str (str) – The decomposition type to which this decomposition is converted.
  • copy (bool) – Whether the data of this decomposition should always be copied or only if needed.
Returns:

If the type of this decomposition is not type_str, a decomposition of type type_str is returned which represents the same decomposed matrix as this decomposition. Otherwise this decomposition or a copy of it is returned, depending on copy.

Return type:

matrix.decompositions.DecompositionBase

check_finite(check_finite=True)

Check if this is a decomposition representing a finite matrix.

Parameters:check_finite (bool) – Whether to perform this check. default: True
Raises:matrix.errors.DecompositionNotFiniteError – If this is a decomposition representing a non-finite matrix.
check_invertible()

Check if this is a decomposition representing an invertible matrix.

Raises:matrix.errors.DecompositionSingularError – If this is a decomposition representing a singular matrix.
composed_matrix

The composed matrix represented by this decomposition.

Type:numpy.matrix or scipy.sparse.spmatrix
copy()

Copies this decomposition.

Returns:A copy of this decomposition.
Return type:matrix.decompositions.DecompositionBase
d

The diagonal vector of the matrix D of the decomposition.

Type:numpy.ndarray
inverse_matrix_both_sides_multiplication(x, y=None, dtype=None)[source]

Calculates the both sides (matrix-matrix or matrix-vector) product y.H @ B @ x, where B is the mattrix inverse of the composed matrix represented by this decomposition.

Parameters:
  • x (numpy.ndarray or scipy.sparse.spmatrix) – Vector or matrix in the product y.H @ B @ x. It must hold self.n == x.shape[0].
  • y (numpy.ndarray or scipy.sparse.spmatrix) – Vector or matrix in the product y.H @ B @ x. It must hold self.n == y.shape[0]. optional, default: If y is not passed, x is used as y.
  • dtype (numpy.dtype) – Type to use in computation. optional, default: Determined by the method.
Returns:

The result of y.H @ A @ x.

Return type:

numpy.ndarray or scipy.sparse.spmatrix

Raises:

matrix.errors.DecompositionSingularError – If this is a decomposition representing a singular matrix.

inverse_matrix_right_side_multiplication(x, dtype=None)[source]

Calculates the right side (matrix-matrix or matrix-vector) product B @ x, where B is the matrix inverse of the composed matrix represented by this decomposition.

Parameters:
  • x (numpy.ndarray or scipy.sparse.spmatrix) – Vector or matrix in the product in the matrix-matrix or matrix-vector B @ x. It must hold self.n == x.shape[0].
  • dtype (numpy.dtype) – Type to use in computation. optional, default: Determined by the method.
Returns:

The result of B @ x.

Return type:

numpy.ndarray or scipy.sparse.spmatrix

Raises:

matrix.errors.DecompositionSingularError – If this is a decomposition representing a singular matrix.

is_almost_equal(other, rtol=0.0001, atol=1e-06)[source]

Whether this decomposition is close to passed decomposition.

Parameters:
  • other (str) – The decomposition which to compare to this decomposition.
  • rtol (float) – The relative tolerance parameter.
  • atol (float) – The absolute tolerance parameter.
Returns:

Whether this decomposition is close to passed decomposition.

Return type:

bool

is_equal(other)[source]

Whether this decomposition is equal to passed decomposition.

Parameters:other (str) – The decomposition which to compare to this decomposition.
Returns:Whether this decomposition is equal to passed decomposition.
Return type:bool
is_finite()[source]

Returns whether this is a decomposition representing a finite matrix.

Returns:Whether this is a decomposition representing a finite matrix.
Return type:bool
is_invertible()

Returns whether this is a decomposition representing an invertible matrix.

Returns:Whether this is a decomposition representing an invertible matrix.
Return type:bool
is_permuted

Whether this is a decompositon with permutation.

Type:bool
is_positive_definite()[source]

Returns whether this is a decomposition of a positive definite matrix.

Returns:Whether this is a decomposition of a positive definite matrix.
Return type:bool
is_positive_semidefinite()[source]

Returns whether this is a decomposition of a positive semi-definite matrix.

Returns:Whether this is a decomposition of a positive semi-definite matrix.
Return type:bool
is_singular()[source]

Returns whether this is a decomposition representing a singular matrix.

Returns:Whether this is a decomposition representing a singular matrix.
Return type:bool
is_sparse()[source]

Returns whether this is a decomposition of a sparse matrix.

Returns:Whether this is a decomposition of a sparse matrix.
Return type:bool
is_type(type_str)

Whether this decomposition is of the passed type.

Parameters:type_str (str) – The decomposition type according to which is checked.
Returns:Whether this is a decomposition of the passed type.
Return type:bool
load(filename)

Loads a decomposition of this type.

Parameters:

filename (str) – Where the decomposition is saved.

Raises:
  • FileNotFoundError – If the files are not found in the passed directory.
  • DecompositionInvalidDecompositionTypeFile – If the files contains another decomposition type.
matrix_both_sides_multiplication(x, y=None, dtype=None)[source]

Calculates the both sides (matrix-matrix or matrix-vector) product y.H @ A @ x, where A is the composed matrix represented by this decomposition.

Parameters:
  • x (numpy.ndarray or scipy.sparse.spmatrix) – Vector or matrix in the product y.H @ A @ x. It must hold self.n == x.shape[0].
  • y (numpy.ndarray or scipy.sparse.spmatrix) – Vector or matrix in the product y.H @ A @ x. It must hold self.n == y.shape[0]. optional, default: If y is not passed, x is used as y.
  • dtype (numpy.dtype) – Type to use in computation. optional, default: Determined by the method.
Returns:

The result of y.H @ A @ x.

Return type:

numpy.ndarray or scipy.sparse.spmatrix

matrix_right_side_multiplication(x, dtype=None)[source]

Calculates the right side (matrix-matrix or matrix-vector) product A @ x, where A is the composed matrix represented by this decomposition.

Parameters:
  • x (numpy.ndarray or scipy.sparse.spmatrix) – Vector or matrix in the product in the matrix-matrix or matrix-vector A @ x. It must hold self.n == x.shape[0].
  • dtype (numpy.dtype) – Type to use in computation. optional, default: Determined by the method.
Returns:

The result of A @ x.

Return type:

numpy.ndarray or scipy.sparse.spmatrix

n

The dimension of the squared decomposed matrix.

Type:int
p

The permutation vector. A[p[:, np.newaxis], p[np.newaxis, :]] is the matrix A permuted by the permutation of the decomposition

Type:numpy.ndarray
p_inverse

The permutation vector that undoes the permutation.

Type:numpy.ndarray
permute_matrix(A)

Permutes a matrix by the permutation of the decomposition.

Parameters:A (numpy.ndarray or scipy.sparse.spmatrix) – The matrix that should be permuted.
Returns:The matrix A permuted by the permutation of the decomposition.
Return type:numpy.ndarray or scipy.sparse.spmatrix
save(filename)

Saves this decomposition.

Parameters:filename (str) – Where this decomposition should be saved.
solve(b, dtype=None)

Solves the equation A x = b regarding x, where A is the composed matrix represented by this decomposition.

Parameters:
  • b (numpy.ndarray or scipy.sparse.spmatrix) – Right-hand side vector or matrix in equation A x = b. It must hold self.n == b.shape[0].
  • dtype (numpy.dtype) – Type to use in computation. optional, default: Determined by the method.
Returns:

An x so that A x = b. The shape of x matches the shape of b.

Return type:

numpy.ndarray or scipy.sparse.spmatrix

Raises:

matrix.errors.DecompositionSingularError – If this is a decomposition representing a singular matrix.

type_str = 'LDL'

The type of this decomposition represented as string.

Type:str
unpermute_matrix(A)

Unpermutes a matrix permuted by the permutation of the decomposition.

Parameters:A (numpy.ndarray or scipy.sparse.spmatrix) – The matrix that should be unpermuted.
Returns:The matrix A unpermuted by the permutation of the decomposition.
Return type:numpy.ndarray or scipy.sparse.spmatrix

LDL decomposition compressed

class matrix.decompositions.LDL_DecompositionCompressed(LD=None, p=None)[source]

Bases: matrix.decompositions.DecompositionBase

A matrix decomposition where \(LDL^H\) is the decomposed (permuted) matrix.

L is a lower triangle matrix with ones on the diagonal. D is a diagonal matrix. L and D are stored in one matrix whose diagonal values are the diagonal values of D and whose off-diagonal values are those of L.

Parameters:
  • LD (numpy.ndarray or scipy.sparse.spmatrix) – A matrix whose diagonal values are the diagonal values of D and whose off-diagonal values are those of L. optional, If it is not set yet, it must be set later.
  • p (numpy.ndarray) – The permutation vector used for the decomposition. This decomposition is of A[p[:, np.newaxis], p[np.newaxis, :]] where A is a matrix. optional, default: no permutation
D

The permutation matrix.

Type:scipy.sparse.dia_matrix
L

The matrix L of the decomposition.

Type:numpy.matrix or scipy.sparse.spmatrix
LD

A matrix whose diagonal values are the diagonal values of D and whose off-diagonal values are those of L.

Type:numpy.matrix or scipy.sparse.spmatrix
P

The permutation matrix. P @ A @ P.T is the matrix A permuted by the permutation of the decomposition

Type:scipy.sparse.dok_matrix
append_block_decomposition(dec)[source]

Makes a new decomposition where this decompsition and the passed one are appended.

Parameters:dec (DecompositionBase) – The decomposition that should be appended to this decomposition.
Returns:dec – A new decomposition where this decompsition and the passed one are appended. This desomposition represents the first block and the passed one the second block.
Return type:DecompositionBase
as_LDL_Decomposition()[source]
as_any_type(*type_strs, copy=False)

Converts this decomposition to any of the passed types.

Parameters:
  • *type_strs (str) – The decomposition types to any of them this this decomposition is converted.
  • copy (bool) – Whether the data of this decomposition should always be copied or only if needed.
Returns:

If the type of this decomposition is not in type_strs, a decomposition of type type_str[0] is returned which represents the same decomposed matrix as this decomposition. Otherwise this decomposition or a copy of it is returned, depending on copy.

Return type:

matrix.decompositions.DecompositionBase

as_same_type(dec, copy=False)

Converts the passed decompositions to the same type as this decomposition.

Parameters:
  • doc (DecompositionBase) – The decomposition that should be converted.
  • copy (bool) – Whether the data of the decomposition that sould be converted should always be copied or only if needed.
Returns:

If the type of the passed decomposition is not same type as this decomposition, a decomposition of this type is returned which represents the same decomposed matrix as the passed decomposition. Otherwise this decomposition or a copy of it is returned, depending on copy.

Return type:

matrix.decompositions.DecompositionBase

as_type(type_str, copy=False)[source]

Converts this decomposition to passed type.

Parameters:
  • type_str (str) – The decomposition type to which this decomposition is converted.
  • copy (bool) – Whether the data of this decomposition should always be copied or only if needed.
Returns:

If the type of this decomposition is not type_str, a decomposition of type type_str is returned which represents the same decomposed matrix as this decomposition. Otherwise this decomposition or a copy of it is returned, depending on copy.

Return type:

matrix.decompositions.DecompositionBase

check_finite(check_finite=True)

Check if this is a decomposition representing a finite matrix.

Parameters:check_finite (bool) – Whether to perform this check. default: True
Raises:matrix.errors.DecompositionNotFiniteError – If this is a decomposition representing a non-finite matrix.
check_invertible()

Check if this is a decomposition representing an invertible matrix.

Raises:matrix.errors.DecompositionSingularError – If this is a decomposition representing a singular matrix.
composed_matrix

The composed matrix represented by this decomposition.

Type:numpy.matrix or scipy.sparse.spmatrix
copy()

Copies this decomposition.

Returns:A copy of this decomposition.
Return type:matrix.decompositions.DecompositionBase
d

The diagonal vector of the matrix D of the decomposition.

Type:numpy.ndarray
inverse_matrix_both_sides_multiplication(x, y=None, dtype=None)[source]

Calculates the both sides (matrix-matrix or matrix-vector) product y.H @ B @ x, where B is the mattrix inverse of the composed matrix represented by this decomposition.

Parameters:
  • x (numpy.ndarray or scipy.sparse.spmatrix) – Vector or matrix in the product y.H @ B @ x. It must hold self.n == x.shape[0].
  • y (numpy.ndarray or scipy.sparse.spmatrix) – Vector or matrix in the product y.H @ B @ x. It must hold self.n == y.shape[0]. optional, default: If y is not passed, x is used as y.
  • dtype (numpy.dtype) – Type to use in computation. optional, default: Determined by the method.
Returns:

The result of y.H @ A @ x.

Return type:

numpy.ndarray or scipy.sparse.spmatrix

Raises:

matrix.errors.DecompositionSingularError – If this is a decomposition representing a singular matrix.

inverse_matrix_right_side_multiplication(x, dtype=None)[source]

Calculates the right side (matrix-matrix or matrix-vector) product B @ x, where B is the matrix inverse of the composed matrix represented by this decomposition.

Parameters:
  • x (numpy.ndarray or scipy.sparse.spmatrix) – Vector or matrix in the product in the matrix-matrix or matrix-vector B @ x. It must hold self.n == x.shape[0].
  • dtype (numpy.dtype) – Type to use in computation. optional, default: Determined by the method.
Returns:

The result of B @ x.

Return type:

numpy.ndarray or scipy.sparse.spmatrix

Raises:

matrix.errors.DecompositionSingularError – If this is a decomposition representing a singular matrix.

is_almost_equal(other, rtol=0.0001, atol=1e-06)[source]

Whether this decomposition is close to passed decomposition.

Parameters:
  • other (str) – The decomposition which to compare to this decomposition.
  • rtol (float) – The relative tolerance parameter.
  • atol (float) – The absolute tolerance parameter.
Returns:

Whether this decomposition is close to passed decomposition.

Return type:

bool

is_equal(other)[source]

Whether this decomposition is equal to passed decomposition.

Parameters:other (str) – The decomposition which to compare to this decomposition.
Returns:Whether this decomposition is equal to passed decomposition.
Return type:bool
is_finite()[source]

Returns whether this is a decomposition representing a finite matrix.

Returns:Whether this is a decomposition representing a finite matrix.
Return type:bool
is_invertible()

Returns whether this is a decomposition representing an invertible matrix.

Returns:Whether this is a decomposition representing an invertible matrix.
Return type:bool
is_permuted

Whether this is a decompositon with permutation.

Type:bool
is_positive_definite()[source]

Returns whether this is a decomposition of a positive definite matrix.

Returns:Whether this is a decomposition of a positive definite matrix.
Return type:bool
is_positive_semidefinite()[source]

Returns whether this is a decomposition of a positive semi-definite matrix.

Returns:Whether this is a decomposition of a positive semi-definite matrix.
Return type:bool
is_singular()[source]

Returns whether this is a decomposition representing a singular matrix.

Returns:Whether this is a decomposition representing a singular matrix.
Return type:bool
is_sparse()[source]

Returns whether this is a decomposition of a sparse matrix.

Returns:Whether this is a decomposition of a sparse matrix.
Return type:bool
is_type(type_str)

Whether this decomposition is of the passed type.

Parameters:type_str (str) – The decomposition type according to which is checked.
Returns:Whether this is a decomposition of the passed type.
Return type:bool
load(filename)

Loads a decomposition of this type.

Parameters:

filename (str) – Where the decomposition is saved.

Raises:
  • FileNotFoundError – If the files are not found in the passed directory.
  • DecompositionInvalidDecompositionTypeFile – If the files contains another decomposition type.
matrix_both_sides_multiplication(x, y=None, dtype=None)[source]

Calculates the both sides (matrix-matrix or matrix-vector) product y.H @ A @ x, where A is the composed matrix represented by this decomposition.

Parameters:
  • x (numpy.ndarray or scipy.sparse.spmatrix) – Vector or matrix in the product y.H @ A @ x. It must hold self.n == x.shape[0].
  • y (numpy.ndarray or scipy.sparse.spmatrix) – Vector or matrix in the product y.H @ A @ x. It must hold self.n == y.shape[0]. optional, default: If y is not passed, x is used as y.
  • dtype (numpy.dtype) – Type to use in computation. optional, default: Determined by the method.
Returns:

The result of y.H @ A @ x.

Return type:

numpy.ndarray or scipy.sparse.spmatrix

matrix_right_side_multiplication(x, dtype=None)[source]

Calculates the right side (matrix-matrix or matrix-vector) product A @ x, where A is the composed matrix represented by this decomposition.

Parameters:
  • x (numpy.ndarray or scipy.sparse.spmatrix) – Vector or matrix in the product in the matrix-matrix or matrix-vector A @ x. It must hold self.n == x.shape[0].
  • dtype (numpy.dtype) – Type to use in computation. optional, default: Determined by the method.
Returns:

The result of A @ x.

Return type:

numpy.ndarray or scipy.sparse.spmatrix

n

The dimension of the squared decomposed matrix.

Type:int
p

The permutation vector. A[p[:, np.newaxis], p[np.newaxis, :]] is the matrix A permuted by the permutation of the decomposition

Type:numpy.ndarray
p_inverse

The permutation vector that undoes the permutation.

Type:numpy.ndarray
permute_matrix(A)

Permutes a matrix by the permutation of the decomposition.

Parameters:A (numpy.ndarray or scipy.sparse.spmatrix) – The matrix that should be permuted.
Returns:The matrix A permuted by the permutation of the decomposition.
Return type:numpy.ndarray or scipy.sparse.spmatrix
save(filename)

Saves this decomposition.

Parameters:filename (str) – Where this decomposition should be saved.
solve(b, dtype=None)

Solves the equation A x = b regarding x, where A is the composed matrix represented by this decomposition.

Parameters:
  • b (numpy.ndarray or scipy.sparse.spmatrix) – Right-hand side vector or matrix in equation A x = b. It must hold self.n == b.shape[0].
  • dtype (numpy.dtype) – Type to use in computation. optional, default: Determined by the method.
Returns:

An x so that A x = b. The shape of x matches the shape of b.

Return type:

numpy.ndarray or scipy.sparse.spmatrix

Raises:

matrix.errors.DecompositionSingularError – If this is a decomposition representing a singular matrix.

type_str = 'LDL_compressed'

The type of this decomposition represented as string.

Type:str
unpermute_matrix(A)

Unpermutes a matrix permuted by the permutation of the decomposition.

Parameters:A (numpy.ndarray or scipy.sparse.spmatrix) – The matrix that should be unpermuted.
Returns:The matrix A unpermuted by the permutation of the decomposition.
Return type:numpy.ndarray or scipy.sparse.spmatrix

Base decomposition

class matrix.decompositions.DecompositionBase(p=None)[source]

Bases: object

A matrix decomposition.

This class is a base class for all other matrix decompositions.

Parameters:p (numpy.ndarray) – The permutation vector used for the decomposition. This decomposition is of A[p[:, np.newaxis], p[np.newaxis, :]] where A is a matrix. optional, default: no permutation
P

The permutation matrix. P @ A @ P.T is the matrix A permuted by the permutation of the decomposition

Type:scipy.sparse.dok_matrix
append_block_decomposition(dec)[source]

Makes a new decomposition where this decompsition and the passed one are appended.

Parameters:dec (DecompositionBase) – The decomposition that should be appended to this decomposition.
Returns:dec – A new decomposition where this decompsition and the passed one are appended. This desomposition represents the first block and the passed one the second block.
Return type:DecompositionBase
as_any_type(*type_strs, copy=False)[source]

Converts this decomposition to any of the passed types.

Parameters:
  • *type_strs (str) – The decomposition types to any of them this this decomposition is converted.
  • copy (bool) – Whether the data of this decomposition should always be copied or only if needed.
Returns:

If the type of this decomposition is not in type_strs, a decomposition of type type_str[0] is returned which represents the same decomposed matrix as this decomposition. Otherwise this decomposition or a copy of it is returned, depending on copy.

Return type:

matrix.decompositions.DecompositionBase

as_same_type(dec, copy=False)[source]

Converts the passed decompositions to the same type as this decomposition.

Parameters:
  • doc (DecompositionBase) – The decomposition that should be converted.
  • copy (bool) – Whether the data of the decomposition that sould be converted should always be copied or only if needed.
Returns:

If the type of the passed decomposition is not same type as this decomposition, a decomposition of this type is returned which represents the same decomposed matrix as the passed decomposition. Otherwise this decomposition or a copy of it is returned, depending on copy.

Return type:

matrix.decompositions.DecompositionBase

as_type(type_str, copy=False)[source]

Converts this decomposition to passed type.

Parameters:
  • type_str (str) – The decomposition type to which this decomposition is converted.
  • copy (bool) – Whether the data of this decomposition should always be copied or only if needed.
Returns:

If the type of this decomposition is not type_str, a decomposition of type type_str is returned which represents the same decomposed matrix as this decomposition. Otherwise this decomposition or a copy of it is returned, depending on copy.

Return type:

matrix.decompositions.DecompositionBase

check_finite(check_finite=True)[source]

Check if this is a decomposition representing a finite matrix.

Parameters:check_finite (bool) – Whether to perform this check. default: True
Raises:matrix.errors.DecompositionNotFiniteError – If this is a decomposition representing a non-finite matrix.
check_invertible()[source]

Check if this is a decomposition representing an invertible matrix.

Raises:matrix.errors.DecompositionSingularError – If this is a decomposition representing a singular matrix.
composed_matrix

The composed matrix represented by this decomposition.

Type:numpy.matrix or scipy.sparse.spmatrix
copy()[source]

Copies this decomposition.

Returns:A copy of this decomposition.
Return type:matrix.decompositions.DecompositionBase
inverse_matrix_both_sides_multiplication(x, y=None, dtype=None)[source]

Calculates the both sides (matrix-matrix or matrix-vector) product y.H @ B @ x, where B is the mattrix inverse of the composed matrix represented by this decomposition.

Parameters:
  • x (numpy.ndarray or scipy.sparse.spmatrix) – Vector or matrix in the product y.H @ B @ x. It must hold self.n == x.shape[0].
  • y (numpy.ndarray or scipy.sparse.spmatrix) – Vector or matrix in the product y.H @ B @ x. It must hold self.n == y.shape[0]. optional, default: If y is not passed, x is used as y.
  • dtype (numpy.dtype) – Type to use in computation. optional, default: Determined by the method.
Returns:

The result of y.H @ A @ x.

Return type:

numpy.ndarray or scipy.sparse.spmatrix

Raises:

matrix.errors.DecompositionSingularError – If this is a decomposition representing a singular matrix.

inverse_matrix_right_side_multiplication(x, dtype=None)[source]

Calculates the right side (matrix-matrix or matrix-vector) product B @ x, where B is the matrix inverse of the composed matrix represented by this decomposition.

Parameters:
  • x (numpy.ndarray or scipy.sparse.spmatrix) – Vector or matrix in the product in the matrix-matrix or matrix-vector B @ x. It must hold self.n == x.shape[0].
  • dtype (numpy.dtype) – Type to use in computation. optional, default: Determined by the method.
Returns:

The result of B @ x.

Return type:

numpy.ndarray or scipy.sparse.spmatrix

Raises:

matrix.errors.DecompositionSingularError – If this is a decomposition representing a singular matrix.

is_almost_equal(other, rtol=0.0001, atol=1e-06)[source]

Whether this decomposition is close to passed decomposition.

Parameters:
  • other (str) – The decomposition which to compare to this decomposition.
  • rtol (float) – The relative tolerance parameter.
  • atol (float) – The absolute tolerance parameter.
Returns:

Whether this decomposition is close to passed decomposition.

Return type:

bool

is_equal(other)[source]

Whether this decomposition is equal to passed decomposition.

Parameters:other (str) – The decomposition which to compare to this decomposition.
Returns:Whether this decomposition is equal to passed decomposition.
Return type:bool
is_finite()[source]

Returns whether this is a decomposition representing a finite matrix.

Returns:Whether this is a decomposition representing a finite matrix.
Return type:bool
is_invertible()[source]

Returns whether this is a decomposition representing an invertible matrix.

Returns:Whether this is a decomposition representing an invertible matrix.
Return type:bool
is_permuted

Whether this is a decompositon with permutation.

Type:bool
is_positive_definite()[source]

Returns whether this is a decomposition of a positive definite matrix.

Returns:Whether this is a decomposition of a positive definite matrix.
Return type:bool
is_positive_semidefinite()[source]

Returns whether this is a decomposition of a positive semi-definite matrix.

Returns:Whether this is a decomposition of a positive semi-definite matrix.
Return type:bool
is_singular()[source]

Returns whether this is a decomposition representing a singular matrix.

Returns:Whether this is a decomposition representing a singular matrix.
Return type:bool
is_sparse()[source]

Returns whether this is a decomposition of a sparse matrix.

Returns:Whether this is a decomposition of a sparse matrix.
Return type:bool
is_type(type_str)[source]

Whether this decomposition is of the passed type.

Parameters:type_str (str) – The decomposition type according to which is checked.
Returns:Whether this is a decomposition of the passed type.
Return type:bool
load(filename)[source]

Loads a decomposition of this type.

Parameters:

filename (str) – Where the decomposition is saved.

Raises:
  • FileNotFoundError – If the files are not found in the passed directory.
  • DecompositionInvalidDecompositionTypeFile – If the files contains another decomposition type.
matrix_both_sides_multiplication(x, y=None, dtype=None)[source]

Calculates the both sides (matrix-matrix or matrix-vector) product y.H @ A @ x, where A is the composed matrix represented by this decomposition.

Parameters:
  • x (numpy.ndarray or scipy.sparse.spmatrix) – Vector or matrix in the product y.H @ A @ x. It must hold self.n == x.shape[0].
  • y (numpy.ndarray or scipy.sparse.spmatrix) – Vector or matrix in the product y.H @ A @ x. It must hold self.n == y.shape[0]. optional, default: If y is not passed, x is used as y.
  • dtype (numpy.dtype) – Type to use in computation. optional, default: Determined by the method.
Returns:

The result of y.H @ A @ x.

Return type:

numpy.ndarray or scipy.sparse.spmatrix

matrix_right_side_multiplication(x, dtype=None)[source]

Calculates the right side (matrix-matrix or matrix-vector) product A @ x, where A is the composed matrix represented by this decomposition.

Parameters:
  • x (numpy.ndarray or scipy.sparse.spmatrix) – Vector or matrix in the product in the matrix-matrix or matrix-vector A @ x. It must hold self.n == x.shape[0].
  • dtype (numpy.dtype) – Type to use in computation. optional, default: Determined by the method.
Returns:

The result of A @ x.

Return type:

numpy.ndarray or scipy.sparse.spmatrix

n

The dimension of the squared decomposed matrix.

Type:int
p

The permutation vector. A[p[:, np.newaxis], p[np.newaxis, :]] is the matrix A permuted by the permutation of the decomposition

Type:numpy.ndarray
p_inverse

The permutation vector that undoes the permutation.

Type:numpy.ndarray
permute_matrix(A)[source]

Permutes a matrix by the permutation of the decomposition.

Parameters:A (numpy.ndarray or scipy.sparse.spmatrix) – The matrix that should be permuted.
Returns:The matrix A permuted by the permutation of the decomposition.
Return type:numpy.ndarray or scipy.sparse.spmatrix
save(filename)[source]

Saves this decomposition.

Parameters:filename (str) – Where this decomposition should be saved.
solve(b, dtype=None)[source]

Solves the equation A x = b regarding x, where A is the composed matrix represented by this decomposition.

Parameters:
  • b (numpy.ndarray or scipy.sparse.spmatrix) – Right-hand side vector or matrix in equation A x = b. It must hold self.n == b.shape[0].
  • dtype (numpy.dtype) – Type to use in computation. optional, default: Determined by the method.
Returns:

An x so that A x = b. The shape of x matches the shape of b.

Return type:

numpy.ndarray or scipy.sparse.spmatrix

Raises:

matrix.errors.DecompositionSingularError – If this is a decomposition representing a singular matrix.

type_str = 'base'

The type of this decomposition represented as string.

Type:str
unpermute_matrix(A)[source]

Unpermutes a matrix permuted by the permutation of the decomposition.

Parameters:A (numpy.ndarray or scipy.sparse.spmatrix) – The matrix that should be unpermuted.
Returns:The matrix A unpermuted by the permutation of the decomposition.
Return type:numpy.ndarray or scipy.sparse.spmatrix