ddg.math.linalg module

Linear algebra utils

ddg.math.linalg.rank(A, atol=1e-13, rtol=None)[source]

Estimate the rank of a matrix, i.e. the dimension of its image.

The algorithm used by this function is based on the singular value decomposition of A.

Parameters:
Anumpy.ndarray of shape (m, n)

A 1-D array with length n will be treated as a 2-D array with shape (1, n).

atolfloat, (default=1e-13)

The absolute tolerance for a zero singular value. Singular values smaller than atol are considered to be zero.

rtolfloat, (default=None)

The relative tolerance. Singular values less than rtol*smax are considered to be zero, where smax is the largest singular value.

Returns:
rint

The estimated rank of the matrix.

See also

numpy.linalg.matrix_rank

NumPy’s matrix_rank is basically the same as this function, but it does not provide the option of the absolute tolerance.

Notes

If both atol and rtol are positive, the combined tolerance is the maximum of the two, that is:

tol = max(atol, rtol * smax)

Singular values smaller than tol are considered to be zero.

This function uses the global tolerance defaults if atol or rtol are set to None. See ddg.nonexact for details.

ddg.math.linalg.nullspace(A, atol=1e-13, rtol=None)[source]

Compute an approximate basis for the nullspace of A.

The algorithm used by this function is based on the singular value decomposition of A.

Parameters:
Anumpy.ndarray of shape (m, n)

A 1-D array with length n will be treated as a 2-D array with shape (1, n).

atolfloat, (default=1e-13)

The absolute tolerance for a zero singular value. Singular values smaller than atol are considered to be zero.

rtolfloat, (default=None)

The relative tolerance. Singular values less than rtol*smax are considered to be zero, where smax is the largest singular value.

Returns:
nsnumpy.ndarray

If A is an array with shape (m, n), then ns will be an array with shape (n, k), where k is the estimated dimension of the nullspace of A. The columns of ns form a basis for the nullspace; each element in numpy.dot(A, ns) will be approximately zero.

Notes

If both atol and rtol are positive, the combined tolerance is the maximum of the two, that is:

tol = max(atol, rtol * smax)

Singular values smaller than tol are considered to be zero.

This function uses the global tolerance defaults if atol or rtol are set to None. See ddg.nonexact for details.

ddg.math.linalg.minors(A, k)[source]

Compute all k-minors of a given (m, n)-matrix.

A k-minor is the determinant of a (k, k)-submatrix.

Parameters:
Anumpy.ndarray of shape (m, n)

A 1-D array with length n will be treated as a 2-D array with shape (1, n).

kint

Order of minors.

Returns:
minorslist of length d

The resulting minors where length d is given by:

d = (m choose k)*(n choose k)
Raises:
ValueError

If the order of minors k is greater than min(m, n) for a (m, n)-matrix.

ddg.math.linalg.row_basis(A, atol=1e-13, rtol=None)[source]

Computes an approximate basis for the span of rows of a given matrix.

The algorithm used by this function is based on the singular value decomposition of A.

Parameters:
Anumpy.ndarray of shape (m, n)

A 1-D array with length n will be treated as a 2-D array with shape (1, n).

atolfloat, (default=1e-13)

The absolute tolerance for a zero singular value. Singular values smaller than atol are considered to be zero.

rtolfloat, (default=None)

The relative tolerance. Singular values less than rtol*smax are considered to be zero, where smax is the largest singular value.

Returns:
nsnumpy.ndarray of shape (k, n)

If A is an array with shape (m, n), then ns will be an array with shape (k, n), where k is the estimated dimension of the row space of A. The rows of ns form its basis; they will be pairwise orthogonal and have unit length.

Notes

If both atol and rtol are positive, the combined tolerance is the maximum of the two, that is:

tol = max(atol, rtol * smax)

Singular values smaller than tol are considered to be zero.

This function uses the global tolerance defaults if atol or rtol are set to None. See ddg.nonexact for details.

ddg.math.linalg.col_basis(A, atol=1e-13, rtol=None)[source]

Computes an approximate basis for the span of columns of a given matrix.

