Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

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.3 $
00022 // $Date: 2001/02/17 04:03:10 $
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 <iostream.h>
00043 #include <G3Globals.h>
00044 
00045 #include <Vector.h>
00046 #include <ID.h>
00047 #include <Message.h>
00048 #include <Tensor.h>
00049 
00050 #define MATRIX_VERY_LARGE_VALUE 1.0e213
00051 
00081 class Matrix
00082 {
00083   public:
00085     Matrix(); 
00090     Matrix(int nrows, int ncols);
00091 
00100     Matrix(double *data, int nrows, int ncols);    
00101 
00111     Matrix(const Matrix &M);    
00112 
00122     ~Matrix();
00123 
00124 
00126     inline int noRows() const;
00130     inline int noCols() const;
00131 
00138     void Zero(void);
00139 
00141     int resize(int numRow, int numCol);
00142 
00143     
00145     int  Assemble(const Matrix &,const ID &rows, const ID &cols, 
00146     double fact = 1.0);  
00147     
00149     int Solve(const Vector &V, Vector &res) const;
00151     int Solve(const Matrix &M, Matrix &res) const;
00152 
00154     int addMatrix(double factThis, const Matrix &other, double factOther);
00156     int addMatrixProduct(double factThis, const Matrix &A, const Matrix &B, double factOther); 
00157     int addMatrixTripleProduct(double factThis, const Matrix &A, const Matrix &B, double factOther); // A'BA
00158     //int addMatrixTripleProduct(const Matrix &A, const Matrix &B, const Matrix &C double fact = 1.0); //ABC
00159     
00161     inline double &operator()(int row, int col);
00163     inline double operator()(int row, int col) const;
00165     Matrix operator()(const ID &rows, const ID & cols) const;
00166     
00168     Matrix &operator=(const Matrix &M);
00170     Matrix &operator=(const Tensor &T);
00171     
00172     // matrix operations which will preserve the derived type and
00173     // which can be implemented efficiently without many constructor calls.
00174 
00176     Matrix &operator+=(double fact);
00178     Matrix &operator-=(double fact);
00180     Matrix &operator*=(double fact);
00182     Matrix &operator/=(double fact); 
00183 
00184     // matrix operations which generate a new Matrix. They are not the
00185     // most efficient to use, as constructors must be called twice. They
00186     // however are usefull for matlab like expressions involving Matrices.
00187 
00189     Matrix operator+(double fact) const;
00191     Matrix operator-(double fact) const;
00193     Matrix operator*(double fact) const;
00195     Matrix operator/(double fact) const;
00196     
00198     Vector operator*(const Vector &V) const;
00200     Vector operator^(const Vector &V) const;    
00201 
00202     
00204     Matrix operator+(const Matrix &M) const;
00206     Matrix operator-(const Matrix &M) const;
00208     Matrix operator*(const Matrix &M) const;
00210     Matrix operator^(const Matrix &M) const;
00212     Matrix &operator+=(const Matrix &M);
00214     Matrix &operator-=(const Matrix &M);
00215 
00217     void Output(ostream &s) const;
00219     void Input(istream &s);
00220     
00222     int  Assemble(const Matrix &V, int init_row, int init_col, double fact = 1.0);
00224     int  AssembleTranspose(const Matrix &V, int init_row, int init_col, double fact = 1.0);
00226     int  Extract (const Matrix &V, int init_row, int init_col, double fact = 1.0);
00227 
00229     friend ostream &operator<<(ostream &s, const Matrix &M);
00231     friend istream &operator>>(istream &s, Matrix &M);    
00233     friend Matrix operator*(double a, const Matrix &M);
00234     
00235     
00237     friend #include <Vector.h>    
00238     friend #include <Message.h>
00239     friend #include <TCP_Socket.h>
00240     friend #include <TCP_SocketNoDelay.h>
00241     friend #include <UDP_Socket.h>
00242     friend #include <MPI_Channel.h>
00243 
00244   protected:
00245 
00246   private:
00247     static double MATRIX_NOT_VALID_ENTRY;
00249     static double *matrixWork;
00251     static int *intWork;
00253     int numRows;
00255     int numCols;
00257     int dataSize;
00259     double *data;
00261     int fromFree;
00262 };
00263 
00264 
00265 /********** INLINED MATRIX FUNCTIONS ***********/
00266 inline int 
00267 Matrix::noRows() const 
00268 {
00269   return numRows;
00270 }
00271 
00273 inline int 
00274 Matrix::noCols() const 
00275 {
00276   return numCols;
00277 }
00278 
00279 
00281 inline double &
00282 Matrix::operator()(int row, int col)
00283 { 
00284 #ifdef _G3DEBUG
00285   if ((row < 0) || (row >= numRows)) {
00286     g3ErrorHandler->warning("Matrix::operator() - row %d our of range [0, %d]\n",
00287        row, numRows-1);
00288     return data[0];
00289   } else if ((col < 0) || (col >= numCols)) {
00290     g3ErrorHandler->warning("Matrix::operator() - col %d our of range [0, %d]\n",
00291        col, numCols-1);
00292     return MATRIX_NOT_VALID_ENTRY;
00293   }
00294 #endif
00295   return data[col*numRows + row];
00296 }
00297 
00298 
00300 inline double 
00301 Matrix::operator()(int row, int col) const
00302 { 
00303 #ifdef _G3DEBUG
00304   if ((row < 0) || (row >= numRows)) {
00305     g3ErrorHandler->warning("Matrix::operator(row, col) - row %d our of range [0, %d]\n",
00306        row, numRows-1);
00307     return data[0];
00308   } else if ((col < 0) || (col >= numCols)) {
00309     g3ErrorHandler->warning("Matrix::operator(row, col) - col %d our of range [0, %d]\n",
00310        col, numCols-1);
00311     return MATRIX_NOT_VALID_ENTRY;
00312   }
00313 #endif
00314   return data[col*numRows + row];
00315 }
00316 
00317 #endif
00318 
00319 
00320 
00321 
Copyright Contact Us