ARTS  2.2.66
test_linalg.cc
Go to the documentation of this file.
1 /* Copyright (C) 2002-2012 Claudia Emde <claudia.emde@dlr.de>
2 
3  This program is free software; you can redistribute it and/or modify it
4  under the terms of the GNU General Public License as published by the
5  Free Software Foundation; either version 2, or (at your option) any
6  later version.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16  USA.
17 
18 */
19 
30 #include <iostream>
31 #include "lin_alg.h"
32 #include "make_vector.h"
33 #include "array.h"
34 
35 using std::cout;
36 using std::endl;
37 
38 
40 
44 void test_lusolve1D(void)
45 {
46  Matrix a(1,1);
47  ArrayOfIndex indx(1);
48  Matrix orig(1,1);
49  Matrix b(1,1);
50 
51  /* Assign test-matrix element. */
52  a(0,0) = 3;
53 
54  /* ------------------------------------------------------------------------
55  Test the function ludcmp.
56  ----------------------------------------------------------------------- */
57  cout << "\n LU decomposition test \n";
58  cout << "initial matrix: \n";
59 
60  cout << " " << a(0,0) << endl;
61 
62  /* input: Test-matrix a,
63  output: Decomposed matrix b (includes upper and lower triangle, cp.
64  Numerical Recipies)
65  and index which gives information about pivoting. */
66  ludcmp(b, indx, a);
67 
68  cout << "\n after decomposition";
69  cout << " " << b(0,0) << endl;
70 
71  /* Seperate b into the two triangular matrices. */
72  Matrix l(1,1,0.0);
73  Matrix u(1,1,0.0);
74  Matrix lu(1,1,0.0);
75 
76  l(0,0) = 1.0;
77  u(0,0) = b(0,0);
78 
79  /*-------------------------------------------------------------------
80  end of ludcmp test
81  ------------------------------------------------------------------*/
82  /*--------------------------------------------------------------------
83  test backsubstitution routine lubacksub
84  -------------------------------------------------------------------*/
85 
86  Vector c(1);
87  c[0] = 6;
88  cout << indx[0] << " " << c[0] << endl;
89 
90  Vector x(1);
91  lubacksub(x, b, c, indx);
92 
93  cout << "\n solution vector x";
94  cout << x[0] << endl;
95 
96 }
97 
98 
100 
104 void test_lusolve4D(void)
105 {
106  Matrix a(4,4);
107  ArrayOfIndex indx(4);
108  Matrix orig(4,4);
109  Matrix b(4,4);
110 
111  /* Assign test-matrix elements. */
112 
113  a(0,0) = 1;
114  a(0,1) = 3;
115  a(0,2) = 5;
116  a(0,3) = 6;
117  a(1,0) = 2;
118  a(1,1) = 3;
119  a(1,2) = 4;
120  a(1,3) = 4;
121  a(2,0) = 1;
122  a(2,1) = 2;
123  a(2,2) = 5;
124  a(2,3) = 1;
125  a(3,0) = 7;
126  a(3,1) = 2;
127  a(3,2) = 4;
128  a(3,3) = 3;
129 
130 
131  /* ------------------------------------------------------------------------
132  Test the function ludcmp.
133  ----------------------------------------------------------------------- */
134 
135 
136  cout << "\n LU decomposition test \n";
137  cout << "initial matrix: \n";
138  for( Index i = 0; i<4; i++)
139  {
140  cout << "\n";
141  for (Index j = 0; j<4; j++)
142  cout << " " << a(i,j);
143  }
144  cout << "\n";
145 
146  /* input: Test-matrix a,
147  output: Decomposed matrix b (includes upper and lower triangle, cp.
148  Numerical Recipies)
149  and index which gives information about pivoting. */
150  ludcmp(b, indx, a);
151 
152  cout << "\n after decomposition";
153  for( Index i = 0; i<4; i++)
154  { cout << "\n";
155  for (Index j = 0; j<4; j++)
156  cout << " " << b(i,j);
157  }
158  cout << "\n";
159 
160  /* Seperate b into the two triangular matrices. */
161  Matrix l(4,4,0.0);
162  Matrix u(4,4,0.0);
163  Matrix lu(4,4,0.0);
164 
165 
166  for(Index i = 0; i<4; i++) l(i,i) = 1.0;
167  l(1,0) = b(1,0);
168  l(2,Range(0,2)) = b(2, Range(0,2));
169  l(3,Range(0,3)) = b(3, Range(0,3));
170 
171  cout << "\n Matrix L";
172  for( Index i = 0; i<4; i++)
173  {
174  cout << "\n";
175  for (Index j = 0; j<4; j++)
176  cout << " " << l(i,j);
177  }
178  cout << "\n";
179 
180 
181  u(0,Range(0,4)) = b(0,Range(0,4));
182  u(1,Range(1,3)) = b(1,Range(1,3));
183  u(2,Range(2,2)) = b(2,Range(2,2));
184  u(3,Range(3,1)) = b(3,Range(3,1));
185 
186 
187  cout << "\n Matrix U";
188  for( Index i = 0; i<4; i++)
189  {
190  cout << "\n";
191  for (Index j = 0; j<4; j++)
192  cout << " " << u(i,j);
193  }
194  cout << "\n";
195 
196 
197  /* Test, if LU = a. */
198  mult(lu,l,u);
199 
200  cout << "\n product L*U";
201  for( Index i = 0; i<4; i++)
202  {
203  cout << "\n";
204  for (Index j = 0; j<4; j++)
205  cout << " " << lu(i,j);
206  }
207  cout << "\n";
208 
209  /*-------------------------------------------------------------------
210  end of ludcmp test
211  ------------------------------------------------------------------*/
212 
213 
214  /*--------------------------------------------------------------------
215  test backsubstitution routine lubacksub
216  -------------------------------------------------------------------*/
217 
218  Vector c(4);
219  c[0] = 2;
220  c[1] = 5;
221  c[2] = 6;
222  c[3] = 7;
223 
224  cout << "\n vector indx";
225  for (Index i=0; i<4; i++)
226  {
227  cout << "\n";
228  cout << indx[i] << " " << c[i];
229  }
230 
231  Vector x(4);
232  lubacksub(x, b, c, indx);
233 
234  cout << "\n solution vector x" << endl;
235  for (Index i=0; i<4; i++)
236  {
237  cout << "\n";
238  cout << x[i];
239  }
240 
241  cout << "\n test solution LU*x";
242  Vector y(4);
243  mult(y,lu,x);
244  for (Index i=0; i<4; i++)
245  {
246  cout << "\n";
247  cout << y[i];
248  }
249  cout <<"\n";
250 }
251 
252 
253 
255 
259 {
260  Matrix A(4,4);
261  Matrix F(4,4);
262  A(0,0) = 1;
263  A(0,1) = 3;
264  A(0,2) = 5;
265  A(0,3) = 6;
266  A(1,0) = 2;
267  A(1,1) = 3;
268  A(1,2) = 4;
269  A(1,3) = 4;
270  A(2,0) = 1;
271  A(2,1) = 2;
272  A(2,2) = 5;
273  A(2,3) = 1;
274  A(3,0) = 7;
275  A(3,1) = 2;
276  A(3,2) = 4;
277  A(3,3) = 3;
278 
279  /* set parameter for accuracy */
280  Index q = 8;
281 
282  /*execute matrix exponential function*/
283  matrix_exp(F,A,q);
284 
285 
286  cout << "\n Exponential of Matrix K";
287  for( Index i = 0; i<4; i++)
288  {
289  cout << "\n";
290  for (Index j = 0; j<4; j++)
291  cout << " " << F(i,j);
292  }
293  cout << "\n";
294  }
295 
296 
298 
302 {
303  Matrix A(1,1);
304  Matrix F(1,1);
305  A(0,0) = 5;
306 
307  /* set parameter for accuracy */
308  Index q = 8;
309 
310  /*execute matrix exponential function*/
311  matrix_exp(F,A,q);
312 
313  cout << "\n Exponential of Matrix A:\n";
314  cout << F(0,0);
315  cout <<"\n";
316 }
317 
319 
323 {
324  Matrix A(3,3);
325  Matrix F(3,3);
326  A(0,0) = 1;
327  A(0,1) = 3;
328  A(0,2) = 5;
329  A(1,0) = 2;
330  A(1,1) = 3;
331  A(1,2) = 4;
332  A(2,0) = 1;
333  A(2,1) = 2;
334  A(2,2) = 5;
335 
336 
337  /* set parameter for accuracy */
338  Index q = 8;
339 
340  /*execute matrix exponential function*/
341  matrix_exp(F,A,q);
342 
343 
344  cout << "\n Exponential of Matrix A";
345  for( Index i = 0; i<3; i++)
346  {
347  cout << "\n";
348  for (Index j = 0; j<3; j++)
349  cout << " " << F(i,j);
350  }
351  cout << "\n";
352 }
353 
354 int main(void)
355 {
356  test_lusolve1D();
357  // test_matrix_exp1D();
358  return(0);
359 }
Matrix
The Matrix class.
Definition: matpackI.h:788
array.h
This file contains the definition of Array.
matrix_exp
void matrix_exp(MatrixView F, ConstMatrixView A, const Index &q)
General exponential of a Matrix.
Definition: lin_alg.cc:194
q
#define q
Definition: continua.cc:21469
ludcmp
void ludcmp(MatrixView LU, ArrayOfIndex &indx, ConstMatrixView A)
LU decomposition.
Definition: lin_alg.cc:54
test_matrix_exp3D
void test_matrix_exp3D(void)
Test for the matrix exponential function (3D matrix)
Definition: test_linalg.cc:322
Array< Index >
test_matrix_exp4D
void test_matrix_exp4D(void)
Test for the matrix exponential function (4D matrix)
Definition: test_linalg.cc:258
mult
void mult(VectorView y, const ConstMatrixView &M, const ConstVectorView &x)
Matrix Vector multiplication.
Definition: matpackI.cc:1648
test_lusolve4D
void test_lusolve4D(void)
Definition: test_linalg.cc:104
lin_alg.h
Linear algebra functions.
make_vector.h
The class MakeVector is a special kind of Vector that can be initialized explicitly from one or more ...
test_matrix_exp1D
void test_matrix_exp1D(void)
Test for the matrix exponential function (3D matrix)
Definition: test_linalg.cc:301
test_lusolve1D
void test_lusolve1D(void)
Definition: test_linalg.cc:44
Range
The range class.
Definition: matpackI.h:148
Index
INDEX Index
The type to use for all integer numbers and indices.
Definition: matpack.h:35
Vector
The Vector class.
Definition: matpackI.h:556
main
int main(void)
Definition: test_linalg.cc:354
lubacksub
void lubacksub(VectorView x, ConstMatrixView LU, ConstVectorView b, const ArrayOfIndex &indx)
LU backsubstitution.
Definition: lin_alg.cc:142