cheshirekow  v0.1.0
SturmSequence.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_STURMSEQUENCE_H_
28 #define MPBLOCKS_POLYNOMIAL_STURMSEQUENCE_H_
29 
30 #include <vector>
31 #include <list>
32 
33 namespace mpblocks {
34 namespace polynomial {
35 
36 
37 
38 template < typename Scalar>
40 {
41  public:
44  typedef std::vector< Polynomial<Scalar,Dynamic> > PolyList_t;
45 
46  private:
48 // PolyList_t m_q;
49 
50  public:
52  {}
53 
55  template < class Exp >
57  {
58  rebuild(rhs);
59  }
60 
61  template <class Exp>
62  void rebuild( const RValue<Scalar,Exp>& rhs )
63  {
64  m_seq.resize( rhs.size() );
65 // m_q.resize( rhs.size() );
66 
68  lvalue(m_seq[0]) = normalized(rhs);
69 
71  Poly_t f1;
72  differentiate( rhs, f1, 1 );
73  lvalue(m_seq[1]) = normalized(f1);
74 
75  // all others are found by the following recursion
76  for(int i=2; i < rhs.size(); i++)
77  {
78  Quotient_t quot = m_seq[i-2] / m_seq[i-1];
79 // lvalue(m_q[i]) = quot.q();
80  lvalue(m_seq[i]) = -quot.r();
81  }
82  }
83 
85  int signChanges( Scalar x )
86  {
87  int nChanges = 0;
88  Scalar prev = polyval(m_seq[0],x);
89  for(int i=1; i < m_seq.size(); i++)
90  {
91  Scalar next = polyval(m_seq[i],x);
92  if( sgn(next) )
93  {
94  if( sgn(next) != sgn(prev) )
95  nChanges++;
96  prev = next;
97  }
98  }
99 
100  return nChanges;
101  }
102 
103  const RValue< Scalar, Poly_t >& operator[]( int i ) const
104  {
105  return m_seq[i];
106  }
107 
108 // const RValue< Scalar, Poly_t >& operator()( int i ) const
109 // {
110 // return m_q[i];
111 // }
112 
113  int size() const
114  {
115  return m_seq.size();
116  }
117 
118 };
119 
120 
121 } // namespace polynomial
122 } // namespace mpblocks
123 
124 
125 
126 
127 
128 
129 
130 
131 
132 
133 
134 
135 
136 
137 
138 
139 #endif // STURMSEQUENCE_H_
int signChanges(Scalar x)
return the number of sign changes at the specified point
Definition: SturmSequence.h:85
const RValue< Scalar, Poly_t > & operator[](int i) const
Quotient< Scalar, Poly_t, Poly_t > Quotient_t
Definition: SturmSequence.h:43
expression template for sum of two matrix expressions
Definition: Quotient.h:35
Size_t size() const
return the size for a vector
Definition: RValue.h:41
int sgn(T val)
signum
Definition: polynomial.h:40
LValue< Scalar, Exp > & lvalue(LValue< Scalar, Exp > &exp)
Definition: LValue.h:97
void rebuild(const RValue< Scalar, Exp > &rhs)
Definition: SturmSequence.h:62
expression template for rvalues
Definition: RValue.h:35
Normalized< Scalar, Exp > normalized(RValue< Scalar, Exp > const &exp)
Definition: Normalized.h:70
A dense, dynamically sized polynomial.
Definition: Polynomial.h:158
SturmSequence(const RValue< Scalar, Exp > &rhs)
builds a sturm sequence from a polynomial
Definition: SturmSequence.h:56
std::vector< Polynomial< Scalar, Dynamic > > PolyList_t
Definition: SturmSequence.h:44
Polynomial< Scalar, Dynamic > Poly_t
Definition: SturmSequence.h:42
void differentiate(const RValue< Scalar, Exp1 > &in, LValue< Scalar, Exp2 > &out, int n)
evaluate a polynomial
Definition: differentiate.h:36
RValue< Scalar, Polynomial< Scalar, Dynamic > > & r()
Definition: Quotient.h:80
Scalar polyval(const RValue< Scalar, Exp > &poly, Scalar x)
evaluate a polynomial
Definition: polyval.h:35