simple.h and simple.cc for algorithms independent on external libraries
This commit is contained in:
61
vecmat3.h
61
vecmat3.h
@@ -157,67 +157,6 @@ public:
|
||||
};
|
||||
|
||||
|
||||
//a simple gauss elimination as a template also for larger-size matrices in form of C-style arrays
|
||||
#define SWAP(a,b) {T temp=(a);(a)=(b);(b)=temp;}
|
||||
|
||||
template<typename T, int n, int m>
|
||||
T simple_gaussj(T (&a)[n][n],T (&b)[m][n]) //returns determinant, m is number of rhs to solve
|
||||
{
|
||||
int indxc[n],indxr[n],ipiv[n];
|
||||
int i,icol,irow,j,k,l,ll;
|
||||
T det,big,dum,pivinv;
|
||||
|
||||
det=1;
|
||||
for (j=0;j<n;j++) ipiv[j]=0;
|
||||
for (i=0;i<n;i++) {
|
||||
big=0.0;
|
||||
for (j=0;j<n;j++)
|
||||
if (ipiv[j] != 1)
|
||||
for (k=0;k<n;k++) {
|
||||
if (ipiv[k] == 0) {
|
||||
if (abs(a[j][k]) >= big) {
|
||||
big=abs(a[j][k]);
|
||||
irow=j;
|
||||
icol=k;
|
||||
}
|
||||
} else if (ipiv[k] > 1) {return 0;}
|
||||
}
|
||||
++(ipiv[icol]);
|
||||
if (irow != icol) {
|
||||
det = (-det);
|
||||
for (l=0;l<n;l++) SWAP(a[irow][l],a[icol][l])
|
||||
for (l=0;l<m;l++) SWAP(b[l][irow],b[l][icol])
|
||||
}
|
||||
indxr[i]=irow;
|
||||
indxc[i]=icol;
|
||||
if (a[icol][icol] == 0) {return 0;}
|
||||
pivinv=1/a[icol][icol];
|
||||
det *= a[icol][icol];
|
||||
a[icol][icol]=1.0;
|
||||
for (l=0;l<n;l++) a[icol][l] *= pivinv;
|
||||
for (l=0;l<m;l++) b[l][icol] *= pivinv;
|
||||
for (ll=0;ll<n;ll++)
|
||||
if (ll != icol) {
|
||||
dum=a[ll][icol];
|
||||
a[ll][icol]=0.0;
|
||||
for (l=0;l<n;l++) a[ll][l] -= a[icol][l]*dum;
|
||||
for (l=0;l<m;l++) b[l][ll] -= b[l][icol]*dum;
|
||||
}
|
||||
}
|
||||
for (l=n-1;l>=0;l--) {
|
||||
if (indxr[l] != indxc[l])
|
||||
for (k=0;k<n;k++)
|
||||
SWAP(a[k][indxr[l]],a[k][indxc[l]]);
|
||||
}
|
||||
return det;
|
||||
}
|
||||
|
||||
#undef SWAP
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//stream I/O
|
||||
#ifndef AVOID_STDSTREAM
|
||||
template <typename T>
|
||||
|
||||
Reference in New Issue
Block a user