Home | Doxygen Documentation | Tutorials | Developer Tools (restricted)

function/vector.hh
Go to the documentation of this file.
00001 /* Function derivation: Vector, stores the values in an array
00002  */
00003 
00004 #ifndef vector_hh
00005 #define vector_hh
00006 
00007 #include <cmath>
00008 #include <string>
00009 
00010 #include "basics/typedefs.hh"
00011 #include "basis.hh"
00012 #include "space/spaceSet.hh"
00013 #include "toolbox/array.hh"
00014 
00015 namespace concepts {
00016 
00017   // forward declaration
00018   template<class F, class G>
00019   class LinearForm;
00020 
00021   template<class F>
00022   class Space;
00023 
00024   // **************************************************************** Vector **
00025 
00036   template<class F>
00037   class Vector : public Function<F> {
00038   public:
00040     Vector(const Vector<F>& f);
00042     template<class H>
00043     Vector(const Vector<H>& f);
00044 
00048     template<class G>
00049     Vector(const Space<G>& spc) 
00050       : Function<F>(spc), data_(this->dim_, 0), v_((F*)data_) {}
00051 
00055     Vector(const uint dim) 
00056       : Function<F>(dim), data_(this->dim_, 0), v_((F*)data_)  {}
00057 
00062     template<class G>
00063     Vector(const Space<G>& spc, LinearForm<F,G>& lf);
00064 
00070     template<class G>
00071     Vector(const Space<G>& spc, const std::string& fname);
00072 
00081     template<class G>
00082     Vector(const Space<G>& spc, F* data) :
00083       Function<F>(spc), data_(spc.dim()), v_((F*)data_) {
00084       std::memcpy(v_, data, this->dim_ * sizeof(v_[0]));
00085     }
00086     Vector(uint dim, F* data) :
00087       Function<F>(dim), data_(dim), v_((F*)data_) {
00088       std::memcpy(v_, data, this->dim_ * sizeof(v_[0]));
00089     }
00090 
00096     template<class H>
00097     Vector(const Vector<H>& fncX, F fnc(const H&));
00098     template<class H>
00099     Vector(const Vector<H>& fncX, const F& fnc(const H&));
00100 
00106     Vector(const Vector<typename Realtype<F>::type>& V_R,
00107            const Vector<typename Realtype<F>::type>& V_I);
00108     
00114     template<class H>
00115     Vector(const Vector<H>& fnc, const Set<IndexRange>& indices);
00116 
00117     virtual ~Vector();
00118 
00119     virtual Function<F>& operator=(const Function<F>& fnc);
00120 
00122     Vector<F>& operator=(const Vector<F>& fnc);
00123 
00124     /* Assignement operator from different number type
00125 
00126        This is extra, because virtual member templates don't exist.
00127 
00128        The compiler checks, if the assigment is possible, e.g.
00129          Cmplx = Real, possible
00130          Real = Cmplx, not possible
00131      */
00132     template<class H>
00133     Function<F>& operator=(const Function<H>& fnc);
00134 
00136     Vector<F>& operator=(F c);
00137 
00138     virtual F& operator()(uint i) {
00139       conceptsAssert3(i < this->dim_, Assertion(), "i = " << i << ", n = " << this->dim_);
00140       return v_[i]; }
00141     virtual F operator()(uint i) const {
00142       conceptsAssert3(i < this->dim_, Assertion(), "i = " << i << ", n = " << this->dim_);
00143       return v_[i]; }
00144 
00145     int size() {
00146       return n();
00147     }
00148 
00150     virtual Function<F>& operator+=(const Function<F>& fnc);
00151     virtual Function<F>& operator+=(F c);
00152     Vector<F> operator+ (const Function<F>& fnc) const;
00153     Vector<F> operator+ (F c) const;
00154 
00156     virtual Function<F>& operator-=(const Function<F>& fnc);
00157     virtual Function<F>& operator-=(F c);
00158     Vector<F> operator- (const Function<F>& fnc) const;
00159     Vector<F> operator- (F c) const;
00160 
00162     virtual Function<F>& operator*=(F sc);
00163     Vector<F> operator* (F c) const;
00164 
00170     F operator*(const Vector<F>& fnc) const;
00171 
00173     template<class G>
00174     Vector<F>& assemble(const Space<G>& spc, LinearForm<F,G>& lf);
00175 
00178     Vector<F>& apply(F fnc(const F&));
00179 
00183     operator F*() const { return v_; }
00184     
00185     F* data() const { return v_; }
00186 
00187     virtual Function<F>& add(const Function<F>& fnc, F sc);
00188 
00190     template<class H>
00191     Vector<F>& add(const Vector<H>& fnc, F sc, uint offset = 0);
00192 
00194     template<class H>
00195     Vector<F>& add(const Vector<H>& fnc) { return add(fnc, F(1.0)); }
00196 
00198     Real l1() const;
00199 
00201     inline Real l2() const { return sqrt(l2_2()); }
00202 
00204     Real l2_2() const;
00205 
00207     Real max() const;
00208 
00210     uint n() const { return this->dim_; }
00211 
00215     const Vector<F>& write(const std::string& fname) const;
00217     void storeMatlab(const char* filename, const char* name = 0,
00218                      bool append = false) const;
00219   protected:
00220     virtual std::ostream& info(std::ostream& os) const;
00221 
00222     template<class G>
00223     void fillEntries_(const Space<G>& spc, LinearForm<F,G>& lf);
00225     Array<F> data_;
00226   private:
00228     F* v_;
00230     static uint storeMatlabCounter_;
00231   };
00232 
00233   template<class F>
00234   template<class H>
00235   Vector<F>::Vector(const Vector<H>& f) : 
00236     Function<F>(f.dim()), data_(this->dim_), v_((F*)data_) {
00237     memorycpy(v_, (H*)f, this->dim_);
00238   }
00239 
00240   template<class F>
00241   template<class H>
00242   Vector<F>::Vector(const Vector<H>& fncX, F fnc(const H&)) 
00243     : Function<F>(fncX.dim()), data_(this->dim_, 0), v_((F*)data_)
00244   {
00245     F* v = v_;
00246     H* w = (H*)fncX;
00247     for(uint i = this->dim_; i; --i) *v++= fnc(*w++);
00248   }
00249 
00250   template<class F>
00251   template<class G>
00252   Vector<F>::Vector(const Space<G>& spc, LinearForm<F,G>& lf) : 
00253     Function<F>(spc), data_(this->dim_), v_((F*)data_) {
00254     fillEntries_(spc, lf);
00255   }
00256 
00257   template<class F>
00258   template<class H>
00259   Vector<F>::Vector(const Vector<H>& fncX, const F& fnc(const H&))
00260     : Function<F>(fncX.dim()), data_(this->dim_, 0), v_((F*)data_)
00261   {
00262     F* v = v_;
00263     H* w = (H*)fncX;
00264     for(uint i = this->dim_; i; --i) *v++= fnc(*w++);
00265   }
00266 
00267   template<class F>
00268   template<class H>
00269   Vector<F>::Vector(const Vector<H>& fnc, const Set<IndexRange>& indices) :
00270     Function<F>(indices.dim()), data_(this->dim_), v_((F*)data_)
00271   {
00272     F* d = data_;
00273     for (Set<IndexRange>::index_iterator i = indices.index_begin();
00274          i != indices.index_end(); )
00275       *d++ = fnc(*i++);
00276   }
00277 
00278   template<class F>
00279   template<class H>
00280   Function<F>& Vector<F>::operator=(const Function<H>& fnc)
00281   {
00282     for(uint i = 0; i < this->dim_; ++i) v_[i] = fnc(i);
00283     return *this;
00284   }
00285 
00286 
00287   template<class F>
00288   template<class H>
00289   Vector<F>& Vector<F>::add(const Vector<H>& fnc, F sc, uint offset)
00290   {
00291     F* d = v_ + offset;
00292     const H* s = (H*)fnc;
00293     for (uint i = std::min(this->dim_-offset,fnc.dim()); i; --i) *d++ += sc * *s++;
00294     return *this;
00295   }
00296 
00297 } // namespace concepts
00298 
00299 #endif // vector_hh

Home | Doxygen Documentation | Tutorials | Developer Tools (restricted)