// ======================< VECTOR.HPP >======================= // * Class Vector * // * for operations between matrices and vectors * // * Description: Chapter 12 * // * Scientific C++ Building Numerical Libraries * // * the Object-Oriented Way * // * by G. Buzzi-Ferraris * // * Addison-Wesley (1993) * // =========================================================== // ****** Constructors for Vector: * // * Vector v; // default * // * Vector v = x; // copy-initializer * // * Vector v(n); // sizes and places at 0 * // * Vector v(5,1.,2.,3.,4.,5.); // vector 1,2,3,4,5 * // * float x[5]={1.,2.,3.,4.,5.}; * // * Vector v(5,x); // from array * // * Vector v("VET.DAT"); // Formatted file * // * Vector v('*',VET.BIN"); // Binary file * // * Vector v(nc,w); // subvector of w * // * Vector(nc,i,w);// subvector of w; starting from i * // *********************************************************** // ***** Access functions : * // * int i = v.Size();// dimensions * // * float xf = v.GetValue(i) // element i * // * Vector w = GetVector(nc,i,v); // subvector * // * float xf = v(i); v(i) = 4.; * // * float xf = v[i]; v[i] = 7.; * // * v.SetValue(i,7.); * // * v.SetVector(i,w); // Vector w * // *********************************************************** // ***** Assignment: * // * v = w; // Assignment * // *********************************************************** // ***** Operators for forming vectors: * // * v = w&&y; // appends one vector to another * // *********************************************************** // ***** Operators for tests: * // * if(v == w) * // * if(v != w) * // *********************************************************** // ***** Implemented operations : * // * Sum (a,b,&c); // c = a + b; * // * c = a + b; // c = a + b; * // * Sum (a,b,&a); // a = a + b; * // * Sum (&a,b); // a = a + b; * // * a += b; // a = a + b; * // * Sum (a,b,&b); // b = a + b; * // * Sum (b,&a); // a = b + a; * // * Sum (a,a,&a); // a = a + a; * // * Sum (&a); // a = a + a; * // * Difference(a,b,&c); // c = a - b; * // * c = a - b; // c = a - b; * // * Difference(a,b,&a); // a = a - b; * // * Difference(&a,b); // a = a - b; * // * a -= b; // a = a - b; * // * Difference(a,b,&b); // b = a - b; * // * Difference(b,&a); // a = b - a; * // * Difference(a,a,&a); // a = a - a; * // * Difference(&a); // a = a - a; * // * Minus (a,&b); // b = -a; * // * b = -a; * // * Minus (&a); // a = -a; * // * Product(A,x,&y); // y = A*x; A Matrix * // * y = A*x; // y = A*x * // * Product(A,x,&x); // x = A*x; * // * Product(A,&x); // x = A*x; * // * Product(R,x,&y); // y = R*x; R MatrixRight * // * y =R*x; * // * Product(L,x,&y); // y = L*x; L MatrixLeft * // * y = L*x; * // * Product(3.,x,&y); // y = 3.*x; * // * y = 3.*x; * // * Product(3.,x,&x); // x = 3.*x; * // * Product(3.,&x); // x = 3.*x; * // * x *= 3.; // x = 3.*x; * // * TProduct(x,y,&xf); // xf = xTy; * // * xf = Dot(x,y); // xf = xTy; * // * xf = x%y; // xf = xTy; * // * TProduct(A,x,&y); // y = ATx; * // * y = A%x; // y = ATx; * // * TProduct(A,x,&x); // x = ATx; * // * TProduct(A,&x); // x = ATx; * // * ProductT(x,y,&A); // A = xyT; * // * A = x->*y; // A = xyT; * // * Division(x,3.,&y); // y = x/3.; * // * y = x/3.; * // * Division(&x,3.); // x = x/3.; * // * x /= 3.; // x = x/3.; * // *********************************************************** // * Other functions: * // * v.Print("comment"); * // * v.Save("Vet.DAT"); * // * v.Save('*',"VET.BIN"); * // * float xf = v.Max(imax); * // * xf = v.MaxAbs(imax); * // * xf = v.Min(imin); * // * xf = v.MinAbs(imin); * // * xf = v.Norm1(); * // * xf = v.Norm2(); * // * xf = v.NormI(); * // * Delete(&v); // eliminate a Vector * // * ChangeDimensions(newdim,&v); * // * Recover(&v,"VET.DAT"); * // * Recover(&v,'*',"VET.BIN"); * // * Normalize(&v); // vTv = 1 * // * Reverse(&v); * // * Sort(&v); * // * Swap(&x,&y); * // *********************************************************** #ifndef VECTOR_HPP #define VECTOR_HPP // preventive statements class Vector; class Matrix; class MatrixLeft; class MatrixRight; class MatrixSymm; class MatrixSparse; class Factored; class FactoredPLR; class FactoredGauss; class FactoredQRLQ; class FactoredQR; class FactoredLQ; class FactoredSVD; class FactoredSymm; // =========================================================== // =================== class Vector ====================== // =========================================================== class Vector { friend class Matrix; friend class MatrixRight; friend class MatrixLeft; friend class MatrixSymm; friend class MatrixSparse; friend class Factored; friend class FactoredPLR; friend class FactoredGauss; friend class FactoredQRLQ; friend class FactoredQR; friend class FactoredLQ; friend class FactoredSVD; friend class FactoredSymm; private: static const char *const ERROR; static int count; // for whoAmI float *vector; int dimensions; int whoAmI; // initialise constructors void Initialize(int nc); // private constructor Vector('*',nc) Vector(char,int nc); public: // =========================================================== // ****************** constructors *********************** // =========================================================== // default constructor Vector v; Vector(void); // copy-initializer Vector(const Vector &rval); // sizes and initialises at 0 Vector(int nc); // sizes and initialises Vector(int nc,double v1,...); // data pointer constructor Vector(int nc,float *initvalues); // file v(FILE) constructor; Vector(char *filevector); // unformatted v('*',FILE) constructor saved with Save Vector(char,char *filevector); // makes a subvector of size nc<=n Vector(int nc,const Vector &rval); // likewise starting from ielem Vector(int nc,int ielem,const Vector &rval); // =========================================================== // ******************** destructor *********************** // =========================================================== ~Vector(void); // =========================================================== // ********** Non-modifying access functions ************* // =========================================================== int Size(void) const {return dimensions;} // dimensions int WhoAmI(void) const {return whoAmI;} // receives the value of the vector with control float GetValue(int i) const; // subvector (similar to Vector(nc,ielem,Vector) friend Vector GetVector (int nc,int ielem,const Vector &rval); // =========================================================== // ************* Modifying access functions ************** // =========================================================== // assigns and receives vector values with control float &operator () (int i); // assigns and receives values without control float &operator [] (int i){return vector[i];} // assigns vector values with control void SetValue(int i,float val); // modifies the vector starting from ielem with rval void SetVector(int ielem,const Vector &rval); // =========================================================== // **************** assignment operators ******************** // =========================================================== Vector &operator = (const Vector &rval); // =========================================================== // *********** operators for composing vectors *********** // =========================================================== //adds one vector to another in sequence friend Vector operator && (const Vector &lval,const Vector &rval); // =========================================================== // **************** operators for tests ****************** // =========================================================== friend char operator == (const Vector &lval,const Vector &rval); friend char operator != (const Vector &lval,const Vector &rval); // =========================================================== // ==================== OPERATIONS ======================= // =========================================================== // =========================================================== // *********************** Sum *************************** // =========================================================== friend void Sum(const Vector &lval,const Vector &rval, Vector *result); // Sum (a,b,&c); c = a + b; friend Vector operator + (const Vector &lval,const Vector &rval); // Sum (&a,b); a = a + b; friend void Sum(Vector *lvalAndResult,const Vector &rval); Vector &operator += (const Vector &rval); // Sum (b,&a); a = b + a; friend void Sum(const Vector &lval,Vector *rvalAndResult); // Sum (&a); a = a + a; friend void Sum(Vector *lvalRvalAndResult); // =========================================================== // ******************** Difference *********************** // =========================================================== // Difference(a,b,&c); c = a - b; friend void Difference(const Vector &lval, const Vector &rval,Vector *result); // c = a - b; friend Vector operator - (const Vector &lval,const Vector &rval); // Difference(&a,b); a = a - b; friend void Difference(Vector *lvalAndResult, const Vector &rval); // a -= b; a = a - b; Vector &operator -= (const Vector &rval); // Difference(b,&a); a = b - a; friend void Difference(const Vector &lval, Vector *rvalAndResult); // Difference(&a); a = a - a; friend void Difference(Vector *lvalRvalAndResult); // =========================================================== // *********************** Minus ************************* // =========================================================== // Minus (a,&b); b = -a; friend void Minus (const Vector &rval,Vector *result); Vector operator -();// unary minus // Minus (&a); a = -a; friend void Minus (Vector *rvalAndResult); // =========================================================== // ********************** Product ************************ // =========================================================== // Product(A,x,&y); y = A*x; friend void Product (const Matrix &lval,const Vector &rval,Vector *result); // y = A*x; friend Vector operator * (const Matrix &lval,const Vector &rval); // Product(A,&x); x = A*x; friend void Product (const Matrix &lval,Vector *rvalAndresult); // Product(R,x,&y); y = R*x; friend void Product (const MatrixRight &lval,const Vector &rval, Vector *result); friend Vector operator * //y =R*x; (const MatrixRight &lval,const Vector &rval); // Product(L,x,&y); y = L*x; friend void Product (const MatrixLeft &lval,const Vector &rval, Vector *result); friend Vector operator * // y = L*x; (const MatrixLeft &lval,const Vector &rval); // Product(3.,x,&y); y = 3.*x; friend void Product (float lval,const Vector &rval,Vector *result); friend Vector operator * (float lval,const Vector &rval); // Product(3.,&x); x = 3.*x; friend void Product (float lval,Vector *rvalAndResult); Vector &operator *= (float rval); // =========================================================== // ****************** TProduct, Dot ********************** // =========================================================== // TProduct(x,y,&c); c = xTy; c = x%y; c = Dot(x,y); friend void TProduct(const Vector &lval, const Vector &rval,float *result); // c = Dot(x,y); c = xTy; friend float Dot(const Vector &lval,const Vector &rval); // c = x%y; c = xTy; friend float operator % (const Vector &lval,const Vector &rval); // TProduct(A,x,&y); y =ATx; y =A%x; friend void TProduct(const Matrix &lval, const Vector &rval,Vector *result); // y = ATx; friend Vector operator % (const Matrix &lval,const Vector &rval); // TProduct(A,&x); x = ATx; friend void TProduct (const Matrix &lval,Vector *rvalAndresult); // =========================================================== // ********************* ProductT ************************ // =========================================================== // ProductT(x,y,&A); A = xyT; A = x->*y; friend void ProductT(const Vector &lval, const Vector &rval,Matrix *result); friend Matrix operator ->* (const Vector &lval,const Vector &rval); // =========================================================== // ********************** Division *********************** // =========================================================== // Division(x,3.,&y); y = x/3.; friend void Division (const Vector &lval,float rval,Vector *result); friend Vector operator / // y = x/3. (const Vector &lval,float rval); // Division(&x,3.); x = x/3.; friend void Division (Vector *lvalAndResult,float rval); // x /= 3.; x = x/3.; Vector &operator /= (float rval); // =========================================================== // =========== Non-modifying functions =================== // =========================================================== // ***********************< Print >*************************** void Print(char *msg=""); void Save(char *filevector); // formatted void Save(char,char *filevector);// binary // *********************< Max and Min >*********************** float Max(int *imax = 0); float MaxAbs(int *imax=0); float Min(int *imin=0); float MinAbs(int *imin=0); // ***********************< Norms >*************************** float Norm1(void); float Norm2(void); float NormI(void); // =========================================================== // ============= Modifying Functions ======================= // =========================================================== friend void Delete(Vector *result); // eliminates Vector friend void ChangeDimensions(int dim,Vector *result, char zero = 0); // recovery from Save friend void Recover (Vector *result,char *filevector); // formatted friend void Recover (Vector *result,char,char *filevector);// binary friend void SumRankOne(const Vector &u, const Vector &vT,Matrix *result); friend void SumRankOne(float product,const Vector &u, const Vector &vT,Matrix *result); friend void SumRankOne(const Vector &u, const Vector &vT,float divisor,Matrix *result); //normalises (xTx=1) and returns norm2 friend float Normalize(Vector *result); friend void Reverse(Vector *result); //inverts the vector friend void Sort(Vector *result); // orders the vector // swaps the contents of two vectors friend void Swap(Vector *lval,Vector *rval); }; #endif VECTOR_HPP