ddg.math.symmetric_matrices module

Symmetric matrix and signature utilities.

Includes diagonalization, projective/affine normalization and signature classes.

ddg.math.symmetric_matrices.signature_sort_key(x)[source]

Sorting key function for signatures.

Desired order of values: positive, negative, zero with each category sorted by absolute value (decreasing)

Parameters:
xfloat
Returns:
tuple(int, float)

Examples

>>> from ddg.math.symmetric_matrices import signature_sort_key
>>> signature_sort_key(1)
(0, -1)
>>> signature_sort_key(-1)
(1, -1)
>>> signature_sort_key(0)
(2, 0)
>>> signature_sort_key(15)
(0, -15)
>>> signature_sort_key(15.1)
(0, -15.1)
ddg.math.symmetric_matrices.diagonalize(A, sort_key=<function signature_sort_key>, atol=0, rtol=0)[source]

Diagonalize a symmetric matrix.

Diagonalization of symmetric matrix but with customizable order of eigenvalues. Uses np.linalg.eigh, then eigenvalues and eigenvectors. Makes eigenvalues close to zero exact.

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

Symmetric matrix

sort_keyCallable (default=signature_sort_key)

Sorting key function. Eigenvalues are sorted according to the result of this function for each eigenvalue.

atol, rtolfloat (default=0)

Eigenvalues close to 0 according to these tolerances are set to 0.

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

Returns:
evlist

List of n Eigenvalues, sorted according to sort_key.

Snumpy.ndarray of shape (n, n)

Columns of S are the n Eigenvectors in the order of ev.

Examples

>>> import numpy as np
>>> from ddg.math.symmetric_matrices import diagonalize
>>> A = np.array([[10, 0, 0], [0, 5, 0], [0, 0, 3]])
>>> A
array([[10,  0,  0],
       [ 0,  5,  0],
       [ 0,  0,  3]])
>>> ev, S = diagonalize(A)
>>> ev
(10.0, 5.0, 3.0)
>>> S
array([[1., 0., 0.],
       [0., 1., 0.],
       [0., 0., 1.]])
>>> B = np.array([[1, 0, 2], [0, 5, 0], [2, 0, 3]])
>>> B
array([[1, 0, 2],
       [0, 5, 0],
       [2, 0, 3]])
>>> ev, S = diagonalize(B)
>>> ev
(5.0, 4.23606797749979, -0.2360679774997897)
>>> S
array([[ 0.        , -0.52573111, -0.85065081],
       [-1.        ,  0.        ,  0.        ],
       [ 0.        , -0.85065081,  0.52573111]])
ddg.math.symmetric_matrices.symmetrize(A)[source]

Symmetrize square matrix.

Parameters:
Anumpy.ndarary of shape (n, n)
Returns:
numpy.ndarray of shape (n, n)

(A.T + A) / 2

Examples

>>> import numpy as np
>>> from ddg.math.symmetric_matrices import symmetrize
>>> A = np.array([[1, 0, 2], [0, 5, 0], [2, 0, 3]])
>>> A
array([[1, 0, 2],
       [0, 5, 0],
       [2, 0, 3]])
>>> symmetrize(A)
array([[1., 0., 2.],
       [0., 5., 0.],
       [2., 0., 3.]])
>>> B = np.array([[1, 1, 2], [0, 5, 0], [2, 0, 3]])
>>> B
array([[1, 1, 2],
       [0, 5, 0],
       [2, 0, 3]])
>>> symmetrize(B)
array([[1. , 0.5, 2. ],
       [0.5, 5. , 0. ],
       [2. , 0. , 3. ]])
ddg.math.symmetric_matrices.is_symmetric(A, atol=1e-13, rtol=None)[source]

Checks whether a square matrix is symmetric.

Parameters:
Anumpy.ndarray of shape (n, n)
atolfloat (default=1e-13)
rtolfloat (default=None)

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

Returns:
bool

Examples

>>> import numpy as np
>>> from ddg.math.symmetric_matrices import is_symmetric
>>> A = np.array([[1, 0, 2], [0, 5, 0], [2, 0, 3]])
>>> A
array([[1, 0, 2],
       [0, 5, 0],
       [2, 0, 3]])
>>> is_symmetric(A)
True
>>> B = np.array([[1, 1, 2], [0, 5, 0], [2, 0, 3]])
>>> B
array([[1, 1, 2],
       [0, 5, 0],
       [2, 0, 3]])
>>> is_symmetric(B)
False
ddg.math.symmetric_matrices.symmetric_matrix_from_diagonal(sgn, parabolic=False)[source]

A normalized affine quadric can be reconstructed from its diagonal and the additional knowledge whether it is parabolic.

For what “parabolic” means, see the docstring of AffineSignature.

Parameters:
sgnarray_like of shape (n,)

Diagonal of the matrix.

parabolicbool (default=False)

If this is True, sgn must have 0 at positions -1 and -2.

Raises:
ValueError

If parabolic is True and sgn does not have 0 at positions -1 and -2.

See also

AffineSignature

Examples

>>> from ddg.math.symmetric_matrices import symmetric_matrix_from_diagonal
>>> symmetric_matrix_from_diagonal((1, 0, 1))
array([[1, 0, 0],
       [0, 0, 0],
       [0, 0, 1]])
>>> symmetric_matrix_from_diagonal([10, 2, 15, 1])
array([[10,  0,  0,  0],
       [ 0,  2,  0,  0],
       [ 0,  0, 15,  0],
       [ 0,  0,  0,  1]])
>>> symmetric_matrix_from_diagonal((1, 2, 0, 0), parabolic=True)
array([[1, 0, 0, 0],
       [0, 2, 0, 0],
       [0, 0, 0, 1],
       [0, 0, 1, 0]])