cgma
CubitMatrix.hpp
Go to the documentation of this file.
00001 //- Class: CubitMatrix
00002 //-
00003 //- Description: This file defines the CubitMatrix class which is a
00004 //- standard NxM Matrix. All relevant arithmetic operators are
00005 //- overloaded so CubitMatrix's can be used similar to built-in types.
00006 //-
00007 //- Owner: Dan Goodrich
00008 //- Checked by:
00009 
00010 
00011 #ifndef CUBITMATRIX_HPP
00012 #define CUBITMATRIX_HPP
00013 
00014 #include "CubitVector.hpp"
00015 #include <vector>
00016 #include <stdexcept>
00017 #include <string>
00018 #include "CGMUtilConfigure.h"
00019 
00020 class CUBIT_UTIL_EXPORT CubitMatrix
00021 {
00022 public:
00023   
00024     //- Heading: Constructors and Destructor
00025   CubitMatrix();
00026     //- Default constructor. 3x3 init to zeros.
00027   
00028   CubitMatrix( const int num_rows, const int num_cols);
00029     //- Initializes NxM matrix to zeros.
00030   
00031   CubitMatrix( const CubitVector& vec1, const CubitVector& vec2,
00032                const CubitVector& vec3);
00033     //- Constructor: create 3x3 Matrix from three vector components
00034     //  where each vector forms one column of the matrix.
00035   
00036   CubitMatrix( const CubitVector& vec1, const CubitVector& vec2,
00037                const CubitVector& vec3, const CubitVector& vec4 );
00038     //- Constructor: create 3x4 Matrix from four vector components
00039     //  where each vector forms one column of the matrix.
00040 
00041   CubitMatrix( const CubitVector& vec1, const CubitVector& vec2 );
00042     //- Constructor: create 3x3 Matrix from two vectors (otimes)
00043     //  ( vec1 otimes vec2 )_ij = vec1_i * vec2_j
00044   
00045   CubitMatrix( const int n );
00046     //- Constructor: create n x n Identity matrix
00047 
00048   CubitMatrix( std::vector<int> &is, std::vector<int> &js, std::vector<double> &es, int n, int m );
00049     // - uses vectors i, j, and s to generate an m-by-n
00050     // - matrix, S,  such that S(i(k),j(k)) = s(k).
00051     // - Vectors i, j, and s are all the same length.
00052     // - Any elements of s that have duplicate values of i and j are added
00053     // - together.
00054 
00055   CubitMatrix( const CubitMatrix& matrix);
00056     //- Copy Constructor
00057   
00058   virtual ~CubitMatrix();
00059     //- destructor
00060   
00061   void reset_size( const int n, const int m, double default_value = 0.0 );
00062     //- resize this matrix and set all values to a default value.
00063 
00064     //- Heading: Set and Inquire Functions
00065   int num_rows() const { return numRows; }
00066   int num_cols() const { return numCols; }
00067   
00068   void print_matrix() const;
00069   void print_matrix( char *filename ) const;
00070     //- Prints matrix
00071 
00072   void set_to_identity();
00073   
00074   bool is_identity( double tol = 1e-8 ) const;
00075   bool is_equal( const CubitMatrix &other, double tol = 1e-8 ) const;
00076 
00077    //- Add an identity matrix into this matrix.
00078   void plus_identity();
00079   
00080     //- Change Matrix component row n, column m to val.
00081   void set(const int n, const int m, const double val)
00082     {
00083       if(n < 0 || n >= numRows || m < 0 || m >= numCols)
00084         throw std::invalid_argument (std::string("Index out of bounds"));
00085       //assert (n >= 0 && n < numRows);
00086       //assert (m >= 0 && m < numCols);
00087       matrixPtr[n][m] = val;
00088     }
00089 
00090     //- add the value to the component row n col m
00091   void add(const int n, const int m, const double val)
00092     {
00093       matrixPtr[n][m] += val;
00094     }
00095   
00096     //- Gets the values of the matrix at position (n,m)
00097   double get( int n, int m ) const
00098     {
00099       if(n < 0 || n >= numRows || m < 0 || m >= numCols)
00100         throw std::invalid_argument ("Index out of Bounds");
00101       //assert (n >= 0 && n < numRows);
00102       //assert (m >= 0 && m < numCols);
00103       return matrixPtr[n][m];
00104     }
00105   
00106     //- Heading: Matrix Algebra functions 
00107     //  (assert if incompatible element sizes)
00108   CubitMatrix operator= (const CubitMatrix& matrix);
00109   CubitMatrix operator* (const CubitMatrix& matrix) const;
00110   CubitVector operator* (const CubitVector& vector) const;
00111   std::vector<double> operator* (const std::vector<double> & vector) const;
00112   CubitMatrix operator* (double val ) const;
00113   CubitMatrix operator/ (double val ) const;
00114   CubitMatrix operator+ (const CubitMatrix& matrix) const;
00115   CubitMatrix operator- (const CubitMatrix& matrix) const;
00116   
00117   CubitMatrix& operator+=(const CubitMatrix &matrix);
00118   CubitMatrix& operator*=(const double multiplier);
00119     //- Scale the matrix by a linear factor: {this = this * multiplier},
00120   
00121   CubitMatrix inverse(); // inverts 1x1, 2x2, or 3x3 matrix
00122     // asserts if singular
00123   CubitMatrix symm() const; // returns matrix^transpose * matrix
00124  
00125   CubitBoolean positive_definite() const;  // returns true if matrix is positive definite
00126   
00127   double determinant () const;
00128   double inf_norm () const; // infinity norm
00129   double frobenius_norm_squared() const; // square of frobenius norm of A
00130   double frobenius_norm_squared_symm() const; // square of frobenius norm of A^t A
00131   double frobenius_norm_squared_adj() const; // square of frobenius norm of adjoint A
00132   double frobenius_norm_squared_inv() const; // square of frobenius norm of A-inverse
00133   double condition() const;  // condition number of A using frobenius norm
00134   int rank( void ) const;
00135 
00136   double cofactor (const int row, const int col) const;
00137   CubitMatrix adjoint() const;
00138   CubitMatrix transpose() const;
00139 
00140      // row and col indicate the row and column to leave out of the
00141     // resulting matrix. 
00142   CubitMatrix sub_matrix( const int row, const int col ) const;
00143 
00144   // Create a matrix containing the rows and cols of this that are true in
00145   // rows_to_include and cols_to_include.
00146   void sub_matrix( const std::vector<bool> &rows_to_include,
00147                    const std::vector<bool> &cols_to_include,
00148                    CubitMatrix &submatrix );
00149   
00150     // routines to perform Gaussian elimination with pivoting and scaling
00151     // on 3x3 matrix
00152   int  gauss_elim (CubitVector &b);
00153   int  factor (CubitVector &pivot);
00154   void solve (CubitVector &b, const CubitVector& pivot);
00155 
00156     // routines to solve NxN system (from Numerical Recipes in C)
00157   CubitStatus solveNxN( CubitMatrix& rhs,  // must be NxM, where M is any
00158                                            // number of colums.
00159                         CubitMatrix& coef ); // must be NxM, same as rhs.
00160   CubitStatus solveNxN( const std::vector<double> &rhs,
00161                         std::vector<double> &coef );
00162   CubitStatus ludcmp( double *indx, double& d );
00163   CubitStatus lubksb( double *indx, double *b );
00164 
00165   // matrix must have been sized to be 3x3 before calling.
00166   bool vector_outer_product(const CubitVector& vec1,
00167                             const CubitVector& vec2 );
00168   
00169 private:
00170   double **matrixPtr;
00171   double *matrixMem;
00172   int numRows;
00173   int numCols;
00174 };
00175 
00176 #endif
00177 
00178 
00179 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines