-
Notifications
You must be signed in to change notification settings - Fork 214
/
Copy pathqf_matrix.cpp
153 lines (106 loc) · 3.2 KB
/
qf_matrix.cpp
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
// Minimal matrix implementation to carry out Hessenberg QR factorization
#undef _QF_MATRIX_DEBUG
#include "qf_common.h"
#include "qf_matrix.h"
// Matrix by vector (BLAS Level 2) multiplications
Rarray operator * (qf_matrix& M, Rarray& V) {
assert (V.size () == M.n);
Rarray W (M.n);
for (unsigned i = 0; i < M.n; i++) {
Rarray row = (* M.H)[slice (M.n * i, M.n, 1)];
W[i] = (row * V).sum ();
}
return W;
}
// Transpose form
Rarray operator * (Rarray& V, qf_matrix& M) {
assert (V.size () == M.n);
Rarray W (M.n);
for (unsigned i = 0; i < M.n; i ++) {
Rarray col = (* M.H)[slice (i, M.n, M.n)];
W[i] = (col * V).sum ();
}
return W;
}
// This function generates a generalized slice corresponding to a the
// submatrix H[r:k, c:l] (that is of size r-k+1 x c-l+1)
gslice qf_matrix::submx (unsigned r, unsigned k, unsigned c, unsigned l) {
valarray <size_t> len (2), stride (2);
r --; c--;
k -= r;
l -= c;
size_t offset = r * n + c;
len[0] = k;
len[1] = l;
stride[0] = n;
stride[1] = 1;
return gslice (offset, len, stride);
}
// Forms the complex product K = b * H [i:j,k:l] * V * Vt
Rarray qf_matrix::bHVtV (Rarray& V, gslice& gs) {
unsigned r = (gs.size ())[0];
unsigned s = (gs.size ())[1];
assert (V.size () == s);
// Shortcut if b == 0
if (V[0] == 0)
return Rarray (0.0, r * s);
// Form submatix into independant matrix
Rarray M (r * s), R (r * s);
M = (*H)[gs];
// Multiply H[i:j,k:l] by V: W = HV
Rarray W (r);
for (unsigned i = 0; i < r; i ++) {
Rarray VR (V[slice (1, s - 1, 1)]);
W[i] = M[i * s]; // Since V(0) = 1
VR *= M[slice (i * s + 1, s - 1, 1)];
W[i] += VR.sum ();
}
// And now form the inner product WVt
R[slice (0, r, s)] = W * V[0]; // Since V(0) = 1 and V[0] = b
for (unsigned i = 1; i < s; i ++)
R[slice (i, r, s)] = W * (V[i] * V[0]);
return R;
}
// Transposed form. Computes b * V * Vt * H[i:j,k:l]
Rarray qf_matrix::bVtVH (Rarray& V, gslice& gs) {
unsigned r = (gs. size ()) [0];
unsigned s = (gs. size ()) [1];
assert (V. size () == r);
// Shortcut if b == 0
if (V[0] == 0)
return Rarray (0.0, r * s);
// Form submatix into independant matrix
Rarray M (r * s), R (r * s);
M = (*H)[gs];
// Multiply Vt by H[i:j,k:l] (tW = Vt * H[i:j,k:l])
Rarray W (s);
for (unsigned i = 0; i < s; i ++) {
Rarray VR (V[slice (1, r - 1, 1)]);
W[i] = M[i]; // Since V(0) = 1
VR *= M[slice (i + s, r - 1, s)];
W[i] += VR.sum ();
}
// Inner product V* Wt
R[slice (0, s, 1)] = W * V[0]; // Since V(0) = 1 and V[0] = b
for (unsigned i = 1; i < r; i ++)
R[slice (s * i, s, 1)] = W * (V[i] * V[0]);
return R;
}
// Basic display
void qf_matrix::disp (unsigned m, unsigned n) {
cout << setprecision (4);
cout << endl;
for (unsigned i = 1; i <= m; i ++) {
for (unsigned j = 1; j <= n; j ++)
cout << setw (12) << left << (*this) (i, j);
cout << endl;
}
cout << endl;
}
// Vector display with name
void qf_matrix::disp (char id, Rarray& V) {
cout << setprecision (4) << id << " = [";
for (unsigned i = 0; i < V.size () - 1; i ++)
cout << setw (12) << left << V[i];
cout << V[V. size () - 1] << "]" << endl;
}