Code

update to latest 2geom. this adds gsl dependency, doesn't seem to make inskape execut...
[inkscape.git] / src / 2geom / sbasis.h
1 /*
2  *  sbasis.h - S-power basis function class
3  *
4  *  Authors:
5  *   Nathan Hurst <njh@mail.csse.monash.edu.au>
6  *   Michael Sloan <mgsloan@gmail.com>
7  *
8  * Copyright (C) 2006-2007 authors
9  *
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.
17  *
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
23  *
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/
28  *
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.
32  */
34 #ifndef SEEN_SBASIS_H
35 #define SEEN_SBASIS_H
36 #include <vector>
37 #include <cassert>
38 #include <iostream>
40 #include "linear.h"
41 #include "interval.h"
42 #include "utils.h"
43 #include "exception.h"
45 namespace Geom {
47 /*** An empty SBasis is identically 0. */
48 class SBasis : public std::vector<Linear>{
49 public:
50     SBasis() {}
51     explicit SBasis(double a) {
52         push_back(Linear(a,a));
53     }
54     SBasis(SBasis const & a) :
55         std::vector<Linear>(a)
56     {}
57     SBasis(Linear const & bo) {
58         push_back(bo);
59     }
61     //IMPL: FragmentConcept
62     typedef double output_type;
63     inline bool isZero() const {
64         if(empty()) return true;
65         for(unsigned i = 0; i < size(); i++) {
66             if(!(*this)[i].isZero()) return false;
67         }
68         return true;
69     }
70     inline bool isConstant() const {
71         if (empty()) return true;
72         for (unsigned i = 0; i < size(); i++) {
73             if(!(*this)[i].isConstant()) return false;
74         }
75         return true;
76     }
78     bool isFinite() const;
79     inline double at0() const { 
80         if(empty()) return 0; else return (*this)[0][0];
81     }
82     inline double at1() const{
83         if(empty()) return 0; else return (*this)[0][1];
84     }
86     double valueAt(double t) const {
87         double s = t*(1-t);
88         double p0 = 0, p1 = 0;
89         double sk = 1;
90 //TODO: rewrite as horner
91         for(unsigned k = 0; k < size(); k++) {
92             p0 += sk*(*this)[k][0];
93             p1 += sk*(*this)[k][1];
94             sk *= s;
95         }
96         return (1-t)*p0 + t*p1;
97     }
98     double valueAndDerivative(double t, double &der) const {
99         double s = t*(1-t);
100         double p0 = 0, p1 = 0;
101         double sk = 1;
102 //TODO: rewrite as horner
103         for(unsigned k = 0; k < size(); k++) {
104             p0 += sk*(*this)[k][0];
105             p1 += sk*(*this)[k][1];
106             sk *= s;
107         }
108         // p0 and p1 at this point form a linear approximation at t
109         der = p1 - p0;
110         return (1-t)*p0 + t*p1;
111     }
112     double operator()(double t) const {
113         return valueAt(t);
114     }
116     std::vector<double> valueAndDerivatives(double t, unsigned n) const;
118     SBasis toSBasis() const { return SBasis(*this); }
120     double tailError(unsigned tail) const;
122 // compute f(g)
123     SBasis operator()(SBasis const & g) const;
125     Linear operator[](unsigned i) const {
126         assert(i < size());
127         return std::vector<Linear>::operator[](i);
128     }
130 //MUTATOR PRISON
131     Linear& operator[](unsigned i) { return this->at(i); }
133     //remove extra zeros
134     void normalize() {
135         while(!empty() && 0 == back()[0] && 0 == back()[1])
136             pop_back();
137     }
138     void truncate(unsigned k) { if(k < size()) resize(k); }
139 };
141 //TODO: figure out how to stick this in linear, while not adding an sbasis dep
142 inline SBasis Linear::toSBasis() const { return SBasis(*this); }
144 //implemented in sbasis-roots.cpp
145 Interval bounds_exact(SBasis const &a);
146 Interval bounds_fast(SBasis const &a, int order = 0);
147 Interval bounds_local(SBasis const &a, const Interval &t, int order = 0);
149 inline SBasis reverse(SBasis const &a) {
150     SBasis result;
151     result.reserve(a.size());
152     for(unsigned k = 0; k < a.size(); k++)
153        result.push_back(reverse(a[k]));
154     return result;
157 //IMPL: ScalableConcept
158 inline SBasis operator-(const SBasis& p) {
159     if(p.isZero()) return SBasis();
160     SBasis result;
161     result.reserve(p.size());
162         
163     for(unsigned i = 0; i < p.size(); i++) {
164         result.push_back(-p[i]);
165     }
166     return result;
168 SBasis operator*(SBasis const &a, double k);
169 inline SBasis operator*(double k, SBasis const &a) { return a*k; }
170 inline SBasis operator/(SBasis const &a, double k) { return a*(1./k); }
171 SBasis& operator*=(SBasis& a, double b);
172 inline SBasis& operator/=(SBasis& a, double b) { return (a*=(1./b)); }
174 //IMPL: AddableConcept
175 SBasis operator+(const SBasis& a, const SBasis& b);
176 SBasis operator-(const SBasis& a, const SBasis& b);
177 SBasis& operator+=(SBasis& a, const SBasis& b);
178 SBasis& operator-=(SBasis& a, const SBasis& b);
180 //TODO: remove?
181 inline SBasis operator+(const SBasis & a, Linear const & b) {
182     if(b.isZero()) return a;
183     if(a.isZero()) return b;
184     SBasis result(a);
185     result[0] += b;
186     return result;
188 inline SBasis operator-(const SBasis & a, Linear const & b) {
189     if(b.isZero()) return a;
190     SBasis result(a);
191     result[0] -= b;
192     return result;
194 inline SBasis& operator+=(SBasis& a, const Linear& b) {
195     if(a.isZero())
196         a.push_back(b);
197     else
198         a[0] += b;
199     return a;
201 inline SBasis& operator-=(SBasis& a, const Linear& b) {
202     if(a.isZero())
203         a.push_back(-b);
204     else
205         a[0] -= b;
206     return a;
209 //IMPL: OffsetableConcept
210 inline SBasis operator+(const SBasis & a, double b) {
211     if(a.isZero()) return Linear(b, b);
212     SBasis result(a);
213     result[0] += b;
214     return result;
216 inline SBasis operator-(const SBasis & a, double b) {
217     if(a.isZero()) return Linear(-b, -b);
218     SBasis result(a);
219     result[0] -= b;
220     return result;
222 inline SBasis& operator+=(SBasis& a, double b) {
223     if(a.isZero())
224         a.push_back(Linear(b,b));
225     else
226         a[0] += b;
227     return a;
229 inline SBasis& operator-=(SBasis& a, double b) {
230     if(a.isZero())
231         a.push_back(Linear(-b,-b));
232     else
233         a[0] -= b;
234     return a;
237 SBasis shift(SBasis const &a, int sh);
238 SBasis shift(Linear const &a, int sh);
240 inline SBasis truncate(SBasis const &a, unsigned terms) {
241     SBasis c;
242     c.insert(c.begin(), a.begin(), a.begin() + std::min(terms, (unsigned)a.size()));
243     return c;
246 SBasis multiply(SBasis const &a, SBasis const &b);
248 SBasis integral(SBasis const &c);
249 SBasis derivative(SBasis const &a);
251 SBasis sqrt(SBasis const &a, int k);
253 // return a kth order approx to 1/a)
254 SBasis reciprocal(Linear const &a, int k);
255 SBasis divide(SBasis const &a, SBasis const &b, int k);
257 inline SBasis operator*(SBasis const & a, SBasis const & b) {
258     return multiply(a, b);
261 inline SBasis& operator*=(SBasis& a, SBasis const & b) {
262     a = multiply(a, b);
263     return a;
266 //valuation: degree of the first non zero coefficient.
267 inline unsigned 
268 valuation(SBasis const &a, double tol=0){
269     unsigned val=0;
270     while( val<a.size() &&
271            fabs(a[val][0])<tol &&
272            fabs(a[val][1])<tol ) 
273         val++;
274     return val;
277 // a(b(t))
278 SBasis compose(SBasis const &a, SBasis const &b);
279 SBasis compose(SBasis const &a, SBasis const &b, unsigned k);
280 SBasis inverse(SBasis a, int k);
281 //compose_inverse(f,g)=compose(f,inverse(g)), but is numerically more stable in some good cases...
282 //TODO: requires g(0)=0 & g(1)=1 atm. generalization should be obvious.
283 SBasis compose_inverse(SBasis const &f, SBasis const &g, unsigned order=2, double tol=1e-3);
285 inline SBasis portion(const SBasis &t, double from, double to) { return compose(t, Linear(from, to)); }
287 // compute f(g)
288 inline SBasis
289 SBasis::operator()(SBasis const & g) const {
290     return compose(*this, g);
292  
293 inline std::ostream &operator<< (std::ostream &out_file, const Linear &bo) {
294     out_file << "{" << bo[0] << ", " << bo[1] << "}";
295     return out_file;
298 inline std::ostream &operator<< (std::ostream &out_file, const SBasis & p) {
299     for(unsigned i = 0; i < p.size(); i++) {
300         out_file << p[i] << "s^" << i << " + ";
301     }
302     return out_file;
305 SBasis sin(Linear bo, int k);
306 SBasis cos(Linear bo, int k);
308 std::vector<double> roots(SBasis const & s);
309 std::vector<std::vector<double> > multi_roots(SBasis const &f,
310                                  std::vector<double> const &levels,
311                                  double htol=1e-7,
312                                  double vtol=1e-7,
313                                  double a=0,
314                                  double b=1);
315     
318 /*
319   Local Variables:
320   mode:c++
321   c-file-style:"stroustrup"
322   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
323   indent-tabs-mode:nil
324   fill-column:99
325   End:
326 */
327 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :
328 #endif