Code-Eli  0.3.6
descartes_rule.hpp
Go to the documentation of this file.
1 /*********************************************************************************
2 * Copyright (c) 2013 David D. Marshall <ddmarsha@calpoly.edu>
3 *
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * David D. Marshall - initial code and implementation
11 ********************************************************************************/
12 
13 #ifndef eli_mutil_poly_root_descartes_rule_hpp
14 #define eli_mutil_poly_root_descartes_rule_hpp
15 
16 #include <vector>
17 
18 #include "eli/code_eli.hpp"
19 
22 
23 namespace eli
24 {
25  namespace mutil
26  {
27  namespace poly
28  {
29  namespace root
30  {
31  template<typename data__>
32  int descartes_rule(const polynomial<data__> &f, bool positive)
33  {
34  // catch special case
35  if (f.degree()==0)
36  return 0;
37 
38  // get the coefficients from the polynomial
39  std::vector<data__> a(f.degree()+1);
40  for (size_t i=0; i<a.size(); ++i)
41  {
42  a[i]=f.coefficient(i);
43 
44  // change the sign of odd powers if want the negative root count
45  if (!positive && i%2==1)
46  a[i]*=-1;
47  }
48 
49  return eli::mutil::poly::root::sign_changes(a.begin(), a.end());
50  }
51 
52  template<typename data__>
54  {
55  return descartes_rule(f, true)+descartes_rule(f, false);
56  }
57  }
58  }
59  }
60 }
61 
62 #endif
data_type coefficient(const index_type &i) const
Definition: polynomial.hpp:77
Definition: math.hpp:20
int descartes_rule(const polynomial< data__ > &f, bool positive)
Definition: descartes_rule.hpp:32
Definition: polynomial.hpp:31
index_type degree() const
Definition: polynomial.hpp:72
int sign_changes(const it__ itb, const it__ ite)
Definition: sign_changes.hpp:28