Linear algebra

Decomposition

numpy_sugar.linalg.economic_qs(K, epsilon=1.4901161193847656e-08)[source]

Economic eigen decomposition for symmetric matrices.

A symmetric matrix K can be decomposed in \(\mathrm Q_0 \mathrm S_0 \mathrm Q_0^\intercal + \mathrm Q_1\ \mathrm S_1 \mathrm Q_1^ \intercal\), where \(\mathrm S_1\) is a zero matrix with size determined by K’s rank deficiency.

Parameters:
  • K (array_like) – Symmetric matrix.
  • epsilon (float) – Eigen value threshold. Default is sqrt(finfo(float).eps).
Returns:

((Q0, Q1), S0).

Return type:

tuple

numpy_sugar.linalg.economic_qs_linear(G)[source]

Economic eigen decomposition for symmetric matrices dot(G, G.T).

It is theoretically equivalent to economic_qs(dot(G, G.T)). Refer to numpy_sugar.economic_qs() for further information.

Parameters:G (array_like) – Matrix.
Returns:((Q0, Q1), S0).
Return type:tuple
numpy_sugar.linalg.economic_svd(G, epsilon=1.4901161193847656e-08)[source]

Economic Singular Value Decomposition.

Parameters:
  • G (array_like) – Matrix to be factorized.
  • epsilon (float) – Threshold on the square root of the eigen values. Default is sqrt(finfo(float).eps).
Returns:

Unitary matrix. numpy.ndarray: Singular values. numpy.ndarray: Unitary matrix.

Return type:

numpy.ndarray

See also

numpy.linalg.svd()
Cholesky decomposition.

Determinant

numpy_sugar.linalg.plogdet(K)[source]

Log of the pseudo-determinant.

It assumes that K is a positive semi-definite matrix.

Parameters:K (array_like) – matrix.
Returns:log of the pseudo-determinant.
Return type:float

Dot and sum

numpy_sugar.linalg.sum2diag(A, D, out=None)[source]

Add values D to the diagonal of matrix A.

Parameters:
  • A (array_like) – Left-hand side.
  • D (array_like or float) – Values to add.
  • out (numpy.ndarray, optional) – copy result to.
Returns:

Resulting matrix.

Return type:

numpy.ndarray

numpy_sugar.linalg.ddot(L, R, left=True, out=None)[source]

Dot product of a matrix and a diagonal one.

Parameters:
  • L (array_like) – Left matrix.
  • R (array_like) – Right matrix.
  • left (bool) – True if L is the diagonal matrix; False otherwise.
  • out (numpy.ndarray, optional) – copy result to.
Returns:

Resulting matrix.

Return type:

numpy.ndarray

Properties

numpy_sugar.linalg.check_definite_positiveness(A)[source]

Check if A is a definite positive matrix.

Parameters:A (array_like) – Matrix.
Returns:True if A is definite positive; False otherwise.
Return type:bool
numpy_sugar.linalg.check_symmetry(A)[source]

Check if A is a symmetric matrix.

Parameters:A (array_like) – Matrix.
Returns:True if A is symmetric; False otherwise.
Return type:bool

Reducers

numpy_sugar.linalg.lu_slogdet(LU)[source]

Natural logarithm of a LU decomposition.

Parameters:LU (tuple) – LU decomposition.
Returns:sign and log-determinant.
Return type:tuple
numpy_sugar.linalg.trace2(A, B)[source]

Trace of \(\mathrm A \mathrm B^\intercal\).

Parameters:
  • A (array_like) – Left-hand side.
  • B (array_like) – Right-hand side.
Returns:

Trace of \(\mathrm A \mathrm B^\intercal\).

Return type:

float

numpy_sugar.linalg.dotd(A, B, out=None)[source]

Diagonal of \(\mathrm A\mathrm B^\intercal\).

If A is \(n\times p\) and B is \(p\times n\), it is done in \(O(pn)\).

Parameters:
  • A (array_like) – Left matrix.
  • B (array_like) – Right matrix.
  • out (numpy.ndarray, optional) – copy result to.
Returns:

Resulting diagonal.

Return type:

numpy.ndarray

Solvers

numpy_sugar.linalg.cho_solve(L, b)[source]

Solve for Cholesky decomposition.

Solve the linear equations \(\mathrm A \mathbf x = \mathbf b\), given the Cholesky factorization of \(\mathrm A\).

Parameters:
  • L (array_like) – Lower triangular matrix.
  • b (array_like) – Right-hand side.
