[quan] / quan-trunk / quan_matters / examples / fusion / matrix_mux6.cpp
(logo)

View of /quan-trunk/quan_matters/examples/fusion/matrix_mux6.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (download) (annotate)
Mon Jan 15 22:48:14 2007 UTC (19 months, 2 weeks ago) by kwikius
Branch: MAIN
CVS Tags: HEAD
Changes since 1.2: +0 -0 lines
FILE REMOVED
cleaning
    1 
    2 // Copyright Andrew Little 2005
    3 //
    4 // Distributed under the Boost Software License, Version 1.0.
    5 // (See accompanying file LICENSE_1_0.txt or copy at
    6 // http://www.boost.org/LICENSE_1_0.txt)
    7 //
    8 // See QUAN_ROOT/quan_matters/index.html for documentation.
    9 
   10 /*
   11     in place matrix multiply
   12 */
   13 
   14 #define FUSION_MAX_VECTOR_SIZE 20
   15 #include <quan/fusion/boost/matrix_column_sequence.hpp>
   16 #include <quan/fusion/boost/matrix_row_sequence.hpp>
   17 #include <quan/fusion/dot_product.hpp>
   18 #include <quan/matrix/rc_matrix_def.hpp>
   19 #include <quan/static.hpp>
   20 #include <quan/matrix/out/matrix.hpp>
   21 #include <cassert>
   22 
   23 
   24 template <typename RhsSequence>
   25 struct push_back_dot_product{
   26 
   27     push_back_dot_product(RhsSequence const & rs):m_rhs_sequence(rs){}
   28     RhsSequence const & m_rhs_sequence;
   29 
   30     template <typename LhsSequence,typename InputSequence>
   31     struct result{
   32 
   33         typedef typename boost::fusion::result_of::push_back<
   34             InputSequence const ,
   35             typename quan::fusion::dot_product::result<
   36                 LhsSequence,RhsSequence
   37             >::type
   38         >::type type;
   39     };
   40 
   41     template <typename LhsSequence,typename InputSequence>
   42     typename result<LhsSequence ,InputSequence  >::type
   43     operator()(LhsSequence const & lhs, InputSequence const & input_seq)const
   44     {
   45         quan::fusion::dot_product dot;
   46         return boost::fusion::push_back(
   47             input_seq,
   48             dot(lhs,m_rhs_sequence)
   49         );
   50     }
   51 };
   52 
   53 template <typename SeqOfSeq>
   54 struct fold_seq_of_seq_by_seq{
   55 
   56    /* fold_seq_of_seq_by_seq(SeqOfSeq const & in):m_seq_of_seq(in){}
   57     SeqOfSeq const & m_seq_of_seq;*/
   58 
   59     template <typename Sequence, typename InputSequence>
   60     struct result{
   61         typedef typename boost::fusion::result_of::fold<
   62             SeqOfSeq,
   63             InputSequence,
   64             push_back_dot_product<Sequence>
   65         >::type type;
   66     };
   67 
   68   /*  template <typename Sequence, typename InputSequence>
   69     typename result<Sequence ,InputSequence const>::type
   70     operator()(Sequence const & seq, InputSequence const & input_sequence)const
   71     {
   72         return boost::fusion::fold(
   73            m_seq_of_seq,
   74            input_sequence,
   75            push_back_dot_product<Sequence>(seq)
   76         );
   77     }*/
   78 };
   79 
   80 struct matrix_mux_elements{
   81     template <typename SeqOfSequenceL, typename SeqOfSequenceR>
   82     struct result{
   83         typedef fold_seq_of_seq_by_seq<SeqOfSequenceR> const start;
   84         typedef typename boost::fusion::result_of::fold<
   85             SeqOfSequenceL,
   86             boost::fusion::vector0,
   87             start
   88         >::type type;
   89     };
   90 
   91    /* template <typename SeqOfSequenceL, typename SeqOfSequenceR>
   92     typename result<SeqOfSequenceL  ,SeqOfSequenceR  >::type
   93     operator()(SeqOfSequenceL const &  sosL, SeqOfSequenceR const & sosR)const
   94     {
   95         boost::fusion::vector0 v;
   96         fold_seq_of_seq_by_seq<SeqOfSequenceR> start(sosR);
   97         return boost::fusion::fold(
   98            sosL,
   99            v,
  100            start
  101         );
  102     }*/
  103 };
  104 
  105 
  106 template <int R, int C>
  107 struct eval_dot{
  108 
  109     template <typename Matrix1, typename Matrix2>
  110     struct result{
  111          typedef boost::fusion::matrix_row<Matrix1,R> row;
  112          typedef boost::fusion::matrix_column<Matrix2,C> col;
  113          typedef typename row::sequence_type  row_seq;
  114          typedef typename col::sequence_type  col_seq;
  115          typedef typename quan::fusion::dot_product::result<row_seq,col_seq>::type type;
  116     };
  117 
  118     template <typename Matrix1, typename Matrix2>
  119     typename result<const Matrix1,const Matrix2>::type
  120     operator() (Matrix1 const & m1, Matrix2 const & m2) const
  121     {
  122        typename result< const Matrix1, const Matrix2>::row row(m1);
  123        typename result< const Matrix1, const Matrix2>::col col(m2);
  124        quan::fusion::dot_product dot;
  125        return dot(row.m_sequence,col.m_sequence);
  126     }
  127 };
  128 
  129 template <int R, int C, typename Matrix1, typename Matrix2,typename ResultMatrix>
  130 struct eval_rc;
  131 
  132 template < typename Matrix1, typename Matrix2,typename ResultMatrix>
  133 struct eval_rc<0,0,Matrix1, Matrix2,ResultMatrix>{
  134     void operator()(Matrix1 const & m1, Matrix2 const & m2,ResultMatrix & r)const
  135     {
  136         eval_dot<0,0> dot;
  137         r.at<0,0>() = dot(m1,m2);
  138          std::cout << "result <" << 0 << "," << 0 << "> = " << r.at<0,0>() ;
  139         std::cout << "in = " << dot(m1,m2) <<'\n';
  140     }
  141 };
  142 
  143 template <int R, typename Matrix1, typename Matrix2, typename ResultMatrix>
  144 struct eval_rc<R,0,Matrix1, Matrix2,ResultMatrix>{
  145     void operator()(Matrix1 const & m1, Matrix2 const & m2,ResultMatrix & r)const
  146     {
  147         eval_rc<R-1,Matrix2::cols -1,Matrix1, Matrix2,ResultMatrix> prev;
  148         prev(m1,m2,r);
  149         eval_dot<R,0> dot;
  150         r.at<R,0>() = dot(m1,m2) ;
  151         std::cout << "result <" << R << "," << 0 << "> = " << r.at<R,0>() ;
  152         std::cout << "in = " << dot(m1,m2) <<'\n';
  153     }
  154 };
  155 
  156 template <int R, int C, typename Matrix1, typename Matrix2, typename ResultMatrix>
  157 struct eval_rc{
  158     void operator()(Matrix1 const & m1, Matrix2 const & m2,ResultMatrix & r)const
  159     {
  160         eval_rc<R,C-1,Matrix1, Matrix2,ResultMatrix> prev;
  161         prev(m1,m2,r);
  162         eval_dot<R,C> dot;
  163         r.at<R,C>() = dot(m1,m2) ;
  164         std::cout << "result <" << R << "," << C << "> = " << r.at<R,C>() ;
  165         std::cout << "in = " << dot(m1,m2) <<'\n';
  166     }
  167 };
  168 
  169 struct matrix_mux1{
  170 
  171     template <typename Matrix1, typename Matrix2>
  172     struct result{
  173 
  174         typedef boost::fusion::matrix_row_sequence<Matrix1> seqL;
  175         typedef boost::fusion::matrix_column_sequence<Matrix2> seqR;
  176 
  177         typedef typename boost::fusion::result_of::as_vector<
  178             typename matrix_mux_elements::template result<seqL,seqR>::type
  179         >::type elements_type;
  180         typedef quan::rc_matrix<Matrix1::rows,Matrix2::cols,elements_type> type;
  181     };
  182 
  183     template <typename Matrix1, typename Matrix2>
  184     typename result<Matrix1,Matrix2>::type
  185     operator()(Matrix1 const & m1, Matrix2 const & m2)const
  186     {
  187         typedef  typename result<Matrix1,Matrix2>::type result_type;
  188         eval_rc<result_type::rows -1,result_type::cols -1,Matrix1,Matrix2,result_type> eval;
  189         result_type result;
  190         eval(m1,m2,result);
  191         return result;
  192     }
  193 };
  194 
  195 int main()
  196 {
  197     using quan::static_;
  198 
  199     typedef static_<double,0>::type zero;
  200     typedef static_<double,1>::type one;
  201 
  202     typedef boost::fusion::vector16<
  203         double, double, double , zero,
  204         double, double, double , zero,
  205         double, double, double , zero,
  206         one  ,   zero, zero   , one
  207     > elements;
  208 
  209     typedef quan::rc_matrix<4,4,elements> matrix_type;
  210 
  211     matrix_type matrix(
  212         elements(
  213             1.,     2.,     3.,     zero(),
  214             4.,     5.,     6.,     zero(),
  215             7.,     8.,     9.,     zero(),
  216             one(), zero(), zero(), one()
  217         )
  218     );
  219 
  220     //std::cout << matrix <<'\n';
  221    /* eval_rc<3,3,matrix_type, matrix_type> dot;*/
  222     matrix_mux1 mux;
  223     std::cout << typeid(matrix_mux1::result<matrix_type,matrix_type>::type).name() <<'\n';
  224     std::cout << mux(matrix,matrix);
  225     std::cout << '\n';
  226 
  227 
  228 
  229 }
  230 

Back to SourceForge.net

Powered by ViewVC 1.0.3

ViewVC and Help