Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages | Examples

mvasamp3.cpp

Go to the documentation of this file.
00001 #include "cammva.h"
00002 #include <iostream.h>
00003 #include <stdio.h>
00004 #include <stdlib.h>
00005 #include <math.h>
00006 //
00007 //######################################################################
00008 //
00009 //  CAMmva Test Program #3
00010 //
00011 //  This program demonstrates the use of sub-matrix and sub-vector access,
00012 //  and index range access, by implementing a naive Gaussian elimination routine
00013 //  and a Gaussian elimination routine with partial pivoting.
00014 //
00015 //  Chris Andersion (C) UCLA 1997
00016 //######################################################################
00017 //
00018 int main()
00019 {
00020 
00021     long N = 5;
00022 
00023     CAMdoubleMatrix M(N,N);
00024     CAMdoubleVector B(N);
00025     CAMdoubleVector X(N);
00026 
00027 //
00028 //  Initialize a test matrix and right hand side
00029 //
00030     long i,j;
00031     for(i=1; i<= N; i++)
00032     for(j=1; j<= N; j++)
00033     {
00034      M(i,j)=1.0/(double(i) + double(j));
00035     }
00036     
00037     for(i=1; i<=N; i++)
00038     {
00039      B(i)=double(i);
00040     }
00041 
00042     cout << " Test Matrix " << endl;
00043     cout << M << endl;
00044     cout << " Test Right Hand Side " << endl;
00045     cout << B << endl;
00046 //
00047 //  Standard Gaussian Elimination (without pivoting)
00048 //
00049     CAMdoubleMatrix A(N,N+1);
00050     CAMdoubleMatrix Z(1,N+1);
00051 
00052     A(_(1,N),_(1,N))   =M;
00053     A(_,N+1)           =B;
00054 
00055     for(j=1;   j<=N-1; j++)
00056     {
00057     for(i=j+1; i<=N; i++)
00058     {
00059     A(i,_) = A(i,_) - (A(i,j)/A(j,j))*A(j,_);
00060     }}
00061 //
00062 //   Back Solve
00063 //
00064     X(N) = A(N,N+1)/A(N,N);
00065 
00066     for(i=N-1; i>= 1; i--)
00067     {
00068     X(i) = A(i,N+1)/A(i,i);
00069     for(j=i+1; j<= N; j++)
00070     {
00071     X(i) = X(i) -(A(i,j)/A(i,i))*X(j);
00072     }}
00073 
00074     cout << "Residual with standard Gaussian elimination " << endl;
00075     cout << M*X - B << endl;
00076 
00077 //
00078 //  Partial Pivoting
00079 //
00080 
00081     A(1,_(1,N))        = M(3,_);
00082     A(2,_(1,N))        = M(2,_);
00083     A(3,_(1,N))        = M(1,_);
00084     A(1,N+1)           = B(3);
00085     A(2,N+1)           = B(2);
00086     A(3,N+1)           = B(1);
00087 
00088     CAMdoubleMatrix TMP(1,N+1);
00089 
00090     double pmax;
00091     long   imax;
00092 
00093     for(j=1;   j<=N-1; j++)
00094     {
00095 
00096     pmax = fabs(A(j,j));
00097     imax = j;
00098     for(i = j+1; i <=N; i++)
00099     {
00100     if(fabs(A(i,j)) > pmax){pmax = A(i,j); imax = i;}
00101     }
00102     TMP = A(j,_);
00103     A(j,_) = A(imax,_);
00104     A(imax,_) = TMP;
00105 
00106     for(i=j+1; i<=N; i++)
00107     {
00108     A(i,_) = A(i,_) - (A(i,j)/A(j,j))*A(j,_);
00109     }}
00110 //
00111 //   Back Solve
00112 //
00113     X(N) = A(N,N+1)/A(N,N);
00114 
00115     for(i=N-1; i>= 1; i--)
00116     {
00117     X(i) = A(i,N+1)/A(i,i);
00118     for(j=i+1; j<= N; j++)
00119     {
00120     X(i) = X(i) -(A(i,j)/A(i,i))*X(j);
00121     }}
00122 
00123     cout << "Residual with Gaussian elimination and pivoting " << endl;
00124     cout << M*X - B << endl;
00125 
00126 
00127     cout << M/B - X << endl;
00128 
00129     cout << M*(M/B) - B << endl;
00130 
00131     cout << " Program End : Hit Any Key to Terminate " << endl;
00132     getchar();
00133     return 0;
00134 }
00135 
00136   

Generated on Wed Sep 5 12:54:23 2007 for DSACSS Operational Code by  doxygen 1.3.9.1