-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
157 lines (133 loc) · 4.02 KB
/
main.py
File metadata and controls
157 lines (133 loc) · 4.02 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/python3
# -*- encoding: utf-8 -*-
import math
import sys
import numpy as np
def cholesky(A):
assert A.shape[0] == A.shape[1]
assert np.array_equal(A, A.T)
L = np.copy(A)
n = A.shape[0]
for k in range(n-1):
try:
L[k, k] = math.sqrt(L[k, k])
except ValueError:
print('The matrix is not positive definite.')
sys.exit(1)
for i in range(k+1, n):
try:
L[i, k] /= L[k, k]
except ZeroDivisionError:
print('The matrix is not positive definite.')
sys.exit(1)
for j in range(k+1, n):
for i in range(j, n):
L[i, j] -= L[i, k] * L[j, k]
try:
L[n-1, n-1] = math.sqrt(L[n-1, n-1])
except ValueError:
print('The matrix is not positive definite.')
sys.exit(1)
for i in range(n-1):
for j in range(i+1, n):
L[i, j] = 0.0
return L
def qr_givens(A_orig):
def _rot_matrix(a, b):
norm = math.sqrt(a**2 + b**2)
assert norm != 0
c = a / norm
s = -b / norm
return np.array([[c, -s], [s, c]], dtype=float)
def _join_bigger_rotation(total_size, rot, i, j):
ret = np.identity(total_size, dtype=float)
ret[i, i] = rot[0, 0]
ret[j, j] = rot[1, 1]
ret[i, j] = rot[0, 1]
ret[j, i] = rot[1, 0]
return ret
A = np.copy(A_orig)
assert A.shape[0] == A.shape[1]
n = A.shape[0]
Q = np.identity(n, dtype=float)
for column_index in range(n):
for row_index in range(column_index + 1, n):
if A[row_index, column_index] == 0.0:
continue
a = A[column_index, column_index]
b = A[row_index, column_index]
curr_q_smaller = _rot_matrix(a, b)
curr_q = _join_bigger_rotation(n, curr_q_smaller, column_index, row_index)
Q = np.dot(curr_q, Q)
A = np.dot(curr_q, A)
Q = Q.T
return Q, A
def qr_householder(A_orig):
def _householder_matrix(x):
aux = np.zeros(x.shape[0], dtype=float)
aux[0] = np.linalg.norm(x)
assert aux[0] != 0.0
v = x - aux
return np.identity(x.shape[0]) - 2 * np.outer(v, v) / np.dot(v, v)
def _join_identity_q(total_size, q):
assert q.shape[0] == q.shape[1]
assert total_size >= q.shape[0]
ret = np.identity(total_size, dtype=float)
ret[-q.shape[0]:, -q.shape[0]:] = q
return ret
A = np.copy(A_orig)
assert A.shape[0] == A.shape[1]
n = A.shape[0]
Q = np.identity(n, dtype=float)
for column_index in range(n):
x = A[column_index:, column_index]
if np.array_equal(x, np.eye(1, x.shape[0], 0, dtype=float)[0]):
continue
curr_q_smaller = _householder_matrix(x)
curr_q = _join_identity_q(n, curr_q_smaller)
Q = np.dot(curr_q, Q)
A = np.dot(curr_q, A)
Q = Q.T
return Q, A
if __name__ == '__main__':
print('CHOLESKY')
print()
A = np.array([[1, 0, 0], [0, 2, -2], [0, -2, 3]], dtype=float)
print('A')
print(A)
L = cholesky(A)
print('L')
print(L)
print('np.dot(L, L.T)')
print(np.dot(L, L.T))
print()
print('-'*50)
print()
print('QR')
print()
A = np.array([[1, 1, 1], [0, 1, -1], [0, 1, 1]], dtype=float)
print('A')
print(A)
Q_numpy, R_numpy = np.linalg.qr(A)
print('Q_numpy')
print(Q_numpy)
print('R_numpy')
print(R_numpy)
print('np.dot(Q_numpy, R_numpy)')
print(np.dot(Q_numpy, R_numpy))
print()
Q_givens, R_givens = qr_givens(A)
print('Q_givens')
print(Q_givens)
print('R_givens')
print(R_givens)
print('np.dot(Q_givens, R_givens)')
print(np.dot(Q_givens, R_givens))
print()
Q_householder, R_householder = qr_householder(A)
print('Q_householder')
print(Q_householder)
print('R_householder')
print(R_householder)
print('np.dot(Q_householder, R_householder)')
print(np.dot(Q_householder, R_householder))