21 for(
unsigned int i = 0; i < A->size(); ++i )
23 for(
unsigned int j = 0; j < A->size(); ++j )
24 cout << (*A)[i][j] <<
" ";
31 for(
unsigned int j = 0; j < v->size(); ++j )
32 cout << (*v)[j] <<
" ";
38 unsigned int n = A->size();
41 for(
unsigned int i = 0; i < n; ++i )
43 for(
unsigned int j = 0; j < n; ++j )
45 for(
unsigned int k = 0; k < n; ++k )
46 (*C)[i][j] += (*A)[i][k] * (*B)[k][j];
59 if ( resIndex ==
FIRST )
61 else if ( resIndex ==
SECOND )
69 unsigned int n = A->size();
73 for (
unsigned int i = 0; i < n; ++i )
75 for (
unsigned int j = 0; j < n; ++j )
81 (*A)[i][j] += (*A)[i][k] * (*B)[k][j];
90 unsigned int i,j,n,index;
94 while (!swaps->empty())
96 index = swaps->back();
99 j = (index / 10 ) % 10;
102 for(
unsigned int l = 0; l < n; ++l )
105 (*A)[l][i] = (*A)[l][j];
113 unsigned int n = A->size();
116 for(
unsigned int i = 0; i < n; ++i )
118 for(
unsigned int j = 0; j < n; ++j )
119 (*C)[i][j] = alpha * (*A)[i][j] + beta * (*B)[i][j];
126 unsigned int resIndex )
129 unsigned int n = A->size();
131 if ( resIndex ==
FIRST ) {
133 }
else if ( resIndex ==
SECOND ) {
136 cerr <<
"matMatAdd : Invalid index supplied to store result.\n";
140 for(
unsigned int i = 0; i < n; ++i )
142 for(
unsigned int j = 0; j < n; ++j )
143 (*C)[i][j] = alpha * (*A)[i][j] + beta * (*B)[i][j];
149 unsigned int n = A->size();
152 for(
unsigned int i = 0; i < n; ++i )
154 for(
unsigned int j = 0; j < n; ++j )
157 (*B)[i][j] = (*A)[i][j] + k;
159 (*B)[i][j] = (*A)[i][j];
168 unsigned int n = A->size();
171 for(
unsigned int i = 0; i < n; ++i )
177 unsigned int n = A->size();
180 for(
unsigned int i = 0; i < n; ++i )
182 for(
unsigned int j = 0; j < n; ++j )
183 (*C)[i][j] = (*A)[i][j] * mul + add;
191 unsigned int n = A->size();
194 for(
unsigned int i = 0; i < n; ++i )
196 for(
unsigned int j = 0; j < n; ++j )
197 (*A)[i][j] = (*A)[i][j] * mul + add;
203 unsigned int n = A->size();
206 for(
unsigned int i = 0; i < n; ++i )
208 for(
unsigned int j = 0; j < n; ++j )
209 (*w)[i] += (*v)[j] * (*A)[j][i];
217 unsigned int n = v->size();
220 for (
unsigned int i = 0; i < n; ++i )
221 (*w)[i] = scal * (*v)[i] + shift;
228 unsigned int n = v->size();
231 for (
unsigned int i = 0; i < n; ++i )
232 (*v)[i] += scal * (*v)[i] + shift;
237 unsigned int n = A->size();
240 for(
unsigned int i = 0; i < n; ++i )
242 for(
unsigned int j = 0; j < n; ++j )
243 (*w)[i] += (*A)[i][j] * (*v)[j];
250 double alpha,
double beta )
252 unsigned int n = v1->size();
255 for (
unsigned int i = 0; i < n; ++i )
256 (*w)[i] = (*v1)[i] * alpha + (*v2)[i] * beta;
264 unsigned int n = v1->size();
267 for (
unsigned int i = 0; i < n; ++i )
268 (*v1)[i] = (*v1)[i] * alpha + (*v2)[i] * beta;
273 unsigned int n = A->size();
276 for (
unsigned int i = 0; i < n; ++i )
284 double norm = 0, colSum = 0;
285 unsigned int n = A->size();
287 for(
unsigned int i = 0; i < n; ++i )
290 for(
unsigned int j = 0; j < n; ++j )
291 colSum += fabs( (*A)[j][i] );
302 unsigned int n = A->size();
305 for(
unsigned int i = 0; i < n; ++i )
307 for(
unsigned int j = 0; j < n; ++j )
308 (*At)[i][j] = (*A)[j][i];
315 vector< unsigned int >* swaps )
317 unsigned int n = A->size(), pivotRow = row;
318 double pivot = (*A)[row][col];
320 for(
unsigned i = row; i < n; ++i )
322 if( fabs( (*A)[i][col] ) > pivot )
325 pivot = (*A)[i][col];
330 if ( !
doubleEq(pivot,0.0) && pivotRow != row )
332 Matrix::iterator pivotRowItr = (A->begin() + pivotRow);
333 Matrix::iterator currRowItr = (A->begin() + row);
334 swap( *pivotRowItr, *currRowItr );
339 swaps->push_back( 10 * pivotRow + row );
342 else if ( !
doubleEq(pivot, 0.0) && pivotRow == row )
343 return (*A)[row][col];
351 unsigned int n = A->size(), i, j, diagPos;
352 double pivot, rowMultiplier1, rowMultiplier2;
362 for (i = 0; i < n; ++i)
368 for (
unsigned int k = 0; k < n; ++k )
375 while( diagPos < n - 1 )
377 rowMultiplier1 = (*invA)[diagPos][j];
378 rowMultiplier2 = (*invA)[i][j];
381 for(
unsigned int k = j + 1; k < n; ++k )
382 (*invA)[i][k] = ( (*invA)[i][k] * rowMultiplier1 -
383 (*invA)[diagPos][k] *rowMultiplier2 ) / rowMultiplier1;
386 (*L)[i][j] = rowMultiplier2 / rowMultiplier1;
396 if (
doubleEq( (*invA)[diagPos][diagPos], 0.0 ) )
398 cerr <<
"Warning : Singularity detected. Proceeding with computation"
400 (*invA)[diagPos][diagPos] =
EPSILON;
418 for(
int k = n - 1; k >= 0; --k )
420 for (
int l = k; l >= 0; --l )
426 for (
int m = k; m > l; --m )
427 sum += (*invA)[l][m] * (*invA)[m][k];
431 (*invA)[l][k] = (1 - sum)/(*invA)[l][l];
433 (*invA)[l][k] = -sum/(*invA)[l][l];
443 for(
unsigned int k = 0; k < n - 1; ++k )
444 (*invL)[k+1][k] = -(*invL)[k+1][k];
446 for(
unsigned int k = 0; k <= n - 1; ++k )
448 for(
unsigned int l = k + 2; l <= n - 1; ++l )
451 for (
unsigned int m = k + 1; m <= n - 1; ++m )
452 invTemp -= (*invL)[m][k] * (*L)[l][m];
454 (*invL)[l][k] = invTemp;
473 if ( !swaps->empty() )
485 for (
unsigned int i = 0; i < n; ++i )
486 (*A)[i].resize( n, 0.0 );
495 vec->resize( n, 0.0 );
Matrix * matMatAdd(const Matrix *A, const Matrix *B, double alpha, double beta)
Matrix * matTrans(Matrix *A)
Matrix * matScalShift(const Matrix *A, double mul, double add)
static DestFinfo dummy("dummy","This Finfo is a dummy. If you are reading this you have used an invalid index", 0)
Vector * vecScalShift(const Vector *v, double scal, double shift)
void matPermMul(Matrix *A, vector< unsigned int > *swaps)
Vector * vecVecScalAdd(const Vector *v1, const Vector *v2, double alpha, double beta)
Vector * vecMatMul(const Vector *v, Matrix *A)
bool doubleEq(double x, double y)
Matrix * matMatMul(Matrix *A, Matrix *B)
Vector * matVecMul(Matrix *A, Vector *v)
void triMatMul(Matrix *A, Matrix *B)
Matrix * matEyeAdd(const Matrix *A, double k)
void matInv(Matrix *A, vector< unsigned int > *swaps, Matrix *invA)
double doPartialPivot(Matrix *A, unsigned int row, unsigned int col, vector< unsigned int > *swaps)
vector< vector< double > > Matrix
Vector * vecAlloc(unsigned int n)
double matColNorm(Matrix *A)
double matTrace(Matrix *A)
Matrix * matAlloc(unsigned int n)