ortho_complement#

detkit.ortho_complement(Xp, X, X_orth=False)#

Orthogonalize the columns of matrix Xp against X.

Parameters:
Xparray_like

Output matrix. This matrix will be overwritten in place of the output orthogonal matrix. X and Xp must have the same number of rows, but they can have different number of columns.

Xarray_like

Input matrix.

X_orth: bool, default=False

Determines whether X is already orthonormalized or not.

Notes

The Gram-Schmidt method is used to orthogonalize the columns of the \(n \times q\) matrix \(\mathbf{X}^{\perp}\) (given by Xp) against the columns of the \(n \times p\) matrix \(\mathbf{X}\) (given by X) so that \(\mathbf{X}^{\perp}\) satisfies

\[\mathbf{X}^{\intercal} \mathbf{X}^{\perp} = \mathbf{O}_{p \times q},\]

where \(\mathbf{O}_{p \times q}\) is the zero matrix of the size \(p \times q\).

Warning

The input matrix will be overwritten inplace.

Examples

Input Matrix \(\mathbf{X}\) is Not Orthogonal:

>>> # Create a random matrix
>>> import numpy
>>> numpy.random.seed(0)
>>> X = numpy.random.rand(6, 4)

>>> # Create a second random matrix
>>> Xp = numpy.random.rand(6, 2)

>>> # Check orthogonality of X and Xp
>>> numpy.around(X.T @ Xp, decimals=3)
array([[0.806, 2.207],
       [1.017, 2.91 ],
       [0.93 , 1.91 ],
       [0.903, 2.455]])

>>> # Orthogonalize X against X
>>> from detkit import ortho_complement
>>> ortho_complement(Xp, X, X_orth=False)

>>> # Check orthogonality of X and Xp again
>>> numpy.around(X.T @ Xp, decimals=14)
array([[ 0., -0.],
       [ 0., -0.],
       [ 0., -0.],
       [ 0., -0.]])

Input Matrix \(\mathbf{X}\) is Orthogonal:

>>> # Create a random matrix
>>> import numpy
>>> numpy.random.seed(0)
>>> X = numpy.random.rand(6, 4)

>>> # Orthogonalize matrix X
>>> from detkit import orthogonalize
>>> orthogonalize(X)

>>> # Create a second random matrix
>>> Xp = numpy.random.rand(6, 2)

>>> # Check orthogonality of X and Xp
>>> numpy.around(X.T @ Xp, decimals=3)
array([[ 0.492,  1.345],
       [ 0.283,  0.892],
       [ 0.336,  0.013],
       [-0.237,  0.222]])

>>> # Orthogonalize X against X
>>> from detkit import ortho_complement
>>> ortho_complement(Xp, X, X_orth=True)

>>> # Check orthogonality of X and Xp again
>>> numpy.around(X.T @ Xp, decimals=15)
array([[ 0., -0.],
       [ 0., -0.],
       [ 0., -0.],
       [ 0., -0.]])