cheshirekow  v0.1.0
Sum.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_SUM_H_
28 #define MPBLOCKS_POLYNOMIAL_SUM_H_
29 
30 namespace mpblocks {
31 namespace polynomial {
32 
34 template <typename Scalar, class Exp1, class Exp2>
35 class Sum :
36  public RValue<
37  Scalar,
38  Sum<Scalar,Exp1,Exp2>
39  >
40 {
41  Exp1 const& m_A;
42  Exp2 const& m_B;
43 
44  public:
45  typedef std::size_t Size_t;
46 
47  Sum( Exp1 const& A, Exp2 const& B ):
48  m_A(A),
49  m_B(B)
50  {
51  }
52 
54  Size_t size() const
55  {
56  return std::max( (Size_t) m_A.size() , (Size_t) m_B.size() );
57  }
58 
60  Scalar operator[]( Size_t i ) const
61  {
62  if( i < m_A.size() && i < m_B.size() )
63  return (m_A[i] + m_B[i]);
64  else if( i < m_A.size() )
65  return m_A[i];
66  else if( i < m_B.size() )
67  return m_B[i];
68  else
69  return 0;
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 Sum<Scalar,Exp1,Exp2> Sum_t;
87  return Sum_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 // SUM_H_
Exp2 const & m_B
Definition: Sum.h:42
std::size_t Size_t
Definition: Sum.h:45
Scalar operator[](Size_t i) const
return the evaluated i'th element of a vector expression
Definition: Sum.h:60
Sum(Exp1 const &A, Exp2 const &B)
Definition: Sum.h:47
Scalar eval(Scalar x)
Definition: Sum.h:72
expression template for rvalues
Definition: RValue.h:35
Size_t size() const
return the size for a vector
Definition: Sum.h:54
expression template for sum of two matrix expressions
Definition: Sum.h:35
Exp1 const & m_A
Definition: Sum.h:41
Sum< Scalar, Exp1, Exp2 > operator+(RValue< Scalar, Exp1 > const &A, RValue< Scalar, Exp2 > const &B)
Definition: Sum.h:82