Class documentation of Concepts

Loading...
Searching...
No Matches
vector.hh
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
17namespace 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
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
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
153 virtual Function<F>& operator+=(F c);
154 Vector<F> operator+ (const Function<F>& fnc) const;
155 Vector<F> operator+ (F c) const;
156
159 virtual Function<F>& operator-=(F c);
160 Vector<F> operator- (const Function<F>& fnc) const;
161 Vector<F> operator- (F c) const;
162
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>
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>
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>
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>
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>
316 void Vector<F>::reverse(){
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
uint dim_
Dimension of the function (or of the space the function is defined)
Definition basis.hh:57
uint dim() const
Returns the dimension of the function.
Definition basis.hh:53
virtual F & operator()(uint i)
Index operator.
Definition vector.hh:140
Vector< F > & operator=(const Vector< F > &fnc)
Assignement operator.
Vector(const Vector< H > &fnc, const Set< IndexRange > &indices)
Definition vector.hh:287
Vector(const Vector< F > &f)
Copy constructor.
virtual F operator()(uint i) const
Index operator.
Definition vector.hh:143
virtual void resize(uint n)
Sets a new size, previous data might be lost
Definition vector.hh:195
virtual Function< F > & operator+=(F c)
Addition operator.
Vector(const uint dim=0)
Definition vector.hh:57
virtual Function< F > & operator-=(F c)
Subtraction operator.
Vector< F > & operator=(F c)
Assignement operator.
Real l2() const
l2 norm
Definition vector.hh:215
Vector(const Vector< H > &fncX, F fnc(const H &))
Definition vector.hh:260
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(const Vector< typename Realtype< F >::type > &V_R, const Vector< typename Realtype< F >::type > &V_I)
void storeMatlab(const char *filename, const char *name=0, bool append=false) const
Stores the vector in a Matlab sparse matrix.
uint n() const
Elements in the vector.
Definition vector.hh:224
Vector(const Space< G > &spc)
Definition vector.hh:51
virtual Function< F > & operator*=(F sc)
Scaling operator.
const Vector< F > & write(const std::string &fname) const
Vector< F > & add(const Vector< H > &fnc)
Adds a vector of possible different length and type.
Definition vector.hh:209
Vector< F > & apply(F fnc(const F &))
virtual std::ostream & info(std::ostream &os) const
Returns information in an output stream.
Vector(const Space< G > &spc, const std::string &fname)
Array< F > data_
Vector data.
Definition vector.hh:243
Vector< F > & assemble(const Space< G > &spc, const LinearForm< F, G > &lf)
Assembles the vector w.r.t. linear form lf and space spc.
virtual Function< F > & operator-=(const Function< F > &fnc)
Subtraction operator.
virtual Vector< F > & operator=(const Function< F > &fnc)
Assignment operator.
Real l1() const
l1 norm
Vector(const Space< G > &spc, F *data)
Definition vector.hh:84
virtual Function< F > & operator+=(const Function< F > &fnc)
Addition operator.
virtual Function< F > & add(const Function< F > &fnc, F sc)
Adds a times fnc to this function.
Real max() const
Maximum of the absolute values in the vector, ie. norm.
Real l2_2() const
l2 norm squared
Vector(const Vector< H > &f)
Copy constructor.
Definition vector.hh:253
Vector(const Space< G > &spc, const LinearForm< F, G > &lf)
Definition vector.hh:270
F operator*(const Vector< F > &fnc) const
#define conceptsAssert3(cond, exc, msg)
double Real
Definition typedefs.hh:39
void memorycpy(F *dest, const G *src, size_t n)
Copies n entries from src to dest (faster than std::memcpy)
Set< F > makeSet(uint n, const F &first,...)
Definition set.hh:320