fixed.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 fixed_matrix_h
00014 #define fixed_matrix_h
00015 
00016 #include <cml/core/fixed_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, int Rows, int Cols,
00025     typename BasisOrient, typename Layout>
00026 class matrix<Element,fixed<Rows,Cols>,BasisOrient,Layout>
00027 : public fixed_2D<Element,Rows,Cols,Layout>
00028 {
00029   public:
00030 
00031     /* Shorthand for the generator: */
00032     typedef fixed<Rows,Cols> generator_type;
00033 
00034     /* Shorthand for the array type: */
00035     typedef fixed_2D<Element,Rows,Cols,Layout> 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     typedef matrix_type& expr_reference;
00052     typedef const matrix_type& expr_const_reference;
00053 
00054     /* For matching by basis: */
00055     typedef BasisOrient basis_orient;
00056 
00057     /* For matching by memory layout: */
00058     typedef typename array_type::layout layout;
00059 
00060     /* For matching by storage type if necessary: */
00061     typedef typename array_type::memory_tag memory_tag;
00062 
00063     /* For matching by size type if necessary: */
00064     typedef typename array_type::size_tag size_tag;
00065 
00066     /* For matching by result type: */
00067     typedef cml::et::matrix_result_tag result_tag;
00068 
00069     /* For matching by assignability: */
00070     typedef cml::et::assignable_tag assignable_tag;
00071 
00072     /* To simplify the matrix transpose operator: */
00073     typedef matrix<
00074         Element,
00075         typename array_type::transposed_type::generator_type,
00076         BasisOrient,
00077         Layout
00078     > transposed_type;
00079 
00080     /* To simplify the matrix row and column operators: */
00081     typedef vector<
00082         Element,
00083         typename array_type::row_array_type::generator_type
00084     > row_vector_type;
00085 
00086     typedef vector<
00087         Element,
00088         typename array_type::col_array_type::generator_type
00089     > col_vector_type;
00090 
00091 
00092   public:
00093 
00095     matrix_type& zero() {
00096         typedef cml::et::OpAssign<Element,Element> OpT;
00097         cml::et::UnrollAssignment<OpT>(*this,Element(0));
00098         return *this;
00099     }
00100 
00106     matrix_type& identity() {
00107         for(size_t i = 0; i < this->rows(); ++ i) {
00108             for(size_t j = 0; j < this->cols(); ++ j) {
00109                 (*this)(i,j) = value_type((i == j)?1:0);
00110             }
00111         }
00112         return *this;
00113     }
00114 
00120     matrix_type& transpose() {
00121         /* transpose() returns a temporary: */
00122         *this = cml::transpose(*this);
00123         return *this;
00124     }
00125 
00131     matrix_type& inverse() {
00132         /* inverse() returns a temporary: */
00133         *this = cml::inverse(*this);
00134         return *this;
00135     }
00136 
00137     /* NOTE: minimize() and maximize() no longer supported (Jesse) */
00138 
00139     #if 0
00140 
00141     template<typename E, class AT, typename L>
00142     void minimize(const matrix<E,AT,basis_orient,L>& v) {
00143       /* XXX This should probably use ScalarPromote: */
00144       for (size_t i = 0; i < this->rows(); ++i) {
00145         for (size_t j = 0; j < this->cols(); ++j) {
00146           (*this)(i,j) = std::min((*this)(i,j),v(i,j));
00147         }
00148       }
00149     }
00150 
00152     template<typename E, class AT, typename L>
00153     void maximize(const matrix<E,AT,basis_orient,L>& v) {
00154       /* XXX This should probably use ScalarPromote: */
00155       for (size_t i = 0; i < this->rows(); ++i) {
00156         for (size_t j = 0; j < this->cols(); ++j) {
00157           (*this)(i,j) = std::max((*this)(i,j),v(i,j));
00158         }
00159       }
00160     }
00161     #endif
00162 
00163     /* Set each element to a random number in the range [min,max] */
00164     void random(ELEMENT_ARG_TYPE min, ELEMENT_ARG_TYPE max) {
00165       for(size_t i = 0; i < this->rows(); ++i) {
00166         for(size_t j = 0; j < this->cols(); ++j) {
00167           (*this)(i,j) = cml::random_real(min,max);
00168         }
00169       }
00170     }
00171 
00172 
00173   public:
00174 
00181     matrix() {}
00182 
00183 
00184   public:
00185 
00187     matrix_size size() const {
00188         return matrix_size(this->rows(),this->cols());
00189     }
00190 
00192     value_type basis_element(size_t i, size_t j) const {
00193         return basis_element(i,j,basis_orient());
00194     }
00195 
00197     void set_basis_element(size_t i, size_t j, ELEMENT_ARG_TYPE s) {
00198         set_basis_element(i,j,s,basis_orient());
00199     }
00200 
00202     void set_row(size_t i, const row_vector_type& row) {
00203       for(size_t j = 0; j < this->cols(); ++ j) (*this)(i,j) = row[j];
00204     }
00205 
00207     void set_col(size_t j, const col_vector_type& col) {
00208       for(size_t i = 0; i < this->rows(); ++ i) (*this)(i,j) = col[i];
00209     }
00210 
00211 
00212   public:
00213 
00214     /* Define common class operators: */
00215 
00216     CML_CONSTRUCT_MAT_22
00217     CML_CONSTRUCT_MAT_33
00218     CML_CONSTRUCT_MAT_44
00219 
00220     CML_MAT_COPY_FROM_FIXED_ARRAY(
00221             array_type::array_rows, array_type::array_cols)
00222 
00223     CML_MAT_COPY_FROM_MATTYPE
00224     CML_MAT_COPY_FROM_MAT
00225     CML_MAT_COPY_FROM_MATXPR
00226 
00227     CML_ASSIGN_MAT_22
00228     CML_ASSIGN_MAT_33
00229     CML_ASSIGN_MAT_44
00230 
00231     CML_MAT_ASSIGN_FROM_MATTYPE
00232 
00233     CML_MAT_ASSIGN_FROM_MAT(=, et::OpAssign)
00234     CML_MAT_ASSIGN_FROM_MAT(+=, et::OpAddAssign)
00235     CML_MAT_ASSIGN_FROM_MAT(-=, et::OpSubAssign)
00236 
00237     CML_MAT_ASSIGN_FROM_MATXPR(=, et::OpAssign)
00238     CML_MAT_ASSIGN_FROM_MATXPR(+=, et::OpAddAssign)
00239     CML_MAT_ASSIGN_FROM_MATXPR(-=, et::OpSubAssign)
00240 
00241     CML_MAT_ASSIGN_FROM_SCALAR(*=, et::OpMulAssign)
00242     CML_MAT_ASSIGN_FROM_SCALAR(/=, et::OpDivAssign)
00243 
00244     CML_ACCUMULATED_MATRIX_MULT(const matrix_type&)
00245 
00246     template<typename E, class AT, typename BO, typename L>
00247         CML_ACCUMULATED_MATRIX_MULT(const TEMPLATED_MATRIX_MACRO&)
00248 
00249     template<class XprT>
00250         CML_ACCUMULATED_MATRIX_MULT(MATXPR_ARG_TYPE)
00251 
00252 
00253   protected:
00254 
00255     value_type basis_element(size_t i, size_t j, row_basis) const {
00256         return (*this)(i,j);
00257     }
00258 
00259     value_type basis_element(size_t i, size_t j, col_basis) const {
00260         return (*this)(j,i);
00261     }
00262 
00263     void set_basis_element(size_t i, size_t j, ELEMENT_ARG_TYPE s, row_basis) {
00264         (*this)(i,j) = s;
00265     }
00266 
00267     void set_basis_element(size_t i, size_t j, ELEMENT_ARG_TYPE s, col_basis) {
00268         (*this)(j,i) = s;
00269     }
00270 
00271 
00272   public:
00273 
00274     /* Braces should only be used for testing: */
00275 #if defined(CML_ENABLE_MATRIX_BRACES)
00276     CML_MATRIX_BRACE_OPERATORS
00277 #endif
00278 };
00279 
00280 } // namespace cml
00281 
00282 #endif
00283 
00284 // -------------------------------------------------------------------------
00285 // vim:ft=cpp

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