xc
Matrix.h
1 //----------------------------------------------------------------------------
2 // XC program; finite element analysis code
3 // for structural analysis and design.
4 //
5 // Copyright (C) Luis Claudio Pérez Tato
6 //
7 // This program derives from OpenSees <http://opensees.berkeley.edu>
8 // developed by the «Pacific earthquake engineering research center».
9 //
10 // Except for the restrictions that may arise from the copyright
11 // of the original program (see copyright_opensees.txt)
12 // XC is free software: you can redistribute it and/or modify
13 // it under the terms of the GNU General Public License as published by
14 // the Free Software Foundation, either version 3 of the License, or
15 // (at your option) any later version.
16 //
17 // This software is distributed in the hope that it will be useful, but
18 // WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 // GNU General Public License for more details.
21 //
22 //
23 // You should have received a copy of the GNU General Public License
24 // along with this program.
25 // If not, see <http://www.gnu.org/licenses/>.
26 //----------------------------------------------------------------------------
27 /* ****************************************************************** **
28 ** OpenSees - Open System for Earthquake Engineering Simulation **
29 ** Pacific Earthquake Engineering Research Center **
30 ** **
31 ** **
32 ** (C) Copyright 1999, The Regents of the University of California **
33 ** All Rights Reserved. **
34 ** **
35 ** Commercial use of this program without express permission of the **
36 ** University of California, Berkeley, is strictly prohibited. See **
37 ** file 'COPYRIGHT' in main directory for information on usage and **
38 ** redistribution, and for a DISCLAIMER OF ALL WARRANTIES. **
39 ** **
40 ** Developed by: **
41 ** Frank McKenna (fmckenna@ce.berkeley.edu) **
42 ** Gregory L. Fenves (fenves@ce.berkeley.edu) **
43 ** Filip C. Filippou (filippou@ce.berkeley.edu) **
44 ** **
45 ** ****************************************************************** */
46 
47 // $Revision: 1.10 $
48 // $Date: 2003/04/02 22:02:46 $
49 // $Source: /usr/local/cvs/OpenSees/SRC/utility/matrix/Matrix.h,v $
50 
51 
52 #ifndef Matrix_h
53 #define Matrix_h
54 
55 // File: ~/utility/matrix/Matrix.h
56 //
57 // Written: fmk
58 // Created: 11/96
59 // Revision: A
60 //
61 // Description: This file contains the class definition for Matrix.
62 // Matrix is a concrete class implementing the matrix abstraction.
63 // Matrix class is used to provide the abstraction for the most
64 // general type of matrix, that of an unsymmetric full matrix.
65 //
66 // What: "@(#) Matrix.h, revA"
67 
68 #include "xc_utils/src/kernel/CommandEntity.h"
69 #include "xc_basic/src/matrices/m_double.h"
70 #include "Vector.h"
71 
72 namespace XC {
73 class ID;
74 class Vector;
75 class Message;
76 class AuxMatrix;
77 
78 #define MATRIX_VERY_LARGE_VALUE 1.0e213
79 
108 class Matrix: public CommandEntity
109  {
110  private:
111  static double MATRIX_NOT_VALID_ENTRY;
112  static AuxMatrix auxMatrix;
113 
114  int numRows;
115  int numCols;
116  Vector data;
117 
118  public:
119  friend class Vector;
120  friend class Message;
121  friend class TCP_Socket;
122  friend class TCP_SocketNoDelay;
123  friend class UDP_Socket;
124  friend class MPI_Channel;
125 
126  // constructors and destructor
127  Matrix(void);
128  Matrix(int nrows, int ncols);
129  Matrix(double *data, int nrows, int ncols);
130  Matrix(const boost::python::list &l);
131  inline virtual ~Matrix(void) {}
132 
133  // utility methods
134  int setData(double *newData, int nRows, int nCols);
135  const double *getDataPtr(void) const;
136  double *getDataPtr(void);
137  bool isEmpty(void) const;
138  int getDataSize(void) const;
139  int getNumBytes(void) const;
140  int noRows() const;
141  int noCols() const;
142  void Zero(void);
143  void Identity(void);
144  int resize(int numRow, int numCol);
145  //void from_string(const std::string &str);
146 
147  int Assemble(const Matrix &,const ID &rows, const ID &cols, double fact = 1.0);
148 
149  int Solve(const Vector &V, Vector &res) const;
150  int Solve(const Matrix &M, Matrix &res) const;
151  int Invert(Matrix &res) const;
152  Matrix getInverse(void) const;
153 
154  double rowSum(int i) const;
155  double columnSum(int j) const;
156  double rowNorm(void) const;
157  double columnNorm(void) const;
158  double Norm2(void) const;
159  double Norm(void) const;
160  double OneNorm(void) const;
161  double RCond(void) const;
162 
163  int addMatrix(double factThis, const Matrix &other, double factOther);
164  int addMatrixProduct(double factThis, const Matrix &A, const Matrix &B, double factOther); // AB
165  int addMatrixTripleProduct(double factThis, const Matrix &A, const Matrix &B, double factOther); // A'BA
166  //int addMatrixTripleProduct(const Matrix &A, const Matrix &B, const Matrix &C double fact = 1.0); //ABC
167 
168  // overloaded operators all of which are pure
169  double &operator()(int row, int col);
170  const double &operator()(int row, int col) const;
171  Matrix operator()(const ID &rows, const ID & cols) const;
172  Vector getRow(int row) const;
173  Vector getCol(int col) const;
174 
175  template <class TNSR>
176  Matrix &operator=(const TNSR &);
177 
178  inline bool isRow(void) const
179  { return (numRows == 1); }
180  inline bool isColumn(void) const
181  { return (numCols == 1); }
182 
183  // matrix operations which will preserve the derived type and
184  // which can be implemented efficiently without many constructor calls.
185 
186  // matrix-scalar operations
187  Matrix &operator+=(double fact);
188  Matrix &operator-=(double fact);
189  Matrix &operator*=(double fact);
190  Matrix &operator/=(double fact);
191 
192  // matrix operations which generate a new Matrix. They are not the
193  // most efficient to use, as constructors must be called twice. They
194  // however are usefull for matlab like expressions involving Matrices.
195 
196  // matrix-scalar operations
197  Matrix operator+(double fact) const;
198  Matrix operator-(double fact) const;
199  Matrix operator*(double fact) const;
200  Matrix operator/(double fact) const;
201 
202  // matrix-vector operations
203  Vector operator*(const Vector &V) const;
204  Vector operator^(const Vector &V) const;
205 
206 
207  Matrix operator-(void) const;
208  // matrix-matrix operations
209  Matrix operator+(const Matrix &M) const;
210  Matrix operator-(const Matrix &M) const;
211  Matrix operator*(const Matrix &M) const;
212 // Matrix operator/(const Matrix &M) const;
213  Matrix operator^(const Matrix &M) const;
214  Matrix &operator+=(const Matrix &M);
215  Matrix &operator-=(const Matrix &M);
216 
217  Matrix getTrn(void) const;
218 
219  // methods to read/write to/from the matrix
220  void Output(std::ostream &s) const;
221  void Input(const std::string &);
222  void write(std::ofstream &);
223  void read(std::ifstream &);
224  // void Input(istream &s);
225 
226  // methods added by Remo
227  int Assemble(const Matrix &V, int init_row, int init_col, double fact = 1.0);
228  int AssembleTranspose(const Matrix &V, int init_row, int init_col, double fact = 1.0);
229  int Extract(const Matrix &V, int init_row, int init_col, double fact = 1.0);
230 
231 
232  friend std::ostream &operator<<(std::ostream &, const Matrix &);
233  friend std::string to_string(const Matrix &);
234  inline std::string toString(void) const
235  { return to_string(*this); }
236  friend Matrix operator*(double ,const Matrix &);
237 
238  };
239 
240 Matrix operator*(double ,const Matrix &);
241 std::ostream &operator<<(std::ostream &, const Matrix &);
242 
243 Matrix m_double_to_matrix(const m_double &m);
244 m_double matrix_to_m_double(const Matrix &m);
245 Matrix identity(const Matrix &m);
246 
247 
248 /********* INLINED MATRIX FUNCTIONS ***********/
249 inline bool Matrix::isEmpty(void) const
250  { return data.isEmpty(); }
251 
252 inline int Matrix::getDataSize() const
253  { return data.Size(); }
254 
255 inline int Matrix::getNumBytes(void) const
256  { return data.getNumBytes(); }
257 
259 inline int Matrix::noRows(void) const
260  { return numRows; }
261 
263 inline int Matrix::noCols(void) const
264  { return numCols; }
265 
266 inline const double *Matrix::getDataPtr(void) const
267  { return data.getDataPtr(); }
268 
269 inline double *Matrix::getDataPtr(void)
270  { return data.getDataPtr(); }
271 
282 inline double &Matrix::operator()(int row, int col)
283  {
284 #ifdef _G3DEBUG
285  if((row < 0) || (row >= numRows))
286  {
287  std::cerr << getClassName() << "::" << __FUNCTION__
288  << "; row " << row << " out of range [0, "
289  << numRows-1 << std::endl;
290  return data(0);
291  }
292  else if((col < 0) || (col >= numCols))
293  {
294  std::cerr << getClassName() << "::" << __FUNCTION__
295  << "; row " << col << " out of range [0, "
296  << numCols-1 << std::endl;
297  return MATRIX_NOT_VALID_ENTRY;
298  }
299 #endif
300  return data(col*numRows + row);
301  }
302 
303 
314 inline const double &Matrix::operator()(int row, int col) const
315  {
316 #ifdef _G3DEBUG
317  if((row < 0) || (row >= numRows))
318  {
319  std::cerr << getClassName() << "::" << __FUNCTION__
320  << "; row " << row
321  << " out of range [0, " << numRows-1 << std::endl;
322  return data(0);
323  }
324  else if((col < 0) || (col >= numCols))
325  {
326  std::cerr << getClassName() << "::" << __FUNCTION__
327  << "; row " << col
328  << " out of range [0, " << numCols-1 << std::endl;
329  return MATRIX_NOT_VALID_ENTRY;
330  }
331 #endif
332  return data(col*numRows + row);
333  }
334 
336 inline Matrix transposed(const Matrix &m)
337  { return m.getTrn(); }
338 
340 template <class TNSR>
341 XC::Matrix &XC::Matrix::operator=(const TNSR &V)
342  {
343  int rank= V.rank();
344  if(rank != 4)
345  {
346  std::cerr << getClassName() << "::" << __FUNCTION__
347  << "; BJtensor must be of rank 4.\n";
348  return *this;
349  }
350  int dim= V.dim(1);
351  if(dim != V.dim(2) != V.dim(3) != V.dim(4))
352  {
353  std::cerr << getClassName() << "::" << __FUNCTION__
354  << "; BJtensor must have square dimensions.\n";
355  return *this;
356  }
357 
358  if(dim != 2 || dim != 3 || dim != 1)
359  {
360  std::cerr << getClassName() << "::" << __FUNCTION__
361  << "; BJtensor must be of dimension 2 or 3.\n";
362  return *this;
363  }
364 
365  if(dim == 1)
366  {
367  if((numCols != 1) || (numRows != 1))
368  {
369  std::cerr << getClassName() << "::" << __FUNCTION__
370  << "; matrix must be 1x1 for BJtensor of dimension 3.\n";
371  return *this;
372  }
373  (*this)(0,0)= V.cval(1,1,1,1);
374 
375  }
376  else if(dim == 2)
377  {
378  if((numCols != 3) || (numRows != 3))
379  {
380  std::cerr << getClassName() << "::" << __FUNCTION__
381  << "; matrix must be 1x1 for BJtensor of dimension 3.\n";
382  return *this;
383  }
384  (*this)(0,0)= V.cval(1,1,1,1);
385  (*this)(0,1)= V.cval(1,1,2,2);
386  (*this)(0,2)= V.cval(1,1,1,2);
387 
388  (*this)(1,0)= V.cval(2,2,1,1);
389  (*this)(1,1)= V.cval(2,2,2,2);
390  (*this)(1,2)= V.cval(2,2,1,2);
391 
392  (*this)(2,0)= V.cval(1,2,1,1);
393  (*this)(2,1)= V.cval(1,2,2,2);
394  (*this)(2,2)= V.cval(1,2,1,2);
395 
396  }
397  else
398  {
399  if((numCols != 6) || (numRows != 6))
400  {
401  std::cerr << getClassName() << "::" << __FUNCTION__
402  << "; matrix must be 1x1 for BJtensor of dimension 3.\n";
403  return *this;
404  }
405  (*this)(0,0)= V.cval(1,1,1,1);
406  (*this)(0,1)= V.cval(1,1,2,2);
407  (*this)(0,2)= V.cval(1,1,3,3);
408  (*this)(0,3)= V.cval(1,1,1,2);
409  (*this)(0,4)= V.cval(1,1,1,3);
410  (*this)(0,5)= V.cval(1,1,2,3);
411 
412  (*this)(1,0)= V.cval(2,2,1,1);
413  (*this)(1,1)= V.cval(2,2,2,2);
414  (*this)(1,2)= V.cval(2,2,3,3);
415  (*this)(1,3)= V.cval(2,2,1,2);
416  (*this)(1,4)= V.cval(2,2,1,3);
417  (*this)(1,5)= V.cval(2,2,2,3);
418 
419  (*this)(2,0)= V.cval(3,3,1,1);
420  (*this)(2,1)= V.cval(3,3,2,2);
421  (*this)(2,2)= V.cval(3,3,3,3);
422  (*this)(2,3)= V.cval(3,3,1,2);
423  (*this)(2,4)= V.cval(3,3,1,3);
424  (*this)(2,5)= V.cval(3,3,2,3);
425 
426  (*this)(3,0)= V.cval(1,2,1,1);
427  (*this)(3,1)= V.cval(1,2,2,2);
428  (*this)(3,2)= V.cval(1,2,3,3);
429  (*this)(3,3)= V.cval(1,2,1,2);
430  (*this)(3,4)= V.cval(1,2,1,3);
431  (*this)(3,5)= V.cval(1,2,2,3);
432 
433  (*this)(4,0)= V.cval(1,3,1,1);
434  (*this)(4,1)= V.cval(1,3,2,2);
435  (*this)(4,2)= V.cval(1,3,3,3);
436  (*this)(4,3)= V.cval(1,3,1,2);
437  (*this)(4,4)= V.cval(1,3,1,3);
438  (*this)(4,5)= V.cval(1,3,2,3);
439 
440  (*this)(5,0)= V.cval(2,3,1,1);
441  (*this)(5,1)= V.cval(2,3,2,2);
442  (*this)(5,2)= V.cval(2,3,3,3);
443  (*this)(5,3)= V.cval(2,3,1,2);
444  (*this)(5,4)= V.cval(2,3,1,3);
445  (*this)(5,5)= V.cval(2,3,2,3);
446  }
447  return *this;
448  }
449 } // end of XC namespace
450 
451 #endif
452 
453 
454 
455 
double Norm2(void) const
Returns the squared modulus (euclidean norm) of the matrix.
Definition: Matrix.cpp:1530
Matrix transposed(const Matrix &m)
Return the transposed of the parameter.
Definition: Matrix.h:336
friend std::string to_string(const Matrix &)
Returns a string that represents the matrix.
Definition: Matrix.cpp:1336
Float vector abstraction.
Definition: Vector.h:93
double columnSum(int j) const
Returns the sum of the j-th row.
Definition: Matrix.cpp:1521
double OneNorm(void) const
Returns the value of the one norm.
Definition: Matrix.cpp:1574
void Input(const std::string &)
Read from string.
Definition: Matrix.cpp:1311
Vector getCol(int col) const
Return the column which index being passed as parameter.
Definition: Matrix.cpp:800
double rowNorm(void) const
Returns the maximum value of the elements of the vector that results that contains the row sums...
Definition: Matrix.cpp:1557
double & operator()(int row, int col)
Returns a reference to the data at location({row,col}).
Definition: Matrix.h:282
double Norm(void) const
Returns the modulus (euclidean norm) of the matrix.
Definition: Matrix.cpp:1552
void Zero(void)
Zero&#39;s out the Matrix.
Definition: Matrix.cpp:182
Matrix operator-(void) const
Unary minus operator.
Definition: Matrix.cpp:1084
int noCols() const
Returns the number of columns, numCols, of the Matrix.
Definition: Matrix.h:263
double columnNorm(void) const
Returns the maximum value of the elements of the vector that results that contains the column sums...
Definition: Matrix.cpp:1566
Matrix(void)
Default constructor.
Definition: Matrix.cpp:81
Vector of integers.
Definition: ID.h:93
m_double matrix_to_m_double(const Matrix &m)
Converts a Matrix into an m_double.
Definition: Matrix.cpp:1299
Matrix & operator-=(double fact)
A method to subtract fact from each component of the current Matrix.
Definition: Matrix.cpp:854
TCP_Socket is a sub-class of channel.
Definition: TCP_Socket.h:71
Vector operator^(const Vector &V) const
A method to return a new Vector, of size numCols, whose components are equal to the product of the tr...
Definition: Matrix.cpp:1064
Matrix operator*(double fact) const
A method to return a new Matrix, whose components are equal to the components of the current Matrix t...
Definition: Matrix.cpp:970
Definition: AuxMatrix.h:42
double RCond(void) const
Return an estimation of the reciprocal of the condition number.
Definition: Matrix.cpp:1616
int Size(void) const
Returns the size of the Vector.
Definition: Vector.h:228
TCP_SocketNoDelay is a sub-class of channel.
Definition: TCP_SocketNoDelay.h:72
int Assemble(const Matrix &, const ID &rows, const ID &cols, double fact=1.0)
Assembles the argument into the current Matrix.
Definition: Matrix.cpp:237
int getNumBytes(void) const
Number of bytes occupied by the vector.
Definition: Vector.h:232
int Invert(Matrix &res) const
Return the inverse matrix in the argument.
Definition: Matrix.cpp:442
bool isEmpty(void) const
Return true if the vector has no data.
Definition: Vector.h:251
Matrix operator/(double fact) const
A method to return a new Matrix whose components are equal to the components of the current Matrix di...
Definition: Matrix.cpp:986
int Solve(const Vector &V, Vector &res) const
Solve the equation {} for the Vector x, which is returned.
Definition: Matrix.cpp:298
void write(std::ofstream &)
Write to a binary file.
Definition: Matrix.cpp:1320
MPI_Channel is a sub-class of channel.
Definition: MPI_Channel.h:69
int noRows() const
Returns the number of rows, numRows, of the Matrix.
Definition: Matrix.h:259
int addMatrix(double factThis, const Matrix &other, double factOther)
Add a factor fact times the Matrix other to the current Matrix.
Definition: Matrix.cpp:512
Matrix & operator*=(double fact)
A method to multiply each component of the current Matrix by fact.
Definition: Matrix.cpp:874
void Output(std::ostream &s) const
Write to the argument stream.
Definition: Matrix.cpp:1266
Message between processes.
Definition: Message.h:76
Vector getRow(int row) const
Return the row which index being passed as parameter.
Definition: Matrix.cpp:791
Matrix & operator/=(double fact)
A method which will divide each component of the current Matrix by fact.
Definition: Matrix.cpp:896
Matrix getTrn(void) const
Return the transpuesta.
Definition: Matrix.cpp:1397
Open source finite element program for structural analysis.
Definition: ContinuaReprComponent.h:34
void read(std::ifstream &)
Read from a bynary file.
Definition: Matrix.cpp:1328
const double * getDataPtr(void) const
Return a pointer to the float date.
Definition: Vector.h:236
Matrix of floats.
Definition: Matrix.h:108
Matrix m_double_to_matrix(const m_double &m)
Converts a matrix of type m_double into other of type Matrix.
Definition: Matrix.cpp:1287
double rowSum(int i) const
Returns the sum of the i-th row.
Definition: Matrix.cpp:1512
Matrix operator+(double fact) const
A method to return a new Matrix, whose components are equal to the components of the current Matrix p...
Definition: Matrix.cpp:938
Matrix & operator+=(double fact)
A method to add fact to each component of the current Matrix.
Definition: Matrix.cpp:834
DP_Socket is a sub-class of channel.
Definition: UDP_Socket.h:76