Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
polynomial.h
Go to the documentation of this file.
1/*
5 * Authors:
6 * MenTaLguY <mental@rydia.net>
7 * Krzysztof Kosiński <tweenk.pl@gmail.com>
8 * Rafał Siejakowski <rs@rs-math.net>
9 *
10 * Copyright 2007-2015 Authors
11 *
12 * This library is free software; you can redistribute it and/or
13 * modify it either under the terms of the GNU Lesser General Public
14 * License version 2.1 as published by the Free Software Foundation
15 * (the "LGPL") or, at your option, under the terms of the Mozilla
16 * Public License Version 1.1 (the "MPL"). If you do not alter this
17 * notice, a recipient may use your version of this file under either
18 * the MPL or the LGPL.
19 *
20 * You should have received a copy of the LGPL along with this library
21 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * You should have received a copy of the MPL along with this library
24 * in the file COPYING-MPL-1.1
25 *
26 * The contents of this file are subject to the Mozilla Public License
27 * Version 1.1 (the "License"); you may not use this file except in
28 * compliance with the License. You may obtain a copy of the License at
29 * http://www.mozilla.org/MPL/
30 *
31 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
32 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
33 * the specific language governing rights and limitations.
34 */
35
36#ifndef LIB2GEOM_SEEN_POLY_H
37#define LIB2GEOM_SEEN_POLY_H
38#include <assert.h>
39#include <vector>
40#include <iostream>
41#include <algorithm>
42#include <complex>
43#include <2geom/coord.h>
44#include <2geom/utils.h>
45
46namespace Geom {
47
50class Poly : public std::vector<double>{
51public:
52 // coeff; // sum x^i*coeff[i]
53
54 //unsigned size() const { return coeff.size();}
55 unsigned degree() const { return size()-1;}
56
57 //double operator[](const int i) const { return (*this)[i];}
58 //double& operator[](const int i) { return (*this)[i];}
59
60 Poly operator+(const Poly& p) const {
62 const unsigned out_size = std::max(size(), p.size());
63 const unsigned min_size = std::min(size(), p.size());
64 result.reserve(out_size);
65
66 for(unsigned i = 0; i < min_size; i++) {
67 result.push_back((*this)[i] + p[i]);
68 }
69 for(unsigned i = min_size; i < size(); i++)
70 result.push_back((*this)[i]);
71 for(unsigned i = min_size; i < p.size(); i++)
72 result.push_back(p[i]);
73 assert(result.size() == out_size);
74 return result;
75 }
76 Poly operator-(const Poly& p) const {
78 const unsigned out_size = std::max(size(), p.size());
79 const unsigned min_size = std::min(size(), p.size());
80 result.reserve(out_size);
81
82 for(unsigned i = 0; i < min_size; i++) {
83 result.push_back((*this)[i] - p[i]);
84 }
85 for(unsigned i = min_size; i < size(); i++)
86 result.push_back((*this)[i]);
87 for(unsigned i = min_size; i < p.size(); i++)
88 result.push_back(-p[i]);
89 assert(result.size() == out_size);
90 return result;
91 }
92 Poly operator-=(const Poly& p) {
93 const unsigned out_size = std::max(size(), p.size());
94 const unsigned min_size = std::min(size(), p.size());
95 resize(out_size);
96
97 for(unsigned i = 0; i < min_size; i++) {
98 (*this)[i] -= p[i];
99 }
100 for(unsigned i = min_size; i < out_size; i++)
101 (*this)[i] = -p[i];
102 return *this;
103 }
104 Poly operator-(const double k) const {
105 Poly result;
106 const unsigned out_size = size();
107 result.reserve(out_size);
108
109 for(unsigned i = 0; i < out_size; i++) {
110 result.push_back((*this)[i]);
111 }
112 result[0] -= k;
113 return result;
114 }
115 Poly operator-() const {
116 Poly result;
117 result.resize(size());
118
119 for(unsigned i = 0; i < size(); i++) {
120 result[i] = -(*this)[i];
121 }
122 return result;
123 }
124 Poly operator*(const double p) const {
125 Poly result;
126 const unsigned out_size = size();
127 result.reserve(out_size);
128
129 for(unsigned i = 0; i < out_size; i++) {
130 result.push_back((*this)[i]*p);
131 }
132 assert(result.size() == out_size);
133 return result;
134 }
135 // equivalent to multiply by x^terms, negative terms are disallowed
136 Poly shifted(unsigned const terms) const {
137 Poly result;
138 size_type const out_size = size() + terms;
139 result.reserve(out_size);
140
141 result.resize(terms, 0.0);
142 result.insert(result.end(), this->begin(), this->end());
143
144 assert(result.size() == out_size);
145 return result;
146 }
147 Poly operator*(const Poly& p) const;
148
149 template <typename T>
150 T eval(T x) const {
151 T r = 0;
152 for(int k = size()-1; k >= 0; k--) {
153 r = r*x + T((*this)[k]);
154 }
155 return r;
156 }
157
158 template <typename T>
159 T operator()(T t) const { return (T)eval(t);}
160
161 void normalize();
162
163 void monicify();
164 Poly() {}
165 Poly(const Poly& p) : std::vector<double>(p) {}
166 Poly(const double a) {push_back(a);}
167
168public:
169 template <class T, class U>
170 void val_and_deriv(T x, U &pd) const {
171 pd[0] = back();
172 int nc = size() - 1;
173 int nd = pd.size() - 1;
174 for(unsigned j = 1; j < pd.size(); j++)
175 pd[j] = 0.0;
176 for(int i = nc -1; i >= 0; i--) {
177 int nnd = std::min(nd, nc-i);
178 for(int j = nnd; j >= 1; j--)
179 pd[j] = pd[j]*x + operator[](i);
180 pd[0] = pd[0]*x + operator[](i);
181 }
182 double cnst = 1;
183 for(int i = 2; i <= nd; i++) {
184 cnst *= i;
185 pd[i] *= cnst;
186 }
187 }
188
189 static Poly linear(double ax, double b) {
190 Poly p;
191 p.push_back(b);
192 p.push_back(ax);
193 return p;
194 }
195};
196
197inline Poly operator*(double a, Poly const & b) { return b * a;}
198
199Poly integral(Poly const & p);
200Poly derivative(Poly const & p);
201Poly divide_out_root(Poly const & p, double x);
202Poly compose(Poly const & a, Poly const & b);
203Poly divide(Poly const &a, Poly const &b, Poly &r);
204Poly gcd(Poly const &a, Poly const &b, const double tol=1e-10);
205
206/*** solve(Poly p)
207 * find all p.degree() roots of p.
208 * 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?
209 */
210std::vector<std::complex<double> > solve(const Poly & p);
211
212#ifdef HAVE_GSL
213/*** solve_reals(Poly p)
214 * find all real solutions to Poly p.
215 * currently we just use solve and pick out the suitably real looking values, there may be a better algorithm.
216 */
217std::vector<double> solve_reals(const Poly & p);
218#endif
219double polish_root(Poly const & p, double guess, double tol);
220
221
225std::vector<Coord> solve_quadratic(Coord a, Coord b, Coord c);
226
230std::vector<Coord> solve_cubic(Coord a, Coord b, Coord c, Coord d);
231
235std::vector<Coord> solve_quartic(Coord a, Coord b, Coord c, Coord d, Coord e);
236
237inline std::ostream &operator<< (std::ostream &out_file, const Poly &in_poly) {
238 if(in_poly.size() == 0)
239 out_file << "0";
240 else {
241 for(int i = (int)in_poly.size()-1; i >= 0; --i) {
242 if(i == 1) {
243 out_file << "" << in_poly[i] << "*x";
244 out_file << " + ";
245 } else if(i) {
246 out_file << "" << in_poly[i] << "*x^" << i;
247 out_file << " + ";
248 } else
249 out_file << in_poly[i];
250
251 }
252 }
253 return out_file;
254}
255
256} // namespace Geom
257
258#endif //LIB2GEOM_SEEN_POLY_H
259
260/*
261 Local Variables:
262 mode:c++
263 c-file-style:"stroustrup"
264 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
265 indent-tabs-mode:nil
266 fill-column:99
267 End:
268*/
269// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
Various utility functions.
Polynomial in canonical (monomial) basis.
Definition polynomial.h:50
Poly operator-=(const Poly &p)
Definition polynomial.h:92
Poly operator+(const Poly &p) const
Definition polynomial.h:60
T eval(T x) const
Definition polynomial.h:150
void monicify()
Poly(const Poly &p)
Definition polynomial.h:165
Poly shifted(unsigned const terms) const
Definition polynomial.h:136
Poly operator*(const double p) const
Definition polynomial.h:124
static Poly linear(double ax, double b)
Definition polynomial.h:189
void val_and_deriv(T x, U &pd) const
Definition polynomial.h:170
Poly operator-() const
Definition polynomial.h:115
unsigned degree() const
Definition polynomial.h:55
Poly(const double a)
Definition polynomial.h:166
Poly operator-(const Poly &p) const
Definition polynomial.h:76
void normalize()
Poly operator-(const double k) const
Definition polynomial.h:104
T operator()(T t) const
Definition polynomial.h:159
Integral and real coordinate types and some basic utilities.
Css & result
Geom::IntPoint size
double c[8][4]
double Coord
Floating point type used to store coordinates.
Definition coord.h:76
Various utility functions.
Definition affine.h:22
std::vector< Coord > solve_quartic(Coord a, Coord b, Coord c, Coord d, Coord e)
Analytically solve quartic equation.
SBasisN< n > divide(SBasisN< n > const &a, SBasisN< n > const &b, int k)
Poly gcd(Poly const &a, Poly const &b, const double tol=1e-10)
std::vector< Coord > solve_quadratic(Coord a, Coord b, Coord c)
Analytically solve quadratic equation.
Bezier operator*(Bezier const &f, Bezier const &g)
Definition bezier.cpp:221
std::vector< Coord > solve_cubic(Coord a, Coord b, Coord c, Coord d)
Analytically solve cubic equation.
D2< T > compose(D2< T > const &a, T const &b)
Definition d2.h:405
std::vector< double > solve_reals(const Poly &p)
std::vector< std::complex< double > > solve(const Poly &p)
Bezier integral(Bezier const &a)
Definition bezier.cpp:294
Bezier derivative(Bezier const &a)
Definition bezier.cpp:282
Poly divide_out_root(Poly const &p, double x)
double polish_root(Poly const &p, double guess, double tol)
STL namespace.
void resize()
Definition resize.cpp:80