2
* sbasis.cpp - S-power basis function class + supporting classes
5
* Nathan Hurst <njh@mail.csse.monash.edu.au>
6
* Michael Sloan <mgsloan@gmail.com>
8
* Copyright (C) 2006-2007 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.
43
/*** At some point we should work on tighter bounds for the error. It is clear that the error is
44
* bounded by the L1 norm over the tail of the series, but this is very loose, leading to far too
45
* many cubic beziers. I've changed this to be \sum _i=tail ^\infty |hat a_i| 2^-i but I have no
46
* evidence that this is correct.
50
double SBasis::tail_error(unsigned tail) const {
51
double err = 0, s = 1./(1<<(2*tail)); // rough
52
for(unsigned i = tail; i < size(); i++) {
53
err += (fabs((*this)[i][0]) + fabs((*this)[i][1]))*s;
60
double SBasis::tailError(unsigned tail) const {
61
Interval bs = bounds_fast(*this, tail);
62
return std::max(fabs(bs.min()),fabs(bs.max()));
65
bool SBasis::isFinite() const {
66
for(unsigned i = 0; i < size(); i++) {
67
if(!(*this)[i].isFinite())
73
SBasis operator+(const SBasis& a, const SBasis& b) {
75
const unsigned out_size = std::max(a.size(), b.size());
76
const unsigned min_size = std::min(a.size(), b.size());
77
result.reserve(out_size);
79
for(unsigned i = 0; i < min_size; i++) {
80
result.push_back(a[i] + b[i]);
82
for(unsigned i = min_size; i < a.size(); i++)
83
result.push_back(a[i]);
84
for(unsigned i = min_size; i < b.size(); i++)
85
result.push_back(b[i]);
87
assert(result.size() == out_size);
91
SBasis operator-(const SBasis& a, const SBasis& b) {
93
const unsigned out_size = std::max(a.size(), b.size());
94
const unsigned min_size = std::min(a.size(), b.size());
95
result.reserve(out_size);
97
for(unsigned i = 0; i < min_size; i++) {
98
result.push_back(a[i] - b[i]);
100
for(unsigned i = min_size; i < a.size(); i++)
101
result.push_back(a[i]);
102
for(unsigned i = min_size; i < b.size(); i++)
103
result.push_back(-b[i]);
105
assert(result.size() == out_size);
109
SBasis& operator+=(SBasis& a, const SBasis& b) {
110
const unsigned out_size = std::max(a.size(), b.size());
111
const unsigned min_size = std::min(a.size(), b.size());
114
for(unsigned i = 0; i < min_size; i++)
116
for(unsigned i = min_size; i < b.size(); i++)
119
assert(a.size() == out_size);
123
SBasis& operator-=(SBasis& a, const SBasis& b) {
124
const unsigned out_size = std::max(a.size(), b.size());
125
const unsigned min_size = std::min(a.size(), b.size());
128
for(unsigned i = 0; i < min_size; i++)
130
for(unsigned i = min_size; i < b.size(); i++)
133
assert(a.size() == out_size);
137
SBasis operator*(SBasis const &a, double k) {
140
for(unsigned i = 0; i < a.size(); i++)
141
c.push_back(a[i] * k);
145
SBasis& operator*=(SBasis& a, double b) {
146
if (a.isZero()) return a;
150
for(unsigned i = 0; i < a.size(); i++)
155
SBasis shift(SBasis const &a, int sh) {
158
c.insert(c.begin(), sh, Linear(0,0));
165
SBasis shift(Linear const &a, int sh) {
168
c.insert(c.begin(), sh, Linear(0,0));
174
SBasis multiply(SBasis const &a, SBasis const &b) {
175
// c = {a0*b0 - shift(1, a.Tri*b.Tri), a1*b1 - shift(1, a.Tri*b.Tri)}
177
// shift(1, a.Tri*b.Tri)
179
if(a.isZero() || b.isZero())
181
c.resize(a.size() + b.size(), Linear(0,0));
183
for(unsigned j = 0; j < b.size(); j++) {
184
for(unsigned i = j; i < a.size()+j; i++) {
185
double tri = Tri(b[j])*Tri(a[i-j]);
186
c[i+1/*shift*/] += Linear(Hat(-tri));
189
for(unsigned j = 0; j < b.size(); j++) {
190
for(unsigned i = j; i < a.size()+j; i++) {
191
for(unsigned dim = 0; dim < 2; dim++)
192
c[i][dim] += b[j][dim]*a[i-j][dim];
196
//assert(!(0 == c.back()[0] && 0 == c.back()[1]));
200
SBasis integral(SBasis const &c) {
202
a.resize(c.size() + 1, Linear(0,0));
205
for(unsigned k = 1; k < c.size() + 1; k++) {
206
double ahat = -Tri(c[k-1])/(2*k);
210
for(int k = c.size()-1; k >= 0; k--) {
211
aTri = (Hat(c[k]).d + (k+1)*aTri/2)/(2*k+1);
219
SBasis derivative(SBasis const &a) {
221
c.resize(a.size(), Linear(0,0));
223
for(unsigned k = 0; k < a.size(); k++) {
224
double d = (2*k+1)*Tri(a[k]);
226
for(unsigned dim = 0; dim < 2; dim++) {
230
c[k][dim] = d - (k+1)*a[k+1][dim];
232
c[k][dim] = d + (k+1)*a[k+1][dim];
240
//TODO: convert int k to unsigned k, and remove cast
241
SBasis sqrt(SBasis const &a, int k) {
243
if(a.isZero() || k == 0)
245
c.resize(k, Linear(0,0));
246
c[0] = Linear(std::sqrt(a[0][0]), std::sqrt(a[0][1]));
247
SBasis r = a - multiply(c, c); // remainder
249
for(unsigned i = 1; i <= (unsigned)k && i<r.size(); i++) {
250
Linear ci(r[i][0]/(2*c[0][0]), r[i][1]/(2*c[0][1]));
251
SBasis cisi = shift(ci, i);
252
r -= multiply(shift((c*2 + cisi), i), SBasis(ci));
255
if(r.tailError(i) == 0) // if exact
262
// return a kth order approx to 1/a)
263
SBasis reciprocal(Linear const &a, int k) {
266
c.resize(k, Linear(0,0));
267
double r_s0 = (Tri(a)*Tri(a))/(-a[0]*a[1]);
269
for(unsigned i = 0; i < (unsigned)k; i++) {
270
c[i] = Linear(r_s0k/a[0], r_s0k/a[1]);
276
SBasis divide(SBasis const &a, SBasis const &b, int k) {
279
SBasis r = a; // remainder
282
r.resize(k, Linear(0,0));
283
c.resize(k, Linear(0,0));
285
for(unsigned i = 0; i < (unsigned)k; i++) {
286
Linear ci(r[i][0]/b[0][0], r[i][1]/b[0][1]); //H0
288
r -= shift(multiply(ci,b), i);
290
if(r.tailError(i) == 0) // if exact
298
// return a0 + s(a1 + s(a2 +... where s = (1-u)u; ak =(1 - u)a^0_k + ua^1_k
299
SBasis compose(SBasis const &a, SBasis const &b) {
300
SBasis s = multiply((SBasis(Linear(1,1))-b), b);
303
for(int i = a.size()-1; i >= 0; i--) {
304
r = SBasis(Linear(Hat(a[i][0]))) - b*a[i][0] + b*a[i][1] + multiply(r,s);
310
// return a0 + s(a1 + s(a2 +... where s = (1-u)u; ak =(1 - u)a^0_k + ua^1_k
311
SBasis compose(SBasis const &a, SBasis const &b, unsigned k) {
312
SBasis s = multiply((SBasis(Linear(1,1))-b), b);
315
for(int i = a.size()-1; i >= 0; i--) {
316
r = SBasis(Linear(Hat(a[i][0]))) - b*a[i][0] + b*a[i][1] + multiply(r,s);
323
Inversion algorithm. The notation is certainly very misleading. The
324
pseudocode should say:
329
c_i(v) := H_0(r_i(u)/(t_1)^i; u)
330
c(v) := c(v) + c_i(v)*t^i
331
r(u) := r(u) ? c_i(u)*(t(u))^i
335
//#define DEBUG_INVERSION 1
337
SBasis inverse(SBasis a, int k) {
338
assert(a.size() > 0);
339
// the function should have 'unit range'("a00 = 0 and a01 = 1") and be monotonic.
345
assert(a1 != 0); // not invertable.
350
SBasis c; // c(v) := 0
351
if(a.size() >= 2 && k == 2) {
352
c.push_back(Linear(0,1));
353
Linear t1(1+a[1][0], 1-a[1][1]); // t_1
354
c.push_back(Linear(-a[1][0]/t1[0], -a[1][1]/t1[1]));
355
} else if(a.size() >= 2) { // non linear
356
SBasis r = Linear(0,1); // r(u) := r_0(u) := u
357
Linear t1(1./(1+a[1][0]), 1./(1-a[1][1])); // 1./t_1
359
Linear t1i = one; // t_1^0
360
SBasis one_minus_a = SBasis(one) - a;
361
SBasis t = multiply(one_minus_a, a); // t(u)
362
SBasis ti(one); // t(u)^0
363
#ifdef DEBUG_INVERSION
364
std::cout << "a=" << a << std::endl;
365
std::cout << "1-a=" << one_minus_a << std::endl;
366
std::cout << "t1=" << t1 << std::endl;
367
//assert(t1 == t[1]);
370
c.resize(k+1, Linear(0,0));
371
for(unsigned i = 0; i < (unsigned)k; i++) { // for i:=0 to k do
372
#ifdef DEBUG_INVERSION
373
std::cout << "-------" << i << ": ---------" <<std::endl;
374
std::cout << "r=" << r << std::endl
375
<< "c=" << c << std::endl
376
<< "ti=" << ti << std::endl
379
if(r.size() <= i) // ensure enough space in the remainder, probably not needed
380
r.resize(i+1, Linear(0,0));
381
Linear ci(r[i][0]*t1i[0], r[i][1]*t1i[1]); // c_i(v) := H_0(r_i(u)/(t_1)^i; u)
382
#ifdef DEBUG_INVERSION
383
std::cout << "t1i=" << t1i << std::endl;
384
std::cout << "ci=" << ci << std::endl;
386
for(int dim = 0; dim < 2; dim++) // t1^-i *= 1./t1
388
c[i] = ci; // c(v) := c(v) + c_i(v)*t^i
389
// change from v to u parameterisation
390
SBasis civ = one_minus_a*ci[0] + a*ci[1];
391
// r(u) := r(u) - c_i(u)*(t(u))^i
392
// We can truncate this to the number of final terms, as no following terms can
393
// contribute to the result.
394
r -= multiply(civ,ti);
396
if(r.tailError(i) == 0)
400
#ifdef DEBUG_INVERSION
401
std::cout << "##########################" << std::endl;
404
c = Linear(0,1); // linear
405
c -= a0; // invert the offset
406
c /= a1; // invert the slope
410
SBasis sin(Linear b, int k) {
411
SBasis s = Linear(std::sin(b[0]), std::sin(b[1]));
414
s.push_back(Linear(std::cos(b[0])*t2 - tr, -std::cos(b[1])*t2 + tr));
417
for(int i = 0; i < k; i++) {
418
Linear bo(4*(i+1)*s[i+1][0] - 2*s[i+1][1],
419
-2*s[i+1][0] + 4*(i+1)*s[i+1][1]);
420
bo -= s[i]*(t2/(i+1));
423
s.push_back(bo/double(i+2));
429
SBasis cos(Linear bo, int k) {
430
return sin(Linear(bo[0] + M_PI_2,
435
//compute fog^-1. ("zero" = double comparison threshold. *!*we might divide by "zero"*!*)
436
//TODO: compute order according to tol?
437
//TODO: requires g(0)=0 & g(1)=1 atm... adaptation to other cases should be obvious!
438
SBasis compose_inverse(SBasis const &f, SBasis const &g, unsigned order, double zero){
439
SBasis result; //result
440
SBasis r=f; //remainder
441
SBasis Pk=Linear(1)-g,Qk=g,sg=Pk*Qk;
444
Pk.resize(order,Linear(0.));
445
Qk.resize(order,Linear(0.));
446
r.resize(order,Linear(0.));
448
int vs= valuation(sg,zero);
450
for (unsigned k=0; k<order; k+=vs){
451
double p10 = Pk.at(k)[0];// we have to solve the linear system:
452
double p01 = Pk.at(k)[1];//
453
double q10 = Qk.at(k)[0];// p10*a + q10*b = r10
454
double q01 = Qk.at(k)[1];// &
455
double r10 = r.at(k)[0];// p01*a + q01*b = r01
456
double r01 = r.at(k)[1];//
458
double det = p10*q01-p01*q10;
460
//TODO: handle det~0!!
465
a=( q01*r10-q10*r01)/det;
466
b=(-p01*r10+p10*r01)/det;
468
result.push_back(Linear(a,b));
486
c-file-style:"stroustrup"
487
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
492
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :