Matrix.h

Go to the documentation of this file.
00001 /* ****************************************************************** **
00002 **    OpenSees - Open System for Earthquake Engineering Simulation    **
00003 **          Pacific Earthquake Engineering Research Center            **
00004 **                                                                    **
00005 **                                                                    **
00006 ** (C) Copyright 1999, The Regents of the University of California    **
00007 ** All Rights Reserved.                                               **
00008 **                                                                    **
00009 ** Commercial use of this program without express permission of the   **
00010 ** University of California, Berkeley, is strictly prohibited.  See   **
00011 ** file 'COPYRIGHT'  in main directory for information on usage and   **
00012 ** redistribution,  and for a DISCLAIMER OF ALL WARRANTIES.           **
00013 **                                                                    **
00014 ** Developed by:                                                      **
00015 **   Frank McKenna (fmckenna@ce.berkeley.edu)                         **
00016 **   Gregory L. Fenves (fenves@ce.berkeley.edu)                       **
00017 **   Filip C. Filippou (filippou@ce.berkeley.edu)                     **
00018 **                                                                    **
00019 ** ****************************************************************** */
00020                                                                         
00021 // $Revision: 1.10 $
00022 // $Date: 2003/04/02 22:02:46 $
00023 // $Source: /usr/local/cvs/OpenSees/SRC/matrix/Matrix.h,v $
00024                                                                         
00025                                                                         
00026 #ifndef Matrix_h
00027 #define Matrix_h 
00028 
00029 // File: ~/matrix/Matrix.h
00030 //
00031 // Written: fmk 
00032 // Created: 11/96
00033 // Revision: A
00034 //
00035 // Description: This file contains the class definition for Matrix.
00036 // Matrix is a concrete class implementing the matrix abstraction.
00037 // Matrix class is used to provide the abstraction for the most
00038 // general type of matrix, that of an unsymmetric full matrix.
00039 //
00040 // What: "@(#) Matrix.h, revA"
00041 
00042 #include <OPS_Globals.h>
00043 
00044 class Vector;
00045 class ID;
00046 class Message;
00047 
00048 #include <Tensor.h> // cannot use class as Tensor is itself defined in Tensor.h
00049 
00050 
00051 #define MATRIX_VERY_LARGE_VALUE 1.0e213
00052 
00053 class Matrix
00054 {
00055   public:
00056     // constructors and destructor
00057     Matrix();   
00058     Matrix(int nrows, int ncols);
00059     Matrix(double *data, int nrows, int ncols);    
00060     Matrix(const Matrix &M);    
00061     ~Matrix();
00062 
00063     // utility methods
00064     int setData(double *newData, int nRows, int nCols);
00065     inline int noRows() const;
00066     inline int noCols() const;
00067     void Zero(void);
00068     int resize(int numRow, int numCol);
00069     
00070     int  Assemble(const Matrix &,const ID &rows, const ID &cols, 
00071                   double fact = 1.0);  
00072     
00073     int Solve(const Vector &V, Vector &res) const;
00074     int Solve(const Matrix &M, Matrix &res) const;
00075     int Invert(Matrix &res) const;
00076 
00077     int addMatrix(double factThis, const Matrix &other, double factOther);
00078     int addMatrixProduct(double factThis, const Matrix &A, const Matrix &B, double factOther); // AB
00079     int addMatrixTripleProduct(double factThis, const Matrix &A, const Matrix &B, double factOther); // A'BA
00080     //int addMatrixTripleProduct(const Matrix &A, const Matrix &B, const Matrix &C double fact = 1.0); //ABC
00081     
00082     // overloaded operators all of which are pure 
00083     inline double &operator()(int row, int col);
00084     inline double operator()(int row, int col) const;
00085     Matrix operator()(const ID &rows, const ID & cols) const;
00086     
00087     Matrix &operator=(const Matrix &M);
00088     Matrix &operator=(const Tensor &T);
00089     
00090     // matrix operations which will preserve the derived type and
00091     // which can be implemented efficiently without many constructor calls.
00092 
00093     // matrix-scalar operations
00094     Matrix &operator+=(double fact);
00095     Matrix &operator-=(double fact);
00096     Matrix &operator*=(double fact);
00097     Matrix &operator/=(double fact); 
00098 
00099     // matrix operations which generate a new Matrix. They are not the
00100     // most efficient to use, as constructors must be called twice. They
00101     // however are usefull for matlab like expressions involving Matrices.
00102 
00103     // matrix-scalar operations
00104     Matrix operator+(double fact) const;
00105     Matrix operator-(double fact) const;
00106     Matrix operator*(double fact) const;
00107     Matrix operator/(double fact) const;
00108     
00109     // matrix-vector operations
00110     Vector operator*(const Vector &V) const;
00111     Vector operator^(const Vector &V) const;    
00112 
00113     
00114     // matrix-matrix operations
00115     Matrix operator+(const Matrix &M) const;
00116     Matrix operator-(const Matrix &M) const;
00117     Matrix operator*(const Matrix &M) const;
00118 //     Matrix operator/(const Matrix &M) const;    
00119     Matrix operator^(const Matrix &M) const;
00120     Matrix &operator+=(const Matrix &M);
00121     Matrix &operator-=(const Matrix &M);
00122 
00123     // methods to read/write to/from the matrix
00124     void Output(OPS_Stream &s) const;
00125     //    void Input(istream &s);
00126     
00127     // methods added by Remo
00128     int  Assemble(const Matrix &V, int init_row, int init_col, double fact = 1.0);
00129     int  AssembleTranspose(const Matrix &V, int init_row, int init_col, double fact = 1.0);
00130     int  Extract (const Matrix &V, int init_row, int init_col, double fact = 1.0);
00131 
00132     friend OPS_Stream &operator<<(OPS_Stream &s, const Matrix &M);
00133     //    friend istream &operator>>(istream &s, Matrix &M);    
00134     friend Matrix operator*(double a, const Matrix &M);
00135     
00136     
00137     friend class Vector;    
00138     friend class Message;
00139     friend class TCP_Socket;
00140     friend class TCP_SocketNoDelay;
00141     friend class UDP_Socket;
00142     friend class MPI_Channel;
00143     friend class MySqlDatastore;
00144     friend class BerkeleyDbDatastore;
00145 
00146   protected:
00147 
00148   private:
00149     static double MATRIX_NOT_VALID_ENTRY;
00150     static double *matrixWork;
00151     static int *intWork;
00152     static int sizeDoubleWork;
00153     static int sizeIntWork;
00154 
00155     int numRows;
00156     int numCols;
00157     int dataSize;
00158     double *data;
00159     int fromFree;
00160 };
00161 
00162 
00163 /********* INLINED MATRIX FUNCTIONS ***********/
00164 inline int 
00165 Matrix::noRows() const 
00166 {
00167   return numRows;
00168 }
00169 
00170 inline int 
00171 Matrix::noCols() const 
00172 {
00173   return numCols;
00174 }
00175 
00176 
00177 inline double &
00178 Matrix::operator()(int row, int col)
00179 { 
00180 #ifdef _G3DEBUG
00181   if ((row < 0) || (row >= numRows)) {
00182     opserr << "Matrix::operator() - row " << row << " our of range [0, " <<  numRows-1 << endln;
00183     return data[0];
00184   } else if ((col < 0) || (col >= numCols)) {
00185     opserr << "Matrix::operator() - row " << col << " our of range [0, " <<  numCols-1 << endln;
00186     return MATRIX_NOT_VALID_ENTRY;
00187   }
00188 #endif
00189   return data[col*numRows + row];
00190 }
00191 
00192 
00193 inline double 
00194 Matrix::operator()(int row, int col) const
00195 { 
00196 #ifdef _G3DEBUG
00197   if ((row < 0) || (row >= numRows)) {
00198     opserr << "Matrix::operator() - row " << row << " our of range [0, " <<  numRows-1 << endln;
00199     return data[0];
00200   } else if ((col < 0) || (col >= numCols)) {
00201     opserr << "Matrix::operator() - row " << col << " our of range [0, " <<  numCols-1 << endln;
00202     return MATRIX_NOT_VALID_ENTRY;
00203   }
00204 #endif
00205   return data[col*numRows + row];
00206 }
00207 
00208 #endif
00209 
00210 
00211 
00212 

Generated on Mon Oct 23 15:05:23 2006 for OpenSees by doxygen 1.5.0