dynamic.h

Go to the documentation of this file.
00001 /* -*- C++ -*- ------------------------------------------------------------
00002  
00003 Copyright (c) 2007 Jesse Anders and Demian Nave http://cmldev.net/
00004 
00005 The Configurable Math Library (CML) is distributed under the terms of the
00006 Boost Software License, v1.0 (see cml/LICENSE for details).
00007 
00008  *-----------------------------------------------------------------------*/
00013 #ifndef dynamic_matrix_h
00014 #define dynamic_matrix_h
00015 
00016 #include <cml/core/dynamic_2D.h>
00017 #include <cml/matrix/matrix_expr.h>
00018 #include <cml/matrix/class_ops.h>
00019 #include <cml/matrix/matrix_unroller.h>
00020 
00021 namespace cml {
00022 
00024 template<typename Element, typename Alloc,
00025     typename BasisOrient, typename Layout>
00026 class matrix<Element,dynamic<Alloc>,BasisOrient,Layout>
00027 : public dynamic_2D<Element,Layout,Alloc>
00028 {
00029   public:
00030 
00031     /* Shorthand for the generator: */
00032     typedef dynamic<Alloc> generator_type;
00033 
00034     /* Shorthand for the array type: */
00035     typedef dynamic_2D<Element,Layout,Alloc> array_type;
00036 
00037     /* Shorthand for the type of this matrix: */
00038     typedef matrix<Element,generator_type,BasisOrient,Layout> matrix_type;
00039 
00040     /* For integration into the expression template code: */
00041     typedef matrix_type expr_type;
00042 
00043     /* For integration into the expression template code: */
00044     typedef matrix_type temporary_type;
00045 
00046     /* Standard: */
00047     typedef typename array_type::value_type value_type;
00048     typedef typename array_type::reference reference;
00049     typedef typename array_type::const_reference const_reference;
00050 
00051     /* For integration into the expression templates code: */
00052     typedef matrix_type& expr_reference;
00053     typedef const matrix_type& expr_const_reference;
00054 
00055     /* For matching by basis: */
00056     typedef BasisOrient basis_orient;
00057 
00058     /* For matching by memory layout: */
00059     typedef typename array_type::layout layout;
00060 
00061     /* For matching by storage type: */
00062     typedef typename array_type::memory_tag memory_tag;
00063 
00064     /* For matching by size type if necessary: */
00065     typedef typename array_type::size_tag size_tag;
00066 
00067     /* For matching by resizability: */
00068     typedef typename array_type::resizing_tag resizing_tag;
00069 
00070     /* For matching by result type: */
00071     typedef cml::et::matrix_result_tag result_tag;
00072 
00073     /* For matching by assignability: */
00074     typedef cml::et::assignable_tag assignable_tag;
00075 
00076     /* To simplify the matrix transpose operator: */
00077     typedef matrix<
00078         Element,
00079         typename array_type::transposed_type::generator_type,
00080         BasisOrient,
00081         Layout
00082     > transposed_type;
00083 
00084     /* To simplify the matrix row and column operators: */
00085     typedef vector<
00086         Element,
00087         typename array_type::row_array_type::generator_type
00088     > row_vector_type;
00089 
00090     typedef vector<
00091         Element,
00092         typename array_type::col_array_type::generator_type
00093     > col_vector_type;
00094 
00095 
00096   public:
00097 
00099     matrix_type& zero() {
00100         typedef cml::et::OpAssign<Element,Element> OpT;
00101         cml::et::UnrollAssignment<OpT>(*this,Element(0));
00102         return *this;
00103     }
00104 
00110     matrix_type& identity() {
00111         for(size_t i = 0; i < this->rows(); ++ i) {
00112             for(size_t j = 0; j < this->cols(); ++ j) {
00113                 (*this)(i,j) = value_type((i == j)?1:0);
00114             }
00115         }
00116         return *this;
00117     }
00118 
00124     matrix_type& transpose() {
00125         /* transpose() returns a temporary: */
00126         *this = cml::transpose(*this);
00127         return *this;
00128     }
00129 
00135     matrix_type& inverse() {
00136         /* inverse() returns a temporary: */
00137         *this = cml::inverse(*this);
00138         return *this;
00139     }
00140 
00141     /* NOTE: minimize() and maximize() no longer supported (Jesse) */
00142 
00143     #if 0
00144 
00145     template<typename E, class AT, typename L>
00146     void minimize(const matrix<E,AT,basis_orient,L>& v) {
00147       /* XXX This should probably use ScalarPromote: */
00148       for (size_t i = 0; i < this->rows(); ++i) {
00149         for (size_t j = 0; j < this->cols(); ++j) {
00150           (*this)(i,j) = std::min((*this)(i,j),v(i,j));
00151         }
00152       }
00153     }
00154 
00156     template<typename E, class AT, typename L>
00157     void maximize(const matrix<E,AT,basis_orient,L>& v) {
00158       /* XXX This should probably use ScalarPromote: */
00159       for (size_t i = 0; i < this->rows(); ++i) {
00160         for (size_t j = 0; j < this->cols(); ++j) {
00161           (*this)(i,j) = std::max((*this)(i,j),v(i,j));
00162         }
00163       }
00164     }
00165     #endif
00166 
00167     /* Set each element to a random number in the range [min,max] */
00168     void random(ELEMENT_ARG_TYPE min, ELEMENT_ARG_TYPE max) {
00169       for(size_t i = 0; i < this->rows(); ++i) {
00170         for(size_t j = 0; j < this->cols(); ++j) {
00171           (*this)(i,j) = cml::random_real(min,max);
00172         }
00173       }
00174     }
00175 
00176 
00177   public:
00178 
00180     matrix() {}
00181 
00187     explicit matrix(size_t rows, size_t cols)
00188         : array_type(rows,cols) {}
00189 
00190 
00191   public:
00192 
00194     matrix_size size() const {
00195         return matrix_size(this->rows(),this->cols());
00196     }
00197 
00199     value_type basis_element(size_t i, size_t j) const {
00200         return basis_element(i,j,basis_orient());
00201     }
00202 
00204     void set_basis_element(size_t i, size_t j, ELEMENT_ARG_TYPE s) {
00205         set_basis_element(i,j,s,basis_orient());
00206     }
00207 
00209     void set_row(size_t i, const row_vector_type& row) {
00210       for(size_t j = 0; j < this->cols(); ++ j) (*this)(i,j) = row[j];
00211     }
00212 
00214     void set_col(size_t j, const col_vector_type& col) {
00215       for(size_t i = 0; i < this->rows(); ++ i) (*this)(i,j) = col[i];
00216     }
00217 
00218 
00219   public:
00220 
00221     /* Define common class operators: */
00222 
00223     CML_CONSTRUCT_MAT_22
00224     CML_CONSTRUCT_MAT_33
00225     CML_CONSTRUCT_MAT_44
00226 
00227     CML_MAT_COPY_FROM_ARRAY(: array_type())
00228     CML_MAT_COPY_FROM_MATTYPE
00229     CML_MAT_COPY_FROM_MAT
00230     CML_MAT_COPY_FROM_MATXPR
00231 
00232     CML_ASSIGN_MAT_22
00233     CML_ASSIGN_MAT_33
00234     CML_ASSIGN_MAT_44
00235 
00236     CML_MAT_ASSIGN_FROM_MATTYPE
00237 
00238     CML_MAT_ASSIGN_FROM_MAT(=, et::OpAssign)
00239     CML_MAT_ASSIGN_FROM_MAT(+=, et::OpAddAssign)
00240     CML_MAT_ASSIGN_FROM_MAT(-=, et::OpSubAssign)
00241 
00242     CML_MAT_ASSIGN_FROM_MATXPR(=, et::OpAssign)
00243     CML_MAT_ASSIGN_FROM_MATXPR(+=, et::OpAddAssign)
00244     CML_MAT_ASSIGN_FROM_MATXPR(-=, et::OpSubAssign)
00245 
00246     CML_MAT_ASSIGN_FROM_SCALAR(*=, et::OpMulAssign)
00247     CML_MAT_ASSIGN_FROM_SCALAR(/=, et::OpDivAssign)
00248 
00254     matrix_type& operator*=(const matrix_type& m) {
00255         /* Matrix multiplication returns a temporary: */
00256         *this = (*this)*m;
00257         return *this;
00258     }
00259 
00265     template<typename E, class AT, typename BO, typename L> matrix_type&
00266     operator*=(const matrix<E,AT,BO,L>& m) {
00267         /* Matrix multiplication returns a temporary: */
00268         *this = (*this)*m;
00269         return *this;
00270     }
00271 
00277     template<class XprT> matrix_type&
00278     operator*=(MATXPR_ARG_TYPE e) {
00279         /* Verify that a promotion exists at compile time: */
00280         typedef typename et::MatrixPromote<
00281             matrix_type, typename XprT::result_type>::type result_type;
00282         /* Matrix multiplication returns a temporary: */
00283         *this = (*this)*e;
00284         return *this;
00285     }
00286 
00287 
00288   protected:
00289 
00290     value_type basis_element(size_t i, size_t j, row_basis) const {
00291         return (*this)(i,j);
00292     }
00293 
00294     value_type basis_element(size_t i, size_t j, col_basis) const {
00295         return (*this)(j,i);
00296     }
00297 
00298     void set_basis_element(size_t i, size_t j, ELEMENT_ARG_TYPE s, row_basis) {
00299         (*this)(i,j) = s;
00300     }
00301 
00302     void set_basis_element(size_t i, size_t j, ELEMENT_ARG_TYPE s, col_basis) {
00303         (*this)(j,i) = s;
00304     }
00305 
00306 
00307   public:
00308 
00309     /* Braces should only be used for testing: */
00310 #if defined(CML_ENABLE_MATRIX_BRACES)
00311     CML_MATRIX_BRACE_OPERATORS
00312 #endif
00313 };
00314 
00315 } // namespace cml
00316 
00317 #endif
00318 
00319 // -------------------------------------------------------------------------
00320 // vim:ft=cpp

Generated on Sat Jul 18 19:35:20 2009 for CML 1.0 by  doxygen 1.5.9