Main Page   Modules   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

Matrix.h

Go to the documentation of this file.
00001 
00002 //
00003 //    Title     :  Matrix.h
00004 //    Project     :  OSCAR Version 2.0
00005 //    Created     :  2/10/1995
00006 //    Author      :  Chetan Kapoor and S. M. Orlow (NIH), Murat Cetin (MATLAB)
00007 //    Platforms   :  All
00008 //    Copyright   :  Copyright, The University of Texas at Austin, 2002. All rights reserved.
00009 //
00010 //          This software and documentation constitute an unpublished work
00011 //          and contain valuable trade secrets and proprietary information
00012 //          belonging to the University. None of the foregoing material may be
00013 //          copied or duplicated or disclosed without the express, written
00014 //          permission of University. THE UNIVERSITY EXPRESSLY DISCLAIMS ANY
00015 //          AND ALL WARRANTIES CONCERNING THIS SOFTWARE AND DOCUMENTATION,
00016 //          INCLUDING ANY WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
00017 //          PARTICULAR PURPOSE, AND WARRANTIES OF PERFORMANCE, AND ANY WARRANTY
00018 //          THAT MIGHT OTHERWISE ARISE FROM COURSE OF DEALING OR USAGE OF TRADE.
00019 //          NO WARRANTY IS EITHER EXPRESS OR IMPLIED WITH RESPECT TO THE USE OF
00020 //          THE SOFTWARE OR DOCUMENTATION. Under no circumstances shall the
00021 //          University be liable for incidental, special, indirect, direct or
00022 //          consequential damages or loss of profits, interruption of business,
00023 //          or related expenses which may arise from use of software or documentation,
00024 //          including but not limited to those resulting from defects in software
00025 //          and/or documentation, or loss or inaccuracy of data of any kind.
00026 //
00027 //    Purpose     :  Matrix declares the class Matrix
00028 //
00029 //-----------------------------------------------------------------------------
00030 //
00031 //    Classes     :  Matrix, MatrixCol, MatrixRow
00032 //
00033 //    Global
00034 //    Functions   :  ostream& operator<<(ostream& strm,const Matrix& m);
00035 //             ostream& operator<<(ostream& strm,const MatrixRow& m);
00036 //                   ostream& operator<<(ostream& strm,const MatrixCol& m);
00037 //
00038 //-----------------------------------------------------------------------------
00039 //
00040 //    $Revisions$  1.1 1 /usr/Components/src/SCCS/s.Matrix.hpp /usr/Components/src/SCCS/s.Matrix.hpp
00041 //        Added GetRow, GetCol, SetRow, SetCol on Dec. 17, 2001 - chetan kapoor
00042 //        Added constructor Matrix(unsigned int,unsigned int,const Vector&,
00043 //                                       OSCARError*,const String&) - Troy Harden 1/17/2002
00044 //        Modified constructor Matrix(unsigned int,unsigned int,const Vector&,
00045 //                                       OSCARError&,const String&) - Chetan Kapoor 3/25/2002
00046 //        Added MatrixNorm method that does not require Matlab - Mark Tisius 9/25/2002
00047 //        Removed coFactor methods because they didn't work correctly and they didn't even
00048 //                                       take the determinant - Mark Tisius 10/1/02
00049 //        Removed PolarDecomp method because no implementation existed - Mark Tisius 10/9/02
00050 //        Made inverses virtual and moved global multiply and operator* to utilities - Mark Tisius 10/13/02
00051 //        Changed 'at' and 'operator()' methods to enforce constness, added non-const versions - Ethan Swint 8/12/03
00052 //
00053 //    $Log: Matrix.h,v $
00054 //    Revision 1.32  2005/03/18 23:05:24  chetan
00055 //    Fixed g++ compile warnings. Removed virtual inheritance from Base, added initialization of base class in copy constructors, added newline at EOF
00056 //
00057 //    Revision 1.31  2005/03/14 22:04:21  pmarch
00058 //    Added to namespace OSCAR and removed "RR" from front of class names.
00059 //
00060 //    Revision 1.30  2005/03/10 17:12:41  josh
00061 //    Added LUSolve to Matrix and updated EqnSolver accordingly.
00062 //
00063 //    Revision 1.29  2005/03/07 21:21:01  josh
00064 //    Added Cholesky Decomposition.
00065 //
00066 //    Revision 1.28  2005/01/06 17:54:02  josh
00067 //    Added scratch vectors to help speed up the SpeedInverse method.  Changed constrctuors and destructors accordingly
00068 //
00069 //    Revision 1.27  2005/01/06 16:37:46  chetan
00070 //    Added a scratch matrix to help speed up the SpeedInverse method.  Changed constrctuors and destructors accordingly
00071 //
00072 //    Revision 1.26  2004/11/05 17:31:46  chetan
00073 //    Added method ToFlatString()
00074 //
00075 //    Revision 1.25  2004/07/26 22:25:22  chetan
00076 //    Made changes to compile with Warning Level 4 and also under the latest C++ standard. Also made compilable with VC extensions turned off
00077 //
00078 //    Revision 1.24  2004/07/19 16:46:29  edjung
00079 //    -changed copy and operator= to take a const reference for ANSI compliance
00080 //
00081 //    Revision 1.23  2004/07/19 16:37:01  edjung
00082 //    -made fix to constructors for ANSI C++ compliance with temporary OSCARError objects
00083 //    -added DUMMY_ERROR class
00084 //
00085 //    Revision 1.22  2004/06/10 19:11:56  aspencer
00086 //    Added /ingroup tags.
00087 //
00088 //    Revision 1.21  2004/06/04 21:58:33  chetan
00089 //    using namespace std in CommIncludes.h was removed. std:: was added anywhere STL components such as iostream and vector were being used
00090 //
00091 //    Revision 1.20  2004/06/04 20:09:42  chetan
00092 //    Changed all local Matrix and new int[], new double[] code in method calls to use static Matrix, static VectorI, and static Vector respectively. This change is based on the assumption that an Matrix object cannot be resized
00093 //
00094 //    Revision 1.19  2004/05/26 20:01:58  pmarch
00095 //    Added /defgroup and /ingroup tags.
00096 //
00097 //    Revision 1.18  2004/01/27 17:06:30  pmarch
00098 //    Various commenting fixes.
00099 //
00100 //    Revision 1.17  2004/01/26 20:54:33  pmarch
00101 //    Fixed documentation errors as reported by Doxygen.
00102 //
00103 //    Revision 1.16  2003/10/06 09:13:32  edjung
00104 //    -Changed constructors to use String() instead of String("")
00105 //
00106 //    Revision 1.15  2003/09/11 03:00:20  eswint
00107 //    - Changed signatures of public methods combineRows and upperTriangular to CombineRows and UpperTriangular
00108 //
00109 //    Revision 1.14  2003/09/10 22:53:27  eswint
00110 //    - Changed signature of max() to Max() to avoid naming conflict with macro.
00111 //
00112 //    Revision 1.13  2003/09/09 17:09:36  eswint
00113 //    - Corrected Multply(Vector rhs, Vector result)
00114 //
00115 //    Revision 1.12  2003/09/05 02:08:57  eswint
00116 //    - Corrected documentation for SwitchRows and SwitchCols
00117 //
00118 //    Revision 1.11  2003/09/04 22:53:06  eswint
00119 //    - Changed default arguments: OSCARError(0,"No Error"...) to OSCARError(noError) and String(" ") to String("")
00120 //    - Added GetArray() methods (const & non-const) to allow fast data access
00121 //
00122 //    Revision 1.10  2003/09/02 20:29:13  eswint
00123 //    - Added Multiply methods for matrices and vectors, avoids construction of temporaries
00124 //
00125 //    Revision 1.9  2003/08/28 20:37:17  eswint
00126 //    - Changed unary assignment operators to return a reference to the object
00127 //    - Added default constructor argument bool initMtx = true
00128 //
00129 //    Revision 1.8  2003/08/26 03:29:08  eswint
00130 //    - Changed signature of the norm() method (misnomer) to max()
00131 //
00132 //    Revision 1.7  2003/08/26 01:50:49  eswint
00133 //    - Changed signature of MatrixRow::operator^ to operator* as it is mathematically defined and standard notation
00134 //
00135 //    Revision 1.6  2003/08/26 01:30:17  eswint
00136 //    - Modified MatrixRow and MatrixCol classes to preserve constness of parent matrix
00137 //    - Disallowed copy or assignment from const MatrixRow or const MatrixCol objects
00138 //
00139 //    Revision 1.5  2003/08/25 22:18:54  eswint
00140 //    - Changed signature of switchRows and switchCols to SwitchRows and SwitchCols
00141 //
00142 //    Revision 1.4  2003/08/25 21:07:42  eswint
00143 //    - Added basic math operators
00144 //    - Changed signature of sameSize(int,int) to SameSize(Matrix)
00145 //    - Added operator[] to Matrix and MatrixRow to allow built-in 2-D array syntax to be used
00146 //
00147 //    Revision 1.3  2003/08/19 02:27:20  eswint
00148 //    Modified the .at(int) to maintain const-ness for MatrixRow and MatrixCol.  Required const member function returning const reference to member, and non-const m.f. returning non-const reference.
00149 //
00150 //    Revision 1.2  2003/08/18 18:22:53  eswint
00151 //    Project files updated to include Matlab libs
00152 //
00153 //    Revision 1.1  2003/08/18 15:23:32  eswint
00154 //    Initial Commit
00155 //
00156 //        Checked in at 14:22:35 on 12 Oct 1995
00157 //
00159 #ifndef Matrix_hpp
00160 #define Matrix_hpp
00161 
00162 #include "MathDefs.h"
00163 #include "Base/Base.h"
00164 #include "Base/BaseError.h"
00165 #include "MathErrors.h"
00166 #include "Vector.h"
00167 
00169 namespace OSCAR {
00170 
00171   class MatrixCol;
00172   class MatrixRow;
00173 
00174   typedef double* DoubleP;
00175 
00184   class Matrix  : public Base
00185   {
00186 
00187   public:
00188 
00207     Matrix(unsigned int nr,unsigned int nc, const double* f=0, const String& name = String(),bool initMtx = true);
00208 
00223     Matrix(unsigned int nr,unsigned int nc,const Vector& v,
00224       OSCARError &err = DUMMY_ERROR(noError),
00225       const String& name = String());
00226 
00238     Matrix(unsigned int k, double* f, const String& name = String());
00239 
00246     Matrix(const MatrixRow& rhs);
00247 
00254     Matrix(const MatrixCol& rhs);
00255 
00262     Matrix(const Matrix& rhs);
00263 
00269     virtual ~Matrix();
00270 
00283     double& at(unsigned int irow,unsigned int icol);
00284 
00297     const double& at(unsigned int irow,unsigned int icol) const;
00298 
00299     /*
00300      * \brief Overloaded operator()
00301      *
00302      *  This overloaded operator() is used to access an element of an Matrix object. It just internally
00303      *  calls the .at method. This method is the similar to the .at() method except it performs range
00304      *  checking in all modes.  This method is slower than .at(), but it will never return garbage if a
00305      *  cell that is out of range is specified.  An Error is returned if the matrix does not contain the
00306      *  cell specified.
00307      * \param irow The row position - 0 is the first row.
00308      * \param icol The column position - 0 is the first column.
00309      * \return The Matrix element specified.
00310      * \exception outOfRange #outOfRange
00311      */
00312     double& operator()(unsigned int irow,unsigned int icol);
00313 
00314     /*
00315      * \brief Overloaded operator()
00316      *
00317      *  This overloaded operator() is used to access an element of an Matrix object. It just internally
00318      *  calls the .at method. This method is the similar to the .at() method except it performs range
00319      *  checking in all modes.  This method is slower than .at(), but it will never return garbage if a
00320      *  cell that is out of range is specified.  An Error is returned if the matrix does not contain the
00321      *  cell specified.
00322      * \param irow The row position - 0 is the first row.
00323      * \param icol The column position - 0 is the first column.
00324      * \return The Matrix element specified.
00325      * \exception outOfRange #outOfRange
00326      */
00327     const double& operator()(unsigned int irow, unsigned int icol) const;
00328 
00338      MatrixRow GetRow(unsigned int row);
00339 
00349      const MatrixRow GetRow(unsigned int row) const;
00350 
00360     MatrixRow operator[](unsigned int row);
00361 
00371     const MatrixRow operator[](unsigned int row) const;
00372 
00383     bool SetRow(unsigned int row,const MatrixRow& rowObject);
00384 
00394     MatrixCol GetCol(unsigned int col);
00395 
00405     const MatrixCol GetCol(unsigned int col) const;
00406 
00417     bool SetCol(unsigned int col,const MatrixCol& colObject);
00418 
00425     unsigned int nRow() const;
00426 
00433     unsigned int nCol() const;
00434 
00441     bool isSquare() const;
00442 
00450     bool SameSize(const Matrix& m) const;
00451 
00461     Matrix& operator=(const Matrix& rhs);
00462 
00471     Matrix& operator=(double rhs);
00472 
00473   
00483     bool Copy(const Matrix& matrixToCopy);
00484 
00493     bool operator==(const Matrix& rhs) const;
00494 
00502     Matrix& operator*=(double d);
00503 
00511     Matrix& operator/=(double d);
00512 
00520     Matrix& operator+=(const Matrix& rhs);
00521 
00529     Matrix& operator-=(const Matrix& rhs);
00530 
00538     Matrix& operator*=(const Matrix& m);
00539 
00549     Matrix operator+(const Matrix& rhs) const;
00550 
00560     Matrix operator-(const Matrix& rhs) const;
00561 
00569     Matrix operator-() const;
00570 
00578     Matrix operator*(double d) const;
00579 
00589     bool Multiply(const Matrix& rhs, Matrix& result) const;
00590 
00601     bool Multiply(const Vector& vec, Vector& result) const;
00602 
00610     Vector operator*(const Vector& rhs) const;
00611 
00622     Matrix operator*(const Matrix& rhs) const;
00623 
00631     Matrix operator/(double d) const;
00632 
00643     Matrix operator&(const Matrix& rhs) const;
00644 
00655     bool SwitchRows(unsigned int r1, unsigned int r2);
00656 
00667     bool SwitchCols(unsigned int c1, unsigned int c2);
00668 
00678     void CombineRows(unsigned int i, double b, unsigned int j);
00679 
00686     double Max() const;
00687 
00696     bool Identity();
00697 
00705     Matrix t() const;
00706 
00717     bool t(Matrix& rhs) const;
00718 
00729     bool MultiplyWithTranspose(Matrix& result) const;
00730 
00737     bool isUpperTriangle() const;
00738 
00747     Matrix UpperTriangle();
00748 
00762     bool LUDecomposition(int* indx, double* d);
00763 
00775     void LUFowBackSubstitution(const int *indx, double b[]) const;
00776 
00793     bool LUSolve(const Vector& b, Vector& x, double& determinant) const;
00794 
00806     bool CholeskyDecomposition();
00807 
00818     void CholeskyFowBackSubstitution(double b[], double x[]) const;
00819 
00834     bool CholeskySolve(const Vector& b, Vector& x, double& determinant) const;
00835 
00845     bool Determinant(double& det) const;
00846 
00863     virtual bool Inverse(Matrix& inverse) const;
00864 
00883     virtual bool Inverse(Matrix& inverse, double& determinant) const;
00884 
00900     virtual bool SpeedInverse(Matrix& inverse, double& determinant) const;
00901 
00910     Matrix    operator~() const;
00911 
00928     bool PseudoInvSVD(Matrix &inverse, double weight=.0001) const;
00929 
00968     bool SVD(Matrix& U, Vector& W, Matrix& V) const;
00969 
00995 
00996     //  The Non-matlab version of EigenValues is not working.
00998 
00999     bool EigenValues(Vector& W) const;
01000 
01016     double MatrixNorm(double P) const;
01017 
01025     String  ToFlatString() const;
01026 
01027 
01028   #ifdef MATLAB
01029 
01053     bool EigenVectors(Matrix& EIGVEC, Vector& DIAG) const;
01054 
01065     double CondNo(double P) const;
01066 
01075     double  Subspace(Matrix& A) const;
01076 
01077   #endif // ifdef MATLAB
01078 
01085     virtual void printOn(std::ostream& out) const;
01086 
01093     virtual void dumpOn(std::ostream& out) const;
01094 
01106     void sizeError(char* where,unsigned int a,unsigned int b) const;
01107 
01117     const double* GetArray() const;
01118 
01127     double* GetArray();
01128 
01129   protected:
01130 
01131     unsigned int  nrow, ncol;   //Number of rows, Number of columns in the Matrix
01132 
01133     DoubleP *_row;
01134 
01135     double  *_m;
01136     mutable Matrix  *scratchMatrix_NbyM;
01137     mutable VectorI *scratchIndxVectorI_M;
01138     mutable Vector  *scratchColVector_M;
01139 
01147     Matrix(const String& name = String());
01148 
01157     virtual void m_alloc(unsigned int rows, unsigned int cols);
01158   };
01159 
01160 
01170   class MatrixCol : public virtual Base
01171   {
01172   public:
01173 
01183     MatrixCol(unsigned int col, const Matrix& m, const String& name = "");
01184 
01185 
01193     MatrixCol(const MatrixCol& rhs);
01194 
01195 
01202     unsigned int nRow() const;
01203 
01204 
01213     double& at(unsigned int i);
01214 
01223     const double& at(unsigned int i) const;
01224 
01234     double& operator()(unsigned int i);
01235 
01245     const double& operator()(unsigned int i) const;
01246 
01256     Matrix Cross(const MatrixCol& b);
01257 
01258 
01268     MatrixCol& operator=(const MatrixCol& rhs);
01269 
01278     void operator+=(const MatrixCol& rhs);
01279 
01280 
01287     virtual void printOn(std::ostream& out) const;
01288 
01289   protected:
01290     const Matrix*   pm;
01291     unsigned int      _col;    // index of this column
01292   };
01293 
01294 
01295 
01296 
01297 
01307   class MatrixRow : public virtual Base
01308   {
01309   public:
01310 
01320     MatrixRow(unsigned int row, const Matrix& m, const String& name = "");
01321 
01322 
01330     MatrixRow(const MatrixRow& rhs);
01331 
01332 
01339     unsigned int nCol() const;
01340 
01341 
01350     double& at(unsigned int i);
01351 
01360     const double& at(unsigned int i) const;
01361 
01371     double& operator()(unsigned int i);
01372 
01382     const double& operator()(unsigned int i) const;
01383 
01394     double operator*(const MatrixCol& rhs) const;
01395 
01405     MatrixRow& operator=(const MatrixRow& rhs);
01406 
01416     double& operator[](unsigned int j);
01417 
01427     const double& operator[](unsigned int j) const;
01428 
01435     virtual void printOn(std::ostream& out) const;
01436 
01437   protected:
01438     const Matrix* pm;
01439     unsigned int _row;    // index of this row
01440   };
01441 
01451   extern std::ostream& operator<<(std::ostream& out, const Matrix& m);
01452 
01462   extern std::ostream& operator<<(std::ostream& out,const MatrixRow& m);
01463 
01473   extern std::ostream& operator<<(std::ostream& out,const MatrixCol& m);
01474 
01475   #include "Matrix.ipp"
01476 }
01477 #endif // ifdef Matrix_hpp
01478 
RRG Homepage OSCAR Overview OSCAR Tutorials Simulations