1 | //
|
---|
2 | // matrixtest.cc
|
---|
3 | //
|
---|
4 | // Copyright (C) 1996 Limit Point Systems, Inc.
|
---|
5 | //
|
---|
6 | // Author: Curtis Janssen <cljanss@limitpt.com>
|
---|
7 | // Maintainer: LPS
|
---|
8 | //
|
---|
9 | // This file is part of the SC Toolkit.
|
---|
10 | //
|
---|
11 | // The SC Toolkit is free software; you can redistribute it and/or modify
|
---|
12 | // it under the terms of the GNU Library General Public License as published by
|
---|
13 | // the Free Software Foundation; either version 2, or (at your option)
|
---|
14 | // any later version.
|
---|
15 | //
|
---|
16 | // The SC Toolkit is distributed in the hope that it will be useful,
|
---|
17 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
18 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
19 | // GNU Library General Public License for more details.
|
---|
20 | //
|
---|
21 | // You should have received a copy of the GNU Library General Public License
|
---|
22 | // along with the SC Toolkit; see the file COPYING.LIB. If not, write to
|
---|
23 | // the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
24 | //
|
---|
25 | // The U.S. Government is granted a limited license as per AL 91-7.
|
---|
26 | //
|
---|
27 |
|
---|
28 | #include <iostream>
|
---|
29 | #include <math.h>
|
---|
30 | #include <math/scmat/matrix.h>
|
---|
31 | #include <math/scmat/blkiter.h>
|
---|
32 | #include <math/scmat/elemop.h>
|
---|
33 | #include <util/misc/timer.h>
|
---|
34 |
|
---|
35 | using namespace std;
|
---|
36 | using namespace sc;
|
---|
37 |
|
---|
38 | class Prod3: public SCElementOp3 {
|
---|
39 | private:
|
---|
40 | public:
|
---|
41 | void process(SCMatrixBlockIter& i1,
|
---|
42 | SCMatrixBlockIter& i2,
|
---|
43 | SCMatrixBlockIter& i3) {
|
---|
44 | for (i1.reset(),i2.reset(),i3.reset();
|
---|
45 | i1;
|
---|
46 | ++i1,++i2,++i3) {
|
---|
47 | i1.set(i2.get()*i3.get());
|
---|
48 | }
|
---|
49 | }
|
---|
50 | int has_side_effects() { return 1; }
|
---|
51 | };
|
---|
52 |
|
---|
53 | void
|
---|
54 | randomize(RefSCMatrix&m)
|
---|
55 | {
|
---|
56 | for (int i=0; i<m.rowdim().n(); i++) {
|
---|
57 | for (int j=0; j<m.coldim().n(); j++) {
|
---|
58 | m.set_element(i,j,drand48());
|
---|
59 | }
|
---|
60 | }
|
---|
61 | }
|
---|
62 |
|
---|
63 | void
|
---|
64 | randomize(RefSymmSCMatrix&m)
|
---|
65 | {
|
---|
66 | for (int i=0; i<m.dim().n(); i++) {
|
---|
67 | for (int j=0; j<=i; j++) {
|
---|
68 | m.set_element(i,j,drand48());
|
---|
69 | }
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 | void
|
---|
74 | randomize(RefSCVector&m)
|
---|
75 | {
|
---|
76 | for (int i=0; i<m.dim().n(); i++) {
|
---|
77 | m.set_element(i,drand48());
|
---|
78 | }
|
---|
79 | }
|
---|
80 |
|
---|
81 | // test abstract matrices
|
---|
82 | void
|
---|
83 | matrixtest(Ref<SCMatrixKit> kit, Ref<KeyVal> keyval,
|
---|
84 | RefSCDimension d1,RefSCDimension d2,RefSCDimension d3,
|
---|
85 | bool have_svd)
|
---|
86 | {
|
---|
87 | if (d1.null()) d1 << keyval->describedclassvalue("d1");
|
---|
88 | if (d2.null()) d2 << keyval->describedclassvalue("d2");
|
---|
89 | if (d3.null()) d3 << keyval->describedclassvalue("d3");
|
---|
90 |
|
---|
91 | d1.print();
|
---|
92 | d2.print();
|
---|
93 | d3.print();
|
---|
94 |
|
---|
95 | tim_enter("matrixtest");
|
---|
96 | int i;
|
---|
97 | int j;
|
---|
98 |
|
---|
99 | // seed the random number generator
|
---|
100 | srand48(0);
|
---|
101 |
|
---|
102 | RefSCMatrix a(d1,d2,kit);
|
---|
103 | RefSCMatrix a2(d1,d2,kit);
|
---|
104 | RefSCMatrix a3(d1,d2,kit);
|
---|
105 | RefSCMatrix b(d2,d3,kit);
|
---|
106 | RefSCMatrix c(d1,d3,kit);
|
---|
107 |
|
---|
108 | cout << "a(" << a.nrow() << "," << a.ncol() << ")\n";
|
---|
109 |
|
---|
110 | a.assign(7.0);
|
---|
111 | a2.assign(5.0);
|
---|
112 | a3.assign(3.0);
|
---|
113 | Ref<SCElementOp3> op3 = new Prod3;
|
---|
114 | a.element_op(op3,a2,a3);
|
---|
115 | a.print("a");
|
---|
116 | a2.print("a2");
|
---|
117 | a3.print("a3");
|
---|
118 |
|
---|
119 | /////////////////////////////////
|
---|
120 |
|
---|
121 | RefSymmSCMatrix sa(d3,kit);
|
---|
122 | RefSymmSCMatrix sa2(d3,kit);
|
---|
123 | RefSymmSCMatrix sa3(d3,kit);
|
---|
124 |
|
---|
125 | sa.assign(7.0);
|
---|
126 | sa2.assign(5.0);
|
---|
127 | sa3.assign(3.0);
|
---|
128 | sa.element_op(op3,sa2,sa3);
|
---|
129 | sa.print("sa");
|
---|
130 | sa2.print("sa2");
|
---|
131 | sa3.print("sa3");
|
---|
132 |
|
---|
133 | /////////////////////////////////
|
---|
134 |
|
---|
135 | RefDiagSCMatrix da(d3,kit);
|
---|
136 | RefDiagSCMatrix da2(d3,kit);
|
---|
137 | RefDiagSCMatrix da3(d3,kit);
|
---|
138 |
|
---|
139 | da.assign(7.0);
|
---|
140 | da2.assign(5.0);
|
---|
141 | da3.assign(3.0);
|
---|
142 | da.element_op(op3,da2,da3);
|
---|
143 | da.print("da");
|
---|
144 | da2.print("da2");
|
---|
145 | da3.print("da3");
|
---|
146 |
|
---|
147 | /////////////////////////////////
|
---|
148 |
|
---|
149 | RefSCVector vva(d3,kit);
|
---|
150 | RefSCVector vva2(d3,kit);
|
---|
151 | RefSCVector vva3(d3,kit);
|
---|
152 |
|
---|
153 | vva.assign(7.0);
|
---|
154 | vva2.assign(5.0);
|
---|
155 | vva3.assign(3.0);
|
---|
156 | vva.element_op(op3,vva2,vva3);
|
---|
157 | vva.print("vva");
|
---|
158 | vva2.print("vva2");
|
---|
159 | vva3.print("vva3");
|
---|
160 |
|
---|
161 | ////////////////////////////////
|
---|
162 |
|
---|
163 | a.assign(0.0);
|
---|
164 | b.assign(1.0);
|
---|
165 | c.assign(2.0);
|
---|
166 |
|
---|
167 | a.print("a");
|
---|
168 | b.print("b");
|
---|
169 | c.print("c");
|
---|
170 |
|
---|
171 | tim_enter("mxm");
|
---|
172 | RefSCMatrix d = c * b.t();
|
---|
173 | tim_exit("mxm");
|
---|
174 |
|
---|
175 | d.print("d");
|
---|
176 |
|
---|
177 | RefSCDimension d4; d4 << keyval->describedclassvalue("d4");
|
---|
178 | int nd4 = d4->n();
|
---|
179 | cout << "n4 = " << nd4 << endl;
|
---|
180 | d4.print();
|
---|
181 | RefSCMatrix aaa(d4,d4,kit);
|
---|
182 | RefSCMatrix bbb(d4,d4,kit);
|
---|
183 | aaa.assign(1.0);
|
---|
184 | bbb.assign(2.0);
|
---|
185 | tim_enter("mxm2");
|
---|
186 | RefSCMatrix ccc = aaa*bbb;
|
---|
187 | tim_exit("mxm2");
|
---|
188 |
|
---|
189 | d.print("d later");
|
---|
190 |
|
---|
191 | RefSymmSCMatrix e(d3,kit);
|
---|
192 |
|
---|
193 | e.assign(1.0);
|
---|
194 | e.print("e");
|
---|
195 | e.eigvals().print("e.eigvals()");
|
---|
196 | e.eigvecs().print("e.eigvecs()");
|
---|
197 |
|
---|
198 | RefSymmSCMatrix f(d3,kit);
|
---|
199 | for (i=0; i<d3.n(); i++) {
|
---|
200 | for (j=0; j<=i; j++) {
|
---|
201 | f(i,j) = i + sqrt((double)j);
|
---|
202 | }
|
---|
203 | }
|
---|
204 | f.print("f");
|
---|
205 | f.eigvals().print("f.eigvals()");
|
---|
206 | f.i().print("f.i()");
|
---|
207 |
|
---|
208 | RefSymmSCMatrix h(d3,kit);
|
---|
209 | for (i=0; i<d3.n(); i++) {
|
---|
210 | for (j=0; j<=i; j++) {
|
---|
211 | h(i,j) = f(i,j);
|
---|
212 | }
|
---|
213 | }
|
---|
214 | h.print("h should be equal to f");
|
---|
215 |
|
---|
216 | RefSCMatrix g(d3,d3,kit);
|
---|
217 | for (i=0; i<d3.n(); i++) {
|
---|
218 | for (j=0; j<d3.n(); j++) {
|
---|
219 | if (i>j) g(i,j) = i + sqrt((double)j);
|
---|
220 | else g(i,j) = j + sqrt((double)i);
|
---|
221 | }
|
---|
222 | }
|
---|
223 | g.print("g");
|
---|
224 | g.i().print("g.i()");
|
---|
225 | (g * g.i()).print("g * g.i()");
|
---|
226 |
|
---|
227 | if (have_svd) {
|
---|
228 | g.gi().print("g.gi()");
|
---|
229 | (g * g.gi()).print("g * g.gi()");
|
---|
230 | (g.gi() * g).print("g.gi() * g");
|
---|
231 | }
|
---|
232 |
|
---|
233 | RefSCVector v(d3,kit);
|
---|
234 | for (i=0; i<d3.n(); i++) {
|
---|
235 | v(i) = 1.0/(i+1);
|
---|
236 | }
|
---|
237 | v.print("Vector v");
|
---|
238 |
|
---|
239 | RefSCVector wa(d3,kit);
|
---|
240 | RefSCMatrix ma(d1,d3,kit);
|
---|
241 | randomize(ma);
|
---|
242 | randomize(wa);
|
---|
243 | RefSCVector va = ma * wa;
|
---|
244 | ma.print("Matrix ma");
|
---|
245 | va.print("Vector va");
|
---|
246 | wa.print("Vector wa");
|
---|
247 |
|
---|
248 | if (have_svd) {
|
---|
249 | ma.gi().print("ma.gi()");
|
---|
250 | (ma * ma.gi()).print("ma * ma.gi()");
|
---|
251 | (ma.gi() * ma).print("ma.gi() * ma");
|
---|
252 | }
|
---|
253 |
|
---|
254 | RefSCVector wb(d1,kit);
|
---|
255 | RefSCMatrix mb(d3,d1,kit);
|
---|
256 | randomize(mb);
|
---|
257 | randomize(wb);
|
---|
258 | RefSCVector vb = mb * wb;
|
---|
259 | ma.print("Matrix mb");
|
---|
260 | va.print("Vector vb");
|
---|
261 | wa.print("Vector wb");
|
---|
262 |
|
---|
263 | if (have_svd) {
|
---|
264 | mb.gi().print("mb.gi()");
|
---|
265 | (mb * mb.gi()).print("mb * mb.gi()");
|
---|
266 | (mb.gi() * mb).print("mb.gi() * mb");
|
---|
267 | }
|
---|
268 |
|
---|
269 | RefSymmSCMatrix bmbt(d3,kit);
|
---|
270 | RefSCMatrix redundant_ortho(d2,d3,kit);
|
---|
271 | RefSymmSCMatrix bmbt_fixed(d2,kit);
|
---|
272 | RefSCMatrix bmbt_fix_red(d2,d3,kit);
|
---|
273 | bmbt.assign(0.0);
|
---|
274 | randomize(redundant_ortho);
|
---|
275 | randomize(bmbt_fixed);
|
---|
276 | randomize(bmbt_fix_red);
|
---|
277 | bmbt.accumulate_transform(redundant_ortho.t(), bmbt_fixed);
|
---|
278 | bmbt.print("bmbt");
|
---|
279 |
|
---|
280 | bmbt.assign(0.0);
|
---|
281 | bmbt.accumulate_transform(redundant_ortho, bmbt_fixed,
|
---|
282 | SCMatrix::TransposeTransform);
|
---|
283 | bmbt.print("bmbt (2)");
|
---|
284 |
|
---|
285 | bmbt.accumulate_symmetric_sum(redundant_ortho.t() * bmbt_fix_red);
|
---|
286 | bmbt.print("bmbt (symmetric_sum)");
|
---|
287 |
|
---|
288 | RefSCMatrix bmbt_test;
|
---|
289 | RefSCMatrix bmbt_fixed_test(d2,d2,kit);
|
---|
290 | for (i=0; i<d2.n(); i++) {
|
---|
291 | for (j=0; j<=i; j++) {
|
---|
292 | bmbt_fixed_test(i,j) = bmbt_fixed(i,j);
|
---|
293 | bmbt_fixed_test(j,i) = bmbt_fixed(i,j);
|
---|
294 | }
|
---|
295 | }
|
---|
296 | RefSCMatrix tmp = redundant_ortho.t() * bmbt_fix_red;
|
---|
297 | bmbt_test = redundant_ortho.t() * bmbt_fixed_test * redundant_ortho
|
---|
298 | + tmp + tmp.t();
|
---|
299 | bmbt_test.print("bmbt_test");
|
---|
300 |
|
---|
301 | tim_exit("matrixtest");
|
---|
302 | tim_print(0);
|
---|
303 | }
|
---|
304 |
|
---|
305 | /////////////////////////////////////////////////////////////////////////////
|
---|
306 |
|
---|
307 | // Local Variables:
|
---|
308 | // mode: c++
|
---|
309 | // c-file-style: "CLJ"
|
---|
310 | // End:
|
---|