00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00030 #include <stdexcept>
00031 #include <iostream>
00032 #include "matpackII.h"
00033 #include "xml_io.h"
00034
00035 void test3()
00036 {
00037 Sparse M(10,15);
00038
00039
00040
00041
00042
00043 for (Index i=3; i<10; ++i)
00044 M.rw(i,i) = (Numeric)i+1;
00045 M.rw(0,0) = 1;
00046 M.rw(0,1) = 2;
00047 M.rw(0,2) = 3;
00048
00049
00050 cout << "\nM = \n" << M;
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103 }
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179 void test40()
00180 {
00181 cout << "Testing the new simplified Sparse matrices:\n";
00182
00183 Sparse A(3,3);
00184 cout << "Empty A: " << A << "\n";
00185
00186 A.rw(0,0) = 11;
00187 A.rw(1,1) = 22;
00188 A.rw(2,2) = 33;
00189 cout << "Diagonal A:\n" << A << "\n";
00190
00191 Vector b(1,3,1), c(3);
00192 cout << "b:\n" << b << "\n";
00193
00194 mult(c,A,b);
00195 cout << "c = A*b (should be [11,44,99]):\n" << c << "\n";
00196
00197 Matrix D(3,2);
00198 D(joker,0) = b;
00199 D(joker,1) = b;
00200 D(joker,1) *= 2;
00201 cout << "D:\n" << D << "\n";
00202
00203 Matrix E(3,2);
00204 mult(E,A,D);
00205 cout << "E = A*D (should be [11,22],[44,88],[99,198]):\n" << E << "\n";
00206 }
00207
00208 void test41()
00209 {
00210 cout << "Testing transpose for the new simplified sparse matrices:\n";
00211
00212 Sparse B(4,5);
00213 Index r[] = {0, 1, 1, 2, 2, 2, 3, 1, 3};
00214 Index c[] = {0, 0, 1, 1, 2, 3, 3, 4, 4};
00215 for ( Index i=0; i<9; i++ )
00216 B.rw(r[i],c[i]) = (Numeric)(i+1);
00217
00218 cout << "B:\n" << B << "\n";
00219
00220 Sparse A(5,4);
00221
00222 transpose(A, B);
00223
00224 cout << "A:\n" << A << "\n";
00225
00226 cout << "Testing with a fully occupied matrix:\n";
00227
00228 for ( Index ri=0; ri<4; ri++ )
00229 for ( Index ci=0; ci<5; ci++ )
00230 {
00231 B.rw(ri,ci) = (Numeric)(ri*10+ci);
00232 }
00233
00234 cout << "B:\n" << B << "\n";
00235 transpose(A, B);
00236 cout << "A:\n" << A << "\n";
00237 }
00238
00239 void test42()
00240 {
00241 cout << "Testing sparse-sparse matrix multiplication:\n";
00242
00243 Sparse B(4,5);
00244 Index r[] = {0, 1, 1, 2, 2, 2, 3, 1, 3};
00245 Index c[] = {0, 0, 1, 1, 2, 3, 3, 4, 4};
00246 for ( Index i=0; i<9; i++ )
00247 B.rw(r[i],c[i]) = (Numeric)(i+1);
00248
00249 Sparse A(4,4), Bt(5,4);
00250 transpose(Bt,B);
00251 mult(A,B,Bt);
00252
00253 cout << "A:\n" << A << "\n";
00254 }
00255
00256 void test43()
00257 {
00258 cout << "Testing sparse copying:\n";
00259
00260 Sparse B(4,5);
00261 Index r[] = {0, 1, 1, 2, 2, 2, 3, 1, 3};
00262 Index c[] = {0, 0, 1, 1, 2, 3, 3, 4, 4};
00263 for ( Index i=0; i<9; i++ )
00264 B.rw(r[i],c[i]) = (Numeric)(i+1);
00265
00266 cout << "B:\n" << B << "\n";
00267
00268 Sparse A;
00269
00270 A = B;
00271
00272 cout << "A:\n" << A << "\n";
00273
00274 for ( Index i=0; i<100; ++i )
00275 {
00276 B.rw(0,0) += 1;
00277 A = B;
00278 }
00279
00280 cout << "A now:\n" << A << "\n";
00281 }
00282
00283 void test44()
00284 {
00285 cout << "Test to insert row in sparse:\n";
00286
00287 Vector v(5,10);
00288
00289 Sparse B(4,5);
00290 Index r[] = {0, 1, 1, 2, 2, 2, 3, 1, 3};
00291 Index c[] = {0, 0, 1, 1, 2, 3, 3, 4, 4};
00292 for ( Index i=0; i<9; i++ )
00293 B.rw(r[i],c[i]) = (Numeric)(i+1);
00294
00295 cout << "B["<<B.nrows()<<","<<B.ncols()<<"]:\n" << B << "\n";
00296 cout << "v:\n" << v << "\n";
00297
00298 B.insert_row(3, v);
00299
00300 cout << "B (after insertion):\n" << B << "\n";
00301 }
00302
00303 void test45()
00304 {
00305 cout << "Test Sparse-Sparse multiplication reading matrices from xml "
00306 "files:\n";
00307
00308 Sparse A, B;
00309 String a = "antenna.xml";
00310 String b = "backend.xml";
00311
00312 try {
00313 cout << " Reading " << a << "...";
00314 xml_read_from_file (a, A);
00315 cout << "done.\n Reading " << b << "...";
00316 xml_read_from_file (b, B);
00317 cout << "done.\n";
00318 } catch (runtime_error e) {
00319 cerr << e.what () << endl;
00320 }
00321
00322 Sparse C(B.nrows(),A.ncols());
00323 cout << " Performing multiplication...";
00324 mult(C,B,A);
00325 cout << "done.\n";
00326
00327
00328 try {
00329 cout << " Writing product to file: test45.xml...";
00330 xml_write_to_file ("test45.xml", C);
00331 cout << "done.\n";
00332 } catch (runtime_error e) {
00333 cerr << e.what () << endl;
00334 }
00335 }
00336
00337 void test46()
00338 {
00339 cout << "Test transpose with large matrix read from xml file:\n";
00340
00341 Sparse A;
00342 String a = "backend.xml";
00343
00344 try {
00345 cout << " Reading " << a << "...";
00346 xml_read_from_file (a, A);
00347 cout << "done.\n";
00348 } catch (runtime_error e) {
00349 cerr << e.what () << endl;
00350 }
00351
00352
00353
00354 Sparse B(A.ncols(), A.nrows());
00355 transpose(B,A);
00356
00357 try {
00358 cout << " Writing transpose(A) to file test46.xml" << endl;
00359 xml_write_to_file ("test46.xml", B);
00360 } catch (runtime_error e) {
00361 cerr << e.what () << endl;
00362 }
00363
00364
00365 }
00366
00367 void test47()
00368 {
00369 cout << "Test make Identity matrix:\n";
00370
00371 Sparse A;
00372
00373 A.make_I(6,5);
00374
00375 cout << "A:\n" << A << endl;
00376 }
00377
00378 void test48()
00379 {
00380 cout << "Test absolute values of sparse matrix:\n";
00381
00382 Sparse B(4,5);
00383 Index r[] = {0, 1, 1, 2, 2, 2, 3, 1, 3};
00384 Index c[] = {0, 0, 1, 1, 2, 3, 3, 4, 4};
00385 for ( Index i=0; i<9; i++ )
00386 B.rw(r[i],c[i]) = -(Numeric)i*0.5;
00387 cout << "B:\n" << B << endl;
00388
00389 Sparse A( B );
00390 abs(A,B);
00391
00392 cout << "abs(B):\n" << A << endl;
00393
00394 }
00395
00396 int main()
00397 {
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409 test48();
00410
00411 return 0;
00412 }