XC Open source finite element analysis program
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/nucleo/EntCmd.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 
81 //
82 class Matrix: public EntCmd
83  {
84  private:
85  static double MATRIX_NOT_VALID_ENTRY;
86  static AuxMatrix auxMatrix;
87 
88  int numRows;
89  int numCols;
90  Vector data;
91 
92  protected:
93 
94  public:
95  friend class Vector;
96  friend class Message;
97  friend class TCP_Socket;
98  friend class TCP_SocketNoDelay;
99  friend class UDP_Socket;
100  friend class MPI_Channel;
101 
102  // constructors and destructor
103  Matrix(void);
104  Matrix(int nrows, int ncols);
105  Matrix(double *data, int nrows, int ncols);
106  Matrix(const boost::python::list &l);
107  inline virtual ~Matrix(void) {}
108 
109  // utility methods
110  int setData(double *newData, int nRows, int nCols);
111  const double *getDataPtr(void) const;
112  double *getDataPtr(void);
113  bool Nula(void) const;
114  int getDataSize(void) const;
115  int getNumBytes(void) const;
116  int noRows() const;
117  int noCols() const;
118  void Zero(void);
119  void Identity(void);
120  int resize(int numRow, int numCol);
121  //void from_string(const std::string &str);
122 
123  int Assemble(const Matrix &,const ID &rows, const ID &cols, double fact = 1.0);
124 
125  int Solve(const Vector &V, Vector &res) const;
126  int Solve(const Matrix &M, Matrix &res) const;
127  int Invert(Matrix &res) const;
128 
129  double rowSum(int i) const;
130  double columnSum(int j) const;
131  double rowNorm(void) const;
132  double columnNorm(void) const;
133  double Norm2(void) const;
134  double Norm(void) const;
135 
136  int addMatrix(double factThis, const Matrix &other, double factOther);
137  int addMatrixProduct(double factThis, const Matrix &A, const Matrix &B, double factOther); // AB
138  int addMatrixTripleProduct(double factThis, const Matrix &A, const Matrix &B, double factOther); // A'BA
139  //int addMatrixTripleProduct(const Matrix &A, const Matrix &B, const Matrix &C double fact = 1.0); //ABC
140 
141  // overloaded operators all of which are pure
142  double &operator()(int row, int col);
143  const double &operator()(int row, int col) const;
144  Matrix operator()(const ID &rows, const ID & cols) const;
145  Vector getRow(int row) const;
146  Vector getCol(int col) const;
147 
148  template <class TNSR>
149  Matrix &operator=(const TNSR &);
150 
151  inline bool isRow(void) const
152  { return (numRows == 1); }
153  inline bool isColumn(void) const
154  { return (numCols == 1); }
155 
156  // matrix operations which will preserve the derived type and
157  // which can be implemented efficiently without many constructor calls.
158 
159  // matrix-scalar operations
160  Matrix &operator+=(double fact);
161  Matrix &operator-=(double fact);
162  Matrix &operator*=(double fact);
163  Matrix &operator/=(double fact);
164 
165  // matrix operations which generate a new Matrix. They are not the
166  // most efficient to use, as constructors must be called twice. They
167  // however are usefull for matlab like expressions involving Matrices.
168 
169  // matrix-scalar operations
170  Matrix operator+(double fact) const;
171  Matrix operator-(double fact) const;
172  Matrix operator*(double fact) const;
173  Matrix operator/(double fact) const;
174 
175  // matrix-vector operations
176  Vector operator*(const Vector &V) const;
177  Vector operator^(const Vector &V) const;
178 
179 
180  // matrix-matrix operations
181  Matrix operator+(const Matrix &M) const;
182  Matrix operator-(const Matrix &M) const;
183  Matrix operator*(const Matrix &M) const;
184 // Matrix operator/(const Matrix &M) const;
185  Matrix operator^(const Matrix &M) const;
186  Matrix &operator+=(const Matrix &M);
187  Matrix &operator-=(const Matrix &M);
188 
189  Matrix getTrn(void) const;
190 
191  // methods to read/write to/from the matrix
192  void Output(std::ostream &s) const;
193  void Input(const std::string &);
194  void write(std::ofstream &);
195  void read(std::ifstream &);
196  // void Input(istream &s);
197 
198  // methods added by Remo
199  int Assemble(const Matrix &V, int init_row, int init_col, double fact = 1.0);
200  int AssembleTranspose(const Matrix &V, int init_row, int init_col, double fact = 1.0);
201  int Extract(const Matrix &V, int init_row, int init_col, double fact = 1.0);
202 
203 
204  friend std::ostream &operator<<(std::ostream &s, const Matrix &M);
205  friend std::string to_string(const Matrix &);
206  inline std::string toString(void) const
207  { return to_string(*this); }
208  // friend istream &operator>>(istream &s, Matrix &M);
209  friend Matrix operator*(double a,const Matrix &M);
210 
211  };
212 
213 Matrix m_double_to_matrix(const m_double &m);
214 m_double matrix_to_m_double(const Matrix &m);
215 Matrix identity(const Matrix &m);
216 
217 
218 /********* INLINED MATRIX FUNCTIONS ***********/
219 inline bool Matrix::Nula(void) const
220  { return data.Nulo(); }
221 
222 inline int Matrix::getDataSize() const
223  { return data.Size(); }
224 
225 inline int Matrix::getNumBytes(void) const
226  { return data.getNumBytes(); }
227 
228 inline int Matrix::noRows() const
229  { return numRows; }
230 
231 inline int Matrix::noCols() const
232  { return numCols; }
233 
234 inline const double *Matrix::getDataPtr(void) const
235  { return data.getDataPtr(); }
236 
237 inline double *Matrix::getDataPtr(void)
238  { return data.getDataPtr(); }
239 
240 inline double &Matrix::operator()(int row, int col)
241  {
242 #ifdef _G3DEBUG
243  if((row < 0) || (row >= numRows))
244  {
245  std::cerr << "Matrix::operator() - row " << row << " our of range [0, " << numRows-1 << endln;
246  return data(0);
247  }
248  else
249  if((col < 0) || (col >= numCols))
250  {
251  std::cerr << "Matrix::operator() - row " << col << " our of range [0, " << numCols-1 << endln;
252  return MATRIX_NOT_VALID_ENTRY;
253  }
254 #endif
255  return data(col*numRows + row);
256  }
257 
258 
259 inline const double &Matrix::operator()(int row, int col) const
260  {
261 #ifdef _G3DEBUG
262  if((row < 0) || (row >= numRows))
263  {
264  std::cerr << "Matrix::operator() - row " << row
265  << " our of range [0, " << numRows-1 << endln;
266  return data(0);
267  }
268  else if((col < 0) || (col >= numCols))
269  {
270  std::cerr << "Matrix::operator() - row " << col
271  << " our of range [0, " << numCols-1 << endln;
272  return MATRIX_NOT_VALID_ENTRY;
273  }
274 #endif
275  return data(col*numRows + row);
276  }
277 
278 inline Matrix transposed(const Matrix &m)
279  { return m.getTrn(); }
280 
281 template <class TNSR>
282 XC::Matrix &XC::Matrix::operator=(const TNSR &V)
283  {
284  int rank= V.rank();
285  if(rank != 4)
286  {
287  std::cerr << "Matrix::operator=() - BJtensor must be of rank 4\n";
288  return *this;
289  }
290  int dim= V.dim(1);
291  if(dim != V.dim(2) != V.dim(3) != V.dim(4))
292  {
293  std::cerr << "Matrix::operator=() - BJtensor must have square dimensions\n";
294  return *this;
295  }
296 
297  if(dim != 2 || dim != 3 || dim != 1)
298  {
299  std::cerr << "Matrix::operator=() - BJtensor must be of dimension 2 or 3\n";
300  return *this;
301  }
302 
303  if(dim == 1)
304  {
305  if((numCols != 1) || (numRows != 1))
306  {
307  std::cerr << "Matrix::operator=() - matrix must be 1x1 for XC::BJtensor of dimension 3\n";
308  return *this;
309  }
310  (*this)(0,0)= V.cval(1,1,1,1);
311 
312  }
313  else if(dim == 2)
314  {
315  if((numCols != 3) || (numRows != 3))
316  {
317  std::cerr << "Matrix::operator=() - matrix must be 1x1 for XC::BJtensor of dimension 3\n";
318  return *this;
319  }
320  (*this)(0,0)= V.cval(1,1,1,1);
321  (*this)(0,1)= V.cval(1,1,2,2);
322  (*this)(0,2)= V.cval(1,1,1,2);
323 
324  (*this)(1,0)= V.cval(2,2,1,1);
325  (*this)(1,1)= V.cval(2,2,2,2);
326  (*this)(1,2)= V.cval(2,2,1,2);
327 
328  (*this)(2,0)= V.cval(1,2,1,1);
329  (*this)(2,1)= V.cval(1,2,2,2);
330  (*this)(2,2)= V.cval(1,2,1,2);
331 
332  }
333  else
334  {
335  if((numCols != 6) || (numRows != 6))
336  {
337  std::cerr << "Matrix::operator=() - matrix must be 1x1 for XC::BJtensor of dimension 3\n";
338  return *this;
339  }
340  (*this)(0,0)= V.cval(1,1,1,1);
341  (*this)(0,1)= V.cval(1,1,2,2);
342  (*this)(0,2)= V.cval(1,1,3,3);
343  (*this)(0,3)= V.cval(1,1,1,2);
344  (*this)(0,4)= V.cval(1,1,1,3);
345  (*this)(0,5)= V.cval(1,1,2,3);
346 
347  (*this)(1,0)= V.cval(2,2,1,1);
348  (*this)(1,1)= V.cval(2,2,2,2);
349  (*this)(1,2)= V.cval(2,2,3,3);
350  (*this)(1,3)= V.cval(2,2,1,2);
351  (*this)(1,4)= V.cval(2,2,1,3);
352  (*this)(1,5)= V.cval(2,2,2,3);
353 
354  (*this)(2,0)= V.cval(3,3,1,1);
355  (*this)(2,1)= V.cval(3,3,2,2);
356  (*this)(2,2)= V.cval(3,3,3,3);
357  (*this)(2,3)= V.cval(3,3,1,2);
358  (*this)(2,4)= V.cval(3,3,1,3);
359  (*this)(2,5)= V.cval(3,3,2,3);
360 
361  (*this)(3,0)= V.cval(1,2,1,1);
362  (*this)(3,1)= V.cval(1,2,2,2);
363  (*this)(3,2)= V.cval(1,2,3,3);
364  (*this)(3,3)= V.cval(1,2,1,2);
365  (*this)(3,4)= V.cval(1,2,1,3);
366  (*this)(3,5)= V.cval(1,2,2,3);
367 
368  (*this)(4,0)= V.cval(1,3,1,1);
369  (*this)(4,1)= V.cval(1,3,2,2);
370  (*this)(4,2)= V.cval(1,3,3,3);
371  (*this)(4,3)= V.cval(1,3,1,2);
372  (*this)(4,4)= V.cval(1,3,1,3);
373  (*this)(4,5)= V.cval(1,3,2,3);
374 
375  (*this)(5,0)= V.cval(2,3,1,1);
376  (*this)(5,1)= V.cval(2,3,2,2);
377  (*this)(5,2)= V.cval(2,3,3,3);
378  (*this)(5,3)= V.cval(2,3,1,2);
379  (*this)(5,4)= V.cval(2,3,1,3);
380  (*this)(5,5)= V.cval(2,3,2,3);
381  }
382  return *this;
383  }
384 } // end of XC namespace
385 
386 #endif
387 
388 
389 
390 
double rowSum(int i) const
Returns the sum of the i-th row.
Definition: Matrix.cpp:1278
double columnNorm(void) const
Returns the maximum value of the elements of the vector that results that contains the column sums...
Definition: Matrix.cpp:1331
void read(std::ifstream &)
Lee la matriz de un archivo binario.
Definition: Matrix.cpp:1094
Definition: Vector.h:82
void Input(const std::string &)
Lectura desde cadena de caracteres.
Definition: Matrix.cpp:1077
double columnSum(int j) const
Returns the sum of the j-th row.
Definition: Matrix.cpp:1287
MPI_Channel is a sub-class of channel. It is implemented with Berkeley stream sockets using the TCP p...
Definition: MPI_Channel.h:69
void write(std::ofstream &)
Escribe la matriz en un archivo binario.
Definition: Matrix.cpp:1086
double Norm(void) const
Returns the modulus (euclidean norm) of the matrix.
Definition: Matrix.cpp:1317
DP_Socket is a sub-class of channel. It is implemented with Berkeley datagram sockets using the UDP p...
Definition: UDP_Socket.h:76
TCP_Socket is a sub-class of channel. It is implemented with Berkeley stream sockets using the TCP pr...
Definition: TCP_Socket.h:71
Matrix(void)
Constructor.
Definition: Matrix.cpp:81
double rowNorm(void) const
Returns the maximum value of the elements of the vector that results that contains the row sums...
Definition: Matrix.cpp:1322
Vector getCol(int col) const
Return the columna which index being passed as parameter.
Definition: Matrix.cpp:711
Matrix getTrn(void) const
Return the transpuesta.
Definition: Matrix.cpp:1163
friend std::string to_string(const Matrix &)
Returns a string that represents the matrix.
Definition: Matrix.cpp:1102
Definition: ID.h:77
Matrix m_double_to_matrix(const m_double &m)
Convierte una matriz de tipo m_double otra de tipo Matrix.
Definition: Matrix.cpp:1053
double Norm2(void) const
Returns the squared modulus (euclidean norm) of the matrix.
Definition: Matrix.cpp:1296
Definition: AuxMatrix.h:42
Definition: Matrix.h:82
TCP_SocketNoDelay is a sub-class of channel. It is implemented with Berkeley stream sockets using the...
Definition: TCP_SocketNoDelay.h:72
m_double matrix_to_m_double(const Matrix &m)
Convierte a una matriz de tipo m_double.
Definition: Matrix.cpp:1065
Message between processes.
Definition: Message.h:67
void Output(std::ostream &s) const
Output method.
Definition: Matrix.cpp:1032
================================================================================
Definition: ContinuaReprComponent.h:34
Vector getRow(int row) const
Return the fila which index being passed as parameter.
Definition: Matrix.cpp:702