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