Scribus
Open source desktop publishing at your fingertips
poly.h
1 #ifndef SEEN_POLY_H
2 #define SEEN_POLY_H
3 #include <assert.h>
4 #include <vector>
5 #include <iostream>
6 #include <algorithm>
7 #include <complex>
8 #include "utils.h"
9 
10 class Poly : public std::vector<double>{
11 public:
12  // coeff; // sum x^i*coeff[i]
13 
14  //unsigned size() const { return coeff.size();}
15  unsigned degree() const { return size()-1;}
16 
17  //double operator[](const int i) const { return (*this)[i];}
18  //double& operator[](const int i) { return (*this)[i];}
19 
20  Poly operator+(const Poly& p) const {
21  Poly result;
22  // const unsigned out_size = std::max(size(), p.size());
23  const unsigned min_size = std::min(size(), p.size());
24  //result.reserve(out_size);
25 
26  for(unsigned i = 0; i < min_size; i++) {
27  result.push_back((*this)[i] + p[i]);
28  }
29  for(unsigned i = min_size; i < size(); i++)
30  result.push_back((*this)[i]);
31  for(unsigned i = min_size; i < p.size(); i++)
32  result.push_back(p[i]);
33  // assert(result.size() == out_size);
34  return result;
35  }
36  Poly operator-(const Poly& p) const {
37  Poly result;
38  const unsigned out_size = std::max(size(), p.size());
39  const unsigned min_size = std::min(size(), p.size());
40  result.reserve(out_size);
41 
42  for(unsigned i = 0; i < min_size; i++) {
43  result.push_back((*this)[i] - p[i]);
44  }
45  for(unsigned i = min_size; i < size(); i++)
46  result.push_back((*this)[i]);
47  for(unsigned i = min_size; i < p.size(); i++)
48  result.push_back(-p[i]);
49  assert(result.size() == out_size);
50  return result;
51  }
52  Poly operator-=(const Poly& p) {
53  const unsigned out_size = std::max(size(), p.size());
54  const unsigned min_size = std::min(size(), p.size());
55  resize(out_size);
56 
57  for(unsigned i = 0; i < min_size; i++) {
58  (*this)[i] -= p[i];
59  }
60  for(unsigned i = min_size; i < out_size; i++)
61  (*this)[i] = -p[i];
62  return *this;
63  }
64  Poly operator-(const double k) const {
65  Poly result;
66  const unsigned out_size = size();
67  result.reserve(out_size);
68 
69  for(unsigned i = 0; i < out_size; i++) {
70  result.push_back((*this)[i]);
71  }
72  result[0] -= k;
73  return result;
74  }
75  Poly operator-() const {
76  Poly result;
77  result.resize(size());
78 
79  for(unsigned i = 0; i < size(); i++) {
80  result[i] = -(*this)[i];
81  }
82  return result;
83  }
84  Poly operator*(const double p) const {
85  Poly result;
86  const unsigned out_size = size();
87  result.reserve(out_size);
88 
89  for(unsigned i = 0; i < out_size; i++) {
90  result.push_back((*this)[i]*p);
91  }
92  assert(result.size() == out_size);
93  return result;
94  }
95 // equivalent to multiply by x^terms, discard negative terms
96  Poly shifted(unsigned terms) const {
97  Poly result;
98  // This was a no-op and breaks the build on x86_64, as it's trying
99  // to take maximum of 32-bit and 64-bit integers
100  //const unsigned out_size = std::max(unsigned(0), size()+terms);
101  const size_type out_size = size() + terms;
102  result.reserve(out_size);
103 
104  /*if(terms < 0) {
105  for(unsigned i = 0; i < out_size; i++) {
106  result.push_back((*this)[i-terms]);
107  }
108  } else*/
109  {
110  for(unsigned i = 0; i < terms; i++) {
111  result.push_back(0.0);
112  }
113  for(unsigned i = 0; i < size(); i++) {
114  result.push_back((*this)[i]);
115  }
116  }
117 
118  assert(result.size() == out_size);
119  return result;
120  }
121  Poly operator*(const Poly& p) const;
122 
123  template <typename T>
124  T eval(T x) const {
125  T r = 0;
126  for(int k = size()-1; k >= 0; k--) {
127  r = r*x + T((*this)[k]);
128  }
129  return r;
130  }
131 
132  template <typename T>
133  T operator()(T t) const { return (T)eval(t);}
134 
135  void normalize();
136 
137  void monicify();
138  Poly() {}
139  Poly(const Poly& p) : std::vector<double>(p) {}
140  Poly(const double a) {push_back(a);}
141 
142 public:
143  template <class T, class U>
144  void val_and_deriv(T x, U &pd) const {
145  pd[0] = back();
146  int nc = size() - 1;
147  int nd = pd.size() - 1;
148  for(unsigned j = 1; j < pd.size(); j++)
149  pd[j] = 0.0;
150  for(int i = nc -1; i >= 0; i--) {
151  int nnd = std::min(nd, nc-i);
152  for(int j = nnd; j >= 1; j--)
153  pd[j] = pd[j]*x + operator[](i);
154  pd[0] = pd[0]*x + operator[](i);
155  }
156  double cnst = 1;
157  for(int i = 2; i <= nd; i++) {
158  cnst *= i;
159  pd[i] *= cnst;
160  }
161  }
162 
163  static Poly linear(double ax, double b) {
164  Poly p;
165  p.push_back(b);
166  p.push_back(ax);
167  return p;
168  }
169 };
170 
171 inline Poly operator*(double a, Poly const & b) { return b * a;}
172 
173 Poly integral(Poly const & p);
174 Poly derivative(Poly const & p);
175 Poly divide_out_root(Poly const & p, double x);
176 Poly compose(Poly const & a, Poly const & b);
177 Poly divide(Poly const &a, Poly const &b, Poly &r);
178 Poly gcd(Poly const &a, Poly const &b, const double tol=1e-10);
179 
180 /*** solve(Poly p)
181  * find all p.degree() roots of p.
182  * This function can take a long time with suitably crafted polynomials, but in practice it should be fast. Should we provide special forms for degree() <= 4?
183  */
184 std::vector<std::complex<double> > solve(const Poly & p);
185 
186 /*** solve_reals(Poly p)
187  * find all real solutions to Poly p.
188  * currently we just use solve and pick out the suitably real looking values, there may be a better algorithm.
189  */
190 std::vector<double> solve_reals(const Poly & p);
191 double polish_root(Poly const & p, double guess, double tol);
192 
193 inline std::ostream &operator<< (std::ostream &out_file, const Poly &in_poly) {
194  if(in_poly.size() == 0)
195  out_file << "0";
196  else {
197  for(int i = (int)in_poly.size()-1; i >= 0; --i) {
198  if(i == 1) {
199  out_file << "" << in_poly[i] << "*x";
200  out_file << " + ";
201  } else if(i) {
202  out_file << "" << in_poly[i] << "*x^" << i;
203  out_file << " + ";
204  } else
205  out_file << in_poly[i];
206 
207  }
208  }
209  return out_file;
210 }
211 
212 
213 /*
214  Local Variables:
215  mode:c++
216  c-file-style:"stroustrup"
217  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
218  indent-tabs-mode:nil
219  fill-column:99
220  End:
221 */
222 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :
223 #endif
std::ostream & operator<<(std::ostream &out_file, const Geom::Matrix &m)
Definition: matrix.h:109
Definition: poly.h:10