square_matrix.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 square_matrix_h
00023 #define square_matrix_h
00024 
00025 #include "general_matrix.h"
00026 
00027 namespace fmatvec {
00028 
00038   template <class AT> class SquareMatrix<Ref,AT> : public Matrix<General,Ref,Ref,AT> {
00039 
00040     using Matrix<General,Ref,Ref,AT>::m;
00041     using Matrix<General,Ref,Ref,AT>::n;
00042     using Matrix<General,Ref,Ref,AT>::lda;
00043     using Matrix<General,Ref,Ref,AT>::ele;
00044     using Matrix<General,Ref,Ref,AT>::tp;
00045     using Matrix<General,Ref,Ref,AT>::memory;
00046     using Matrix<General,Ref,Ref,AT>::elePtr;
00047 
00048     public:
00049 
00051 
00052     template <class T> friend SquareMatrix<Ref,T> trans(const SquareMatrix<Ref,T> &A);
00053 
00054     friend const SquareMatrix<Ref,AT> Matrix<General,Ref,Ref,AT>::operator()(const Index &I) const;
00055     friend SquareMatrix<Ref,AT> Matrix<General,Ref,Ref,AT>::operator()(const Index &I);
00056 
00057     protected:
00058 
00059     SquareMatrix(int n, int lda, int tp, Memory<AT> memory, const AT* ele) : Matrix<General,Ref,Ref,AT>(n, n, lda, tp, memory, ele) {
00060     }
00061 
00063 
00064     public:
00065 
00070       SquareMatrix() : Matrix<General,Ref,Ref,AT>() { }
00071 
00072 //      template<class Ini=All<AT> >
00073 //      SquareMatrix(int m, Ini ini=All<AT>()) : Matrix<General,Ref,Ref,AT>(m,m,ini) { } 
00074 
00075       SquareMatrix(int m, Noinit ini) : Matrix<General,Ref,Ref,AT>(m,m,ini) { } 
00076       SquareMatrix(int m, Init ini=INIT, const AT &a=0) : Matrix<General,Ref,Ref,AT>(m,m,ini,a) { } 
00077       SquareMatrix(int m, Eye ini, const AT &a=1) : Matrix<General,Ref,Ref,AT>(m,m,ini,a) { } 
00078 
00086       SquareMatrix(int m, AT* ele) : Matrix<General,Ref,Ref,AT>(m,m,ele) { }
00087 
00088       SquareMatrix(const char *str) : Matrix<General,Ref,Ref,AT>(str) { }
00089 
00097       SquareMatrix(const SquareMatrix<Ref,AT>&  A) : Matrix<General,Ref,Ref,AT>(A) { }
00098 
00103       explicit SquareMatrix(const Matrix<General,Ref,Ref,AT>&  A) : Matrix<General,Ref,Ref,AT>(A) {
00104 #ifndef FMATVEC_NO_SIZE_CHECK
00105         assert(A.rows() == A.cols());
00106 #endif
00107       }
00108 
00109       template<class Type, class Row, class Col>
00110         explicit SquareMatrix(const Matrix<Type,Row,Col,AT> &x) : Matrix<General,Ref,Ref,AT>(x)  {
00111         }
00112      
00113       SquareMatrix<Ref,AT>& resize() {
00114         Matrix<General,Ref,Ref,AT>::resize();
00115         return *this;
00116       }
00117 
00118       SquareMatrix<Ref,AT>& resize(int m, Noinit) {
00119         Matrix<General,Ref,Ref,AT>::resize(m,m,Noinit());
00120         return *this;
00121       }
00122 
00123       SquareMatrix<Ref,AT>& resize(int m, Init ini=INIT, const AT &a=0) {
00124         Matrix<General,Ref,Ref,AT>::resize(m,m,ini,a);
00125         return *this;
00126       }
00127 
00128       SquareMatrix<Ref,AT>& resize(int m, Eye ini, const AT &a=1) {
00129         Matrix<General,Ref,Ref,AT>::resize(m,m,ini,a);
00130         return *this;
00131       }
00132 
00141       SquareMatrix<Ref,AT>& operator=(const SquareMatrix<Ref,AT>&  A) {
00142         Matrix<General,Ref,Ref,AT>::operator=(A);
00143         return *this;
00144       }
00145 
00152       template<class Type, class Row, class Col> SquareMatrix<Ref,AT>& operator<<(const Matrix<Type,Row,Col,AT> &A) {
00153 #ifndef FMATVEC_NO_SIZE_CHECK
00154         assert(A.rows() == A.cols());
00155 #endif
00156         Matrix<General,Ref,Ref,AT>::operator<<(A);
00157         return *this;
00158       }
00159 
00166       SquareMatrix<Ref,AT>& operator>>(const SquareMatrix<Ref,AT>&  A) {
00167         Matrix<General,Ref,Ref,AT>::operator>>(A);
00168         return *this;
00169       }
00170 
00171       SquareMatrix<Ref,AT>& operator>>(const Matrix<General,Ref,Ref,AT>&  A) {
00172 #ifndef FMATVEC_NO_SIZE_CHECK
00173         assert(A.rows() == A.cols());
00174 #endif
00175         Matrix<General,Ref,Ref,AT>::operator>>(A);
00176         return *this;
00177       }
00178 
00183       int size() const {return m;};
00184 
00190       inline SquareMatrix<Ref,AT> copy() const;
00191 
00192       using Matrix<General,Ref,Ref,AT>::operator();
00193 
00198       inline operator std::vector<std::vector<AT> >();
00199 
00200       SquareMatrix<Ref,AT> T() {
00201         return SquareMatrix<Ref,AT>(n, lda, tp?false:true, memory, ele);
00202       }
00203 
00204       const SquareMatrix<Ref,AT> T() const {
00205         return SquareMatrix<Ref,AT>(n, lda, tp?false:true, memory, ele);
00206       }
00207   };
00208 
00209   template <class AT>
00210     inline SquareMatrix<Ref,AT> SquareMatrix<Ref,AT>::copy() const {
00211 
00212       SquareMatrix<Ref,AT> A(m,NONINIT);
00213       A.deepCopy(*this);
00214 
00215       return A;
00216     }
00217 
00218   template <class AT>
00219     inline SquareMatrix<Ref,AT>::operator std::vector<std::vector<AT> >() {
00220       std::vector<std::vector<AT> > ret(size());
00221       if(tp) {
00222         for(int r=0; r<size(); r++) {
00223           ret[r].resize(size());
00224           for(int c=0; c<size(); c++)
00225             ret[r][c]= this->et(r,c); 
00226         }
00227       }
00228       else {
00229         for(int r=0; r<size(); r++) {
00230           ret[r].resize(size());
00231           for(int c=0; c<size(); c++)
00232             ret[r][c]= this->er(r,c); 
00233         }
00234       }
00235       return ret;
00236     }
00237 
00238 }
00239 
00240 #endif