-
Notifications
You must be signed in to change notification settings - Fork 0
/
GramSchmidt.py
32 lines (27 loc) · 1.03 KB
/
GramSchmidt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import numpy as np
class GramSchmidt(object):
def __init__(self, dtype=np.int32):
self.dtype = dtype
def qrDecomSimple(self, matrix):
nrow, ncol = matrix.shape
Q1 = matrix.copy()
R = np.zeros((ncol, ncol), dtype=self.dtype)
for j in range(ncol):
for i in range(j):
R[i, j] = np.dot(Q1[:, i], matrix[:, j])
Q1[:, j] -= R[i, j] * Q1[:, i]
R[j, j] = np.dot(Q1[:, j], Q1[:, j])
Q1[:, j] /= R[j, j]
return Q1, R
def qrDecompose(self, matrix):
''' Modified (stable) version of Gram Schmidt decomposition '''
nrow, ncol = matrix.shape
Q1 = matrix.copy()
R = np.zeros((ncol, ncol), dtype=self.dtype)
for j in range(ncol):
for i in range(j):
R[i, j] = np.dot(Q1[:, i], Q1[:, j])
Q1[:, j] -= R[i, j] * Q1[:, i]
R[j, j] = np.dot(Q1[:, j], Q1[:, j])
Q1[:, j] /= R[j, j]
return Q1, R