Returns:

The solution to the system

\(\mathrm A \mathbf x = \mathbf b\).

Return type:

numpy.ndarray

See also

numpy.linalg.cholesky()
Cholesky decomposition.
scipy.linalg.cho_solve()
Solve linear equations given Cholesky factorization.
numpy_sugar.linalg.lu_solve(LU, b)[source]

Solve for LU decomposition.

Solve the linear equations \(\mathrm A \mathbf x = \mathbf b\), given the LU factorization of \(\mathrm A\).

Parameters:
  • LU (array_like) – LU decomposition.
  • b (array_like) – Right-hand side.
Returns:

The solution to the system \(\mathrm A \mathbf x = \mathbf b\).

Return type:

numpy.ndarray

See also

scipy.linalg.lu_factor()
LU decomposition.
scipy.linalg.lu_solve()
Solve linear equations given LU factorization.
numpy_sugar.linalg.solve(A, b)[source]

Solve for the linear equations \(\mathrm A \mathbf x = \mathbf b\).

Parameters:
  • A (array_like) – Coefficient matrix.
  • b (array_like) – Ordinate values.
Returns:

Solution x.

Return type:

numpy.ndarray

numpy_sugar.linalg.rsolve(A, b, epsilon=1.4901161193847656e-08)[source]

Robust solve for the linear equations.

Parameters:
  • A (array_like) – Coefficient matrix.
  • b (array_like) – Ordinate values.
Returns:

Solution x.

Return type:

numpy.ndarray

numpy_sugar.linalg.stl(A, b)[source]

Shortcut to solve_triangular(A, b, lower=True, check_finite=False).

Solve linear systems \(\mathrm A \mathbf x = \mathbf b\) when \(\mathrm A\) is a lower-triangular matrix.

Parameters:
  • A (array_like) – A lower-triangular matrix.
  • b (array_like) – Ordinate values.
Returns:

Solution x.

Return type:

numpy.ndarray

See also

scipy.linalg.solve_triangular()
Solve triangular linear equations.
numpy_sugar.linalg.lstsq(A, b)[source]

Return the least-squares solution to a linear matrix equation.

Parameters:
  • A (array_like) – Coefficient matrix.
  • b (array_like) – Ordinate values.
Returns:

Least-squares solution.

Return type:

numpy.ndarray

numpy_sugar.linalg.cho_solve(L, b)[source]

Solve for Cholesky decomposition.

Solve the linear equations \(\mathrm A \mathbf x = \mathbf b\), given the Cholesky factorization of \(\mathrm A\).

Parameters:
  • L (array_like) – Lower triangular matrix.
  • b (array_like) – Right-hand side.
Returns:

The solution to the system

\(\mathrm A \mathbf x = \mathbf b\).

Return type:

numpy.ndarray

See also

numpy.linalg.cholesky()
Cholesky decomposition.
scipy.linalg.cho_solve()
Solve linear equations given Cholesky factorization.
numpy_sugar.linalg.sum2diag(A, D, out=None)[source]

Add values D to the diagonal of matrix A.

Parameters:
  • A (array_like) – Left-hand side.
  • D (array_like or float) – Values to add.
  • out (numpy.ndarray, optional) – copy result to.
Returns:

Resulting matrix.

Return type:

numpy.ndarray

numpy_sugar.linalg.trace2(A, B)[source]

Trace of \(\mathrm A \mathrm B^\intercal\).

Parameters:
  • A (array_like) – Left-hand side.
  • B (array_like) – Right-hand side.
Returns:

Trace of \(\mathrm A \mathrm B^\intercal\).

Return type:

float

numpy_sugar.linalg.ddot(L, R, left=True, out=None)[source]

Dot product of a matrix and a diagonal one.

Parameters:
  • L (array_like) – Left matrix.
  • R (array_like) – Right matrix.
  • left (bool) – True if L is the diagonal matrix; False otherwise.
  • out (numpy.ndarray, optional) – copy result to.
Returns:

Resulting matrix.

Return type:

numpy.ndarray

numpy_sugar.linalg.dotd(A, B, out=None)[source]

Diagonal of \(\mathrm A\mathrm B^\intercal\).

If A is \(n\times p\) and B is \(p\times n\), it is done in \(O(pn)\).

Parameters:
  • A (array_like) – Left matrix.
  • B (array_like) – Right matrix.
  • out (numpy.ndarray, optional) – copy result to.
Returns:

Resulting diagonal.

Return type:

numpy.ndarray

numpy_sugar.linalg.lu_slogdet(LU)[source]

Natural logarithm of a LU decomposition.

Parameters:LU (tuple) – LU decomposition.
Returns:sign and log-determinant.
Return type:tuple
numpy_sugar.linalg.lu_solve(LU, b)[source]

Solve for LU decomposition.

Solve the linear equations \(\mathrm A \mathbf x = \mathbf b\), given the LU factorization of \(\mathrm A\).

Parameters:
  • LU (array_like) – LU decomposition.
  • b (array_like) – Right-hand side.
Returns:

The solution to the system \(\mathrm A \mathbf x = \mathbf b\).

Return type:

numpy.ndarray

See also

scipy.linalg.lu_factor()
LU decomposition.
scipy.linalg.lu_solve()
Solve linear equations given LU factorization.
numpy_sugar.linalg.check_definite_positiveness(A)[source]

Check if A is a definite positive matrix.

Parameters:A (array_like) – Matrix.
Returns:True if A is definite positive; False otherwise.
Return type:bool
numpy_sugar.linalg.check_symmetry(A)[source]

Check if A is a symmetric matrix.

Parameters:A (array_like) – Matrix.
Returns:True if A is symmetric; False otherwise.
Return type:bool
numpy_sugar.linalg.economic_qs(K, epsilon=1.4901161193847656e-08)[source]

Economic eigen decomposition for symmetric matrices.

A symmetric matrix K can be decomposed in \(\mathrm Q_0 \mathrm S_0 \mathrm Q_0^\intercal + \mathrm Q_1\ \mathrm S_1 \mathrm Q_1^ \intercal\), where \(\mathrm S_1\) is a zero matrix with size determined by K’s rank deficiency.

Parameters:
  • K (array_like) – Symmetric matrix.
  • epsilon (float) – Eigen value threshold. Default is sqrt(finfo(float).eps).
Returns:

((Q0, Q1), S0).

Return type:

tuple

numpy_sugar.linalg.economic_qs_linear(G)[source]

Economic eigen decomposition for symmetric matrices dot(G, G.T).

It is theoretically equivalent to economic_qs(dot(G, G.T)). Refer to numpy_sugar.economic_qs() for further information.

Parameters:G (array_like) – Matrix.
Returns:((Q0, Q1), S0).
Return type:tuple
numpy_sugar.linalg.solve(A, b)[source]

Solve for the linear equations \(\mathrm A \mathbf x = \mathbf b\).

Parameters:
  • A (array_like) – Coefficient matrix.
  • b (array_like) – Ordinate values.
Returns:

Solution x.

Return type:

numpy.ndarray

numpy_sugar.linalg.rsolve(A, b, epsilon=1.4901161193847656e-08)[source]

Robust solve for the linear equations.

Parameters:
  • A (array_like) – Coefficient matrix.
  • b (array_like) – Ordinate values.
Returns:

Solution x.

Return type:

numpy.ndarray

numpy_sugar.linalg.economic_svd(G, epsilon=1.4901161193847656e-08)[source]

Economic Singular Value Decomposition.

Parameters:
  • G (array_like) – Matrix to be factorized.
  • epsilon (float) – Threshold on the square root of the eigen values. Default is sqrt(finfo(float).eps).
Returns:

Unitary matrix. numpy.ndarray: Singular values. numpy.ndarray: Unitary matrix.

Return type:

numpy.ndarray

See also

numpy.linalg.svd()
Cholesky decomposition.
numpy_sugar.linalg.stl(A, b)[source]

Shortcut to solve_triangular(A, b, lower=True, check_finite=False).

Solve linear systems \(\mathrm A \mathbf x = \mathbf b\) when \(\mathrm A\) is a lower-triangular matrix.

Parameters:
  • A (array_like) – A lower-triangular matrix.
  • b (array_like) – Ordinate values.
Returns:

Solution x.

Return type:

numpy.ndarray

See also

scipy.linalg.solve_triangular()
Solve triangular linear equations.
numpy_sugar.linalg.lstsq(A, b)[source]

Return the least-squares solution to a linear matrix equation.

Parameters:
  • A (array_like) – Coefficient matrix.
  • b (array_like) – Ordinate values.
Returns:

Least-squares solution.

Return type:

numpy.ndarray

numpy_sugar.linalg.plogdet(K)[source]

Log of the pseudo-determinant.

It assumes that K is a positive semi-definite matrix.

Parameters:K (array_like) – matrix.
Returns:log of the pseudo-determinant.
Return type:float