Skip to content

util

posneg(x, s)

Return x with the sign of s.

\[ \begin{cases} +x & \text{if $s$ is true} \\ -x & \text{if $s$ is false} \end{cases} \]

To use the unary operators (+/pos) & -/neg) instead of multiplication (*/mul) with \(\pm1\) as the unary operators may be faster and multiplication with integers doesn't have to be implemented (might be the case for custom prototyped number types).

Source code in linalg\util.py
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
def posneg(x:Any, s:bool) -> Any:
    r"""Return `x` with the sign of `s`.

    $$
        \begin{cases}
            +x & \text{if $s$ is true} \\
            -x & \text{if $s$ is false}
        \end{cases}
    $$

    To use the unary operators (`+`/`pos`) & `-`/`neg`)
    instead of multiplication (`*`/`mul`) with $\pm1$
    as the unary operators may be faster
    and multiplication with integers doesn't have to be implemented
    (might be the case for custom prototyped number types).
    """
    return +x if s else -x

assert_matrix(A)

Assert matrix.

Source code in linalg\util.py
32
33
34
35
36
37
def assert_matrix(A:np.ndarray) -> None:
    """Assert matrix."""
    if not isinstance(A, np.ndarray):
        raise TypeError('expected `numpy.ndarray`')
    if A.ndim != 2:
        raise ValueError('expected 2 dimensional `numpy.ndarray`')

assert_sqmatrix(A)

Assert square matrix.

Source code in linalg\util.py
39
40
41
42
43
44
45
46
def assert_sqmatrix(A:np.ndarray) -> None:
    """Assert square matrix."""
    if not isinstance(A, np.ndarray):
        raise TypeError('expected `numpy.ndarray`')
    if A.ndim != 2:
        raise ValueError('expected 2 dimensional `numpy.ndarray`')
    if A.shape[0] != A.shape[1]:
        raise ValueError('expected square `numpy.ndarray`')

swap_rows(A, i, j)

Swap the i-th and j-th row of A in-place.

References

stackoverflow - Swap two rows in a numpy array in python

Source code in linalg\util.py
50
51
52
53
54
55
56
57
def swap_rows(A:np.ndarray, i:int, j:int) -> None:
    """Swap the `i`-th and `j`-th row of `A` in-place.

    References
    ----------
    [stackoverflow - Swap two rows in a numpy array in python](https://stackoverflow.com/a/54069951)
    """
    A[[i, j], :] = A[[j, i], :]

swap_columns(A, i, j)

Swap the i-th and j-th column of A in-place.

References

stackoverflow - Swap two rows in a numpy array in python

Source code in linalg\util.py
59
60
61
62
63
64
65
66
def swap_columns(A:np.ndarray, i:int, j:int) -> None:
    """Swap the `i`-th and `j`-th column of `A` in-place.

    References
    ----------
    [stackoverflow - Swap two rows in a numpy array in python](https://stackoverflow.com/a/54069951)
    """
    A[:, [i, j]] = A[:, [j, i]]

swap_pivot(A, p, i, j)

Swap the p-&i-th rows and p-&j-th columns of A in-place.

References

stackoverflow - Swap two rows in a numpy array in python

Source code in linalg\util.py
68
69
70
71
72
73
74
75
76
def swap_pivot(A:np.ndarray, p:int, i:int, j:int) -> None:
    """Swap the `p`-&`i`-th rows and `p`-&`j`-th columns of `A` in-place.

    References
    ----------
    [stackoverflow - Swap two rows in a numpy array in python](https://stackoverflow.com/a/54069951)
    """
    swap_rows(A, p, i)
    swap_columns(A, p, j)

submatrix(A, i, j)

Return a copy of A without the i-th row and j-th column.

References

stackoverflow - Swap two rows in a numpy array in python

Source code in linalg\util.py
79
80
81
82
83
84
85
86
def submatrix(A:np.ndarray, i:int, j:int) -> np.ndarray:
    """Return a copy of `A` without the `i`-th row and `j`-th column.

    References
    ----------
    [stackoverflow - Swap two rows in a numpy array in python](https://stackoverflow.com/a/54069951)
    """
    return np.delete(np.delete(A, i, 0), j, 1)