// =====================< EXVECTOR.CPP >=========================== // * Examples of Vector class * // * Description: Chapter 12 * // * Scientific C++ Building Numerical Libraries * // * the Object-Oriented Way * // * by G. Buzzi-Ferraris * // * Addison-Wesley (1993) * // **************************************************************** // * To be assembled with: utility.obj, vector.obj, * // * matrix.obj * // **************************************************************** // * To execute: exvector * // * If equals a zero it executes all examples * // * otherwise it executes only that example * // * It writes the results on < file name> * // * If not used, it writes on screen * // * If one gives Y/N equal to Y it prints messages as well * // * the default is Y * // **************************************************************** // * Use: exvector 0 pro.doc * // * Description: Executes all examples * // * Prints the result on pro.doc * // * exvector 1 pro.doc * // * Executes example 1 with messages and results on PRO.DOC * // * exvector * // * Executes everything and sends it to stdout * // ================================================================ // initialisations and assignments void Ex_Vector1(void); // operations void Ex_Vector2(void); // Max and Min void Ex_Vector3(void); // norms void Ex_Vector4(void); // Reverse, Sort, Normalize, Swap void Ex_Vector5(void); #include #include #include #include #include "utility.hpp" #include "vector.hpp" #include "matrix.hpp" int main(int argc,char **argv) { const int NEXVECTOR = 5; const int NMAX = NEXVECTOR+1; int i; void (*ExVector[NMAX])(void); char myfilename[81]; ExVector[1]=Ex_Vector1; ExVector[2]=Ex_Vector2; ExVector[3]=Ex_Vector3; ExVector[4]=Ex_Vector4; ExVector[5]=Ex_Vector5; if(argc>=3) { strcpy(myfilename,argv[2]); if((bzzFileOut=fopen(myfilename,"w"))==NULL) Error("fopen Failed in EXVECTOR"); if(argc==4)bzzYesNo=toupper(argv[3][0]); } else bzzFileOut=stdout; if(argc==1||atoi(argv[1])<=0||atoi(argv[1])>NEXVECTOR) { // all examples for(i=1;i<=NEXVECTOR;i++) (*ExVector[i])(); } else (*ExVector[atoi(argv[1])])(); return 1; } // ================================================================ // ==================== EXAMPLES ============================== // ================================================================ void Ex_Vector1(void) { // ================================================================ // =============== INITIALISATIONS ============================ // ================================================================ { Message("\n\n\n* Examples of Vector initialisation *"); Message("\ndefault constructor\n"); Vector a; a.Print("Vector a"); Message("\nconstructor for sizing\n"); Vector b(3); b.Print("Vector b(3)"); Message("\nsizing and initialising\n"); Vector c(3,1.,2.,3.); c.Print("Vector c(3,1.,2.,3.)"); Message("\ninitialising a Vector with a Vector\n"); Vector d = c; d.Print("Vector d = c"); Message("\ninitialising a Vector with an array\n"); float w[]={1.,2.,3.,4.,5.,6.}; Vector e(6,w); e.Print("Vector e(6,w)"); Message("\ninitialising from an ASCII File \n"); Vector f("VECT.DAT"); f.Print("Vector f(FILE)"); Message("\nsubvector initialised from a Vector\n"); Vector g(3,e); g.Print("Vector g(3,e)"); Message("\nintermediate subvector\n"); Vector p(2,3,e); p.Print("Vector p(2,3,e)"); Message("\nassignment, not initialisation!\n"); Vector h;h=e; h.Print("Vector h;h=e;"); } // ================================================================ // =========== Save and Initialisations ======================= // ================================================================ Message("\n\n\n****** Examples of save and reading *********"); { Message("\nUses the class TempFile from utility.cpp\n"); Message("\n** WARNING: uses drive C:\\TEMP **"); TempFile f1("C:\\TEMP\\"),f2("C:\\TEMP\\"); Message("\nInitialises ASCII File\n"); Vector v("VECT.DAT"); Vector x = v; // initialises x Message("\n Saves unformatted x\n"); x.Save('*',f1.FileName()); Message("\ninitialises y from unformatted file\n"); Vector y('*',f1.FileName()); Message("\naudit printing \n"); v.Print("v"); x.Print("v"); y.Print("v"); Message("\nSaves y on ASCII file\n"); y.Save(f2.FileName()); Message("\nInitialises z with the same File\n"); Vector z(f2.FileName()); z.Print("z"); } // ================================================================ // =============== Save and Recover =========================== // ================================================================ { Message("\n\n\n****** Examples of Save and Recover ********"); Message("\n** WARNING: Recover and Save uses drive C:\\TEMP **"); Message("\nUses a file of the TempFile class\n"); TempFile f1("C:\\TEMP\\"); Vector v(100);v(100)=100.; Message("\nSaving vector v\n"); v.Save(f1.FileName()); // formatted v(10)=5.;v(100)=7.; Message("\nrecovers vector from File\n"); Recover(&v,f1.FileName()); // recovers Message("\n%f",v(100)); Message("\nSaves on an unformatted file\n"); v.Save('*',f1.FileName()); // unformatted v(10)=5.;v(100)=7.; Message("\nRecovers\n"); Recover(&v,'*',f1.FileName()); // recovers Message("\n%f",v(100)); } // ================================================================ // =============== ACCESS FUNCTIONS ============================== // ================================================================ { Message("\n\n\n*** Examples of access functions ******"); float s[]={3.,1.,5.,-31.,7.,19.,21.,33.,1.}; Vector c(9,s); c.Print("Must be 3.,1.,5.,-31.,7.,19.,21.,33.,1."); Message("\nVarious ways of assigning a value\n"); c(1)=11.; // first way WITH CONTROL c[2]=12.; // second way WITHOUT CONTROL c.SetValue(3,13.); // third way WITH CONTROL c.Print("Should be 11,12,13,-31,19,21.,33,1"); Message("\nVarious ways of arriving at a value\n"); Message("\nvalue of 3,4,5 = %f %f %f", c(3),c[4],c.GetValue(5)); } { Message("\n\n** Assignment of portions of vectors **"); float s[]={1.,2.,3.,4.,5.,6.,7.,8.,9.}; Vector a(9,s); a.Print("starting from a"); Vector b(3,a); b.Print("b(3,a) first three values of a"); Vector c(2,4,a); c.Print("c(2,4,a) two values starting from the fourth"); Vector d; d=GetVector(2,4,a); d.Print("d=GetVector(2,4,a) "); d(1)=11.; Message("\nmodifying d(1)=11."); d(2)=12.; Message("\nmodifying d(2)=12."); d.Print("d"); a.SetVector(3,d); a.Print("a.SetVector(3,d)"); Vector e;e = a; e(2)=0.;e(3)=0.; a.Print("a"); e.Print("e as a with e(2)=e(3)=0"); } } // ================================================================ // ================== OPERATIONS ============================== // ================================================================ void Ex_Vector2(void) { { Message("\n\n\n*** Examples of Vector operators ******"); float w[]={1.,2.,3.,4.,5.,6.}; Vector a(6,w),b(3,33.,55.,66.); Message("\nComposed of && \n"); Vector c = a&&b&&a; c.Print("\nOperator &&"); Message("\nlogical operators == e != \n"); if(a==c) Message("\na==c"); else Message("\na!=c"); if(a!=c) Message("\na!=c"); else Message("\na==c"); Vector e; Message("\nMultiple assignment\n"); b=c=a; c.Print("c=b=a;"); } { Message("\n***********< Sum >*************\n"); float w[]= {1.,2.,3.,4.,5.,6.}; float v[]= {7.,8.,9.,10.,11.,12.}; Vector a(6,w); Vector b(6,v); Vector c; Sum(a,b,&c); c.Print("Sum(a,b,&c);"); } { Message("\n***********< Sum >*************\n"); float w[]= {1.,2.,3.,4.,5.,6.}; float v[]= {7.,8.,9.,10.,11.,12.}; Vector a(6,w); Vector b(6,v); Vector c; c = a + b; c.Print("c = a + b;"); } { Message("\n***********< Sum >*************\n"); float w[]= {1.,2.,3.,4.,5.,6.}; float v[]= {7.,8.,9.,10.,11.,12.}; Vector a(6,w); Vector b(6,v); Sum(&a,b); a.Print("Sum(&a,b);"); } { Message("\n***********< Sum >*************\n"); float w[]= {1.,2.,3.,4.,5.,6.}; float v[]= {7.,8.,9.,10.,11.,12.}; Vector a(6,w); Vector b(6,v); a += b; a.Print("a +=b;"); } { Message("\n***********< Sum >*************\n"); float w[]= {1.,2.,3.,4.,5.,6.}; float v[]= {7.,8.,9.,10.,11.,12.}; Vector a(6,w); Vector b(6,v); Sum(a,&b); b.Print("Sum(a,&b);"); } { Message("\n***********< Sum >*************\n"); float w[]= {1.,2.,3.,4.,5.,6.}; Vector a(6,w); Sum(&a); // a = a + a; a.Print("Sum(&a);"); } { Message("\n***********< Difference >*************\n"); float w[]= {1.,2.,3.,4.,5.,6.}; float v[]= {7.,8.,9.,10.,11.,12.}; Vector a(6,w); Vector b(6,v); Vector c; c = a - b; c.Print("c = a - b;"); } { Message("\n***********< Difference >*************\n"); float w[]= {1.,2.,3.,4.,5.,6.}; float v[]= {7.,8.,9.,10.,11.,12.}; Vector a(6,w); Vector b(6,v); Vector c; Difference(a,b,&c); c.Print("Difference(a,b,&c);"); } { Message("\n***********< Difference >*************\n"); float w[]= {1.,2.,3.,4.,5.,6.}; float v[]= {7.,8.,9.,10.,11.,12.}; Vector a(6,w); Vector b(6,v); Difference(&a,b); a.Print("Difference(&a,b);"); } { Message("\n***********< Difference >*************\n"); float w[]= {1.,2.,3.,4.,5.,6.}; float v[]= {7.,8.,9.,10.,11.,12.}; Vector a(6,w); Vector b(6,v); a -= b; a.Print("a -=b;"); } { Message("\n***********< Difference >*************\n"); float w[]= {1.,2.,3.,4.,5.,6.}; float v[]= {7.,8.,9.,10.,11.,12.}; Vector a(6,w); Vector b(6,v); Difference(a,&b); b.Print("Difference(a,&b);"); } { Message("\n***********< Minus >*************\n"); float w[]= {1.,2.,3.,4.,5.,6.}; Vector a(6,w); Vector b; Minus(a,&b); b.Print("Minus(a,&b);"); } { Message("\n***********< Minus >*************\n"); float w[]= {1.,2.,3.,4.,5.,6.}; Vector a(6,w); Vector b; b = -a; b.Print("b = -a;"); } { Message("\n***********< Minus >*************\n"); float w[]= {1.,2.,3.,4.,5.,6.}; Vector a(6,w); Minus(&a); a.Print("Minus(&a);"); } { Message("\n***********< Product >*************\n"); float a[]= {2.,1.,3., 4.,4.,7., 2.,5.,9.}; Matrix A(3,3,a); float w[]= {-.5,-1.,1.}; Vector x(3,w); Vector y; Product(A,x,&y); y.Print("Product(A,x,&y);"); } { Message("\n***********< Product >*************\n"); float a[]= {2.,1.,3., 4.,4.,7., 2.,5.,9.}; Matrix A(3,3,a); float w[]= {-.5,-1.,1.}; Vector x(3,w); Vector y =A*x; y.Print("y = A*x;"); } { Message("\n***********< Product >*************\n"); float w[]= {-.5,-1.,1.}; Vector x(3,w),y; Product(3.,x,&y); y.Print("Product(3.,x,&Y);"); } { Message("\n***********< Product >*************\n"); float w[]= {-.5,-1.,1.}; Vector x(3,w); Vector y =3.*x; y.Print("y = 3.*x;"); } { Message("\n***********< Product >*************\n"); float w[]= {-.5,-1.,1.}; Vector x(3,w); Product(3.,&x); x.Print("Product(3.,&x);"); } { Message("\n***********< Product >*************\n"); float w[]= {-.5,-1.,1.}; Vector x(3,w); x *= 3.; x.Print(" x *= 3.;"); } { Message("\n***********< TProduct >*************\n"); float w[]= {1.,2.,3.}; Vector x(3,w),y(3,w); float c; TProduct(x,y,&c); Message("\nmust be 14 : %f",c); } { Message("\n***********< TProduct >*************\n"); float w[]= {1.,2.,3.}; Vector x(3,w),y(3,w); float c; c = Dot(x,y); Message("\nmust be 14 : %f",c); } { Message("\n***********< TProduct >*************\n"); float w[]= {1.,2.,3.}; Vector x(3,w),y(3,w); float c; c = x%y; Message("\nmust be 14 : %f",c); } { Message("\n***********< ProductT >*************\n"); float w[]= {1.,2.,3.}; Vector x(3,w),y(3,w); Matrix A; ProductT(x,y,&A); A.Print("Product(x,y,&A);"); } { Message("\n***********< ProductT >*************\n"); float w[]= {1.,2.,3.}; Vector x(3,w),y(3,w); Matrix A = x->*y; A.Print("A = x->*y;"); } { Message("\n***********< Division >*************\n"); float w[]= {3.,9.,18.}; Vector x(3,w),y; Division(x,3.,&y); y.Print("Division(x,3.,&Y);"); } { Message("\n***********< Division >*************\n"); float w[]= {3.,9.,18.}; Vector x(3,w); Vector y = x/3.; y.Print("y = x/3.;"); } { Message("\n***********< Division >*************\n"); float w[]= {3.,9.,18.}; Vector x(3,w); Division(&x,3.); x.Print("Division(&x,3.);"); } { Message("\n***********< Division >*************\n"); float w[]= {3.,9.,18.}; Vector x(3,w); x /= 3.; x.Print(" x /= 3.;"); } } // ================================================================ // =================== MAX-MIN ================================ // ================================================================ void Ex_Vector3(void) { { Message("\n\n********* Max Min **********"); float s[]={3.,1.,5.,-31.,7.,19.,21.,33.,1.}; Vector a(9,s); a.Print("Vector"); float q; int i; q=a.Max(); Message("\nMax %f",q); q=a.Max(&i); Message("\nMax %d %f",i,q); q=a.MaxAbs(); Message("\nMaxAbs %f",q); q=a.MaxAbs(&i); Message("\nMaxAbs %d %f",i,q); q=a.Min(); Message("\nMin %f",q); q=a.Min(&i); Message("\nMin %d %f",i,q); q=a.MinAbs(); Message("\nMinAbs %f",q); q=a.MinAbs(&i); Message("\nMinAbs %d %f",i,q); } } // ================================================================ // ==================== NORMS ================================= // ================================================================ void Ex_Vector4(void) { { Message("\n\n********* Vector Norms ****************"); float s[]={3.,1.,5.,-31.,7.,19.,21.,33.,1.}; Vector a(9,s); a.Print("Vector"); Message("\nNorm1=%f",a.Norm1()); Message("\nNorm2=%f",a.Norm2()); Message("\nNormI=%f",a.NormI()); } } // ================================================================ // ================ Reverse, Sort ============================= // =============== Normalize, Swap ============================ // ================================================================ void Ex_Vector5(void) { { Message("\n\n\n******** Example of Reverse ******"); float w[]={111.,-2.,33.,-4.,5.,6.}; Vector a(6,w); a.Print("Before Reverse(&a)"); Reverse(&a); a.Print("Reverse(&a)"); Message("\n\n\n******** Example of Sort ******"); Sort(&a); a.Print("Sort(&a)"); Message("\n\n\n******** Example of Normalize ******"); Vector b(6,w); b.Print("b"); float norm=Normalize(&b); b.Print("b normalised"); Message("\nNorm of b %f",norm); Message("\nVerification of %f",b%b); Message("\n\n\n******** Example of Swap ******"); Vector c(10,1.,2.,3.,4.,5.,6.,7.,8.,9.,10.); a.Print("a before Swap"); c.Print("c before Swap"); Swap(&a,&c); a.Print("a after Swap"); c.Print("c after Swap"); } }