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