1 | //$$ hholder.cpp QR decomposition |
---|
2 | |
---|
3 | // Copyright (C) 1991,2,3,4: R B Davies |
---|
4 | |
---|
5 | #define WANT_MATH |
---|
6 | |
---|
7 | #include "include.h" |
---|
8 | |
---|
9 | #include "newmatap.h" |
---|
10 | |
---|
11 | #ifdef use_namespace |
---|
12 | namespace NEWMAT { |
---|
13 | #endif |
---|
14 | |
---|
15 | #ifdef DO_REPORT |
---|
16 | #define REPORT { static ExeCounter ExeCount(__LINE__,16); ++ExeCount; } |
---|
17 | #else |
---|
18 | #define REPORT {} |
---|
19 | #endif |
---|
20 | |
---|
21 | |
---|
22 | /*************************** QR decompositions ***************************/ |
---|
23 | |
---|
24 | inline Real square(Real x) { return x*x; } |
---|
25 | |
---|
26 | void QRZT(Matrix& X, LowerTriangularMatrix& L) |
---|
27 | { |
---|
28 | REPORT |
---|
29 | Tracer et("QZT(1)"); |
---|
30 | int n = X.Ncols(); int s = X.Nrows(); L.ReSize(s); |
---|
31 | Real* xi = X.Store(); int k; |
---|
32 | for (int i=0; i<s; i++) |
---|
33 | { |
---|
34 | Real sum = 0.0; |
---|
35 | Real* xi0=xi; k=n; while(k--) { sum += square(*xi++); } |
---|
36 | sum = sqrt(sum); |
---|
37 | L.element(i,i) = sum; |
---|
38 | if (sum==0.0) Throw(SingularException(L)); |
---|
39 | Real* xj0=xi0; k=n; while(k--) { *xj0++ /= sum; } |
---|
40 | for (int j=i+1; j<s; j++) |
---|
41 | { |
---|
42 | sum=0.0; |
---|
43 | xi=xi0; Real* xj=xj0; k=n; while(k--) { sum += *xi++ * *xj++; } |
---|
44 | xi=xi0; k=n; while(k--) { *xj0++ -= sum * *xi++; } |
---|
45 | L.element(j,i) = sum; |
---|
46 | } |
---|
47 | } |
---|
48 | } |
---|
49 | |
---|
50 | void QRZT(const Matrix& X, Matrix& Y, Matrix& M) |
---|
51 | { |
---|
52 | REPORT |
---|
53 | Tracer et("QRZT(2)"); |
---|
54 | int n = X.Ncols(); int s = X.Nrows(); int t = Y.Nrows(); |
---|
55 | if (Y.Ncols() != n) |
---|
56 | { Throw(ProgramException("Unequal row lengths",X,Y)); } |
---|
57 | M.ReSize(t,s); |
---|
58 | Real* xi = X.Store(); int k; |
---|
59 | for (int i=0; i<s; i++) |
---|
60 | { |
---|
61 | Real* xj0 = Y.Store(); Real* xi0 = xi; |
---|
62 | for (int j=0; j<t; j++) |
---|
63 | { |
---|
64 | Real sum=0.0; |
---|
65 | xi=xi0; Real* xj=xj0; k=n; while(k--) { sum += *xi++ * *xj++; } |
---|
66 | xi=xi0; k=n; while(k--) { *xj0++ -= sum * *xi++; } |
---|
67 | M.element(j,i) = sum; |
---|
68 | } |
---|
69 | } |
---|
70 | } |
---|
71 | |
---|
72 | /* |
---|
73 | void QRZ(Matrix& X, UpperTriangularMatrix& U) |
---|
74 | { |
---|
75 | Tracer et("QRZ(1)"); |
---|
76 | int n = X.Nrows(); int s = X.Ncols(); U.ReSize(s); |
---|
77 | Real* xi0 = X.Store(); int k; |
---|
78 | for (int i=0; i<s; i++) |
---|
79 | { |
---|
80 | Real sum = 0.0; |
---|
81 | Real* xi = xi0; k=n; while(k--) { sum += square(*xi); xi+=s; } |
---|
82 | sum = sqrt(sum); |
---|
83 | U.element(i,i) = sum; |
---|
84 | if (sum==0.0) Throw(SingularException(U)); |
---|
85 | Real* xj0=xi0; k=n; while(k--) { *xj0 /= sum; xj0+=s; } |
---|
86 | xj0 = xi0; |
---|
87 | for (int j=i+1; j<s; j++) |
---|
88 | { |
---|
89 | sum=0.0; |
---|
90 | xi=xi0; k=n; xj0++; Real* xj=xj0; |
---|
91 | while(k--) { sum += *xi * *xj; xi+=s; xj+=s; } |
---|
92 | xi=xi0; k=n; xj=xj0; |
---|
93 | while(k--) { *xj -= sum * *xi; xj+=s; xi+=s; } |
---|
94 | U.element(i,j) = sum; |
---|
95 | } |
---|
96 | xi0++; |
---|
97 | } |
---|
98 | } |
---|
99 | */ |
---|
100 | |
---|
101 | void QRZ(Matrix& X, UpperTriangularMatrix& U) |
---|
102 | { |
---|
103 | REPORT |
---|
104 | Tracer et("QRZ(1)"); |
---|
105 | int n = X.Nrows(); int s = X.Ncols(); U.ReSize(s); U = 0.0; |
---|
106 | Real* xi0 = X.Store(); Real* u0 = U.Store(); Real* u; |
---|
107 | int j, k; int J = s; int i = s; |
---|
108 | while (i--) |
---|
109 | { |
---|
110 | Real* xj0 = xi0; Real* xi = xi0; k = n; |
---|
111 | if (k) for (;;) |
---|
112 | { |
---|
113 | u = u0; Real Xi = *xi; Real* xj = xj0; |
---|
114 | j = J; while(j--) *u++ += Xi * *xj++; |
---|
115 | if (!(--k)) break; |
---|
116 | xi += s; xj0 += s; |
---|
117 | } |
---|
118 | |
---|
119 | Real sum = sqrt(*u0); *u0 = sum; u = u0+1; |
---|
120 | if (sum == 0.0) Throw(SingularException(U)); |
---|
121 | int J1 = J-1; j = J1; while(j--) *u++ /= sum; |
---|
122 | |
---|
123 | xj0 = xi0; xi = xi0++; k = n; |
---|
124 | if (k) for (;;) |
---|
125 | { |
---|
126 | u = u0+1; Real Xi = *xi; Real* xj = xj0; |
---|
127 | Xi /= sum; *xj++ = Xi; |
---|
128 | j = J1; while(j--) *xj++ -= *u++ * Xi; |
---|
129 | if (!(--k)) break; |
---|
130 | xi += s; xj0 += s; |
---|
131 | } |
---|
132 | u0 += J--; |
---|
133 | } |
---|
134 | } |
---|
135 | |
---|
136 | void QRZ(const Matrix& X, Matrix& Y, Matrix& M) |
---|
137 | { |
---|
138 | REPORT |
---|
139 | Tracer et("QRZ(2)"); |
---|
140 | int n = X.Nrows(); int s = X.Ncols(); int t = Y.Ncols(); |
---|
141 | if (Y.Nrows() != n) |
---|
142 | { Throw(ProgramException("Unequal column lengths",X,Y)); } |
---|
143 | M.ReSize(s,t); M = 0;Real* m0 = M.Store(); Real* m; |
---|
144 | Real* xi0 = X.Store(); |
---|
145 | int j, k; int i = s; |
---|
146 | while (i--) |
---|
147 | { |
---|
148 | Real* xj0 = Y.Store(); Real* xi = xi0; k = n; |
---|
149 | if (k) for (;;) |
---|
150 | { |
---|
151 | m = m0; Real Xi = *xi; Real* xj = xj0; |
---|
152 | j = t; while(j--) *m++ += Xi * *xj++; |
---|
153 | if (!(--k)) break; |
---|
154 | xi += s; xj0 += t; |
---|
155 | } |
---|
156 | |
---|
157 | xj0 = Y.Store(); xi = xi0++; k = n; |
---|
158 | if (k) for (;;) |
---|
159 | { |
---|
160 | m = m0; Real Xi = *xi; Real* xj = xj0; |
---|
161 | j = t; while(j--) *xj++ -= *m++ * Xi; |
---|
162 | if (!(--k)) break; |
---|
163 | xi += s; xj0 += t; |
---|
164 | } |
---|
165 | m0 += t; |
---|
166 | } |
---|
167 | } |
---|
168 | |
---|
169 | /* |
---|
170 | |
---|
171 | void QRZ(const Matrix& X, Matrix& Y, Matrix& M) |
---|
172 | { |
---|
173 | Tracer et("QRZ(2)"); |
---|
174 | int n = X.Nrows(); int s = X.Ncols(); int t = Y.Ncols(); |
---|
175 | if (Y.Nrows() != n) |
---|
176 | { Throw(ProgramException("Unequal column lengths",X,Y)); } |
---|
177 | M.ReSize(s,t); |
---|
178 | Real* xi0 = X.Store(); int k; |
---|
179 | for (int i=0; i<s; i++) |
---|
180 | { |
---|
181 | Real* xj0 = Y.Store(); |
---|
182 | for (int j=0; j<t; j++) |
---|
183 | { |
---|
184 | Real sum=0.0; |
---|
185 | Real* xi=xi0; Real* xj=xj0; k=n; |
---|
186 | while(k--) { sum += *xi * *xj; xi+=s; xj+=t; } |
---|
187 | xi=xi0; k=n; xj=xj0++; |
---|
188 | while(k--) { *xj -= sum * *xi; xj+=t; xi+=s; } |
---|
189 | M.element(i,j) = sum; |
---|
190 | } |
---|
191 | xi0++; |
---|
192 | } |
---|
193 | } |
---|
194 | */ |
---|
195 | |
---|
196 | #ifdef use_namespace |
---|
197 | } |
---|
198 | #endif |
---|
199 | |
---|