/**************************************************************************** // Test which uses routine LU to solve a linear system using LU factorization. // // Usage: a.out < matrix.dat // // where matrix.dat is an ASCII file consisting of the // matrix size (M,N) followed by its values. For example, // enter a 3x3 matrix as: // 8.1 1.2 4.3 // 1.3 4.3 2.9 // 0.4 1.3 6.1 // C.A. Bertulani 04/07/2000 ****************************************************************************/ typedef double Number; #include #include #include #include #include "butil.h" int main() { void LU(Number **,int,int*,Number*); void LUSOLVE(Number **,int,int*,Number b[]); Number **A, **O, **y, *b, *x, *col, d, ax; int N, *indx, i, j, k; cout << " Enter number of rows" << endl; cin >> N; A = matrix(N,N); O = matrix(N,N); y = matrix(N,N); x = vector(N); b = vector(N); col = vector(N); indx = ivector(N); for(i=1;i<=N;i++)b[i]=1; cout << " Enter Matrix A[i][j]" << endl; for(i=1;i<=N;i++){ for(j=1;j<=N;j++) cin >> A[i][j]; } cout << "Original Matrix A: " << endl; for(i=1;i<=N;i++){ for(j=1;j<=N;j++){ O[i][j] = A[i][j]; /* saving original matrix A */ cout <<" " << O[i][j]; } cout << endl; } LU(O,N,indx,&d); LUSOLVE(O,N,indx,b); cout << "Solution x for Ax=b, where b=[1,1,...] " <=1;i--) { /* Now we do the backsubstitution */ sum=b[i]; for (j=i+1;j<=n;j++) sum -= a[i][j]*b[j]; b[i]=sum/a[i][i]; /* Store a component of the solution vector X */ } } /******************************************************************** Given a matrix a[1..n][1..n] this routine replaces it by the LU decomposition of a rowwise permutation of itself. a and n are input. a is also output, indx[1..n] is an output vector that records the row permutation effected by the partial pivoting, d is output as +-1 depending on whether the number of ro interchanges was even or odd, respectively. *********************************************************************/ #define TINY 1.0e-20; void LU(Number **a,int n,int *indx, Number *d) { int i,imax,j,k; Number big,dum,sum,temp; Number *vv; vv=vector(n); /* vv stores the implicit scaling of each row */ *d=1.0; /* No row interchanges yet */ for (i=1;i<=n;i++) { /* Loop over rows to get the implicit scaling */ big=0.0; /* information */ for (j=1;j<=n;j++) if ((temp=fabs(a[i][j])) > big) big=temp; if (big == 0.0) cout << "Singular matrix in routine LU" << endl; /* No nonzero largest element */ vv[i]=1.0/big; /* Save the scaling */ } for (j=1;j<=n;j++) { /* This is the loop over columns of */ for (i=1;i= big) { /* Is the figure of merit for the pivot better than the best so far? */ big=dum; imax=i; } } if (j != imax) { /* Do we need to interchange rows? */ for (k=1;k<=n;k++) { /* Yes, do so... */ dum=a[imax][k]; a[imax][k]=a[j][k]; a[j][k]=dum; } *d = -(*d); /* ...and change the parity of d */ vv[imax]=vv[j]; /* Also interchange the scale factor */ } indx[j]=imax; if (a[j][j] == 0.0) a[j][j]=TINY; if (j != n) { /* Now, finally, divide by the pivot element */ dum=1.0/(a[j][j]); for (i=j+1;i<=n;i++) a[i][j] *= dum; } } /* Go back for the next column in the reduction */ delete [] vv; } #undef TINY