cheshirekow  v0.1.0
Difference.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_DIFFERENCE_H_
28 #define MPBLOCKS_POLYNOMIAL_DIFFERENCE_H_
29 
30 namespace mpblocks {
31 namespace polynomial {
32 
34 template <typename Scalar, class Exp1, class Exp2>
35 class Difference :
36  public RValue<
37  Scalar,
38  Difference<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  Difference( 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 std::max( (Size_t)m_A.size(), (Size_t)m_B.size() );
56  }
57 
59  Scalar operator[]( Size_t i ) const
60  {
61  if( i < m_A.size() && i < m_B.size() )
62  return (m_A[i] - m_B[i]);
63  else if( i < m_A.size() )
64  return m_A[i];
65  else if( i < m_B.size() )
66  return -m_B[i];
67  else
68  return 0;
69  }
70 
71  Scalar eval( Scalar x )
72  {
73  return m_A.eval(x) + m_B.eval(x);
74  }
75 };
76 
77 
78 
79 
80 template <typename Scalar, class Exp1, class Exp2>
82  RValue<Scalar,Exp1> const& A,
83  RValue<Scalar,Exp2> const& B )
84 {
85  typedef Difference<Scalar,Exp1,Exp2> Difference_t;
86  return Difference_t(
87  static_cast<Exp1 const&>(A),
88  static_cast<Exp2 const&>(B));
89 }
90 
91 
92 
93 
94 } // polynomial
95 } // mpblocks
96 
97 
98 
99 
100 
101 #endif // DIFFERENCE_H_
Difference< Scalar, Exp1, Exp2 > operator-(RValue< Scalar, Exp1 > const &A, RValue< Scalar, Exp2 > const &B)
Definition: Difference.h:81
Difference(Exp1 const &A, Exp2 const &B)
Definition: Difference.h:46
Scalar operator[](Size_t i) const
return the evaluated i'th element of a vector expression
Definition: Difference.h:59
Size_t size() const
return the size for a vector
Definition: Difference.h:53
expression template for sum of two matrix expressions
Definition: Difference.h:35
expression template for rvalues
Definition: RValue.h:35