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"
44 namespace Geom {
46 /*** An empty SBasis is identically 0. */
47 class SBasis : public std::vector<Linear>{
48 public:
49 SBasis() {}
50 explicit SBasis(double a) {
51 push_back(Linear(a,a));
52 }
53 SBasis(SBasis const & a) :
54 std::vector<Linear>(a)
55 {}
56 SBasis(Linear const & bo) {
57 push_back(bo);
58 }
60 //IMPL: FragmentConcept
61 typedef double output_type;
62 inline bool isZero() const {
63 if(empty()) return true;
64 for(unsigned i = 0; i < size(); i++) {
65 if(!(*this)[i].isZero()) return false;
66 }
67 return true;
68 }
69 bool isFinite() const;
70 inline double at0() const {
71 if(empty()) return 0; else return (*this)[0][0];
72 }
73 inline double at1() const{
74 if(empty()) return 0; else return (*this)[0][1];
75 }
77 double valueAt(double t) const {
78 double s = t*(1-t);
79 double p0 = 0, p1 = 0;
80 double sk = 1;
81 //TODO: rewrite as horner
82 for(unsigned k = 0; k < size(); k++) {
83 p0 += sk*(*this)[k][0];
84 p1 += sk*(*this)[k][1];
85 sk *= s;
86 }
87 return (1-t)*p0 + t*p1;
88 }
89 double valueAndDerivative(double t, double &der) const {
90 double s = t*(1-t);
91 double p0 = 0, p1 = 0;
92 double sk = 1;
93 //TODO: rewrite as horner
94 for(unsigned k = 0; k < size(); k++) {
95 p0 += sk*(*this)[k][0];
96 p1 += sk*(*this)[k][1];
97 sk *= s;
98 }
99 // p0 and p1 at this point form a linear approximation at t
100 der = p1 - p0;
101 return (1-t)*p0 + t*p1;
102 }
103 double operator()(double t) const {
104 return valueAt(t);
105 }
107 std::vector<double> valueAndDerivatives(double /*t*/, unsigned /*n*/) const {
108 //TODO
109 throwNotImplemented();
110 //throw(NotImplemented(__FILE__, __LINE__));
111 }
113 SBasis toSBasis() const { return SBasis(*this); }
115 double tailError(unsigned tail) const;
117 // compute f(g)
118 SBasis operator()(SBasis const & g) const;
120 Linear operator[](unsigned i) const {
121 assert(i < size());
122 return std::vector<Linear>::operator[](i);
123 }
125 //MUTATOR PRISON
126 Linear& operator[](unsigned i) { return this->at(i); }
128 //remove extra zeros
129 void normalize() {
130 while(!empty() && 0 == back()[0] && 0 == back()[1])
131 pop_back();
132 }
133 void truncate(unsigned k) { if(k < size()) resize(k); }
134 };
136 //TODO: figure out how to stick this in linear, while not adding an sbasis dep
137 inline SBasis Linear::toSBasis() const { return SBasis(*this); }
139 //implemented in sbasis-roots.cpp
140 Interval bounds_exact(SBasis const &a);
141 Interval bounds_fast(SBasis const &a, int order = 0);
142 Interval bounds_local(SBasis const &a, const Interval &t, int order = 0);
144 inline SBasis reverse(SBasis const &a) {
145 SBasis result;
146 result.reserve(a.size());
147 for(unsigned k = 0; k < a.size(); k++)
148 result.push_back(reverse(a[k]));
149 return result;
150 }
152 //IMPL: ScalableConcept
153 inline SBasis operator-(const SBasis& p) {
154 if(p.isZero()) return SBasis();
155 SBasis result;
156 result.reserve(p.size());
158 for(unsigned i = 0; i < p.size(); i++) {
159 result.push_back(-p[i]);
160 }
161 return result;
162 }
163 SBasis operator*(SBasis const &a, double k);
164 inline SBasis operator*(double k, SBasis const &a) { return a*k; }
165 inline SBasis operator/(SBasis const &a, double k) { return a*(1./k); }
166 SBasis& operator*=(SBasis& a, double b);
167 inline SBasis& operator/=(SBasis& a, double b) { return (a*=(1./b)); }
169 //IMPL: AddableConcept
170 SBasis operator+(const SBasis& a, const SBasis& b);
171 SBasis operator-(const SBasis& a, const SBasis& b);
172 SBasis& operator+=(SBasis& a, const SBasis& b);
173 SBasis& operator-=(SBasis& a, const SBasis& b);
175 //TODO: remove?
176 inline SBasis operator+(const SBasis & a, Linear const & b) {
177 if(b.isZero()) return a;
178 if(a.isZero()) return b;
179 SBasis result(a);
180 result[0] += b;
181 return result;
182 }
183 inline SBasis operator-(const SBasis & a, Linear const & b) {
184 if(b.isZero()) return a;
185 SBasis result(a);
186 result[0] -= b;
187 return result;
188 }
189 inline SBasis& operator+=(SBasis& a, const Linear& b) {
190 if(a.isZero())
191 a.push_back(b);
192 else
193 a[0] += b;
194 return a;
195 }
196 inline SBasis& operator-=(SBasis& a, const Linear& b) {
197 if(a.isZero())
198 a.push_back(-b);
199 else
200 a[0] -= b;
201 return a;
202 }
204 //IMPL: OffsetableConcept
205 inline SBasis operator+(const SBasis & a, double b) {
206 if(a.isZero()) return Linear(b, b);
207 SBasis result(a);
208 result[0] += b;
209 return result;
210 }
211 inline SBasis operator-(const SBasis & a, double b) {
212 if(a.isZero()) return Linear(-b, -b);
213 SBasis result(a);
214 result[0] -= b;
215 return result;
216 }
217 inline SBasis& operator+=(SBasis& a, double b) {
218 if(a.isZero())
219 a.push_back(Linear(b,b));
220 else
221 a[0] += b;
222 return a;
223 }
224 inline SBasis& operator-=(SBasis& a, double b) {
225 if(a.isZero())
226 a.push_back(Linear(-b,-b));
227 else
228 a[0] -= b;
229 return a;
230 }
232 SBasis shift(SBasis const &a, int sh);
233 SBasis shift(Linear const &a, int sh);
235 inline SBasis truncate(SBasis const &a, unsigned terms) {
236 SBasis c;
237 c.insert(c.begin(), a.begin(), a.begin() + std::min(terms, (unsigned)a.size()));
238 return c;
239 }
241 SBasis multiply(SBasis const &a, SBasis const &b);
243 SBasis integral(SBasis const &c);
244 SBasis derivative(SBasis const &a);
246 SBasis sqrt(SBasis const &a, int k);
248 // return a kth order approx to 1/a)
249 SBasis reciprocal(Linear const &a, int k);
250 SBasis divide(SBasis const &a, SBasis const &b, int k);
252 inline SBasis operator*(SBasis const & a, SBasis const & b) {
253 return multiply(a, b);
254 }
256 inline SBasis& operator*=(SBasis& a, SBasis const & b) {
257 a = multiply(a, b);
258 return a;
259 }
261 //valuation: degree of the first non zero coefficient.
262 inline unsigned
263 valuation(SBasis const &a, double tol=0){
264 unsigned val=0;
265 while( val<a.size() &&
266 fabs(a[val][0])<tol &&
267 fabs(a[val][1])<tol )
268 val++;
269 return val;
270 }
272 // a(b(t))
273 SBasis compose(SBasis const &a, SBasis const &b);
274 SBasis compose(SBasis const &a, SBasis const &b, unsigned k);
275 SBasis inverse(SBasis a, int k);
276 //compose_inverse(f,g)=compose(f,inverse(g)), but is numerically more stable in some good cases...
277 //TODO: requires g(0)=0 & g(1)=1 atm. generalization should be obvious.
278 SBasis compose_inverse(SBasis const &f, SBasis const &g, unsigned order=2, double tol=1e-3);
280 inline SBasis portion(const SBasis &t, double from, double to) { return compose(t, Linear(from, to)); }
282 // compute f(g)
283 inline SBasis
284 SBasis::operator()(SBasis const & g) const {
285 return compose(*this, g);
286 }
288 inline std::ostream &operator<< (std::ostream &out_file, const Linear &bo) {
289 out_file << "{" << bo[0] << ", " << bo[1] << "}";
290 return out_file;
291 }
293 inline std::ostream &operator<< (std::ostream &out_file, const SBasis & p) {
294 for(unsigned i = 0; i < p.size(); i++) {
295 out_file << p[i] << "s^" << i << " + ";
296 }
297 return out_file;
298 }
300 SBasis sin(Linear bo, int k);
301 SBasis cos(Linear bo, int k);
303 std::vector<double> roots(SBasis const & s);
304 std::vector<std::vector<double> > multi_roots(SBasis const &f,
305 std::vector<double> const &levels,
306 double htol=1e-7,
307 double vtol=1e-7,
308 double a=0,
309 double b=1);
311 }
313 /*
314 Local Variables:
315 mode:c++
316 c-file-style:"stroustrup"
317 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
318 indent-tabs-mode:nil
319 fill-column:99
320 End:
321 */
322 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :
323 #endif