LS via QR

 ``` 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``` ```__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 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 q = makeQ(a, r) a = cleanA(a) return permutation, a, q, 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 cleanA(a): for i in range(0, len(a[0])): a[i+1:len(a), i] = np.zeros(len(a) - (i+1)) return a def fod(a, r): rr = a[:r].transpose() n = len(rr) z = np.identity(n) for i in range(0, r): v = house(rr[i:n, i]) rr[i:n, i:r] = rowHouse(rr[i:n, i:r], v) if i < n: rr[i+1:n, i] = v[1:n] # beta = 2.0 / sum(i ** 2 for i in v) # iuy = np.zeros(i) # kj = np.array(v) # vm = np.array([np.hstack((iuy, kj))]).transpose() # d = np.dot(vm, vm.transpose()) # znew = np.identity(n) - beta * d # z = np.dot(z, znew) u = makeQ(rr, r) rr = cleanA(rr) return u def makePMatr(p): n = len(p) pmatr = np.zeros((n, n)) for i in range (0, n): pmatr[i, p[i]] = 1 return pmatr #a = np.array([[1,2,3], [1,5,6], [1,8,9], [1,11,12]], dtype=float) a = np.array([[6,1], [1,6]], dtype=float) b = np.array([[9], [4]], dtype=float) m = len(a) n = len(a[0]) #x = np.array([[1],[1],[1]]) #b = np.dot(a, x) p, r, q, rank = qrColumnPivoting(np.arange(n), np.copy(a)) pmatr = makePMatr(p) print "Q:" pprint.pprint(q) print "R:" pprint.pprint(r) print "Q * Qt:" pprint.pprint(np.dot(q, q.transpose())) print "Q * R * P:" pprint.pprint(np.dot(np.dot(q, r), np.linalg.inv(pmatr))) ############################################### u = fod(r, rank) print "U:" pprint.pprint(u) print "U * Ut:" pprint.pprint(np.dot(u, u.transpose())) qta = np.dot(q.transpose(), a) print "Qt * A * P:" pprint.pprint(np.dot(qta, pmatr)) print "Qt * A * P * Z:" u = np.dot(pmatr, u) t = np.dot(qta, u) pprint.pprint(t) tinv = np.linalg.inv(t[:rank, :rank]) tm = np.dot(t[:rank, :rank], tinv) qt = q.transpose() cd = np.dot(qt, b) c = cd[:rank] if n > rank: y = np.vstack((np.dot(tinv, c), np.zeros(n-rank))) else: y = np.dot(tinv, c) x = np.dot(u, y) print "X:" pprint.pprint(x) ```