row_vector.h
00001 /* Copyright (C) 2003-2005  Martin Förg
00002 
00003  * This library is free software; you can redistribute it and/or
00004  * modify it under the terms of the GNU Lesser General Public
00005  * License as published by the Free Software Foundation; either
00006  * version 2.1 of the License, or (at your option) any later version.
00007  * 
00008  * This library is distributed in the hope that it will be useful,
00009  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00010  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011  * Lesser General Public License for more details.
00012  * 
00013  * You should have received a copy of the GNU Lesser General Public
00014  * License along with this library; if not, write to the Free Software
00015  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00016  *
00017  * Contact:
00018  *   martin.o.foerg@googlemail.com
00019  *
00020  */
00021 
00022 #ifndef row_vector_h
00023 #define row_vector_h
00024 
00025 #include "general_matrix.h"
00026 
00027 namespace fmatvec {
00036   template <class AT> class RowVector<Ref,AT> : public Matrix<General,Ref,Ref,AT> {
00037     using Matrix<General,Ref,Ref,AT>::m;
00038     using Matrix<General,Ref,Ref,AT>::n;
00039     using Matrix<General,Ref,Ref,AT>::lda;
00040     using Matrix<General,Ref,Ref,AT>::ele;
00041     using Matrix<General,Ref,Ref,AT>::tp;
00042     using Matrix<General,Ref,Ref,AT>::memory;
00043     using Matrix<General,Ref,Ref,AT>::elePtr;
00044 
00045     public:
00046 
00048     
00049     template <class T> friend Vector<Ref,T> trans(const RowVector<Ref,T> &x); 
00050     template <class T> friend RowVector<Ref,T> trans(const Vector<Ref,T> &x);
00051     //friend RowVector<Ref,double> trans(const Vector<Ref,double> &x);
00052 
00053     friend class Vector<Ref,AT>;
00054 
00055     friend RowVector<Ref,AT> Matrix<General,Ref,Ref,AT>::row(int i);
00056     friend const RowVector<Ref,AT> Matrix<General,Ref,Ref,AT>::row(int i) const;
00057 
00058     protected:
00059 
00060     inline void deepCopy(const RowVector<Ref,AT> &x);
00061     template<class Col> inline void deepCopy(const RowVector<Col,AT> &x);
00062 
00063     const AT* elePtr(int i) const {
00064       return tp ? ele+i : ele+lda*i;
00065     };
00066 
00067     AT* elePtr(int i) {
00068       return tp ? ele+i : ele+lda*i;
00069     };
00070 
00071     RowVector(int n_, int lda_, bool tp, Memory<AT> memory, const AT* ele_) : Matrix<General,Ref,Ref,AT>(1,n_, lda_, tp, memory, ele_) {
00072     }
00073 
00075 
00076     public:
00077 
00082       RowVector() : Matrix<General,Ref,Ref,AT>() { m=1; }
00083 
00084 //      template<class Ini=All<AT> >
00085 //        RowVector(int n, Ini ini=All<AT>()) : Matrix<General,Ref,Ref,AT>(1,n,ini) { } 
00086 
00087       RowVector(int n, Noinit ini) : Matrix<General,Ref,Ref,AT>(1,n,ini) { } 
00088       RowVector(int n, Init ini=INIT, const AT &a=0) : Matrix<General,Ref,Ref,AT>(1,n,ini,a) { } 
00089 
00096       RowVector(int n, AT* ele) : Matrix<General,Ref,Ref,AT>(1,n,ele) {
00097       }
00098 
00111       RowVector(const char *str) : Matrix<General,Ref,Ref,AT>(str) {
00112 #ifndef FMATVEC_NO_SIZE_CHECK
00113         assert(m==1);
00114 #endif
00115       }
00116 
00121       explicit RowVector(const Matrix<General,Ref,Ref,AT> &x) : Matrix<General,Ref,Ref,AT>(x) {
00122         LogicAssert(x.rows()==1,"Number of cols() must be 1 in RowVector<Ref,AT>::RowVector(const Matrix<General,Ref,Ref,AT> &x)");
00123       }
00124 
00125       RowVector<Ref,AT>& resize() {
00126         Matrix<General,Ref,Ref,AT>::resize();
00127         return *this;
00128       }
00129 
00130       RowVector<Ref,AT>& resize(int n, Noinit) {
00131         Matrix<General,Ref,Ref,AT>::resize(1,n,Noinit());
00132         return *this;
00133       }
00134 
00135       RowVector<Ref,AT>& resize(int n, Init ini=INIT, const AT &a=0) {
00136         Matrix<General,Ref,Ref,AT>::resize(1,n,ini,a);
00137         return *this;
00138       }
00139       
00147       RowVector(const RowVector<Ref,AT> &x) : Matrix<General,Ref,Ref,AT>(x) {
00148       }
00149 
00156       inline RowVector<Ref,AT>& operator<<(const RowVector<Ref,AT> &x);
00157 
00164       inline RowVector<Ref,AT>& operator>>(const RowVector<Ref,AT> &x);
00165 
00174       inline RowVector<Ref,AT>& operator=(const RowVector<Ref,AT> &x);
00175 
00176       template <class Row>
00177       inline RowVector<Ref,AT>& operator=(const RowVector<Row,AT> &x);
00178 
00189       AT& operator()(int i) {
00190 #ifndef FMATVEC_NO_BOUNDS_CHECK
00191         assert(i>=0);
00192         assert(i<n);
00193 #endif
00194         return e(i);
00195       };
00196 
00201       const AT& operator()(int i) const {
00202 #ifndef FMATVEC_NO_BOUNDS_CHECK
00203         assert(i>=0);
00204         assert(i<n);
00205 #endif
00206         return e(i);
00207       };
00208 
00209       AT& er(int i) {
00210         return ele[i*lda];
00211       };
00212 
00213       const AT& er(int i) const {
00214         return ele[i*lda];
00215       };
00216 
00217       AT& et(int i) {
00218         return ele[i];
00219       };
00220 
00221       const AT& et(int i) const {
00222         return ele[i];
00223       };
00224 
00225        AT& e(int i) {
00226         return tp ? et(i) : er(i);
00227       };
00228 
00229       const AT& e(int i) const {
00230         return tp ? et(i) : er(i);
00231       };
00232 
00240       inline RowVector<Ref,AT>& init(const AT& a); 
00241       inline RowVector<Ref,AT>& init(Init, const AT &a=0) { return init(a); }
00242       inline RowVector<Ref,AT>& init(Noinit, const AT &a=0) { return *this; }
00243 
00248       int size() const {return n;};
00249 
00256       int inc() const {return tp?1:lda;};
00257 
00263       inline RowVector<Ref,AT> copy() const;
00264 
00274       inline RowVector<Ref,AT> operator()(int i1, int i2);
00275 
00280       inline const RowVector<Ref,AT> operator()(int i1, int i2) const;
00281 
00290       inline RowVector<Ref,AT> operator()(const Index &I);
00291 
00296       inline const RowVector<Ref,AT> operator()(const Index &I) const;
00297 
00298       using Matrix<General,Ref,Ref,AT>::operator();
00299 
00300       Vector<Ref,AT> T() {
00301         return Vector<Ref,AT>(n,lda,tp?false:true,memory,ele);
00302       }
00303 
00304       const Vector<Ref,AT> T() const {
00305         return Vector<Ref,AT>(n,lda,tp?false:true,memory,ele);
00306       }
00307   };
00308 
00310 
00311 
00312   template <class AT>
00313     inline RowVector<Ref,AT>& RowVector<Ref,AT>::init(const AT &val) {
00314 
00315       if(tp) {
00316         for(int i=0; i<n; i++) 
00317           ele[i] = val; 
00318       }
00319       else {
00320         for(int i=0; i<n; i++) 
00321           ele[i*lda] = val;
00322       }
00323 
00324       return *this;
00325     }
00326 
00327   template <class AT>
00328     inline RowVector<Ref,AT>& RowVector<Ref,AT>::operator=(const RowVector<Ref,AT> &x) { 
00329 
00330       if(!ele) {
00331         m = 1;
00332         n = x.size();
00333         lda = m;
00334         tp = false;
00335         memory.resize(m);
00336         ele = (AT*)memory.get();
00337       } else {
00338 #ifndef FMATVEC_NO_SIZE_CHECK
00339         assert(n == x.size());
00340 #endif
00341       }
00342 
00343       deepCopy(x);
00344 
00345       return *this;
00346     }
00347 
00348   template <class AT> template<class Row>
00349     inline RowVector<Ref,AT>& RowVector<Ref,AT>::operator=(const RowVector<Row,AT> &x) { 
00350 
00351       if(!ele) {
00352         m = 1;
00353         n = x.size();
00354         lda = m;
00355         tp = false;
00356         memory.resize(m);
00357         ele = (AT*)memory.get();
00358       } else {
00359 #ifndef FMATVEC_NO_SIZE_CHECK
00360         assert(n == x.size());
00361 #endif
00362       }
00363 
00364       deepCopy(x);
00365 
00366       return *this;
00367     }
00368 
00369   template <class AT>
00370     inline RowVector<Ref,AT>& RowVector<Ref,AT>::operator<<(const RowVector<Ref,AT> &x) { 
00371 
00372       if(n!=x.size()) {
00373         m = 1; 
00374         n = x.size();
00375         lda = m;
00376         tp = false;
00377         memory.resize(n);
00378         ele = (AT*)memory.get();
00379       } 
00380 
00381       deepCopy(x);
00382 
00383       return *this;
00384     }
00385 
00386   template <class AT>
00387     inline RowVector<Ref,AT>& RowVector<Ref,AT>::operator>>(const RowVector<Ref,AT> &x) { 
00388 
00389       m = 1;
00390       n = x.n;
00391       memory = x.memory;
00392       ele = x.ele;
00393       lda = x.lda;
00394       tp = x.tp; 
00395 
00396       return *this;
00397     }
00398 
00399   template <class AT>
00400     inline RowVector<Ref,AT> RowVector<Ref,AT>::copy() const {
00401 
00402       RowVector<Ref,AT> x(n,NONINIT);
00403       x.deepCopy(*this);
00404 
00405       return x;
00406     }
00407 
00408   template <class AT> 
00409     inline RowVector<Ref,AT> RowVector<Ref,AT>::operator()(int i1, int i2) {
00410       return operator()(Index(i1,i2));
00411     }
00412 
00413   template <class AT> 
00414     inline const RowVector<Ref,AT> RowVector<Ref,AT>::operator()(int i1, int i2) const {
00415       return operator()(Index(i1,i2));
00416     }
00417 
00418   template <class AT>
00419     inline const RowVector<Ref,AT> RowVector<Ref,AT>::operator()(const Index &I) const {
00420 
00421 #ifndef FMATVEC_NO_BOUNDS_CHECK
00422       assert(I.end()<n);
00423 #endif
00424 
00425       return RowVector<Ref,AT>(I.end()-I.start()+1,lda,tp,memory,elePtr(I.start()));
00426     }
00427 
00428   template <class AT>
00429     inline RowVector<Ref,AT> RowVector<Ref,AT>::operator()(const Index &I) {
00430 
00431 #ifndef FMATVEC_NO_BOUNDS_CHECK
00432       assert(I.end()<n);
00433 #endif
00434 
00435       return RowVector<Ref,AT>(I.end()-I.start()+1,lda,tp,memory,elePtr(I.start()));
00436     }
00437 
00439 
00440   template <class AT>
00441     inline void RowVector<Ref,AT>::deepCopy(const RowVector<Ref,AT> &x) {
00442       if(tp) {
00443         if(x.tp) 
00444           for(int i=0; i<size(); i++)
00445             et(i) = x.et(i); 
00446         else 
00447           for(int i=0; i<size(); i++)
00448             et(i) = x.er(i);
00449       }
00450       else {
00451         if(x.tp)
00452           for(int i=0; i<size(); i++)
00453             er(i) = x.et(i); 
00454         else 
00455           for(int i=0; i<size(); i++)
00456             er(i) = x.er(i);
00457       }
00458     }
00459 
00460   template <class AT> template <class Col>
00461     inline void RowVector<Ref,AT>::deepCopy(const RowVector<Col,AT> &x) {
00462       if(tp) 
00463         for(int i=0; i<size(); i++)
00464           et(i) = x.e(i);
00465       else 
00466         for(int i=0; i<size(); i++)
00467           er(i) = x.e(i);
00468     }
00469 
00471 
00472 }
00473 
00474 #endif