ARTS  2.2.66
test_sparse.cc
Go to the documentation of this file.
1 /* Copyright (C) 2003-2012
2  Stefan Buehler <sbuehler@ltu.se>
3  Mattias Ekstroem <ekstrom@rss.chalmers.se>
4 
5  This program is free software; you can redistribute it and/or modify it
6  under the terms of the GNU General Public License as published by the
7  Free Software Foundation; either version 2, or (at your option) any
8  later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program; if not, write to the Free Software
17  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
18  USA. */
19 
30 #include <stdexcept>
31 #include <iostream>
32 #include "matpackII.h"
33 #include "xml_io.h"
34 
35 void test3()
36 {
37  Sparse M(10,15);
38 
39  /*
40  cout << "M.nrows(), M.ncols() = "
41  << M.nrows() << ", " << M.ncols() << "\n";
42  */
43  for (Index i=3; i<10; ++i)
44  M.rw(i,i) = (Numeric)i+1;
45  M.rw(0,0) = 1;
46  M.rw(0,1) = 2;
47  M.rw(0,2) = 3;
48 
49 
50  cout << "\nM = \n" << M;
51 
52  /*
53  // Test Sparse matrix-Matrix multiplication
54  Matrix A(10,5);
55  Matrix C(15,5,2.0);
56  // C = 2;
57 
58  mult(A, M, C(Range(joker), Range(joker)));
59  cout << "\nA = \n" << A << "\n";
60 
61  */
62 
63  // Test Sparse-Sparse multiplication
64 // Sparse A(10,5);
65 // Sparse C(15,5);
66 // for (Index i=0; i<5; i++) {
67 // C.rw(i*3,i) = i*3+1;
68 // C.rw(i*3+1,i) = i*3+2;
69 // C.rw(i*3+2,i) = i*3+3;
70 // }
71 
72 // mult(A,M,C);
73 
74 // cout << "\nA = \n" << A;
75 
76  /*
77  // Test transpose
78  Sparse B(15,10);
79 
80  transpose(B,M);
81  cout << "\nM' = \n" << B;
82  */
83 
84  /*
85  // Test rw-operator
86  Sparse S(M);
87  S.rw(2,0) = 5;
88 
89  cout << "\nS(2,0) = " << S.ro(2,0) << "\n";
90 
91  cout << "\nS = \n" << S;
92  */
93 
94  /*
95  // Test vector multiplication
96  Vector y(20, 0.0);
97  Vector x(1,30,1);
98 
99  mult(y[Range(1,10,2)], S, x[Range(1,15,2)]);
100 
101  cout << "\ny = \n" << y << "\n";
102  */
103 }
104 
105 
106 // void test38()
107 // {
108 // cout << "Test sparse matrix - sparse matrix multiplication\n";
109 
110 // Sparse B(757,2271);
111 // Sparse C(B.ncols(),B.ncols());
112 // Sparse A(B.nrows(),B.ncols());
113 
114 // Index i=0;
115 // for (Index j=0; j<B.ncols(); j++) {
116 // B.rw(i,j) = (j+1.0);
117 // if( i<B.nrows()-1 )
118 // i++;
119 // else
120 // i=0;
121 // }
122 
123 // for (Index i=0; i<C.nrows(); i++)
124 // C.rw(i,i) = 1;
125 
126 // mult(A,B,C);
127 
128 // cout << "\n(Sparse) A = \n" << A;
129 // cout << "\n(Sparse) B = \n" << B;
130 // cout << "\n(Sparse) C = \n" << C;
131 
132 // Matrix a(5,15), b(5,15), c(15,15);
133 
134 // i=0;
135 // for (Index j=0; j<15; j++) {
136 // b(i,j) = j+1;
137 // if( i<4 )
138 // i++;
139 // else
140 // i=0;
141 // }
142 
143 // for (Index i=0; i<15; i++)
144 // c(i,i) = 1;
145 
146 // mult(a,b,c);
147 
148 // //cout << "\n(Full) a = \n" << a << "\n";
149 
150 // // cout << "\n(Sparse) B = \n" << B << "\n";
151 // // cout << "\n(Full) b = \n" << b << "\n";
152 // // cout << "\n(Sparse) C = \n" << C << "\n";
153 // // cout << "\n(Full) c = \n" << c << "\n";
154 
155 // }
156 
157 // void test39()
158 // {
159 // //Test sparse transpose function
160 // Sparse B(1000,2000);
161 // Sparse Bt(B.ncols(), B.nrows());
162 
163 // Index i=0;
164 // for (Index j=0; j<B.ncols(); j=j+2) {
165 // B.rw(i,j) = j+1;
166 // if( i<B.nrows()-2 )
167 // i += 2;
168 // else
169 // i=0;
170 // }
171 
172 // cout << "\nB = \n" << B;
173 
174 // transpose(Bt,B);
175 
176 // cout << "\ntranspose(B) = \n" << Bt << "\n";
177 // }
178 
179 void test40()
180 {
181  cout << "Testing the new simplified Sparse matrices:\n";
182 
183  Sparse A(3,3);
184  cout << "Empty A: " << A << "\n";
185 
186  A.rw(0,0) = 11;
187  A.rw(1,1) = 22;
188  A.rw(2,2) = 33;
189  cout << "Diagonal A:\n" << A << "\n";
190 
191  Vector b(1,3,1), c(3);
192  cout << "b:\n" << b << "\n";
193 
194  mult(c,A,b);
195  cout << "c = A*b (should be [11,44,99]):\n" << c << "\n";
196 
197  Matrix D(3,2);
198  D(joker,0) = b;
199  D(joker,1) = b;
200  D(joker,1) *= 2;
201  cout << "D:\n" << D << "\n";
202 
203  Matrix E(3,2);
204  mult(E,A,D);
205  cout << "E = A*D (should be [11,22],[44,88],[99,198]):\n" << E << "\n";
206 }
207 
208 void test41()
209 {
210  cout << "Testing transpose for the new simplified sparse matrices:\n";
211 
212  Sparse B(4,5);
213  Index r[] = {0, 1, 1, 2, 2, 2, 3, 1, 3};
214  Index c[] = {0, 0, 1, 1, 2, 3, 3, 4, 4};
215  for ( Index i=0; i<9; i++ )
216  B.rw(r[i],c[i]) = (Numeric)(i+1);
217 
218  cout << "B:\n" << B << "\n";
219 
220  Sparse A(5,4);
221 
222  transpose(A, B);
223 
224  cout << "A:\n" << A << "\n";
225 
226  cout << "Testing with a fully occupied matrix:\n";
227 
228  for ( Index ri=0; ri<4; ri++ )
229  for ( Index ci=0; ci<5; ci++ )
230  {
231  B.rw(ri,ci) = (Numeric)(ri*10+ci);
232  }
233 
234  cout << "B:\n" << B << "\n";
235  transpose(A, B);
236  cout << "A:\n" << A << "\n";
237 }
238 
239 void test42()
240 {
241  cout << "Testing sparse-sparse matrix multiplication:\n";
242 
243  Sparse B(4,5);
244  Index r[] = {0, 1, 1, 2, 2, 2, 3, 1, 3};
245  Index c[] = {0, 0, 1, 1, 2, 3, 3, 4, 4};
246  for ( Index i=0; i<9; i++ )
247  B.rw(r[i],c[i]) = (Numeric)(i+1);
248 
249  Sparse A(4,4), Bt(5,4);
250  transpose(Bt,B);
251  mult(A,B,Bt);
252 
253  cout << "A:\n" << A << "\n";
254 }
255 
256 void test43()
257 {
258  cout << "Testing sparse copying:\n";
259 
260  Sparse B(4,5);
261  Index r[] = {0, 1, 1, 2, 2, 2, 3, 1, 3};
262  Index c[] = {0, 0, 1, 1, 2, 3, 3, 4, 4};
263  for ( Index i=0; i<9; i++ )
264  B.rw(r[i],c[i]) = (Numeric)(i+1);
265 
266  cout << "B:\n" << B << "\n";
267 
268  Sparse A;
269 
270  A = B;
271 
272  cout << "A:\n" << A << "\n";
273 
274  for ( Index i=0; i<100; ++i )
275  {
276  B.rw(0,0) += 1;
277  A = B;
278  }
279 
280  cout << "A now:\n" << A << "\n";
281 }
282 
283 void test44()
284 {
285  cout << "Test to insert row in sparse:\n";
286 
287  Vector v(5,10);
288 
289  Sparse B(4,5);
290  Index r[] = {0, 1, 1, 2, 2, 2, 3, 1, 3};
291  Index c[] = {0, 0, 1, 1, 2, 3, 3, 4, 4};
292  for ( Index i=0; i<9; i++ )
293  B.rw(r[i],c[i]) = (Numeric)(i+1);
294 
295  cout << "B["<<B.nrows()<<","<<B.ncols()<<"]:\n" << B << "\n";
296  cout << "v:\n" << v << "\n";
297 
298  B.insert_row(3, v);
299 
300  cout << "B (after insertion):\n" << B << "\n";
301 }
302 
303 void test45()
304 {
305  cout << "Test Sparse-Sparse multiplication reading matrices from xml "
306  "files:\n";
307 
308  Sparse A, B;
309  String a = "antenna.xml";
310  String b = "backend.xml";
311 
312  try {
313  cout << " Reading " << a << "...";
314  xml_read_from_file (a, A, Verbosity());
315  cout << "done.\n Reading " << b << "...";
316  xml_read_from_file (b, B, Verbosity());
317  cout << "done.\n";
318  } catch (runtime_error e) {
319  cerr << e.what () << endl;
320  }
321 
322  Sparse C(B.nrows(),A.ncols());
323  cout << " Performing multiplication...";
324  mult(C,B,A);
325  cout << "done.\n";
326 
327  //cout << "C=A*B:\n" << A << "\n";
328  try {
329  cout << " Writing product to file: test45.xml...";
330  xml_write_to_file ("test45.xml", C, FILE_TYPE_ASCII, 0, Verbosity());
331  cout << "done.\n";
332  } catch (runtime_error e) {
333  cerr << e.what () << endl;
334  }
335 }
336 
337 void test46()
338 {
339  cout << "Test transpose with large matrix read from xml file:\n";
340 
341  Sparse A;
342  String a = "backend.xml";
343 
344  try {
345  cout << " Reading " << a << "...";
346  xml_read_from_file (a, A, Verbosity());
347  cout << "done.\n";
348  } catch (runtime_error e) {
349  cerr << e.what () << endl;
350  }
351 
352  //cout << "A:\n" << A << endl;
353 
354  Sparse B(A.ncols(), A.nrows());
355  transpose(B,A);
356 
357  try {
358  cout << " Writing transpose(A) to file test46.xml" << endl;
359  xml_write_to_file ("test46.xml", B, FILE_TYPE_ASCII, 0, Verbosity());
360  } catch (runtime_error e) {
361  cerr << e.what () << endl;
362  }
363 
364  //cout << "transpose(A):\n" << B << endl;
365 }
366 
367 void test47()
368 {
369  cout << "Test make Identity matrix:\n";
370 
371  Sparse A;
372 
373  A.make_I(6,5);
374 
375  cout << "A:\n" << A << endl;
376 }
377 
378 void test48()
379 {
380  cout << "Test absolute values of sparse matrix:\n";
381 
382  Sparse B(4,5);
383  Index r[] = {0, 1, 1, 2, 2, 2, 3, 1, 3};
384  Index c[] = {0, 0, 1, 1, 2, 3, 3, 4, 4};
385  for ( Index i=0; i<9; i++ )
386  B.rw(r[i],c[i]) = -(Numeric)i*0.5;
387  cout << "B:\n" << B << endl;
388 
389  Sparse A( B );
390  abs(A,B);
391 
392  cout << "abs(B):\n" << A << endl;
393 
394 }
395 
396 void test49()
397 {
398  cout << "Testing sparse adding:\n";
399 
400  Sparse B(4,5);
401  Index rb[] = {1, 3};
402  Index cb[] = {1, 3};
403  for ( Index i=0; i<2; i++ )
404  B.rw(rb[i],cb[i]) = (Numeric)(i+1);
405 
406  Sparse C(4,5);
407  Index rc[] = {0, 1, 2};
408  Index cc[] = {0, 1, 2};
409  for ( Index i=0; i<3; i++ )
410  C.rw(rc[i],cc[i]) = (Numeric)(i+1);
411 
412  cout << "B:\n" << B << "\n";
413  cout << "C:\n" << C << "\n";
414 
415  Sparse A;
416  add (A, B, C);
417  cout << "A=B+C:\n" << A << "\n";
418  Sparse D;
419  sub (D, B, C);
420  cout << "D=B-C:\n" << D << "\n";
421 }
422 
423 int main()
424 {
425  // test3();
426  // test38();
427  // test39();
428  // test40();
429  // test41();
430  // test42();
431  // test43();
432  // test44();
433  // test45();
434  // test46();
435  // test47();
436  // test48();
437  test49();
438 
439  return 0;
440 }
Matrix
The Matrix class.
Definition: matpackI.h:788
main
int main()
Definition: test_sparse.cc:423
Sparse::ncols
Index ncols() const
Returns the number of columns.
Definition: matpackII.cc:62
Sparse::make_I
void make_I(Index r, Index c)
Make Identity matrix.
Definition: matpackII.cc:430
joker
const Joker joker
Sparse
The Sparse class.
Definition: matpackII.h:55
test43
void test43()
Definition: test_sparse.cc:256
Sparse::insert_row
void insert_row(Index r, Vector v)
Insert row function.
Definition: matpackII.cc:264
test40
void test40()
Definition: test_sparse.cc:179
test49
void test49()
Definition: test_sparse.cc:396
test47
void test47()
Definition: test_sparse.cc:367
test3
void test3()
Definition: test_sparse.cc:35
xml_read_from_file
void xml_read_from_file(const String &filename, T &type, const Verbosity &verbosity)
Reads data from XML file.
Definition: xml_io.cc:831
mult
void mult(VectorView y, const ConstMatrixView &M, const ConstVectorView &x)
Matrix Vector multiplication.
Definition: matpackI.cc:1648
my_basic_string
The implementation for String, the ARTS string class.
Definition: mystring.h:64
abs
#define abs(x)
Definition: continua.cc:20458
sub
void sub(Sparse &A, const Sparse &B, const Sparse &C)
Sparse - Sparse subtraction.
Definition: matpackII.cc:920
Numeric
NUMERIC Numeric
The type to use for all floating point numbers.
Definition: matpack.h:29
Verbosity
Definition: messages.h:50
C
#define C(a, b)
Definition: Faddeeva.cc:254
transpose
ConstMatrixView transpose(ConstMatrixView m)
Const version of transpose.
Definition: matpackI.cc:1799
test46
void test46()
Definition: test_sparse.cc:337
test41
void test41()
Definition: test_sparse.cc:208
test44
void test44()
Definition: test_sparse.cc:283
test48
void test48()
Definition: test_sparse.cc:378
xml_write_to_file
void xml_write_to_file(const String &filename, const T &type, const FileType ftype, const Index no_clobber, const Verbosity &verbosity)
Write data to XML file.
Definition: xml_io.cc:982
Sparse::nrows
Index nrows() const
Returns the number of rows.
Definition: matpackII.cc:56
Sparse::rw
Numeric & rw(Index r, Index c)
Read and write index operator.
Definition: matpackII.cc:93
matpackII.h
Header file for sparse matrices.
FILE_TYPE_ASCII
@ FILE_TYPE_ASCII
Definition: xml_io.h:39
Index
INDEX Index
The type to use for all integer numbers and indices.
Definition: matpack.h:35
M
#define M
Definition: rng.cc:196
test42
void test42()
Definition: test_sparse.cc:239
Vector
The Vector class.
Definition: matpackI.h:556
test45
void test45()
Definition: test_sparse.cc:303
xml_io.h
This file contains basic functions to handle XML data files.
add
void add(Sparse &A, const Sparse &B, const Sparse &C)
Sparse - Sparse addition.
Definition: matpackII.cc:871