forked from ediloren/LinAlgebra
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLUDecomp.cpp
More file actions
179 lines (146 loc) · 5.09 KB
/
LUDecomp.cpp
File metadata and controls
179 lines (146 loc) · 5.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/***************************************************************************
* *
* Copyright (c) 2017 *
* FastFieldSolvers S.R.L. http://www.fastfieldsolvers.com *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License (LGPL) *
* as published by the Free Software Foundation; either version 2 of *
* the License, or (at your option) any later version. *
* for detail see the LICENCE text file. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this program; if not, write to the Free Software *
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
* USA *
* *
***************************************************************************/
// ludecomp.cpp header file
// matrix LU decomposition class
//
#include "stdafx.h"
#include <cmath>
#include "LUDecomp.h"
// right-looking LU factorization algorithm (unblocked)
//
// Factors matrix A into lower and upper triangular matrices
// (L and U respectively) in solving the linear equation Ax=b.
//
//
// Args:
//
// A (input/output) Matrix(1:n, 1:n) In input, matrix to be
// factored. On output, overwritten with lower and
// upper triangular factors.
//
// indx (output) Vector(1:n) Pivot vector. Describes how
// the rows of A were reordered to increase
// numerical stability.
//
// Return value:
//
// int (0 if successful, 1 otherwise)
//
//
int CLin_LU_factor( CLin_Matrix &A, CLin_Vector &indx)
{
CLin_subscript M, N, minMN;
CLin_subscript i, j, k, jp;
CLin_subscript ii,jj;
double t, recp;
M = A.num_rows();
N = A.num_cols();
if (M == 0 || N == 0) return 0;
if (indx.dim() != M)
indx.newsize(M);
i = j = k = jp =0;
// min(M,N);
minMN = (M < N ? M : N) ;
for (j=0; j < minMN; j++)
{
// find pivot in column j and test for singularity.
jp = j;
t = fabs(A[j][j]);
for (i=j+1; i<M; i++)
if ( fabs(A[i][j]) > t)
{
jp = i;
t = fabs(A[i][j]);
}
indx[j] = jp;
// jp now has the index of maximum element
// of column j, below the diagonal
if ( A[jp][j] == 0 )
// factorization failed because of zero pivot
return 1;
// if pivot not already on the diagonal
if (jp != j)
// swap rows j and jp
for (k=0; k<N; k++)
{
t = A[j][k];
A[j][k] = A[jp][k];
A[jp][k] = t;
}
// divide elements j+1:M of jth column by pivot element
// (for the first M-1 cols, last col's below-the-diagonal element
// is already the pivot)
if (j<M-1)
{
// note that A(j,j) was previously A(jp,p), which was
// guaranteed not to be zero
recp = 1.0 / A[j][j];
for (k=j+1; k<M; k++)
A[k][j] *= recp;
}
if (j < minMN-1)
{
// rank-1 update to trailing submatrix: E = E - x*y;
//
// E is the region A(j+1:M, j+1:N)
// x is the column vector A(j+1:M,j)
// y is row vector A(j,j+1:N)
for (ii=j+1; ii<M; ii++)
for (jj=j+1; jj<N; jj++)
A[ii][jj] -= A[ii][j]*A[j][jj];
}
}
return 0;
}
int CLin_LU_solve(const CLin_Matrix &A, const CLin_Vector &indx, CLin_Vector &b)
{
CLin_subscript i, ii, ip, j;
CLin_subscript n;
double sum;
bool flag;
n = b.dim();
flag = false;
sum = 0.0;
for (i=0; i<n; i++)
{
ip = indx[i];
sum = b[ip];
b[ip] = b[i];
if (flag)
for (j=ii; j<=i-1; j++)
sum -= A[i][j]*b[j];
else if (sum) {
ii = i;
flag = true;
}
b[i] = sum;
}
for (i=n-1;i>=0;i--)
{
sum=b[i];
for (j=i+1;j<n;j++)
sum -= A[i][j]*b[j];
b[i] = sum/A[i][i];
}
return 0;
}