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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
|
//======================================
// [m x n] matrices are represented as 1-d arrays
// Row i goes from 0 to m-1.
// Col j goes from 0 to n-1.
// The (i,j) component is stored at 1-d index
// n * i + j
//
// Storage order as for C++ arrays (column index varies fastest);
// NOTE: opposite storage order to Fortran
//======================================
// Factorise A = PLU, where:
// L and U are lower and upper triangular matrices
// P is a permutation matrix
// P is returned here as an n-element array,
// such that P[i] is the row that must be swapped into the ith row
// When solving Ax=b, do this swapping to b first, then solve LUx=b'
//======================================
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <cmath>
#include <algorithm>
using namespace std;
const bool doPivot = true;
const double EPS = 1.0e-30;
//======================================
template <class T> bool LUFactorise( int n, T *A, T *L, T *U, int *P )
{
T *AA = new T[n*n];
// Initialise
fill( L, L + n * n, 0 );
fill( U, U + n * n, 0 );
copy( A, A + n * n, AA );
for ( int i = 0; i < n; i++ ) P[i] = i;
for ( int i = 0; i < n; i++ )
{
int r = i; // Find row r below with largest element in column i
T maxA = abs( AA[n*i+i] );
for ( int k = i + 1; k < n; k++ )
{
int ki = n * k + i;
if ( abs( AA[ki] ) > maxA )
{
r = k;
maxA = abs( AA[ki] );
}
}
if ( doPivot && r != i )
{
swap( P[i], P[r] );
for ( int j = 0; j < n; j++ )
{
int rj = n * r + j;
int ij = n * i + j;
swap( AA[rj], AA[ij] );
if ( j < i ) swap( L[rj], L[ij] );
}
}
// ith row -> U
for ( int j = i; j < n; j++ )
{
int ij = n * i + j;
U[ij] = AA[ij];
for ( int k = 0; k < i; k++ ) U[ij] -= L[n*i+k] * U[n*k+j];
}
if ( abs( U[n*i+i] ) < EPS ) return false;
// ith column -> L
for ( int j = i; j < n; j++ )
{
int ji = n * j + i;
L[ji] = AA[ji];
for ( int k = 0; k < i; k++ ) L[ji] -= L[n*j+k] * U[n*k+i];
L[ji] /= U[n*i+i];
}
}
delete [] AA;
return true;
}
//======================================
template <class T> void solveUpper( int n, T *U, T *B, T *X ) // Solve UX = B, where U is upper-triangular
{
for ( int i = n - 1; i >= 0; i-- )
{
X[i] = B[i];
for ( int j = i + 1; j < n; j++ ) X[i] -= U[n*i+j] * X[j];
X[i] /= U[n*i+i];
}
}
//======================================
template <class T> void solveLower( int n, T *L, T *B, T *X ) // Solve LX = B, where L is lower-triangular
{
for ( int i = 0; i < n; i++ )
{
X[i] = B[i];
for ( int j = 0; j < i; j++ ) X[i] -= L[n*i+j] * X[j];
X[i] /= L[n*i+i];
}
}
//======================================
template <class T> void permute( int m, int n, int *P, T *A ) // Permute rows of an mxn matrix A
{
T *AA = new T[m*n];
copy( A, A + m * n, AA );
for ( int i = 0; i < m; i++ )
{
int r = P[i];
for ( int j = 0; j < n; j++ ) A[n*i+j] = AA[n*r+j];
}
delete [] AA;
}
//======================================
template <class T> void solvePLU( int n, int *P, T *L, T *U, T *B, T *X ) // Solve PLUx = b
{
double *PB = new double[n*1];
double *Y = new double[n*1]; // Temporary solution of LY = B (whence UX = Y gives full solution)
copy( B, B + n, PB );
permute( n, 1, P, PB ); // Permuted rows of B
solveLower( n, L, PB, Y );
solveUpper( n, U, Y , X ); // Final solution is in X
delete [] PB;
delete [] Y;
}
//======================================
template <class T> void matMul( int mA, int nA, int nB, T *A, T *B, T *C ) // Matrix multiply: A * B = C
{
for ( int i = 0; i < mA; i++ )
{
for ( int j = 0; j < nB; j++ )
{
int ij = nB * i + j;
C[ij] = 0.0;
for ( int k = 0; k < nA; k++ ) C[ij] += A[nA*i+k] * B[nB*k+j];
}
}
}
//======================================
template <class T> void pMatrix( int n, int *P, T *A ) // Create a permutation matrix A from P
{
fill( A, A + n * n, 0 );
for ( int i = 0; i < n; i++ ) A[n*P[i]+i] = 1;
}
//======================================
template <class T> void showMatrix( int m, int n, T *A )
{
const double ZERO = 1.0e-10;
const string SPACE = " ";
const int w = 12;
const int p = 4;
cout << fixed << setprecision( p );
for ( int i = 0; i < m; i++ )
{
for ( int j = 0; j < n; j++ )
{
T val = A[n*i+j]; if ( abs( val ) < ZERO ) val = 0.0;
cout << setw( w ) << val << SPACE;
}
cout << '\n';
}
}
//======================================
template <class T> void getData( int &n, T *&A, T *&B )
{
// ifstream in( "matrix.txt" );
stringstream in(
" 7 " // N
" 0 0.3019 0.1562 0.027 1 0.2 0.5 " // <---A
" 0.3019 0 0.1562 0.7637 1 0.8 0.2 " // .
" 0.1562 0.1326 0 0.02263 1 0.7 0.7 " // .
" 0.027 0.7637 0.02263 0 1 0.5 0.5 " // .
" 1 1 1 1 0 0 0 " // .
" 0.2 0.8 0.7 0.5 0 0 0 " // .
" 0.5 0.2 0.7 0.5 0 0 0 " // .
" 0.0 0.0 0.0 0.2 0 0 0 " // <---B
);
in >> n;
A = new double[n*n];
B = new double[n*1];
for ( int ij = 0; ij < n * n; ij++ ) in >> A[ij];
for ( int i = 0; i < n ; i++ ) in >> B[i ];
}
//======================================
int main()
{
int N;
double *A; // Matrix
double *B; // RHS vector
// INPUT
getData( N, A, B );
int *P = new int [N*1]; // Permutation of rows
double *L = new double[N*N]; // Lower-triangular matrix
double *U = new double[N*N]; // Upper-triangular matrix
double *X = new double[N*1]; // Solution of Ax = b
// FACTORISE
bool ok = LUFactorise( N, A, L, U, P );
if ( !ok ) { cout << "Can't factorise\n"; return 1; }
// SOLVE
solvePLU( N, P, L, U, B, X );
// SUMMARY
double *PM = new double[N*N]; // Permutation matrix
pMatrix( N, P, PM );
cout << "\n*** Solve AX = B by LU decomposition ***\n";
cout << "\nOriginal A: \n"; showMatrix( N, N, A );
cout << "\nPermutation PM: \n"; showMatrix( N, N, PM );
cout << "\nLower-triangular L:\n"; showMatrix( N, N, L );
cout << "\nUpper-triangular U:\n"; showMatrix( N, N, U );
cout << "\nSolution X: \n"; showMatrix( N, 1, X );
// CHECKS
double *PL = new double[N*N];
double *PLU= new double[N*N];
matMul( N, N, N, PM, L, PL );
matMul( N, N, N, PL, U, PLU );
cout << "\n\n*** CHECK FACTORISATION, A = PLU ***\n";
cout << "\nA:\n"; showMatrix( N, N, A );
cout << "\nPLU:\n"; showMatrix( N, N, PLU );
delete [] PL;
delete [] PLU;
delete [] PM;
double *AX = new double[N*1];
matMul( N, N, 1, A, X, AX );
cout << "\n\n*** CHECK SOLUTION, Ax = b ***\n";
cout << "\nAX:\n"; showMatrix( N, 1, AX );
cout << "\nB :\n"; showMatrix( N, 1, B );
delete [] AX;
delete [] A;
delete [] B;
delete [] X;
delete [] P;
delete [] L;
delete [] U;
}
| |