Log In | Get Help   
Home My Page Projects Code Snippets Project Openings Mareframe
Summary Activity Forums Tracker Lists Tasks Docs Surveys News SCM Files
[mareframe] Annotation of /trunk/gadget/ludecomposition.cc
[mareframe] / trunk / gadget / ludecomposition.cc Repository:
ViewVC logotype

Annotation of /trunk/gadget/ludecomposition.cc

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1 - (view) (download)

1 : agomez 1 #include "ludecomposition.h"
2 :     #include "errorhandler.h"
3 :     #include "global.h"
4 :     LUDecomposition::LUDecomposition(const DoubleMatrix& A) {
5 :     if (A.Ncol() != A.Nrow())
6 :     handle.logMessage(LOGFAIL, "Error in ludecomposition - matrix not rectangular");
7 :    
8 :     illegal = 0;
9 :     int i, k, j;
10 :     double s, tmp;
11 :    
12 :     size = A.Nrow();
13 :     L = DoubleMatrix(size, size, 0.0);
14 :     U = DoubleMatrix(A);
15 :     logdet = 0.0;
16 :     tmp = 0.0;
17 :    
18 :     for (k = 0; k < size; k++) {
19 :     L[k][k] = 1.0;
20 :    
21 :     if (isZero(U[k][k])) {
22 :     handle.logMessage(LOGWARN, "Warning in ludecomposition - zero on matrix diagonal");
23 :     illegal = 1;
24 :     logdet = verybig;
25 :     tmp = 0.0;
26 :    
27 :     } else if (U[k][k] > 0.0) {
28 :     logdet += log(U[k][k]);
29 :     tmp = 1.0 / U[k][k];
30 :    
31 :     } else if (U[k][k] < 0.0 ) {
32 :     handle.logMessage(LOGWARN, "Warning in ludecomposition - negative number on matrix diagonal");
33 :     illegal = 1;
34 :     logdet = verybig;
35 :     tmp = 0.0;
36 :     }
37 :    
38 :     for (i = k + 1; i < size; i++) {
39 :     s = U[i][k] * tmp;
40 :     L[i][k] = s;
41 :     U[i][k] = 0.0;
42 :    
43 :     for (j = k + 1; j < size; j++)
44 :     U[i][j] -= s * U[k][j];
45 :     }
46 :     }
47 :     }
48 :    
49 :     // calculates the solution of Ax=b using the LU decomposition calculated in the constructor
50 :     DoubleVector LUDecomposition::Solve(const DoubleVector& b) {
51 :     if (size != b.Size())
52 :     handle.logMessage(LOGFAIL, "Error in ludecomposition - sizes not the same");
53 :    
54 :     int i, j;
55 :     double s;
56 :     DoubleVector y(b);
57 :     DoubleVector x(size, 0.0);
58 :    
59 :     for (i = 0; i < size; i++) {
60 :     s = 0.0;
61 :     for (j = 0; j < i; j++)
62 :     s += L[i][j] * y[j];
63 :     y[i] -= s;
64 :     }
65 :    
66 :     for (i = size - 1; i >= 0; i--) {
67 :     x[i] = y[i];
68 :     s = 0.0;
69 :     for (j = i + 1; j < size; j++)
70 :     s += U[i][j] * x[j];
71 :    
72 :     x[i] -= s;
73 :     if (isZero(U[i][i])) {
74 :     handle.logMessage(LOGWARN, "Warning in ludecomposition - divide by zero");
75 :     } else
76 :     x[i] /= U[i][i];
77 :     }
78 :     return x;
79 :     }

root@forge.cesga.es
ViewVC Help
Powered by ViewVC 1.0.0  

Powered By FusionForge