ARTS  2.0.49
test_linalg.cc
Go to the documentation of this file.
1 /* Copyright (C) 2002-2008 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 
37 
41 void test_lusolve1D(void)
42 {
43  Matrix a(1,1);
44  ArrayOfIndex indx(1);
45  Matrix orig(1,1);
46  Matrix b(1,1);
47 
48  /* Assign test-matrix element. */
49  a(0,0) = 3;
50 
51  /* ------------------------------------------------------------------------
52  Test the function ludcmp.
53  ----------------------------------------------------------------------- */
54  cout << "\n LU decomposition test \n";
55  cout << "initial matrix: \n";
56 
57  cout << " " << a(0,0) << endl;
58 
59  /* input: Test-matrix a,
60  output: Decomposed matrix b (includes upper and lower triangle, cp.
61  Numerical Recipies)
62  and index which gives information about pivoting. */
63  ludcmp(b, indx, a);
64 
65  cout << "\n after decomposition";
66  cout << " " << b(0,0) << endl;
67 
68  /* Seperate b into the two triangular matrices. */
69  Matrix l(1,1,0.0);
70  Matrix u(1,1,0.0);
71  Matrix lu(1,1,0.0);
72 
73  l(0,0) = 1.0;
74  u(0,0) = b(0,0);
75 
76  /*-------------------------------------------------------------------
77  end of ludcmp test
78  ------------------------------------------------------------------*/
79  /*--------------------------------------------------------------------
80  test backsubstitution routine lubacksub
81  -------------------------------------------------------------------*/
82 
83  Vector c(1);
84  c[0] = 6;
85  cout << indx[0] << " " << c[0] << endl;
86 
87  Vector x(1);
88  lubacksub(x, b, c, indx);
89 
90  cout << "\n solution vector x";
91  cout << x[0] << endl;
92 
93 }
94 
95 
97 
101 void test_lusolve4D(void)
102 {
103  Matrix a(4,4);
104  ArrayOfIndex indx(4);
105  Matrix orig(4,4);
106  Matrix b(4,4);
107 
108  /* Assign test-matrix elements. */
109 
110  a(0,0) = 1;
111  a(0,1) = 3;
112  a(0,2) = 5;
113  a(0,3) = 6;
114  a(1,0) = 2;
115  a(1,1) = 3;
116  a(1,2) = 4;
117  a(1,3) = 4;
118  a(2,0) = 1;
119  a(2,1) = 2;
120  a(2,2) = 5;
121  a(2,3) = 1;
122  a(3,0) = 7;
123  a(3,1) = 2;
124  a(3,2) = 4;
125  a(3,3) = 3;
126 
127 
128  /* ------------------------------------------------------------------------
129  Test the function ludcmp.
130  ----------------------------------------------------------------------- */
131 
132 
133  cout << "\n LU decomposition test \n";
134  cout << "initial matrix: \n";
135  for( Index i = 0; i<4; i++)
136  {
137  cout << "\n";
138  for (Index j = 0; j<4; j++)
139  cout << " " << a(i,j);
140  }
141  cout << "\n";
142 
143  /* input: Test-matrix a,
144  output: Decomposed matrix b (includes upper and lower triangle, cp.
145  Numerical Recipies)
146  and index which gives information about pivoting. */
147  ludcmp(b, indx, a);
148 
149  cout << "\n after decomposition";
150  for( Index i = 0; i<4; i++)
151  { cout << "\n";
152  for (Index j = 0; j<4; j++)
153  cout << " " << b(i,j);
154  }
155  cout << "\n";
156 
157  /* Seperate b into the two triangular matrices. */
158  Matrix l(4,4,0.0);
159  Matrix u(4,4,0.0);
160  Matrix lu(4,4,0.0);
161 
162 
163  for(Index i = 0; i<4; i++) l(i,i) = 1.0;
164  l(1,0) = b(1,0);
165  l(2,Range(0,2)) = b(2, Range(0,2));
166  l(3,Range(0,3)) = b(3, Range(0,3));
167 
168  cout << "\n Matrix L";
169  for( Index i = 0; i<4; i++)
170  {
171  cout << "\n";
172  for (Index j = 0; j<4; j++)
173  cout << " " << l(i,j);
174  }
175  cout << "\n";
176 
177 
178  u(0,Range(0,4)) = b(0,Range(0,4));
179  u(1,Range(1,3)) = b(1,Range(1,3));
180  u(2,Range(2,2)) = b(2,Range(2,2));
181  u(3,Range(3,1)) = b(3,Range(3,1));
182 
183 
184  cout << "\n Matrix U";
185  for( Index i = 0; i<4; i++)
186  {
187  cout << "\n";
188  for (Index j = 0; j<4; j++)
189  cout << " " << u(i,j);
190  }
191  cout << "\n";
192 
193 
194  /* Test, if LU = a. */
195  mult(lu,l,u);
196 
197  cout << "\n product L*U";
198  for( Index i = 0; i<4; i++)
199  {
200  cout << "\n";
201  for (Index j = 0; j<4; j++)
202  cout << " " << lu(i,j);
203  }
204  cout << "\n";
205 
206  /*-------------------------------------------------------------------
207  end of ludcmp test
208  ------------------------------------------------------------------*/
209 
210 
211  /*--------------------------------------------------------------------
212  test backsubstitution routine lubacksub
213  -------------------------------------------------------------------*/
214 
215  Vector c(4);
216  c[0] = 2;
217  c[1] = 5;
218  c[2] = 6;
219  c[3] = 7;
220 
221  cout << "\n vector indx";
222  for (Index i=0; i<4; i++)
223  {
224  cout << "\n";
225  cout << indx[i] << " " << c[i];
226  }
227 
228  Vector x(4);
229  lubacksub(x, b, c, indx);
230 
231  cout << "\n solution vector x" << endl;
232  for (Index i=0; i<4; i++)
233  {
234  cout << "\n";
235  cout << x[i];
236  }
237 
238  cout << "\n test solution LU*x";
239  Vector y(4);
240  mult(y,lu,x);
241  for (Index i=0; i<4; i++)
242  {
243  cout << "\n";
244  cout << y[i];
245  }
246  cout <<"\n";
247 }
248 
249 
250 
252 
256 {
257  Matrix A(4,4);
258  Matrix F(4,4);
259  A(0,0) = 1;
260  A(0,1) = 3;
261  A(0,2) = 5;
262  A(0,3) = 6;
263  A(1,0) = 2;
264  A(1,1) = 3;
265  A(1,2) = 4;
266  A(1,3) = 4;
267  A(2,0) = 1;
268  A(2,1) = 2;
269  A(2,2) = 5;
270  A(2,3) = 1;
271  A(3,0) = 7;
272  A(3,1) = 2;
273  A(3,2) = 4;
274  A(3,3) = 3;
275 
276  /* set parameter for accuracy */
277  Index q = 8;
278 
279  /*execute matrix exponential function*/
280  matrix_exp(F,A,q);
281 
282 
283  cout << "\n Exponential of Matrix K";
284  for( Index i = 0; i<4; i++)
285  {
286  cout << "\n";
287  for (Index j = 0; j<4; j++)
288  cout << " " << F(i,j);
289  }
290  cout << "\n";
291  }
292 
293 
295 
299 {
300  Matrix A(1,1);
301  Matrix F(1,1);
302  A(0,0) = 5;
303 
304  /* set parameter for accuracy */
305  Index q = 8;
306 
307  /*execute matrix exponential function*/
308  matrix_exp(F,A,q);
309 
310  cout << "\n Exponential of Matrix A:\n";
311  cout << F(0,0);
312  cout <<"\n";
313 }
314 
316 
320 {
321  Matrix A(3,3);
322  Matrix F(3,3);
323  A(0,0) = 1;
324  A(0,1) = 3;
325  A(0,2) = 5;
326  A(1,0) = 2;
327  A(1,1) = 3;
328  A(1,2) = 4;
329  A(2,0) = 1;
330  A(2,1) = 2;
331  A(2,2) = 5;
332 
333 
334  /* set parameter for accuracy */
335  Index q = 8;
336 
337  /*execute matrix exponential function*/
338  matrix_exp(F,A,q);
339 
340 
341  cout << "\n Exponential of Matrix A";
342  for( Index i = 0; i<3; i++)
343  {
344  cout << "\n";
345  for (Index j = 0; j<3; j++)
346  cout << " " << F(i,j);
347  }
348  cout << "\n";
349 }
350 
351 int main(void)
352 {
353  test_lusolve1D();
354  // test_matrix_exp1D();
355  return(0);
356 }
Matrix
The Matrix class.
Definition: matpackI.h:767
array.h
This file contains the definition of Array.
matrix_exp
void matrix_exp(MatrixView F, ConstMatrixView A, const Index &q)
Exponential of a Matrix.
Definition: lin_alg.cc:193
q
#define q
Definition: continua.cc:14103
ludcmp
void ludcmp(MatrixView LU, ArrayOfIndex &indx, ConstMatrixView A)
LU decomposition.
Definition: lin_alg.cc:53
test_matrix_exp3D
void test_matrix_exp3D(void)
Test for the matrix exponential function (3D matrix)
Definition: test_linalg.cc:319
Array< Index >
test_matrix_exp4D
void test_matrix_exp4D(void)
Test for the matrix exponential function (4D matrix)
Definition: test_linalg.cc:255
mult
void mult(VectorView y, const ConstMatrixView &M, const ConstVectorView &x)
Matrix Vector multiplication.
Definition: matpackI.cc:1607
test_lusolve4D
void test_lusolve4D(void)
Definition: test_linalg.cc:101
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:298
test_lusolve1D
void test_lusolve1D(void)
Definition: test_linalg.cc:41
Range
The range class.
Definition: matpackI.h:148
Index
INDEX Index
The type to use for all integer numbers and indices.
Definition: matpack.h:39
Vector
The Vector class.
Definition: matpackI.h:555
main
int main(void)
Definition: test_linalg.cc:351
lubacksub
void lubacksub(VectorView x, ConstMatrixView LU, ConstVectorView b, const ArrayOfIndex &indx)
LU backsubstitution.
Definition: lin_alg.cc:144