голубков

  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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
__author__ = 'Lenovo'
import pprint
from pprint import pprint
from math import sqrt
import numpy as np
np.set_printoptions(suppress=True, linewidth=160, precision=10)
import scipy
from scipy import linalg
import pprint
A = scipy.array([[1,2,3], [1,5,6], [1,8,9], [1,11,12]])
Q, R, P = scipy.linalg.qr(A, pivoting=True)
print "A:"
pprint.pprint(A)
print "Q:"
pprint.pprint(Q)
print "R:"
pprint.pprint(R)
print "P:"
pprint.pprint(P)
####################################################################################
def sign(x):
return cmp(x, 0)
def norm(x):
return sum(i ** 2 for i in x) ** 0.5
def house(x):
n = len(x)
mu = norm(x)
v = x
if mu != 0:
beta = x[0] + sign(x[0]) * mu
v = v / beta
v[0] = 1
return v
def rowHouse(a, v):
m = len(v)
beta = -2.0 / sum(i ** 2 for i in v)
vm = np.array([v]).transpose()
w = beta * np.dot(a.transpose(), vm)
p = np.dot(vm, w.transpose())
pa = a + p
return pa
def swap(a, r, k):
temp = a[:, k].copy()
a[:, k] = a[:, r]
a[:, r] = temp
def swapElems(c, r, k):
temp = c[k]
c[k] = c[r]
c[r] = temp
def qrColumnPivoting(permutation, a):
m = len(a)
n = len(a[0])
c = np.zeros(n)
for i in range(0, n):
c[i] = sum(j**2 for j in a[:,i])
r = 0
t = c.max()
k = c.argmax()
while t > 0:
permutation[r] = k
permutation[k] = r
swap(a, r, k)
swapElems(c, r, k)
v = house(a[r:m, r])
a[r:m, r:n] = rowHouse(a[r:m, r:n], v)
a[r+1:m, r] = v[1:m]
for i in range(r+1, n):
c[i] -= a[r, i]**2
if r < n-1:
t = c[r+1:].max()
k = c[r+1:].argmax()
else:
t = 0
r += 1
return permutation, a, r
def makeQ(a, r):
m = len(a)
q = np.identity(m)
for j in range(r-1, -1, -1):
v = np.hstack(([1], a[j+1:m, j]))
q[j:m, j:m] = rowHouse(q[j:m, j:m], v)
return q
def fod(a, r):
rr = a[:r].transpose()
m = len(a)
u = np.identity(m-r+1)
for i in range(0, r):
v = house(a[r-1:m, i])
a[r-1:m, i:r] = rowHouse(a[r-1:m, i:r], v)
vsq = v.transpose() * v
u = np.dot(u, np.identity(m-r+1) - 2/sum(i ** 2 for i in v)*vsq)
return u
a = np.array([[1,2,3], [1,5,6], [1,8,9], [1,11,12]], dtype=float)
p, a, rank = qrColumnPivoting(np.arange(len(a[0])), a)
q = makeQ(a, rank)
u = fod(a, rank)
print "A:"
pprint.pprint(a)
print "P:"
pprint.pprint(p)
print "Q:"
pprint.pprint(q)
print "Q:"
pprint.pprint(np.dot(q, q.transpose()))
print "U:"
pprint.pprint(u)
print "U:"
pprint.pprint(np.dot(u, u.transpose()))
import numpy as np
import math
from numpy import dot
from numpy.linalg import norm
np.set_printoptions(suppress=True, linewidth=160, precision=15)
def transposition(n, i, j):
trans = np.identity(n)
trans[[i, j]] = trans[[j, i]]
return trans
def givens(n, i, j, x, y):
g_matrix = np.identity(n)
square = math.sqrt(x ** 2 + y ** 2)
cosinus = 1.0 * x / square
sinus = 1.0 * y / square
g_matrix[i, i] = cosinus
g_matrix[j, j] = cosinus
g_matrix[i, j] = sinus
g_matrix[j, i] = -sinus
#g_matrix[[i, j], [i, j]] = np.array([[math.cos(alpha), math.sin(alpha)],[-math.sin(alpha), math.cos(alpha)]])
return g_matrix
def givens_2(x, y):
g_matrix = np.zeros([2, 2])
square = math.sqrt(x ** 2 + y ** 2)
cosinus = 1.0 * x / square
sinus = 1.0 * y / square
g_matrix[0, 0] = cosinus
g_matrix[1, 1] = cosinus
g_matrix[0, 1] = sinus
g_matrix[1, 0] = -sinus
return g_matrix
def max_column_index(A):
return np.argmax(np.fromiter((norm(col) for col in A.T), dtype=np.float))
def qr_choise(A):
m, n = A.shape
Q = np.identity(m)
P = np.identity(n)
MIN_SIZE = min(m, n)
for j in xrange(MIN_SIZE):
max_index = max_column_index(A[j:, j:]) + j
if norm(A[j:, j:].T[max_index - j]) == 0:
print 'Break at rank', str(j-1)
break
trans = transposition(n, j, max_index)
A[:] = dot(A, trans)
P[:] = dot(trans, P)
for i in xrange(m - 1, j, -1):
givens2 = givens_2(A[j, j], A[i, j])
giv = givens(m, i, j, A[j, j], A[i, j])
A[[j, i], j:n] = dot(givens2, A[[j, i], j:n])
Q = dot(Q, giv)
return Q, P
A = scipy.array([[1,2,3], [1,5,6], [1,8,9], [1,11,12]])
print 'matrix rank of A:'
print np.linalg.matrix_rank(A)
#A = np.loadtxt('matrix.txt')
M = A.copy()
Q, P = qr_choise(A)
print 'R matrix:'
print A
print '*'*80
print 'Q matrix:'
print Q
print '*'*80
print 'Prove that Q is orthogonal : Q*Q^t'
print dot(Q, Q.T)
print '*'*80
print 'permutation matrix P:'
print P
print '*'*80
print 'Matrix A:'
print M
print 'QR:'
print dot(dot(Q, A), P)
print 'Difference:'
print dot(dot(Q, A), P) - M