xc
Vector.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.9 $
49 // $Date: 2005/12/14 23:49:48 $
50 // $Source: /usr/local/cvs/OpenSees/SRC/utility/matrix/Vector.h,v $
51 
52 
53 // File: ~/utility/matrix/Vector.h
54 //
55 // Written: fmk
56 // Created: 11/96
57 // Revision: A
58 //
59 // Description: This file contains the class definition for Vector.
60 // Vector is a concrete class implementing the vector abstraction.
61 //
62 // What: "@(#) Vector.h, revA"
63 
64 #ifndef Vector_h
65 #define Vector_h
66 
67 #include "utility/kernel/CommandEntity.h"
68 #include "utility/matrices/m_double.h"
69 #include <cmath>
70 
71 class Vector2d;
72 class Vector3d;
73 
74 namespace XC {
75 class ID;
76 
77 class Matrix;
78 class Message;
79 class SystemOfEqn;
80 
94 class Vector: public CommandEntity
95  {
96  private:
97  static double VECTOR_NOT_VALID_ENTRY;
98  int sz;
99  double *theData;
100  int fromFree;
101  void free_mem(void);
102  void alloc(const size_t &sz);
103  public:
104  typedef double* iterator;
105 
106  // constructors and destructor
107  Vector(void);
108  explicit Vector(const int &, const double &value= 0.0);
109  explicit Vector(const std::vector<double> &v);
110  explicit Vector(const Vector2d &v);
111  explicit Vector(const Vector3d &v);
112  Vector(const double &,const double &,const double &);
113  Vector(const Vector &);
114  Vector(double *data, int size);
115  explicit Vector(const boost::python::list &);
116  virtual ~Vector(void);
117 
118  iterator begin(void);
119  iterator end(void);
120  // utility methods
121  int setData(double *newData, int size);
122  const double *getDataPtr(void) const;
123  double *getDataPtr(void);
124  bool isEmpty(void) const;
125  int Assemble(const Vector &V, const ID &l, double fact = 1.0);
126  double Norm2(void) const;
127  double Norm(void) const;
128  double pNorm(int p) const;
129  double NormInf(void) const;
130  int Size(void) const;
131  int getNumBytes(void) const;
132  int resize(int newSize);
133  void Zero(void);
134  bool isnan(void) const;
135  int reset(const int &newSize);
136  int Normalize(void);
137  int NormalizeInf(void);
138  Vector Normalized(void) const;
139  Vector NormalizedInf(void) const;
140 
141  int addVector(double factThis, const Vector &other, double factOther);
142  int addMatrixVector(double factThis, const Matrix &m, const Vector &v, double factOther);
143  int addMatrixTransposeVector(double factThis, const Matrix &m, const Vector &v, double factOther);
144 
145 
147  virtual double &at(const size_t &f);
149  virtual const double &at(const size_t &f) const;
151  virtual bool CheckIndice0(const size_t &i) const;
152 
153  // overloaded operators
154  const double &operator()(int x) const;
155  double &operator()(int x);
156  const double &operator[](int x) const; // these two operator do bounds checks
157  double &operator[](int x);
158  Vector operator()(const ID &rows) const;
159  Vector &operator=(const Vector &V);
160 
161  template <class TNSR>
162  Vector &operator=(const TNSR &T);
163 
164  Vector &operator+=(double fact);
165  Vector &operator-=(double fact);
166  Vector &operator*=(double fact);
167  Vector &operator/=(double fact);
168 
169  Vector operator-(void) const;
170  Vector operator+(double fact) const;
171  Vector operator-(double fact) const;
172  Vector operator*(double fact) const;
173  Vector operator/(double fact) const;
174 
175  Vector &operator+=(const Vector &V);
176  Vector &operator-=(const Vector &V);
177 
178  Vector operator+(const Vector &V) const;
179  Vector operator-(const Vector &V) const;
180  double operator^(const Vector &V) const;
181  Vector operator/(const Matrix &M) const;
182 
183  double dot(const Vector &) const;
184 
185  // methods added by Remo
186  int Assemble(const Vector &V, int init_row, double fact = 1.0);
187  int Extract(const Vector &V, int init_row, double fact = 1.0);
188 
189  Vector getComponents(const ID &) const;
190  void putComponents(const Vector &,const ID &);
191  void addComponents(const Vector &,const ID &);
192 
193  void write(std::ofstream &);
194  void read(std::ifstream &);
195  friend std::ostream &operator<<(std::ostream &, const Vector &);
196  friend std::string to_string(const Vector &);
197  inline std::string toString(void) const
198  { return to_string(*this); }
199 
200  boost::python::list getPyList(void) const;
201  void setPyList(const boost::python::list &);
202  boost::python::dict getPyDict(void) const;
203  void setPyDict(const boost::python::dict &);
204 
205  // friend istream &operator>>(istream &s, Vector &V);
206  friend Vector operator*(double , const Vector &);
207 
208  friend class Message;
209  friend class SystemOfEqn;
210  friend class Matrix;
211  friend class TCP_SocketNoDelay;
212  friend class TCP_Socket;
213  friend class UDP_Socket;
214  friend class MPI_Channel;
215  };
216 
217 Vector operator*(double , const Vector &);
218 
219 std::vector<double> vector_to_std_vector(const Vector &);
221 
222 double dot(const Vector &a,const Vector &b);
223 Matrix prod_tensor(const Vector &,const Vector &);
224 Matrix operator&(const Vector &u,const Vector &v);
225 
226 Vector normalize(const Vector &);
227 Vector normalize_inf(const Vector &);
228 
229 
230 std::ostream &operator<<(std::ostream &, const Vector &);
231 std::string to_string(const Vector &);
232 
233 /********* INLINED VECTOR FUNCTIONS ***********/
235 inline int Vector::Size(void) const
236  { return sz; }
237 
239 inline int Vector::getNumBytes(void) const
240  { return Size()*sizeof(double); }
241 
243 inline const double *Vector::getDataPtr(void) const
244  { return theData; }
245 
247 inline double *Vector::getDataPtr(void)
248  { return theData; }
249 
251 inline Vector::iterator Vector::begin(void)
252  { return theData; }
254 inline Vector::iterator Vector::end(void)
255  { return theData + sz; }
256 
258 inline bool Vector::isEmpty(void) const
259  { return (theData== nullptr); }
260 
263 inline void Vector::Zero(void)
264  {
265  for(int i=0; i<sz; i++)
266  theData[i] = 0.0;
267  }
268 
270 inline bool Vector::isnan(void) const
271  {
272  bool retval= false;
273  for(int i=0; i<sz; i++)
274  if(std::isnan(theData[i]))
275  {
276  retval= true;
277  break;
278  }
279  return retval;
280  }
281 
283 inline int Vector::reset(const int &newSize)
284  {
285  const int retval= resize(newSize);
286  Zero();
287  return retval;
288  }
289 
294 inline const double &Vector::operator()(int x) const
295  {
296 #ifdef _G3DEBUG
297  // check if it is inside range [0,sz-1]
298  if(x < 0 || x >= sz)
299  {
300  std::cerr << getClassName() << "::" << __FUNCTION__
301  << "; loc " << x << " outside range [0, "
302  << sz-1 << std::endl;
303  return VECTOR_NOT_VALID_ENTRY;
304  }
305 #endif
306  return theData[x];
307  }
308 
309 
314 inline double &Vector::operator()(int x)
315  {
316 #ifdef _G3DEBUG
317  // check if it is inside range [0,sz-1]
318  if (x < 0 || x >= sz)
319  {
320  std::cerr << getClassName() << "::" << __FUNCTION__
321  << "; loc " << x << " outside range [0, " << sz-1
322  << std::endl;
323  return VECTOR_NOT_VALID_ENTRY;
324  }
325 #endif
326  return theData[x];
327  }
328 
329 template <class TNSR>
330 Vector & Vector::operator=(const TNSR &V)
331  {
332  int rank= V.rank();
333  if(rank != 2)
334  {
335  std::cerr << "XC::Vector::operator=() - BJtensor must be of rank 2\n";
336  return *this;
337  }
338  int dim= V.dim(1);
339  if(dim != V.dim(2))
340  {
341  std::cerr << "XC::Vector::operator=() - BJtensor must have square dimensions\n";
342  return *this;
343  }
344 
345  if(dim != 2 || dim != 3 || dim != 1)
346  {
347  std::cerr << "XC::Vector::operator=() - BJtensor must be of dimension 2 or 3\n";
348  return *this;
349  }
350 
351  if(dim == 1)
352  {
353  if(sz != 1)
354  {
355  std::cerr << "Vector::operator=() - Vector size must be 1\n";
356  return *this;
357  }
358  theData[0]= V(1,1);
359  }
360  else if(dim == 2)
361  {
362  if(sz != 3)
363  {
364  std::cerr << "Vector::operator=() - Vector size must be 3\n";
365  return *this;
366  }
367  theData[0]= V(1,1);
368  theData[1]= V(2,2);
369  theData[2]= V(1,2);
370  }
371  else
372  {
373  if(sz != 6)
374  {
375  std::cerr << "Vector::operator=() - Vector size must be 6\n";
376  return *this;
377  }
378  theData[0]= V(1,1);
379  theData[1]= V(2,2);
380  theData[2]= V(3,3);
381  theData[3]= V(1,2);
382  theData[4]= V(1,3);
383  theData[5]= V(2,3);
384  }
385  return *this;
386  }
387 
388 } // end of XC namespace
389 
390 
391 #endif
392 
void setPyDict(const boost::python::dict &)
Set the values of the object members from a Python dictionary.
Definition: Vector.cpp:1281
Vector normalize(const Vector &)
Returns the normalized vector (euclidean norm).
Definition: Vector.cpp:357
Float vector abstraction.
Definition: Vector.h:94
virtual double & at(const size_t &f)
Returns the element at the row being passed as parameter.
Definition: Vector.cpp:819
Vector operator*(double fact) const
The + operator returns a vector of the same size as current, whose components are: return(i)= theData...
Definition: Vector.cpp:1038
m_double vector_to_m_double(const Vector &)
Convierte el vector en un m_double.
Definition: Vector.cpp:1427
void write(std::ofstream &)
Write vector on a binary file.
Definition: Vector.cpp:1241
Vector & operator/=(double fact)
The /= operator divides each element of the vector by fact, theData[i]= theData[i]/fact.
Definition: Vector.cpp:993
double Norm(void) const
Return the norm of vector.
Definition: Vector.cpp:780
void read(std::ifstream &)
Read vector from a binary file.
Definition: Vector.cpp:1289
int resize(int newSize)
Changes vector size.
Definition: Vector.cpp:237
int setData(double *newData, int size)
Help to construct a Vector of order size whose data will be stored in the array pointed to by data...
Definition: Vector.cpp:218
Vector operator+(double fact) const
The + operator returns a Vector of the same size as current, whose components are: return(i)= theData...
Definition: Vector.cpp:1010
Vector & operator*=(double fact)
The *= operator multiplies each element by the factor.
Definition: Vector.cpp:980
Vector of integers.
Definition: ID.h:95
void Zero(void)
Zeros out the Vector, i.e.
Definition: Vector.h:263
double operator^(const Vector &V) const
Method to perform (Vector)transposed * vector.
Definition: Vector.cpp:1191
Vector operator/(double fact) const
The + operator returns a vector of the same size as current, whose components are return(i)= theData[...
Definition: Vector.cpp:1055
TCP_Socket is a sub-class of channel.
Definition: TCP_Socket.h:71
const double & operator()(int x) const
Returns the data at location x in the Vector.
Definition: Vector.h:294
bool isnan(void) const
Return true if one of the component is not a number.
Definition: Vector.h:270
int addVector(double factThis, const Vector &other, double factOther)
To add a factor fact times the Vector other to the current Vector.
Definition: Vector.cpp:372
Vector en dos dimensiones.
Definition: Vector2d.h:40
virtual ~Vector(void)
Destructor, free memory.
Definition: Vector.cpp:209
Vector NormalizedInf(void) const
Returns the normalized vector (infinity norm).
Definition: Vector.cpp:349
Vector & operator+=(double fact)
The += operator adds fact to each element of the vector, data[i]= data[i]+fact.
Definition: Vector.cpp:958
int reset(const int &newSize)
Resize the vector and set all components equal to 0.
Definition: Vector.h:283
int Size(void) const
Returns the size of the Vector.
Definition: Vector.h:235
TCP_SocketNoDelay is a sub-class of channel.
Definition: TCP_SocketNoDelay.h:73
Vector operator-(void) const
Unary minus operator.
Definition: Vector.cpp:1072
int NormalizeInf(void)
Normalize the vector using the infinity norm.
Definition: Vector.cpp:325
System of equations base class.
Definition: SystemOfEqn.h:90
Vector & operator=(const Vector &V)
the assignment operator, This is assigned to be a copy of V.
Definition: Vector.cpp:937
virtual std::string getClassName(void) const
Returns demangled class name.
Definition: EntityWithOwner.cc:90
iterator begin(void)
Iterator that points to the first vector component.
Definition: Vector.h:251
double Norm2(void) const
Returns the square of the vector modulus.
Definition: Vector.cpp:765
int Normalize(void)
Normalizes the vector using the euclidean norm.
Definition: Vector.cpp:309
int getNumBytes(void) const
Number of bytes occupied by the vector.
Definition: Vector.h:239
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
Vector(void)
Default constructor, sets size= 0;.
Definition: Vector.cpp:117
MPI_Channel is a sub-class of channel.
Definition: MPI_Channel.h:70
boost::python::dict getPyDict(void) const
Return a Python dictionary with the object members values.
Definition: Vector.cpp:1273
double NormInf(void) const
Returns the maximum of the absolute values of the components (infinite norm).
Definition: Vector.cpp:785
iterator end(void)
Iterator that points one past the last vector component.
Definition: Vector.h:254
virtual bool CheckIndice0(const size_t &i) const
Check the index being passed as parameter.
Definition: Vector.cpp:845
Message between processes.
Definition: Message.h:77
boost::python::list getPyList(void) const
Return the vector values in a Python list.
Definition: Vector.cpp:1252
void setPyList(const boost::python::list &)
Populate the vector with the values of the given list.
Definition: Vector.cpp:1262
std::vector< double > vector_to_std_vector(const Vector &)
Convierte el vector en un std::vector<double>.
Definition: Vector.cpp:1417
Vector getComponents(const ID &) const
Returns a vector with the specified subset of components.
Definition: Vector.cpp:1388
Vector Normalized(void) const
Returns the normalized vector (euclidean norm).
Definition: Vector.cpp:341
Matrix prod_tensor(const Vector &, const Vector &)
Producto tensorial de dos tensores de primer orden.
Definition: Vector.cpp:1305
Open source finite element program for structural analysis.
Definition: ContinuaReprComponent.h:35
const double & operator[](int x) const
If debug flag is on, safely get the data at location x in the Vector.
Definition: Vector.cpp:879
const double * getDataPtr(void) const
Return a pointer to the float date.
Definition: Vector.h:243
Matrix of floats.
Definition: Matrix.h:111
void addComponents(const Vector &, const ID &)
Sums the specified values to the specified set of vector&#39;s components.
Definition: Vector.cpp:1408
void putComponents(const Vector &, const ID &)
Assigns the specified values to the specified set of vector&#39;s components.
Definition: Vector.cpp:1399
Vector normalize_inf(const Vector &)
Returns the normalized vector (infinity norm).
Definition: Vector.cpp:361
int addMatrixVector(double factThis, const Matrix &m, const Vector &v, double factOther)
To add a factor fact times the Vector formed by the product of the matrix m and the Vector v to the c...
Definition: Vector.cpp:470
DP_Socket is a sub-class of channel.
Definition: UDP_Socket.h:76
Vector & operator-=(double fact)
The -= operator subtracts fact from each element of the vector, data[i]= data[i]-fact.
Definition: Vector.cpp:971
Vector en tres dimensiones.
Definition: Vector3d.h:39
int Assemble(const Vector &V, const ID &l, double fact=1.0)
Method to assemble into this vector the Vector V using the ID l.
Definition: Vector.cpp:278