import numpy as np
import numpy.linalg as la
A = np.random.randn(3, 3)
def test_orthogonality(Q):
print("Q:")
print(Q)
print("Q^T Q:")
QtQ = np.dot(Q.T, Q)
QtQ[np.abs(QtQ) < 1e-15] = 0
print(QtQ)
Q = np.zeros(A.shape)
Now let us generalize the process we used for three vectors earlier:
for k in range(A.shape[1]):
avec = A[:, k]
q = avec
for j in range(k):
q = q - np.dot(avec, Q[:,j])*Q[:,j]
Q[:, k] = q/la.norm(q)
This procedure is called Gram-Schmidt Orthonormalization.
test_orthogonality(Q)
Now let us try a different example (Source):
np.set_printoptions(precision=13)
eps = 1e-8
A = np.array([
[1, 1, 1],
[eps,eps,0],
[eps,0, eps]
])
A
Q = np.zeros(A.shape)
for k in range(A.shape[1]):
avec = A[:, k]
q = avec
for j in range(k):
print(q)
q = q - np.dot(avec, Q[:,j])*Q[:,j]
print(q)
q = q/la.norm(q)
Q[:, k] = q
print("norm -->", q)
print("-------")
test_orthogonality(Q)
Questions:
Q = np.zeros(A.shape)
for k in range(A.shape[1]):
q = A[:, k]
for j in range(k):
q = q - np.dot(q, Q[:,j])*Q[:,j]
Q[:, k] = q/la.norm(q)
test_orthogonality(Q)
This procedure is called Modified Gram-Schmidt Orthogonalization.
Questions: