libROM  v1.0
Data-driven physical simulation library
Vector.h
1 
11 // Description: A simple, parallel Vector class with the utility needed to
12 // support the basis generation methods of this library. A
13 // distributed Vector has its rows distributed across processors.
14 
15 #ifndef included_Vector_h
16 #define included_Vector_h
17 
18 #include "utils/Utilities.h"
19 #include <vector>
20 #include <functional>
21 
22 namespace CAROM {
23 
29 class Vector
30 {
31 public:
32  Vector();
33 
45  Vector(
46  int dim,
47  bool distributed);
48 
65  Vector(
66  double* vec,
67  int dim,
68  bool distributed,
69  bool copy_data = true);
70 
76  Vector(
77  const Vector& other);
78 
82  ~Vector();
83 
91  Vector&
93  const Vector& rhs);
94 
102  Vector&
103  operator += (
104  const Vector& rhs);
105 
113  Vector&
114  operator -= (
115  const Vector& rhs);
116 
125  Vector&
126  operator = (
127  const double& a);
128 
137  Vector&
138  operator *= (
139  const double& a);
140 
149  Vector&
150  transform(std::function<void(const int size, double* vector)> transformer);
151 
161  void
162  transform(Vector& result,
163  std::function<void(const int size, double* vector)> transformer) const;
164 
174  void
175  transform(Vector*& result,
176  std::function<void(const int size, double* vector)> transformer) const;
177 
187  Vector&
188  transform(
189  std::function<void(const int size, double* origVector, double* resultVector)>
190  transformer);
191 
202  void
203  transform(Vector& result,
204  std::function<void(const int size, double* origVector, double* resultVector)>
205  transformer) const;
206 
217  void
218  transform(Vector*& result,
219  std::function<void(const int size, double* origVector, double* resultVector)>
220  transformer) const;
221 
230  void
232  int dim)
233  {
234  if (dim > d_alloc_size) {
235  if (!d_owns_data) {
236  CAROM_ERROR("Can not reallocate externally owned storage.");
237  }
238  if (d_vec) {
239  delete [] d_vec;
240  }
241 
242  // Allocate new array and initialize all values to zero.
243  d_vec = new double [dim] {0.0};
244  d_alloc_size = dim;
245  }
246  d_dim = dim;
247  }
248 
254  bool
255  distributed() const
256  {
257  return d_distributed;
258  }
259 
265  int
266  dim() const
267  {
268  return d_dim;
269  }
270 
283  double
285  const Vector& other) const;
286 
300  double
302  const Vector* other) const
303  {
304  CAROM_VERIFY(other != 0);
305  return inner_product(*other);
306  }
307 
315  double
316  norm() const;
317 
325  double
326  norm2() const;
327 
335  double
336  normalize();
337 
348  Vector*
350  const Vector& other) const
351  {
352  Vector* result = 0;
353  plus(other, result);
354  return result;
355  }
356 
368  Vector*
370  const Vector* other) const
371  {
372  CAROM_VERIFY(other != 0);
373  return plus(*other);
374  }
375 
389  void
390  plus(
391  const Vector& other,
392  Vector*& result) const;
393 
406  void
407  plus(
408  const Vector& other,
409  Vector& result) const;
410 
423  Vector*
425  double factor,
426  const Vector& other)
427  {
428  Vector* result = 0;
429  plusAx(factor, other, result);
430  return result;
431  }
432 
445  Vector*
447  double factor,
448  const Vector* other)
449  {
450  CAROM_VERIFY(other != 0);
451  return plusAx(factor, *other);
452  }
453 
468  void
469  plusAx(
470  double factor,
471  const Vector& other,
472  Vector*& result) const;
473 
487  void
488  plusAx(
489  double factor,
490  const Vector& other,
491  Vector& result) const;
492 
502  void
503  plusEqAx(
504  double factor,
505  const Vector& other);
506 
517  void
519  double factor,
520  const Vector* other)
521  {
522  CAROM_VERIFY(other != 0);
523  plusEqAx(factor, *other);
524  }
525 
537  Vector*
539  const Vector& other) const
540  {
541  Vector* result = 0;
542  minus(other, result);
543  return result;
544  }
545 
558  Vector*
560  const Vector* other) const
561  {
562  CAROM_VERIFY(other != 0);
563  return minus(*other);
564  }
565 
579  void
580  minus(
581  const Vector& other,
582  Vector*& result) const;
583 
596  void
597  minus(
598  const Vector& other,
599  Vector& result) const;
600 
609  Vector*
611  double factor) const
612  {
613  Vector* result = 0;
614  mult(factor, result);
615  return result;
616  }
617 
627  void
628  mult(
629  double factor,
630  Vector*& result) const;
631 
641  void
642  mult(
643  double factor,
644  Vector& result) const;
645 
655  const double&
657  int i) const
658  {
659  CAROM_ASSERT((0 <= i) && (i < dim()));
660  return d_vec[i];
661  }
662 
674  double&
676  int i)
677  {
678  CAROM_ASSERT((0 <= i) && (i < dim()));
679  return d_vec[i];
680  }
681 
691  const double& operator() (int i) const
692  {
693  return item(i);
694  }
695 
707  double& operator() (int i)
708  {
709  return item(i);
710  }
711 
718  void print(const char * prefix) const;
719 
726  void write(const std::string& base_file_name);
727 
734  void read(const std::string& base_file_name);
735 
743  void local_read(const std::string& base_file_name, int rank);
744 
748  double *getData() const
749  {
750  return d_vec;
751  }
752 
760  double localMin(int nmax = 0);
761 
762 private:
766  double* d_vec;
767 
771  int d_dim;
772 
778  int d_alloc_size;
779 
786  bool d_distributed;
787 
791  int d_num_procs;
792 
799  bool d_owns_data;
800 };
801 
806 int getCenterPoint(std::vector<Vector*>& points,
807  bool use_centroid);
808 
813 int getCenterPoint(std::vector<Vector>& points,
814  bool use_centroid);
815 
820 int getClosestPoint(std::vector<Vector*>& points,
821  Vector* test_point);
822 
827 int getClosestPoint(std::vector<Vector>& points,
828  Vector test_point);
829 }
830 
831 #endif
double * getData() const
Get the vector data as a pointer.
Definition: Vector.h:748
void setSize(int dim)
Sets the length of the vector and reallocates storage if needed. All values are initialized to zero.
Definition: Vector.h:231
double & item(int i)
Non-const Vector member access.
Definition: Vector.h:675
Vector * mult(double factor) const
Multiplies this by the supplied constant and returns the result.
Definition: Vector.h:610
void read(const std::string &base_file_name)
read Vector from (a) HDF file(s).
Definition: Vector.cpp:492
const double & operator()(int i) const
Const Vector member access.
Definition: Vector.h:691
Vector & operator-=(const Vector &rhs)
Subtraction operator.
Definition: Vector.cpp:130
Vector * plusAx(double factor, const Vector *other)
Adds factor*other and this and returns the result, pointer version.
Definition: Vector.h:446
bool distributed() const
Returns true if the Vector is distributed.
Definition: Vector.h:255
void plusEqAx(double factor, const Vector *other)
Adds factor*other to this, pointer version.
Definition: Vector.h:518
void print(const char *prefix) const
print Vector into (a) ascii file(s).
Definition: Vector.cpp:476
double inner_product(const Vector *other) const
Inner product, pointer version.
Definition: Vector.h:301
double norm() const
Form the norm of this.
Definition: Vector.cpp:242
Vector * minus(const Vector &other) const
Subtracts other and this and returns the result, reference version.
Definition: Vector.h:538
double normalize()
Normalizes the Vector and returns its norm.
Definition: Vector.cpp:256
void write(const std::string &base_file_name)
write Vector into (a) HDF file(s).
Definition: Vector.cpp:446
Vector * plus(const Vector &other) const
Adds other and this and returns the result, reference version.
Definition: Vector.h:349
const double & item(int i) const
Const Vector member access.
Definition: Vector.h:656
Vector * minus(const Vector *other) const
Subtracts other and this and returns the result, pointer version.
Definition: Vector.h:559
Vector & operator=(const Vector &rhs)
Assignment operator.
Definition: Vector.cpp:110
double inner_product(const Vector &other) const
Inner product, reference form.
Definition: Vector.cpp:222
~Vector()
Destructor.
Definition: Vector.cpp:102
void plusEqAx(double factor, const Vector &other)
Adds factor*other to this, reference version.
Definition: Vector.cpp:352
double norm2() const
Form the squared norm of this.
Definition: Vector.cpp:249
Vector * plusAx(double factor, const Vector &other)
Adds factor*other and this and returns the result, reference version.
Definition: Vector.h:424
Vector & transform(std::function< void(const int size, double *vector)> transformer)
Transform the vector using a supplied function.
Definition: Vector.cpp:153
Vector & operator+=(const Vector &rhs)
Addition operator.
Definition: Vector.cpp:121
double localMin(int nmax=0)
Compute the local minimum of this.
Definition: Vector.cpp:566
void local_read(const std::string &base_file_name, int rank)
read read a single rank of a distributed Vector from (a) HDF file(s).
Definition: Vector.cpp:533
int dim() const
Returns the dimension of the Vector on this processor.
Definition: Vector.h:266
Vector & operator*=(const double &a)
Scaling operator.
Definition: Vector.cpp:146
Vector * plus(const Vector *other) const
Adds other and this and returns the result, pointer version.
Definition: Vector.h:369
int getClosestPoint(std::vector< Vector * > &points, Vector *test_point)
Get closest point to a test point among a group of points.
Definition: Vector.cpp:647
int getCenterPoint(std::vector< Vector * > &points, bool use_centroid)
Get center point of a group of points.
Definition: Vector.cpp:580