3
* \brief \todo brief description
8
* Copyright ?-? authors
10
* This library is free software; you can redistribute it and/or
11
* modify it either under the terms of the GNU Lesser General Public
12
* License version 2.1 as published by the Free Software Foundation
13
* (the "LGPL") or, at your option, under the terms of the Mozilla
14
* Public License Version 1.1 (the "MPL"). If you do not alter this
15
* notice, a recipient may use your version of this file under either
16
* the MPL or the LGPL.
18
* You should have received a copy of the LGPL along with this library
19
* in the file COPYING-LGPL-2.1; if not, write to the Free Software
20
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
* You should have received a copy of the MPL along with this library
22
* in the file COPYING-MPL-1.1
24
* The contents of this file are subject to the Mozilla Public License
25
* Version 1.1 (the "License"); you may not use this file except in
26
* compliance with the License. You may obtain a copy of the License at
27
* http://www.mozilla.org/MPL/
29
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
30
* OF ANY KIND, either express or implied. See the LGPL or the MPL for
31
* the specific language governing rights and limitations.
35
#ifndef LIB2GEOM_SEEN_POLY_H
36
#define LIB2GEOM_SEEN_POLY_H
42
#include <2geom/utils.h>
46
class Poly : public std::vector<double>{
48
// coeff; // sum x^i*coeff[i]
50
//unsigned size() const { return coeff.size();}
51
unsigned degree() const { return size()-1;}
53
//double operator[](const int i) const { return (*this)[i];}
54
//double& operator[](const int i) { return (*this)[i];}
56
Poly operator+(const Poly& p) const {
58
const unsigned out_size = std::max(size(), p.size());
59
const unsigned min_size = std::min(size(), p.size());
60
//result.reserve(out_size);
62
for(unsigned i = 0; i < min_size; i++) {
63
result.push_back((*this)[i] + p[i]);
65
for(unsigned i = min_size; i < size(); i++)
66
result.push_back((*this)[i]);
67
for(unsigned i = min_size; i < p.size(); i++)
68
result.push_back(p[i]);
69
assert(result.size() == out_size);
72
Poly operator-(const Poly& p) const {
74
const unsigned out_size = std::max(size(), p.size());
75
const unsigned min_size = std::min(size(), p.size());
76
result.reserve(out_size);
78
for(unsigned i = 0; i < min_size; i++) {
79
result.push_back((*this)[i] - p[i]);
81
for(unsigned i = min_size; i < size(); i++)
82
result.push_back((*this)[i]);
83
for(unsigned i = min_size; i < p.size(); i++)
84
result.push_back(-p[i]);
85
assert(result.size() == out_size);
88
Poly operator-=(const Poly& p) {
89
const unsigned out_size = std::max(size(), p.size());
90
const unsigned min_size = std::min(size(), p.size());
93
for(unsigned i = 0; i < min_size; i++) {
96
for(unsigned i = min_size; i < out_size; i++)
100
Poly operator-(const double k) const {
102
const unsigned out_size = size();
103
result.reserve(out_size);
105
for(unsigned i = 0; i < out_size; i++) {
106
result.push_back((*this)[i]);
111
Poly operator-() const {
113
result.resize(size());
115
for(unsigned i = 0; i < size(); i++) {
116
result[i] = -(*this)[i];
120
Poly operator*(const double p) const {
122
const unsigned out_size = size();
123
result.reserve(out_size);
125
for(unsigned i = 0; i < out_size; i++) {
126
result.push_back((*this)[i]*p);
128
assert(result.size() == out_size);
131
// equivalent to multiply by x^terms, negative terms are disallowed
132
Poly shifted(unsigned const terms) const {
134
size_type const out_size = size() + terms;
135
result.reserve(out_size);
137
result.resize(terms, 0.0);
138
result.insert(result.end(), this->begin(), this->end());
140
assert(result.size() == out_size);
143
Poly operator*(const Poly& p) const;
145
template <typename T>
148
for(int k = size()-1; k >= 0; k--) {
149
r = r*x + T((*this)[k]);
154
template <typename T>
155
T operator()(T t) const { return (T)eval(t);}
161
Poly(const Poly& p) : std::vector<double>(p) {}
162
Poly(const double a) {push_back(a);}
165
template <class T, class U>
166
void val_and_deriv(T x, U &pd) const {
169
int nd = pd.size() - 1;
170
for(unsigned j = 1; j < pd.size(); j++)
172
for(int i = nc -1; i >= 0; i--) {
173
int nnd = std::min(nd, nc-i);
174
for(int j = nnd; j >= 1; j--)
175
pd[j] = pd[j]*x + operator[](i);
176
pd[0] = pd[0]*x + operator[](i);
179
for(int i = 2; i <= nd; i++) {
185
static Poly linear(double ax, double b) {
193
inline Poly operator*(double a, Poly const & b) { return b * a;}
195
Poly integral(Poly const & p);
196
Poly derivative(Poly const & p);
197
Poly divide_out_root(Poly const & p, double x);
198
Poly compose(Poly const & a, Poly const & b);
199
Poly divide(Poly const &a, Poly const &b, Poly &r);
200
Poly gcd(Poly const &a, Poly const &b, const double tol=1e-10);
203
* find all p.degree() roots of p.
204
* 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?
206
std::vector<std::complex<double> > solve(const Poly & p);
208
/*** solve_reals(Poly p)
209
* find all real solutions to Poly p.
210
* currently we just use solve and pick out the suitably real looking values, there may be a better algorithm.
212
std::vector<double> solve_reals(const Poly & p);
213
double polish_root(Poly const & p, double guess, double tol);
215
inline std::ostream &operator<< (std::ostream &out_file, const Poly &in_poly) {
216
if(in_poly.size() == 0)
219
for(int i = (int)in_poly.size()-1; i >= 0; --i) {
221
out_file << "" << in_poly[i] << "*x";
224
out_file << "" << in_poly[i] << "*x^" << i;
227
out_file << in_poly[i];
236
#endif //LIB2GEOM_SEEN_POLY_H
241
c-file-style:"stroustrup"
242
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
247
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :