cheshirekow  v0.1.0
Quotient.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_QUOTIENT_H_
28 #define MPBLOCKS_POLYNOMIAL_QUOTIENT_H_
29 
30 namespace mpblocks {
31 namespace polynomial {
32 
34 template <typename Scalar, class Exp1, class Exp2>
35 class Quotient
36 {
37  public:
40 
41  private:
44 
45  public:
46  typedef unsigned int Size_t;
47 
48  Quotient( Exp1 const& numerator, Exp2 const& denominator ):
49  m_r(numerator)
50  {
51  m_q.resize(numerator.size()-denominator.size()+1);
52  m_q.fill(0);
53 
54  for( int size = numerator.size();
55  size > 0 && size >= denominator.size(); )
56  {
57  // degree of the new term
58  int n = size - denominator.size();
59 
60  // coefficient of the new term
61  Scalar t = m_r[m_r.size()-1] /
62  denominator[denominator.size()-1];
63 
64  // append the new term to the quotient
65  m_q[n] = t;
66 
67  // update the remainder
68  lvalue(m_r) = numerator - m_q*denominator;
69 
70  // remove the term from the remainder
71  m_r.resize( --size );
72  }
73  }
74 
76  {
77  return m_q;
78  }
79 
81  {
82  return m_r;
83  }
84 };
85 
86 
87 
88 
89 template <typename Scalar, class Exp1, class Exp2>
91  RValue<Scalar,Exp1> const& A,
92  RValue<Scalar,Exp2> const& B )
93 {
94  typedef Quotient<Scalar,Exp1,Exp2> Quotient_t;
95  return Quotient_t(
96  static_cast<Exp1 const&>(A),
97  static_cast<Exp2 const&>(B));
98 }
99 
100 
101 
102 
103 } // polynomial
104 } // mpblocks
105 
106 
107 
108 
109 
110 #endif // QUOTIENT_H_
Size_t size() const
return the size for a vector
Definition: LValue.h:43
expression template for sum of two matrix expressions
Definition: Quotient.h:35
void resize(Size_t size)
Definition: LValue.h:75
LValue< Scalar, Exp > & lvalue(LValue< Scalar, Exp > &exp)
Definition: LValue.h:97
expression template for rvalues
Definition: RValue.h:35
Polynomial< Scalar, Dynamic > Quotient_t
Definition: Quotient.h:38
void fill(Scalar val)
Definition: LValue.h:88
Polynomial< Scalar, Dynamic > Remainder_t
Definition: Quotient.h:39
A dense, dynamically sized polynomial.
Definition: Polynomial.h:158
Quotient< Scalar, Exp1, Exp2 > operator/(RValue< Scalar, Exp1 > const &A, RValue< Scalar, Exp2 > const &B)
Definition: Quotient.h:90
RValue< Scalar, Polynomial< Scalar, Dynamic > > & r()
Definition: Quotient.h:80
RValue< Scalar, Polynomial< Scalar, Dynamic > > & q()
Definition: Quotient.h:75
Quotient(Exp1 const &numerator, Exp2 const &denominator)
Definition: Quotient.h:48