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 std::initializer_list<double> &);
111  explicit Vector(const Vector2d &v);
112  explicit Vector(const Vector3d &v);
113  Vector(const double &,const double &,const double &);
114  Vector(const Vector &);
115  Vector(double *data, int size);
116  explicit Vector(const boost::python::list &);
117  virtual ~Vector(void);
118 
119  iterator begin(void);
120  iterator end(void);
121  // utility methods
122  int setData(double *newData, int size);
123  const double *getDataPtr(void) const;
124  double *getDataPtr(void);
125  bool isEmpty(void) const;
126  int Assemble(const Vector &V, const ID &l, double fact = 1.0);
127  double Norm2(void) const;
128  double Norm(void) const;
129  double pNorm(int p) const;
130  double NormInf(void) const;
131  int getIndexMaxValue(void) const;
132  int getIndexMinValue(void) const;
133  int getIndexMaxAbsValue(void) const;
134  int getIndexMinAbsValue(void) const;
135  int Size(void) const;
136  int getNumBytes(void) const;
137  int resize(int newSize);
138  void Zero(void);
139  bool isnan(void) const;
140  int reset(const int &newSize);
141  int Normalize(void);
142  int NormalizeInf(void);
143  Vector Normalized(void) const;
144  Vector NormalizedInf(void) const;
145 
146  int addVector(double factThis, const Vector &other, double factOther);
147  int addMatrixVector(double factThis, const Matrix &m, const Vector &v, double factOther);
148  int addMatrixTransposeVector(double factThis, const Matrix &m, const Vector &v, double factOther);
149 
150 
152  virtual double &at(const size_t &f);
154  virtual const double &at(const size_t &f) const;
156  virtual bool CheckIndice0(const size_t &i) const;
157 
158  // overloaded operators
159  const double &operator()(int x) const;
160  double &operator()(int x);
161  const double &operator[](int x) const; // these two operator do bounds checks
162  double &operator[](int x);
163  Vector operator()(const ID &rows) const;
164  Vector &operator=(const Vector &V);
165 
166  template <class TNSR>
167  Vector &operator=(const TNSR &T);
168 
169  Vector &operator+=(double fact);
170  Vector &operator-=(double fact);
171  Vector &operator*=(double fact);
172  Vector &operator/=(double fact);
173 
174  Vector operator-(void) const;
175  Vector operator+(double fact) const;
176  Vector operator-(double fact) const;
177  Vector operator*(double fact) const;
178  Vector operator/(double fact) const;
179 
180  Vector &operator+=(const Vector &V);
181  Vector &operator-=(const Vector &V);
182 
183  Vector operator+(const Vector &V) const;
184  Vector operator-(const Vector &V) const;
185  double operator^(const Vector &V) const;
186  Vector operator/(const Matrix &M) const;
187 
188  double dot(const Vector &) const;
189 
190  // methods added by Remo
191  int Assemble(const Vector &V, int init_row, double fact = 1.0);
192  int Extract(const Vector &V, int init_row, double fact = 1.0);
193 
194  Vector getComponents(const ID &) const;
195  void putComponents(const Vector &,const ID &);
196  void addComponents(const Vector &,const ID &);
197 
198  void write(std::ofstream &);
199  void read(std::ifstream &);
200  friend std::ostream &operator<<(std::ostream &, const Vector &);
201  friend std::string to_string(const Vector &);
202  inline std::string toString(void) const
203  { return to_string(*this); }
204 
205  boost::python::list getPyList(void) const;
206  void setPyList(const boost::python::list &);
207  boost::python::dict getPyDict(void) const;
208  void setPyDict(const boost::python::dict &);
209 
210  // friend istream &operator>>(istream &s, Vector &V);
211  friend Vector operator*(double , const Vector &);
212 
213  friend class Message;
214  friend class SystemOfEqn;
215  friend class Matrix;
216  friend class TCP_SocketNoDelay;
217  friend class TCP_Socket;
218  friend class UDP_Socket;
219  friend class MPI_Channel;
220  };
221 
222 Vector operator*(double , const Vector &);
223 
224 std::vector<double> vector_to_std_vector(const Vector &);
226 
227 double dot(const Vector &a,const Vector &b);
228 Matrix prod_tensor(const Vector &,const Vector &);
229 Matrix operator&(const Vector &u,const Vector &v);
230 
231 Vector normalize(const Vector &);
232 Vector normalize_inf(const Vector &);
233 
234 
235 std::ostream &operator<<(std::ostream &, const Vector &);
236 std::string to_string(const Vector &);
237 
238 /********* INLINED VECTOR FUNCTIONS ***********/
240 inline int Vector::Size(void) const
241  { return sz; }
242 
244 inline int Vector::getNumBytes(void) const
245  { return Size()*sizeof(double); }
246 
248 inline const double *Vector::getDataPtr(void) const
249  { return theData; }
250 
252 inline double *Vector::getDataPtr(void)
253  { return theData; }
254 
256 inline Vector::iterator Vector::begin(void)
257  { return theData; }
259 inline Vector::iterator Vector::end(void)
260  { return theData + sz; }
261 
263 inline bool Vector::isEmpty(void) const
264  { return (theData== nullptr); }
265 
268 inline void Vector::Zero(void)
269  {
270  for(int i=0; i<sz; i++)
271  theData[i] = 0.0;
272  }
273 
275 inline bool Vector::isnan(void) const
276  {
277  bool retval= false;
278  for(int i=0; i<sz; i++)
279  if(std::isnan(theData[i]))
280  {
281  retval= true;
282  break;
283  }
284  return retval;
285  }
286 
288 inline int Vector::reset(const int &newSize)
289  {
290  const int retval= resize(newSize);
291  Zero();
292  return retval;
293  }
294 
299 inline const double &Vector::operator()(int x) const
300  {
301 #ifdef _G3DEBUG
302  // check if it is inside range [0,sz-1]
303  if(x < 0 || x >= sz)
304  {
305  std::cerr << getClassName() << "::" << __FUNCTION__
306  << "; loc " << x << " outside range [0, "
307  << sz-1 << std::endl;
308  return VECTOR_NOT_VALID_ENTRY;
309  }
310 #endif
311  return theData[x];
312  }
313 
314 
319 inline double &Vector::operator()(int x)
320  {
321 #ifdef _G3DEBUG
322  // check if it is inside range [0,sz-1]
323  if (x < 0 || x >= sz)
324  {
325  std::cerr << getClassName() << "::" << __FUNCTION__
326  << "; loc " << x << " outside range [0, " << sz-1
327  << std::endl;
328  return VECTOR_NOT_VALID_ENTRY;
329  }
330 #endif
331  return theData[x];
332  }
333 
334 template <class TNSR>
335 Vector & Vector::operator=(const TNSR &V)
336  {
337  int rank= V.rank();
338  if(rank != 2)
339  {
340  std::cerr << "XC::Vector::operator=() - BJtensor must be of rank 2\n";
341  return *this;
342  }
343  int dim= V.dim(1);
344  if(dim != V.dim(2))
345  {
346  std::cerr << "XC::Vector::operator=() - BJtensor must have square dimensions\n";
347  return *this;
348  }
349 
350  if(dim != 2 || dim != 3 || dim != 1)
351  {
352  std::cerr << "XC::Vector::operator=() - BJtensor must be of dimension 2 or 3\n";
353  return *this;
354  }
355 
356  if(dim == 1)
357  {
358  if(sz != 1)
359  {
360  std::cerr << "Vector::operator=() - Vector size must be 1\n";
361  return *this;
362  }
363  theData[0]= V(1,1);
364  }
365  else if(dim == 2)
366  {
367  if(sz != 3)
368  {
369  std::cerr << "Vector::operator=() - Vector size must be 3\n";
370  return *this;
371  }
372  theData[0]= V(1,1);
373  theData[1]= V(2,2);
374  theData[2]= V(1,2);
375  }
376  else
377  {
378  if(sz != 6)
379  {
380  std::cerr << "Vector::operator=() - Vector size must be 6\n";
381  return *this;
382  }
383  theData[0]= V(1,1);
384  theData[1]= V(2,2);
385  theData[2]= V(3,3);
386  theData[3]= V(1,2);
387  theData[4]= V(1,3);
388  theData[5]= V(2,3);
389  }
390  return *this;
391  }
392 
393 } // end of XC namespace
394 
395 
396 #endif
397 
void setPyDict(const boost::python::dict &)
Set the values of the object members from a Python dictionary.
Definition: Vector.cpp:1379
Vector normalize(const Vector &)
Returns the normalized vector (euclidean norm).
Definition: Vector.cpp:367
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:917
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:1136
m_double vector_to_m_double(const Vector &)
Convierte el vector en un m_double.
Definition: Vector.cpp:1525
void write(std::ofstream &)
Write vector on a binary file.
Definition: Vector.cpp:1339
Vector & operator/=(double fact)
The /= operator divides each element of the vector by fact, theData[i]= theData[i]/fact.
Definition: Vector.cpp:1091
double Norm(void) const
Return the norm of vector.
Definition: Vector.cpp:790
void read(std::ifstream &)
Read vector from a binary file.
Definition: Vector.cpp:1387
int resize(int newSize)
Changes vector size.
Definition: Vector.cpp:247
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:228
int getIndexMaxValue(void) const
Returns the index of the maximum of the values of the components.
Definition: Vector.cpp:805
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:1108
Vector & operator*=(double fact)
The *= operator multiplies each element by the factor.
Definition: Vector.cpp:1078
Vector of integers.
Definition: ID.h:95
void Zero(void)
Zeros out the Vector, i.e.
Definition: Vector.h:268
double operator^(const Vector &V) const
Method to perform (Vector)transposed * vector.
Definition: Vector.cpp:1289
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:1153
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:299
bool isnan(void) const
Return true if one of the component is not a number.
Definition: Vector.h:275
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:382
Vector en dos dimensiones.
Definition: Vector2d.h:41
virtual ~Vector(void)
Destructor, free memory.
Definition: Vector.cpp:219
Vector NormalizedInf(void) const
Returns the normalized vector (infinity norm).
Definition: Vector.cpp:359
Vector & operator+=(double fact)
The += operator adds fact to each element of the vector, data[i]= data[i]+fact.
Definition: Vector.cpp:1056
int getIndexMaxAbsValue(void) const
Returns the index of the maximum of the values of the components.
Definition: Vector.cpp:849
int reset(const int &newSize)
Resize the vector and set all components equal to 0.
Definition: Vector.h:288
int Size(void) const
Returns the size of the Vector.
Definition: Vector.h:240
TCP_SocketNoDelay is a sub-class of channel.
Definition: TCP_SocketNoDelay.h:73
int getIndexMinValue(void) const
Returns the index of the maximum of the values of the components.
Definition: Vector.cpp:827
Vector operator-(void) const
Unary minus operator.
Definition: Vector.cpp:1170
int NormalizeInf(void)
Normalize the vector using the infinity norm.
Definition: Vector.cpp:335
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:1035
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:256
double Norm2(void) const
Returns the square of the vector modulus.
Definition: Vector.cpp:775
int Normalize(void)
Normalizes the vector using the euclidean norm.
Definition: Vector.cpp:319
int getNumBytes(void) const
Number of bytes occupied by the vector.
Definition: Vector.h:244
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:263
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:1371
double NormInf(void) const
Returns the maximum of the absolute values of the components (infinite norm).
Definition: Vector.cpp:795
iterator end(void)
Iterator that points one past the last vector component.
Definition: Vector.h:259
virtual bool CheckIndice0(const size_t &i) const
Check the index being passed as parameter.
Definition: Vector.cpp:943
Message between processes.
Definition: Message.h:77
boost::python::list getPyList(void) const
Return the vector values in a Python list.
Definition: Vector.cpp:1350
void setPyList(const boost::python::list &)
Populate the vector with the values of the given list.
Definition: Vector.cpp:1360
std::vector< double > vector_to_std_vector(const Vector &)
Convierte el vector en un std::vector<double>.
Definition: Vector.cpp:1515
Vector getComponents(const ID &) const
Returns a vector with the specified subset of components.
Definition: Vector.cpp:1486
int getIndexMinAbsValue(void) const
Returns the index of the maximum of the values of the components.
Definition: Vector.cpp:871
Vector Normalized(void) const
Returns the normalized vector (euclidean norm).
Definition: Vector.cpp:351
Matrix prod_tensor(const Vector &, const Vector &)
Producto tensorial de dos tensores de primer orden.
Definition: Vector.cpp:1403
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:977
const double * getDataPtr(void) const
Return a pointer to the float date.
Definition: Vector.h:248
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:1506
void putComponents(const Vector &, const ID &)
Assigns the specified values to the specified set of vector&#39;s components.
Definition: Vector.cpp:1497
Vector normalize_inf(const Vector &)
Returns the normalized vector (infinity norm).
Definition: Vector.cpp:371
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:480
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:1069
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:288