matrix_rowcol.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 matrix_rowcol_h
00014 #define matrix_rowcol_h
00015 
00016 #include <cml/vector/vector_expr.h>
00017 #include <cml/matrix/matrix_expr.h>
00018 
00019 namespace cml {
00020 namespace et {
00021 
00022 template<class ExprT>
00023 class MatrixRowOp
00024 {
00025   public:
00026 
00027     typedef MatrixRowOp<ExprT> expr_type;
00028 
00029     /* Record ary-ness of the expression: */
00030     typedef unary_expression expr_ary;
00031 
00032     /* Copy the expression by value into higher-up expressions: */
00033     typedef expr_type expr_const_reference;
00034 
00035     typedef typename ExprT::value_type value_type;
00036     typedef vector_result_tag result_tag;
00037     typedef typename ExprT::size_tag size_tag;
00038 
00039     /* Store the expression traits: */
00040     typedef ExprTraits<ExprT> expr_traits;
00041 
00042     /* Get the reference type: */
00043     typedef typename expr_traits::const_reference expr_reference;
00044 
00045     /* Get the result vector type: */
00046     typedef typename expr_traits::result_type::row_vector_type result_type;
00047 
00048     /* Get the temporary type: */
00049     typedef typename result_type::temporary_type temporary_type;
00050 
00051 
00052   public:
00053 
00055     enum { array_size = result_type::array_size };
00056 
00057 
00058   public:
00059 
00061     matrix_size size() const {
00062         return expr_traits().rows(m_expr);
00063     }
00064 
00066     expr_reference expression() const { return m_expr; }
00067 
00069     value_type operator[](size_t i) const {
00070         return expr_traits().get(m_expr,m_row,i);
00071     }
00072 
00073 
00074   public:
00075 
00077     explicit MatrixRowOp(const ExprT& expr, size_t row)
00078         : m_expr(expr), m_row(row) {}
00079 
00081     MatrixRowOp(const expr_type& e)
00082         : m_expr(e.m_expr), m_row(e.m_row) {}
00083 
00084 
00085   protected:
00086 
00087     expr_reference m_expr;
00088     const size_t m_row;
00089 
00090 
00091   private:
00092 
00093     /* Cannot be assigned to: */
00094     expr_type& operator=(const expr_type&);
00095 };
00096 
00098 template<class ExprT>
00099 struct ExprTraits< MatrixRowOp<ExprT> >
00100 {
00101     typedef MatrixRowOp<ExprT> expr_type;
00102     typedef ExprT arg_type;
00103 
00104     typedef typename expr_type::value_type value_type;
00105     typedef typename expr_type::expr_const_reference const_reference;
00106     typedef typename expr_type::result_tag result_tag;
00107     typedef typename expr_type::size_tag size_tag;
00108     typedef typename expr_type::result_type result_type;
00109     typedef expr_node_tag node_tag;
00110 
00111     value_type get(const expr_type& v, size_t i) const { return v[i]; }
00112     size_t size(const expr_type& e) const { return e.size(); }
00113 };
00114 
00115 template<class ExprT>
00116 class MatrixColOp
00117 {
00118   public:
00119 
00120     typedef MatrixColOp<ExprT> expr_type;
00121 
00122     /* Record ary-ness of the expression: */
00123     typedef unary_expression expr_ary;
00124 
00125     /* Copy the expression by value into higher-up expressions: */
00126     typedef expr_type expr_const_reference;
00127 
00128     typedef typename ExprT::value_type value_type;
00129     typedef vector_result_tag result_tag;
00130     typedef typename ExprT::size_tag size_tag;
00131 
00132     /* Store the expression traits: */
00133     typedef ExprTraits<ExprT> expr_traits;
00134 
00135     /* Get the reference type: */
00136     typedef typename expr_traits::const_reference expr_reference;
00137 
00138     /* Get the result vector type: */
00139     typedef typename expr_traits::result_type::col_vector_type result_type;
00140 
00141     /* Get the temporary type: */
00142     typedef typename result_type::temporary_type temporary_type;
00143 
00144 
00145   public:
00146 
00148     enum { array_size = result_type::array_size };
00149 
00150 
00151   public:
00152 
00154     matrix_size size() const {
00155         return expr_traits().cols(m_expr);
00156     }
00157 
00159     expr_reference expression() const { return m_expr; }
00160 
00162     value_type operator[](size_t i) const {
00163         return expr_traits().get(m_expr,i,m_col);
00164     }
00165 
00166 
00167   public:
00168 
00170     explicit MatrixColOp(const ExprT& expr, size_t col)
00171         : m_expr(expr), m_col(col) {}
00172 
00174     MatrixColOp(const expr_type& e)
00175         : m_expr(e.m_expr), m_col(e.m_col) {}
00176 
00177 
00178   protected:
00179 
00180     expr_reference m_expr;
00181     const size_t m_col;
00182 
00183 
00184   private:
00185 
00186     /* Cannot be assigned to: */
00187     expr_type& operator=(const expr_type&);
00188 };
00189 
00191 template<class ExprT>
00192 struct ExprTraits< MatrixColOp<ExprT> >
00193 {
00194     typedef MatrixColOp<ExprT> expr_type;
00195     typedef ExprT arg_type;
00196 
00197     typedef typename expr_type::value_type value_type;
00198     typedef typename expr_type::expr_const_reference const_reference;
00199     typedef typename expr_type::result_tag result_tag;
00200     typedef typename expr_type::size_tag size_tag;
00201     typedef typename expr_type::result_type result_type;
00202     typedef expr_node_tag node_tag;
00203 
00204     value_type get(const expr_type& v, size_t i) const { return v[i]; }
00205     size_t size(const expr_type& e) const { return e.size(); }
00206 };
00207 
00208 } // namespace et
00209 
00210 /* Define the row and column operators in the cml namespace: */
00211 
00213 template<typename E, class AT, typename BO, typename L>
00214 et::VectorXpr< et::MatrixRowOp< matrix<E,AT,BO,L> > >
00215 row(const matrix<E,AT,BO,L>& expr, size_t i)
00216 {
00217     typedef et::MatrixRowOp< matrix<E,AT,BO,L> > ExprT;
00218     return et::VectorXpr<ExprT>(ExprT(expr,i));
00219 }
00220 
00226 template<class XprT>
00227 et::VectorXpr< et::MatrixRowOp<XprT> >
00228 row(const et::MatrixXpr<XprT>& expr, size_t i)
00229 {
00230     typedef et::MatrixRowOp<XprT> ExprT;
00231     return et::MatrixXpr<ExprT>(ExprT(expr.expression(),i));
00232 }
00233 
00235 template<typename E, class AT, typename BO, typename L>
00236 et::VectorXpr< et::MatrixColOp< matrix<E,AT,BO,L> > >
00237 col(const matrix<E,AT,BO,L>& expr, size_t i)
00238 {
00239     typedef et::MatrixColOp< matrix<E,AT,BO,L> > ExprT;
00240     return et::VectorXpr<ExprT>(ExprT(expr,i));
00241 }
00242 
00248 template<class XprT>
00249 et::VectorXpr< et::MatrixColOp<XprT> >
00250 col(const et::MatrixXpr<XprT>& expr, size_t i)
00251 {
00252     typedef et::MatrixColOp<XprT> ExprT;
00253     return et::VectorXpr<ExprT>(ExprT(expr.expression(),i));
00254 }
00255 
00256 } // namespace cml
00257 
00258 #endif
00259 
00260 // -------------------------------------------------------------------------
00261 // vim:ft=cpp

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