MFEM  v3.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
vector.hpp
Go to the documentation of this file.
1 // Copyright (c) 2010, Lawrence Livermore National Security, LLC. Produced at
2 // the Lawrence Livermore National Laboratory. LLNL-CODE-443211. All Rights
3 // reserved. See file COPYRIGHT for details.
4 //
5 // This file is part of the MFEM library. For more information and source code
6 // availability see http://mfem.googlecode.com.
7 //
8 // MFEM is free software; you can redistribute it and/or modify it under the
9 // terms of the GNU Lesser General Public License (as published by the Free
10 // Software Foundation) version 2.1 dated February 1999.
11 
12 #ifndef MFEM_VECTOR
13 #define MFEM_VECTOR
14 
15 // Data type vector
16 
17 #include "../general/array.hpp"
18 #include <cmath>
19 #include <iostream>
20 
21 namespace mfem
22 {
23 
26 inline int CheckFinite(const double *v, const int n);
27 
29 class Vector
30 {
31 protected:
32 
34  double * data;
35 
36 public:
37 
39  Vector () { allocsize = size = 0; data = 0; }
40 
42  Vector(const Vector &);
43 
45  explicit Vector (int s);
46 
47  Vector (double *_data, int _size)
48  { data = _data; size = _size; allocsize = -size; }
49 
51  void Load (std::istream ** in, int np, int * dim);
52 
54  void Load(std::istream &in, int Size);
55 
57  void Load(std::istream &in) { int s; in >> s; Load (in, s); }
58 
60  void SetSize(int s);
61 
62  void SetData(double *d) { data = d; }
63 
64  void SetDataAndSize(double *d, int s)
65  { data = d; size = s; allocsize = -s; }
66 
67  void NewDataAndSize(double *d, int s)
68  { if (allocsize > 0) delete [] data; SetDataAndSize(d, s); }
69 
70  void MakeDataOwner() { allocsize = abs(allocsize); }
71 
73  void Destroy();
74 
76  inline int Size() const { return size; }
77 
78  // double *GetData() { return data; }
79 
80  inline double *GetData() const { return data; }
81 
82  inline operator double *() { return data; }
83 
84  inline operator const double *() const { return data; }
85 
86  inline bool OwnsData() const { return (allocsize > 0); }
87 
89  inline void StealData(double **p)
90  { *p = data; data = 0; size = allocsize = 0; }
91 
93  inline double *StealData() { double *p; StealData(&p); return p; }
94 
96  double & Elem (int i);
97 
99  const double & Elem (int i) const;
100 
102  inline double & operator() (int i);
103 
105  inline const double & operator() (int i) const;
106 
107  double operator*(const double *) const;
108 
110  double operator*(const Vector &v) const;
111 
112  Vector & operator=(const double *v);
113 
115  Vector & operator=(const Vector &v);
116 
118  Vector & operator=(double value);
119 
120  Vector & operator*=(double c);
121 
122  Vector & operator/=(double c);
123 
124  Vector & operator-=(double c);
125 
126  Vector & operator-=(const Vector &v);
127 
128  Vector & operator+=(const Vector &v);
129 
131  Vector & Add(const double a, const Vector &Va);
132 
134  Vector & Set(const double a, const Vector &x);
135 
136  void SetVector (const Vector &v, int offset);
137 
139  void Neg();
140 
142  friend void swap(Vector *v1, Vector *v2);
143 
145  friend void add(const Vector &v1, const Vector &v2, Vector &v);
146 
148  friend void add(const Vector &v1, double alpha, const Vector &v2, Vector &v);
149 
151  friend void add(const double a, const Vector &x, const Vector &y, Vector &z);
152 
154  friend void add (const double a, const Vector &x,
155  const double b, const Vector &y, Vector &z);
156 
158  friend void subtract(const Vector &v1, const Vector &v2, Vector &v);
159 
161  friend void subtract(const double a, const Vector &x,
162  const Vector &y, Vector &z);
163 
165  void median(const Vector &lo, const Vector &hi);
166 
167  void GetSubVector(const Array<int> &dofs, Vector &elemvect) const;
168  void GetSubVector(const Array<int> &dofs, double *elem_data) const;
169 
170  void SetSubVector(const Array<int> &dofs, const Vector &elemvect);
171  void SetSubVector(const Array<int> &dofs, double *elem_data);
172 
174  void AddElementVector(const Array<int> & dofs, const Vector & elemvect);
175  void AddElementVector(const Array<int> & dofs, double *elem_data);
176  void AddElementVector(const Array<int> & dofs, const double a,
177  const Vector & elemvect);
178 
180  void Print(std::ostream & out = std::cout, int width = 8) const;
181 
183  void Print_HYPRE(std::ostream &out) const;
184 
186  void Randomize(int seed = 0);
188  double Norml2() const;
190  double Normlinf() const;
192  double Norml1() const;
194  double Normlp(double p) const;
196  double Max() const;
198  double Min() const;
200  double Sum() const;
202  double DistanceTo (const double *p) const;
203 
206  int CheckFinite() const { return mfem::CheckFinite(data, size); }
207 
209  ~Vector ();
210 };
211 
212 // Inline methods
213 
214 inline int CheckFinite(const double *v, const int n)
215 {
216  // isfinite didn't appear in a standard until C99, and later C++11
217  // It wasn't standard in C89 or C++98. PGI as of 14.7 still defines
218  // it as a macro, which sort of screws up everybody else.
219  int bad = 0;
220  for (int i = 0; i < n; i++)
221  {
222 #ifdef isfinite
223  if (!isfinite(v[i]))
224 #else
225  if (!std::isfinite(v[i]))
226 #endif
227  {
228  bad++;
229  }
230  }
231  return bad;
232 }
233 
234 inline Vector::Vector (int s)
235 {
236  if (s > 0)
237  {
238  allocsize = size = s;
239  data = new double[s];
240  }
241  else
242  {
243  allocsize = size = 0;
244  data = NULL;
245  }
246 }
247 
248 inline void Vector::SetSize(int s)
249 {
250  if (s == size)
251  return;
252  if (s <= abs(allocsize))
253  {
254  size = s;
255  return;
256  }
257  if (allocsize > 0)
258  delete [] data;
259  allocsize = size = s;
260  data = new double[s];
261 }
262 
263 inline void Vector::Destroy()
264 {
265  if (allocsize > 0)
266  delete [] data;
267  allocsize = size = 0;
268  data = NULL;
269 }
270 
271 inline double & Vector::operator() (int i)
272 {
273 #ifdef MFEM_DEBUG
274  if (data == 0 || i < 0 || i >= size)
275  mfem_error ("Vector::operator()");
276 #endif
277 
278  return data[i];
279 }
280 
281 inline const double & Vector::operator() (int i) const
282 {
283 #ifdef MFEM_DEBUG
284  if (data == 0 || i < 0 || i >= size)
285  mfem_error ("Vector::operator() const");
286 #endif
287 
288  return data[i];
289 }
290 
292 {
293  if (allocsize > 0)
294  delete [] data;
295 }
296 
297 inline double Distance(const double *x, const double *y, const int n)
298 {
299  using namespace std;
300  double d = 0.0;
301 
302  for (int i = 0; i < n; i++)
303  d += (x[i]-y[i])*(x[i]-y[i]);
304 
305  return sqrt(d);
306 }
307 
308 }
309 
310 #endif
void SetVector(const Vector &v, int offset)
Definition: vector.cpp:193
int CheckFinite(const double *v, const int n)
Definition: vector.hpp:214
Vector()
Default constructor for Vector. Sets size = 0 and data = NULL.
Definition: vector.hpp:39
friend void swap(Vector *v1, Vector *v2)
Swap v1 and v2.
Definition: vector.cpp:213
void NewDataAndSize(double *d, int s)
Definition: vector.hpp:67
double & Elem(int i)
Sets value in vector. Index i = 0 .. size-1.
Definition: vector.cpp:67
void Print(std::ostream &out=std::cout, int width=8) const
Prints vector to stream out.
Definition: vector.cpp:483
void SetSize(int s)
Resizes the vector if the new size is different.
Definition: vector.hpp:248
void MakeDataOwner()
Definition: vector.hpp:70
friend void subtract(const Vector &v1, const Vector &v2, Vector &v)
Do v = v1 - v2.
Definition: vector.cpp:341
double Norml2() const
Returns the l2 norm of the vector.
Definition: vector.cpp:532
double & operator()(int i)
Sets value in vector. Index i = 0 .. size-1.
Definition: vector.hpp:271
void GetSubVector(const Array< int > &dofs, Vector &elemvect) const
Definition: vector.cpp:403
void StealData(double **p)
Changes the ownership of the data; after the call the Vector is empty.
Definition: vector.hpp:89
int Size() const
Returns the size of the vector.
Definition: vector.hpp:76
double Normlinf() const
Returns the l_infinity norm of the vector.
Definition: vector.cpp:537
void Randomize(int seed=0)
Set random values in the vector.
Definition: vector.cpp:517
double * GetData() const
Definition: vector.hpp:80
double operator*(const double *) const
Definition: vector.cpp:77
Vector & operator=(const double *v)
Definition: vector.cpp:100
void Load(std::istream **in, int np, int *dim)
Reads a vector from multiple files.
Definition: vector.cpp:43
bool OwnsData() const
Definition: vector.hpp:86
double Normlp(double p) const
Returns the l_p norm of the vector.
Definition: vector.cpp:558
void median(const Vector &lo, const Vector &hi)
v = median(v,lo,hi) entrywise. Implementation assumes lo &lt;= hi.
Definition: vector.cpp:390
void Load(std::istream &in)
Load a vector from an input stream.
Definition: vector.hpp:57
double * StealData()
Changes the ownership of the data; after the call the Vector is empty.
Definition: vector.hpp:93
void SetData(double *d)
Definition: vector.hpp:62
void AddElementVector(const Array< int > &dofs, const Vector &elemvect)
Add (element) subvector to the vector.
Definition: vector.cpp:449
Vector & operator/=(double c)
Definition: vector.cpp:131
double DistanceTo(const double *p) const
Compute the Euclidian distance to another vector.
Definition: vector.cpp:608
Vector(double *_data, int _size)
Definition: vector.hpp:47
Vector & operator*=(double c)
Definition: vector.cpp:124
double Min() const
Returns the minimal element of the vector.
Definition: vector.cpp:587
double Norml1() const
Returns the l_1 norm of the vector.
Definition: vector.cpp:548
double Distance(const double *x, const double *y, const int n)
Definition: vector.hpp:297
void mfem_error(const char *msg)
Definition: error.cpp:23
void SetSubVector(const Array< int > &dofs, const Vector &elemvect)
Definition: vector.cpp:427
void Print_HYPRE(std::ostream &out) const
Prints vector to stream out in HYPRE_Vector format.
Definition: vector.cpp:501
void SetDataAndSize(double *d, int s)
Definition: vector.hpp:64
Vector & Set(const double a, const Vector &x)
(*this) = a * x
Definition: vector.cpp:182
Vector & Add(const double a, const Vector &Va)
(*this) += a * Va
Definition: vector.cpp:168
double Max() const
Returns the maximal element of the vector.
Definition: vector.cpp:576
void Destroy()
Destroy a vector.
Definition: vector.hpp:263
Vector & operator-=(double c)
Definition: vector.cpp:139
int CheckFinite() const
Definition: vector.hpp:206
Vector data type.
Definition: vector.hpp:29
friend void add(const Vector &v1, const Vector &v2, Vector &v)
Do v = v1 + v2.
Definition: vector.cpp:227
int allocsize
Definition: vector.hpp:33
Vector & operator+=(const Vector &v)
Definition: vector.cpp:157
double * data
Definition: vector.hpp:34
double Sum() const
Return the sum of the vector entries.
Definition: vector.cpp:598
~Vector()
Destroys vector.
Definition: vector.hpp:291
void Neg()
(*this) = -(*this)
Definition: vector.cpp:207