vector.hh

Go to the documentation of this file.
1 
6 #ifndef vector_hh
7 #define vector_hh
8 
9 #include <cmath>
10 #include <string>
11 
12 #include "basics/typedefs.hh"
13 #include "basis.hh"
14 #include "space/spaceSet.hh"
15 #include "toolbox/array.hh"
16 
17 namespace concepts {
18 
19  // forward declaration
20  template<class F, class G>
21  class LinearForm;
22 
23  template<class F>
24  class Space;
25 
26  // **************************************************************** Vector **
27 
38  template<class F>
39  class Vector : public Function<F> {
40  public:
42  Vector(const Vector<F>& f);
44  template<class H>
45  Vector(const Vector<H>& f);
46 
50  template<class G>
51  Vector(const Space<G>& spc)
52  : Function<F>(spc), data_(this->dim_, 0), v_((F*)data_) {}
53 
57  Vector(const uint dim=0)
58  : Function<F>(dim), data_(this->dim_, 0), v_((F*)data_) {}
59 
64  template<class G>
65  Vector(const Space<G>& spc, const LinearForm<F,G>& lf);
66 
72  template<class G>
73  Vector(const Space<G>& spc, const std::string& fname);
74 
83  template<class G>
84  Vector(const Space<G>& spc, F* data) :
85  Function<F>(spc), data_(spc.dim()), v_((F*)data_) {
86  std::memcpy(v_, data, this->dim_ * sizeof(v_[0]));
87  }
88  Vector(uint dim, F* data) :
89  Function<F>(dim), data_(dim), v_((F*)data_) {
90  std::memcpy(v_, data, this->dim_ * sizeof(v_[0]));
91  }
92 
98  template<class H>
99  Vector(const Vector<H>& fncX, F fnc(const H&));
100  template<class H>
101  Vector(const Vector<H>& fncX, const F& fnc(const H&));
102 
108  Vector(const Vector<typename Realtype<F>::type>& V_R,
109  const Vector<typename Realtype<F>::type>& V_I);
110 
116  template<class H>
117  Vector(const Vector<H>& fnc, const Set<IndexRange>& indices);
118 
119  virtual ~Vector();
120 
121  virtual Vector<F>& operator=(const Function<F>& fnc);
122 
125 
126  /* Assignement operator from different number type
127 
128  This is extra, because virtual member templates don't exist.
129 
130  The compiler checks, if the assigment is possible, e.g.
131  Cmplx = Real, possible
132  Real = Cmplx, not possible
133  */
134  template<class H>
136 
139 
140  virtual F& operator()(uint i) {
141  conceptsAssert3(i < this->dim_, Assertion(), "i = " << i << ", n = " << this->dim_);
142  return v_[i]; }
143  virtual F operator()(uint i) const {
144  conceptsAssert3(i < this->dim_, Assertion(), "i = " << i << ", n = " << this->dim_);
145  return v_[i]; }
146 
147  uint size() const{
148  return n();
149  }
150 
152  virtual Function<F>& operator+=(const Function<F>& fnc);
153  virtual Function<F>& operator+=(F c);
154  Vector<F> operator+ (const Function<F>& fnc) const;
155  Vector<F> operator+ (F c) const;
156 
158  virtual Function<F>& operator-=(const Function<F>& fnc);
159  virtual Function<F>& operator-=(F c);
160  Vector<F> operator- (const Function<F>& fnc) const;
161  Vector<F> operator- (F c) const;
162 
164  virtual Function<F>& operator*=(F sc);
165  Vector<F> operator* (F c) const;
166  virtual Function<F>& operator/=(F sc);
167  Vector<F> operator/ (F c) const;
168 
169  // template <class T>
170  // friend std::ostream& operator<<(std::ostream & os, const Vector<T> & v);
171 
177  F operator*(const Vector<F>& fnc) const;
178 
180  template<class G>
181  Vector<F>& assemble(const Space<G>& spc, const LinearForm<F,G>& lf);
182 
185  Vector<F>& apply(F fnc(const F&));
186 
190  operator F*() const { return v_; }
191 
192  F* data() const { return v_; }
193 
195  virtual void resize(uint n) {
196  this->dim_ = n;
197  data_.resize(n);
198  v_ = (F*)data_;
199  }
200 
201  virtual Function<F>& add(const Function<F>& fnc, F sc);
202 
204  template<class H>
205  Vector<F>& add(const Vector<H>& fnc, F sc, uint offset = 0);
206 
208  template<class H>
209  Vector<F>& add(const Vector<H>& fnc) { return add(fnc, F(1.0)); }
210 
212  Real l1() const;
213 
215  inline Real l2() const { return sqrt(l2_2()); }
216 
218  Real l2_2() const;
219 
221  Real max() const;
222 
224  uint n() const { return this->dim_; }
225 
226  // reverse the vector
227  void reverse();
228 
232  const Vector<F>& write(const std::string& fname) const;
233 
235  void storeMatlab(const char* filename, const char* name = 0,
236  bool append = false) const;
237  protected:
238  virtual std::ostream& info(std::ostream& os) const;
239 
240  template<class G>
241  void fillEntries_(const Space<G>& spc, const LinearForm<F,G>& lf);
244  private:
246  F* v_;
248  static uint storeMatlabCounter_;
249  };
250 
251  template<class F>
252  template<class H>
254  Function<F>(f.dim()), data_(this->dim_), v_((F*)data_) {
255  memorycpy(v_, (H*)f, this->dim_);
256  }
257 
258  template<class F>
259  template<class H>
260  Vector<F>::Vector(const Vector<H>& fncX, F fnc(const H&))
261  : Function<F>(fncX.dim()), data_(this->dim_, 0), v_((F*)data_)
262  {
263  F* v = v_;
264  H* w = (H*)fncX;
265  for(uint i = this->dim_; i; --i) *v++= fnc(*w++);
266  }
267 
268  template<class F>
269  template<class G>
270  Vector<F>::Vector(const Space<G>& spc, const LinearForm<F,G>& lf) :
271  Function<F>(spc), data_(this->dim_), v_((F*)data_) {
272  fillEntries_(spc, lf);
273  }
274 
275  template<class F>
276  template<class H>
277  Vector<F>::Vector(const Vector<H>& fncX, const F& fnc(const H&))
278  : Function<F>(fncX.dim()), data_(this->dim_, 0), v_((F*)data_)
279  {
280  F* v = v_;
281  H* w = (H*)fncX;
282  for(uint i = this->dim_; i; --i) *v++= fnc(*w++);
283  }
284 
285  template<class F>
286  template<class H>
287  Vector<F>::Vector(const Vector<H>& fnc, const Set<IndexRange>& indices) :
288  Function<F>(indices.dim()), data_(this->dim_), v_((F*)data_)
289  {
290  F* d = data_;
291  for (Set<IndexRange>::index_iterator i = indices.index_begin();
292  i != indices.index_end(); )
293  *d++ = fnc(*i++);
294  }
295 
296  template<class F>
297  template<class H>
299  {
300  for(uint i = 0; i < this->dim_; ++i) v_[i] = fnc(i);
301  return *this;
302  }
303 
304 
305  template<class F>
306  template<class H>
307  Vector<F>& Vector<F>::add(const Vector<H>& fnc, F sc, uint offset)
308  {
309  F* d = v_ + offset;
310  const H* s = (H*)fnc;
311  for (uint i = std::min(this->dim_-offset,fnc.dim()); i; --i) *d++ += sc * *s++;
312  return *this;
313  }
314 
315  template<class F>
317  F *p1 = (F*)data_, *p2 = (F*)data_ + (data_.size()-1);
318  for(uint i = std::floor(double(data_.size())/2.0); i--; )
319  std::swap(*p1++, *p2--);
320  }
321 
322 } // namespace concepts
323 
324 #endif // vector_hh
virtual Function< F > & operator/=(F sc)
Vector(const Vector< H > &fnc, const Set< IndexRange > &indices)
Constructor.
Definition: vector.hh:287
Vector< F > & assemble(const Space< G > &spc, const LinearForm< F, G > &lf)
Assembles the vector w.r.t. linear form lf and space spc.
Vector(const Vector< F > &f)
Copy constructor.
Vector(const uint dim=0)
Constructor.
Definition: vector.hh:57
Vector(const Vector< H > &fncX, F fnc(const H &))
Constructor.
Definition: vector.hh:260
const Vector< F > & write(const std::string &fname) const
Writes the vector to a file.
Vector< F > & add(const Vector< H > &fnc, F sc, uint offset=0)
Adds a vector of possible different length and type with an offset.
Definition: vector.hh:307
Vector< F > & operator=(const Vector< F > &fnc)
Assignement operator.
Vector< F > & operator=(F c)
Assignement operator.
Real l1() const
l1 norm
Abstract class for a function.
Definition: basis.hh:21
Set with operations, output operator, and method of the particular element types.
Definition: traces.hh:18
index_iterator index_begin(uint i=0) const
Returns an iterator to the i th index.
virtual ~Vector()
virtual Function< F > & operator-=(const Function< F > &fnc)
Subtraction operator.
Abstract class for a linear form.
Vector< F > operator*(F c) const
uint size() const
Definition: vector.hh:147
Vector< F > & apply(F fnc(const F &))
Application operator to each component, e.g.
F operator*(const Vector< F > &fnc) const
Inner product (v = this, w = \fnc)
Real l2_2() const
l2 norm squared
Vector(const Space< G > &spc, F *data)
Constructor.
Definition: vector.hh:84
virtual Function< F > & operator+=(const Function< F > &fnc)
Addition operator.
void reverse()
Definition: vector.hh:316
virtual Function< F > & operator-=(F c)
An array of objects.
Definition: bilinearForm.hh:23
Exception class for assertions.
Definition: exceptions.hh:258
virtual Function< F > & add(const Function< F > &fnc, F sc)
Vector(uint dim, F *data)
Definition: vector.hh:88
void storeMatlab(const char *filename, const char *name=0, bool append=false) const
Stores the vector in a Matlab sparse matrix.
Vector(const Space< G > &spc)
Constructor.
Definition: vector.hh:51
F min(const concepts::Array< F > &a)
Returns the minimal value in array a.
Definition: arrayOp.hh:67
Vector(const Vector< H > &fncX, const F &fnc(const H &))
Definition: vector.hh:277
Vector< F > operator/(F c) const
Vector< F > & add(const Vector< H > &fnc)
Adds a vector of possible different length and type.
Definition: vector.hh:209
uint n() const
Elements in the vector.
Definition: vector.hh:224
virtual Function< F > & operator+=(F c)
Real max() const
Maximum of the absolute values in the vector, ie. norm.
const index_iterator index_end() const
Returns an iterator behind the last index.
void memorycpy(F *dest, const G *src, size_t n)
Copies n entries from src to dest (faster than std::memcpy)
F * v_
Pointer to the vector data.
Definition: vector.hh:246
virtual Vector< F > & operator=(const Function< F > &fnc)
virtual F & operator()(uint i)
Definition: vector.hh:140
virtual void resize(uint n)
Sets a new size, previous data might be lost
Definition: vector.hh:195
virtual std::ostream & info(std::ostream &os) const
static uint storeMatlabCounter_
Counts number of Matlab outputs (used to uniquely name the vectors)
Definition: vector.hh:248
Vector(const Vector< H > &f)
Copy constructor.
Definition: vector.hh:253
Vector< F > & operator=(const Function< H > &fnc)
Definition: vector.hh:298
F * data() const
Definition: vector.hh:192
Vector(const Space< G > &spc, const LinearForm< F, G > &lf)
Constructor.
Definition: vector.hh:270
#define conceptsAssert3(cond, exc, msg)
Assert that a certain condition is fulfilled.
Definition: exceptions.hh:442
Real l2() const
l2 norm
Definition: vector.hh:215
Array< F > data_
Vector data.
Definition: vector.hh:243
Vector(const Space< G > &spc, const std::string &fname)
File read constructor.
Vector(const Vector< typename Realtype< F >::type > &V_R, const Vector< typename Realtype< F >::type > &V_I)
Constructor.
double Real
Type normally used for a floating point number.
Definition: typedefs.hh:36
Vector< F > operator-(const Function< F > &fnc) const
virtual F operator()(uint i) const
Definition: vector.hh:143
Basic namespace for Concepts-2.
Definition: pml_formula.h:16
virtual Function< F > & operator*=(F sc)
Scaling operator.
void fillEntries_(const Space< G > &spc, const LinearForm< F, G > &lf)
Vector< F > operator+(const Function< F > &fnc) const
Page URL: http://wiki.math.ethz.ch/bin/view/Concepts/WebHome
21 August 2020
© 2020 Eidgenössische Technische Hochschule Zürich