cheshirekow  v0.1.0
Product.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 Josh Bialkowski (jbialk@mit.edu)
3  *
4  * This file is part of mpblocks.
5  *
6  * mpblocks is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * mpblocks is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with mpblocks. If not, see <http://www.gnu.org/licenses/>.
18  */
27 #ifndef MPBLOCKS_POLYNOMIAL_PRODUCT_H_
28 #define MPBLOCKS_POLYNOMIAL_PRODUCT_H_
29 
30 namespace mpblocks {
31 namespace polynomial {
32 
34 template <typename Scalar, class Exp1, class Exp2>
35 class Product :
36  public RValue<
37  Scalar,
38  Product<Scalar,Exp1,Exp2> >
39 {
40  Exp1 const& m_A;
41  Exp2 const& m_B;
42 
43  public:
44  typedef unsigned int Size_t;
45 
46  Product( Exp1 const& A, Exp2 const& B ):
47  m_A(A),
48  m_B(B)
49  {
50  }
51 
53  Size_t size() const
54  {
55  return m_A.size() + m_B.size() - 1;
56  }
57 
59  Scalar operator[]( Size_t n ) const
60  {
61  Scalar c = 0;
62  for(int i=0; i <= n; i++)
63  {
64  int j = n-i;
65  if( i < m_A.size() && j < m_B.size() )
66  c += m_A[i] * m_B[j];
67  }
68 
69  return c;
70  }
71 
72  Scalar eval( Scalar x )
73  {
74  return m_A.eval(x) * m_B.eval(x);
75  }
76 };
77 
78 
79 
80 
81 template <typename Scalar, class Exp1, class Exp2>
83  RValue<Scalar,Exp1> const& A,
84  RValue<Scalar,Exp2> const& B )
85 {
86  typedef Product<Scalar,Exp1,Exp2> Product_t;
87  return Product_t(
88  static_cast<Exp1 const&>(A),
89  static_cast<Exp2 const&>(B));
90 }
91 
92 
93 
94 
95 } // polynomial
96 } // mpblocks
97 
98 
99 
100 
101 
102 #endif // PRODUCT_H_
Scalar operator[](Size_t n) const
return the evaluated i'th element of a vector expression
Definition: Product.h:59
Size_t size() const
return the size for a vector
Definition: Product.h:53
expression template for rvalues
Definition: RValue.h:35
Product< Scalar, Exp1, Exp2 > operator*(RValue< Scalar, Exp1 > const &A, RValue< Scalar, Exp2 > const &B)
Definition: Product.h:82
Scalar eval(Scalar x)
Definition: Product.h:72
Product(Exp1 const &A, Exp2 const &B)
Definition: Product.h:46
expression template for sum of two matrix expressions
Definition: Product.h:35