The algorithm used by this function is based on the singular value decomposition of A.

Parameters:
Anumpy.ndarray of shape (m, n)

A 1-D array with length n will be treated as a 2-D array with shape (1, n).

atolfloat, (default=1e-13)

The absolute tolerance for a zero singular value. Singular values smaller than atol are considered to be zero.

rtolfloat, (default=None)

The relative tolerance. Singular values less than rtol*smax are considered to be zero, where smax is the largest singular value.

Returns:
nsnumpy.ndarray of shape (m, k)

If A is an array with shape (m, n), then ns will be an array with shape (m, k), where k is the estimated dimension of the column space of A. The columns of ns form its basis; they will be pairwise orthogonal and have unit length.

Notes

If both atol and rtol are positive, the combined tolerance is the maximum of the two, that is:

tol = max(atol, rtol * smax)

Singular values smaller than tol are considered to be zero.

This function uses the global tolerance defaults if atol or rtol are set to None. See ddg.nonexact for details.

ddg.math.linalg.matrix_to_arrays(matrix)[source]

Converts the columns(!) of a (m, n)-matrix to a list of arrays.

Parameters:
matrixnumpy.ndarray of shape (m, n)
Returns:
list of length n of numpy.ndarray, each of shape (m,)
ddg.math.linalg.e(i, n)[source]

Returns the ith basis vector in the canonical basis of K^n over K.

Parameters:
iint

Determines which canonical vector is returned, 0 <= i < n.

nint

Determines the size of canonical vector.

Returns:
numpy.ndarray of shape (n,)
ddg.math.linalg.extend_to_basis(v, i=-1)[source]

Complete a vector in R^n to a basis for R^n.

Parameters:
vnumpy.ndarray of shape (n,)

Vector to complete into a basis.

iint, (default=-1)

Determines position of v in the returned basis.

Returns:
Bnumpy.ndarray of shape (n, n)

If v is a vector of size n, then B will be a matrix of shape (n, n) where the columns are the basis vectors and B[:, i] == v.

Raises:
ValueError

If the given vector v is the zero vector.

ddg.math.linalg.coordinates(vectors, basis, atol=1e-13, rtol=None)[source]

Compute the coordinates of vectors in a given basis.

The basis can be of a certain subspace or of the whole space.

Parameters:
vectorsnumpy.ndarray of shape (n, k) or (n,)

Matrix whose columns are the given vectors.

basisnumpy.ndarray of shape (n, m)

Matrix whose linearly independent columns form a basis.

atolfloat, (default=1e-13)

The absolute tolerance parameter. See numpy.allclose.

rtolfloat, (default=None)

The relative tolerance parameter. See numpy.allclose.

Returns:
coordinatesnumpy.ndarray of shape (m, k) or (m,)

Coordinate matrix such that basis @ coordinates == vectors.

Raises:
ValueError

If at least one vector does not lie in the subspace defined by given basis.

Notes

This function uses the global tolerance defaults if atol or rtol are set to None. See ddg.nonexact for details.

ddg.math.linalg.linear_dependence(points, atol=0.0001, rtol=None)[source]

Computes coefficients of a linear dependence of points, i.e real coefficients cᵢ such that:

∑ᵢ cᵢ * pᵢ = 0

The algorithm used by this function is based on the singular value decomposition of given matrix of points.

Parameters:
pointsnumpy.ndarray of shape (n, m)

Matrix with the given n points as rows.

atolfloat, (default=1e-4)

The absolute tolerance for a zero singular value. Singular values smaller than atol are considered to be zero.

rtolfloat, (default=None)

The relative tolerance. Singular values less than rtol*smax are considered to be zero, where smax is the largest singular value.

Returns:
numpy.ndarray of shape (n,)

A 1-D array of coefficients.

Notes

If both atol and rtol are positive, the combined tolerance is the maximum of the two; that is:

tol = max(atol, rtol * smax)

Singular values smaller than tol are considered to be zero.

This function uses the global tolerance defaults if atol or rtol are set to None. See ddg.nonexact for details.