Skip to content

logarithms

qoptcraft.math.logarithms

Logarithms of matrices.

References

Algorithms can be found in T.A. Loring, Numer. Linear Algebra Appl. 21 (6) (2014) 744-760. https://arxiv.org/abs/1203.6151

log_matrix(matrix, method='diagonalization')

Logarithm of matrix via symmetrized diagonalization.

Parameters:

Name Type Description Default
matrix NDArray

square matrix.

required

Returns:

Name Type Description
NDArray NDArray

matrix logarithm.

Source code in qoptcraft/math/logarithms.py
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
def log_matrix(
    matrix: NDArray,
    method: Literal[
        "diagonalization", "symmetrized", "schur", "polar", "newton"
    ] = "diagonalization",
) -> NDArray:
    """Logarithm of matrix via symmetrized diagonalization.

    Args:
        matrix (NDArray): square matrix.

    Returns:
        NDArray: matrix logarithm.
    """
    if method == "diagonalization":
        return log_matrix_diag(matrix)
    if method == "symmetrized":
        return log_matrix_sym(matrix)
    if method == "schur":
        return log_matrix_schur(matrix)
    if method == "polar":
        return log_matrix_polar_schur(matrix)
    if method == "newton":
        return log_matrix_newton_schur(matrix)
    raise ValueError(
        "Values for method are 'diagonalization', 'symmetrized', 'schur', 'polar' or 'newton'."
    )

log_matrix_diag(matrix)

Logarithm of matrix via symmetrized diagonalization.

Parameters:

Name Type Description Default
matrix NDArray

square matrix.

required

Returns:

Name Type Description
NDArray NDArray

matrix logarithm.

Source code in qoptcraft/math/logarithms.py
45
46
47
48
49
50
51
52
53
54
55
56
57
def log_matrix_diag(matrix: NDArray) -> NDArray:
    """Logarithm of matrix via symmetrized diagonalization.

    Args:
        matrix (NDArray): square matrix.

    Returns:
        NDArray: matrix logarithm.
    """
    eigenvalues, eigenvectors = np.linalg.eig(matrix)
    diag = np.diag(eigenvalues / np.abs(eigenvalues))
    log_matrix = eigenvectors @ logm(diag) @ inv(eigenvectors)
    return (log_matrix + log_matrix.conj().T) / 2

log_matrix_newton_schur(matrix)

Logarithm of matrix using Schur's decomposition. Used to diagonalize nearly unitary matrices.

Parameters:

Name Type Description Default
matrix NDArray

square matrix.

required

Returns:

Name Type Description
NDArray NDArray

matrix logarithm.

Source code in qoptcraft/math/logarithms.py
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
def log_matrix_newton_schur(matrix: NDArray) -> NDArray:
    """Logarithm of matrix using Schur's decomposition. Used to diagonalize
    nearly unitary matrices.

    Args:
        matrix (NDArray): square matrix.

    Returns:
        NDArray: matrix logarithm.
    """
    V1 = (matrix + inv(matrix).conj().T) / 2
    V = (V1 + inv(V1).conj().T) / 2
    U, Q = schur(V)
    diag = np.diag(np.diag(U) / np.abs(np.diag(U)))
    return Q @ logm(diag) @ Q.conj().T

log_matrix_polar_schur(matrix)

Logarithm of matrix using Polar and Schur's decomposition. Used to diagonalize nearly unitary matrices.

Parameters:

Name Type Description Default
matrix NDArray

square matrix.

required

Returns:

Name Type Description
NDArray NDArray

matrix logarithm.

Source code in qoptcraft/math/logarithms.py
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
def log_matrix_polar_schur(matrix: NDArray) -> NDArray:
    """Logarithm of matrix using Polar and Schur's decomposition. Used to
    diagonalize nearly unitary matrices.

    Args:
        matrix (NDArray): square matrix.

    Returns:
        NDArray: matrix logarithm.
    """
    V = matrix @ inv(sqrtm(matrix.conj().T @ matrix))  # TODO: use SVD
    U, Q = schur(V)
    diag = np.diag(np.diag(U) / np.abs(np.diag(U)))
    return Q @ logm(diag) @ Q.conj().T

log_matrix_schur(matrix)

Logarithm of matrix using Schur's decomposition. Used to diagonalize nearly unitary matrices.

Parameters:

Name Type Description Default
matrix NDArray

square matrix.

required

Returns:

Name Type Description
NDArray NDArray

matrix logarithm.

Source code in qoptcraft/math/logarithms.py
73
74
75
76
77
78
79
80
81
82
83
84
85
def log_matrix_schur(matrix: NDArray) -> NDArray:
    """Logarithm of matrix using Schur's decomposition. Used to diagonalize
    nearly unitary matrices.

    Args:
        matrix (NDArray): square matrix.

    Returns:
        NDArray: matrix logarithm.
    """
    U, Q = schur(matrix)
    diag = np.diag(np.diag(U) / np.abs(np.diag(U)))
    return Q @ logm(diag) @ Q.conj().T

log_matrix_sym(matrix)

Symmetrized logarithm of matrix.

Parameters:

Name Type Description Default
matrix NDArray

square matrix.

required

Returns:

Name Type Description
NDArray NDArray

matrix logarithm.

Source code in qoptcraft/math/logarithms.py
60
61
62
63
64
65
66
67
68
69
70
def log_matrix_sym(matrix: NDArray) -> NDArray:
    """Symmetrized logarithm of matrix.

    Args:
        matrix (NDArray): square matrix.

    Returns:
        NDArray: matrix logarithm.
    """
    log_matrix = logm(matrix)
    return (log_matrix + log_matrix.conj().T) / 2