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.
41
/*** At some point we should work on tighter bounds for the error. It is clear that the error is
42
* bounded by the L1 norm over the tail of the series, but this is very loose, leading to far too
43
* many cubic beziers. I've changed this to be \sum _i=tail ^\infty |hat a_i| 2^-i but I have no
44
* evidence that this is correct.
48
double SBasis::tail_error(unsigned tail) const {
49
double err = 0, s = 1./(1<<(2*tail)); // rough
50
for(unsigned i = tail; i < size(); i++) {
51
err += (fabs((*this)[i][0]) + fabs((*this)[i][1]))*s;
58
double SBasis::tailError(unsigned tail) const {
59
Interval bs = bounds_fast(*this, tail);
60
return std::max(fabs(bs.min()),fabs(bs.max()));
63
bool SBasis::isFinite() const {
64
for(unsigned i = 0; i < size(); i++) {
65
if(!(*this)[i].isFinite())
71
SBasis operator+(const SBasis& a, const SBasis& b) {
73
const unsigned out_size = std::max(a.size(), b.size());
74
const unsigned min_size = std::min(a.size(), b.size());
75
result.reserve(out_size);
77
for(unsigned i = 0; i < min_size; i++) {
78
result.push_back(a[i] + b[i]);
80
for(unsigned i = min_size; i < a.size(); i++)
81
result.push_back(a[i]);
82
for(unsigned i = min_size; i < b.size(); i++)
83
result.push_back(b[i]);
85
assert(result.size() == out_size);
89
SBasis operator-(const SBasis& a, const SBasis& b) {
91
const unsigned out_size = std::max(a.size(), b.size());
92
const unsigned min_size = std::min(a.size(), b.size());
93
result.reserve(out_size);
95
for(unsigned i = 0; i < min_size; i++) {
96
result.push_back(a[i] - b[i]);
98
for(unsigned i = min_size; i < a.size(); i++)
99
result.push_back(a[i]);
100
for(unsigned i = min_size; i < b.size(); i++)
101
result.push_back(-b[i]);
103
assert(result.size() == out_size);
107
SBasis& operator+=(SBasis& a, const SBasis& b) {
108
const unsigned out_size = std::max(a.size(), b.size());
109
const unsigned min_size = std::min(a.size(), b.size());
112
for(unsigned i = 0; i < min_size; i++)
114
for(unsigned i = min_size; i < b.size(); i++)
117
assert(a.size() == out_size);
121
SBasis& operator-=(SBasis& a, const SBasis& b) {
122
const unsigned out_size = std::max(a.size(), b.size());
123
const unsigned min_size = std::min(a.size(), b.size());
126
for(unsigned i = 0; i < min_size; i++)
128
for(unsigned i = min_size; i < b.size(); i++)
131
assert(a.size() == out_size);
135
SBasis operator*(SBasis const &a, double k) {
138
for(unsigned i = 0; i < a.size(); i++)
139
c.push_back(a[i] * k);
143
SBasis& operator*=(SBasis& a, double b) {
144
if (a.isZero()) return a;
148
for(unsigned i = 0; i < a.size(); i++)
153
SBasis shift(SBasis const &a, int sh) {
156
c.insert(c.begin(), sh, Linear(0,0));
163
SBasis shift(Linear const &a, int sh) {
166
c.insert(c.begin(), sh, Linear(0,0));
172
SBasis multiply(SBasis const &a, SBasis const &b) {
173
// c = {a0*b0 - shift(1, a.Tri*b.Tri), a1*b1 - shift(1, a.Tri*b.Tri)}
175
// shift(1, a.Tri*b.Tri)
177
if(a.isZero() || b.isZero())
179
c.resize(a.size() + b.size(), Linear(0,0));
181
for(unsigned j = 0; j < b.size(); j++) {
182
for(unsigned i = j; i < a.size()+j; i++) {
183
double tri = Tri(b[j])*Tri(a[i-j]);
184
c[i+1/*shift*/] += Linear(Hat(-tri));
187
for(unsigned j = 0; j < b.size(); j++) {
188
for(unsigned i = j; i < a.size()+j; i++) {
189
for(unsigned dim = 0; dim < 2; dim++)
190
c[i][dim] += b[j][dim]*a[i-j][dim];
194
//assert(!(0 == c.back()[0] && 0 == c.back()[1]));
198
SBasis integral(SBasis const &c) {
200
a.resize(c.size() + 1, Linear(0,0));
203
for(unsigned k = 1; k < c.size() + 1; k++) {
204
double ahat = -Tri(c[k-1])/(2*k);
208
for(int k = c.size()-1; k >= 0; k--) {
209
aTri = (Hat(c[k]).d + (k+1)*aTri/2)/(2*k+1);
217
SBasis derivative(SBasis const &a) {
219
c.resize(a.size(), Linear(0,0));
221
for(unsigned k = 0; k < a.size(); k++) {
222
double d = (2*k+1)*Tri(a[k]);
224
for(unsigned dim = 0; dim < 2; dim++) {
228
c[k][dim] = d - (k+1)*a[k+1][dim];
230
c[k][dim] = d + (k+1)*a[k+1][dim];
238
//TODO: convert int k to unsigned k, and remove cast
239
SBasis sqrt(SBasis const &a, int k) {
241
if(a.isZero() || k == 0)
243
c.resize(k, Linear(0,0));
244
c[0] = Linear(std::sqrt(a[0][0]), std::sqrt(a[0][1]));
245
SBasis r = a - multiply(c, c); // remainder
247
for(unsigned i = 1; i <= (unsigned)k and i<r.size(); i++) {
248
Linear ci(r[i][0]/(2*c[0][0]), r[i][1]/(2*c[0][1]));
249
SBasis cisi = shift(ci, i);
250
r -= multiply(shift((c*2 + cisi), i), SBasis(ci));
253
if(r.tailError(i) == 0) // if exact
260
// return a kth order approx to 1/a)
261
SBasis reciprocal(Linear const &a, int k) {
264
c.resize(k, Linear(0,0));
265
double r_s0 = (Tri(a)*Tri(a))/(-a[0]*a[1]);
267
for(unsigned i = 0; i < (unsigned)k; i++) {
268
c[i] = Linear(r_s0k/a[0], r_s0k/a[1]);
274
SBasis divide(SBasis const &a, SBasis const &b, int k) {
277
SBasis r = a; // remainder
280
r.resize(k, Linear(0,0));
281
c.resize(k, Linear(0,0));
283
for(unsigned i = 0; i < (unsigned)k; i++) {
284
Linear ci(r[i][0]/b[0][0], r[i][1]/b[0][1]); //H0
286
r -= shift(multiply(ci,b), i);
288
if(r.tailError(i) == 0) // if exact
296
// return a0 + s(a1 + s(a2 +... where s = (1-u)u; ak =(1 - u)a^0_k + ua^1_k
297
SBasis compose(SBasis const &a, SBasis const &b) {
298
SBasis s = multiply((SBasis(Linear(1,1))-b), b);
301
for(int i = a.size()-1; i >= 0; i--) {
302
r = SBasis(Linear(Hat(a[i][0]))) - b*a[i][0] + b*a[i][1] + multiply(r,s);
308
// return a0 + s(a1 + s(a2 +... where s = (1-u)u; ak =(1 - u)a^0_k + ua^1_k
309
SBasis compose(SBasis const &a, SBasis const &b, unsigned k) {
310
SBasis s = multiply((SBasis(Linear(1,1))-b), b);
313
for(int i = a.size()-1; i >= 0; i--) {
314
r = SBasis(Linear(Hat(a[i][0]))) - b*a[i][0] + b*a[i][1] + multiply(r,s);
321
Inversion algorithm. The notation is certainly very misleading. The
322
pseudocode should say:
327
c_i(v) := H_0(r_i(u)/(t_1)^i; u)
328
c(v) := c(v) + c_i(v)*t^i
329
r(u) := r(u) ? c_i(u)*(t(u))^i
333
//#define DEBUG_INVERSION 1
335
SBasis inverse(SBasis a, int k) {
336
assert(a.size() > 0);
337
// the function should have 'unit range'("a00 = 0 and a01 = 1") and be monotonic.
343
assert(a1 != 0); // not invertable.
348
SBasis c; // c(v) := 0
349
if(a.size() >= 2 && k == 2) {
350
c.push_back(Linear(0,1));
351
Linear t1(1+a[1][0], 1-a[1][1]); // t_1
352
c.push_back(Linear(-a[1][0]/t1[0], -a[1][1]/t1[1]));
353
} else if(a.size() >= 2) { // non linear
354
SBasis r = Linear(0,1); // r(u) := r_0(u) := u
355
Linear t1(1./(1+a[1][0]), 1./(1-a[1][1])); // 1./t_1
357
Linear t1i = one; // t_1^0
358
SBasis one_minus_a = SBasis(one) - a;
359
SBasis t = multiply(one_minus_a, a); // t(u)
360
SBasis ti(one); // t(u)^0
361
#ifdef DEBUG_INVERSION
362
std::cout << "a=" << a << std::endl;
363
std::cout << "1-a=" << one_minus_a << std::endl;
364
std::cout << "t1=" << t1 << std::endl;
365
//assert(t1 == t[1]);
368
c.resize(k+1, Linear(0,0));
369
for(unsigned i = 0; i < (unsigned)k; i++) { // for i:=0 to k do
370
#ifdef DEBUG_INVERSION
371
std::cout << "-------" << i << ": ---------" <<std::endl;
372
std::cout << "r=" << r << std::endl
373
<< "c=" << c << std::endl
374
<< "ti=" << ti << std::endl
377
if(r.size() <= i) // ensure enough space in the remainder, probably not needed
378
r.resize(i+1, Linear(0,0));
379
Linear ci(r[i][0]*t1i[0], r[i][1]*t1i[1]); // c_i(v) := H_0(r_i(u)/(t_1)^i; u)
380
#ifdef DEBUG_INVERSION
381
std::cout << "t1i=" << t1i << std::endl;
382
std::cout << "ci=" << ci << std::endl;
384
for(int dim = 0; dim < 2; dim++) // t1^-i *= 1./t1
386
c[i] = ci; // c(v) := c(v) + c_i(v)*t^i
387
// change from v to u parameterisation
388
SBasis civ = one_minus_a*ci[0] + a*ci[1];
389
// r(u) := r(u) - c_i(u)*(t(u))^i
390
// We can truncate this to the number of final terms, as no following terms can
391
// contribute to the result.
392
r -= multiply(civ,ti);
394
if(r.tailError(i) == 0)
398
#ifdef DEBUG_INVERSION
399
std::cout << "##########################" << std::endl;
402
c = Linear(0,1); // linear
403
c -= a0; // invert the offset
404
c /= a1; // invert the slope
408
SBasis sin(Linear b, int k) {
409
SBasis s = Linear(std::sin(b[0]), std::sin(b[1]));
412
s.push_back(Linear(std::cos(b[0])*t2 - tr, -std::cos(b[1])*t2 + tr));
415
for(int i = 0; i < k; i++) {
416
Linear bo(4*(i+1)*s[i+1][0] - 2*s[i+1][1],
417
-2*s[i+1][0] + 4*(i+1)*s[i+1][1]);
418
bo -= s[i]*(t2/(i+1));
421
s.push_back(bo/double(i+2));
427
SBasis cos(Linear bo, int k) {
428
return sin(Linear(bo[0] + M_PI/2,
433
//compute fog^-1. ("zero" = double comparison threshold. *!*we might divide by "zero"*!*)
434
//TODO: compute order according to tol?
435
//TODO: requires g(0)=0 & g(1)=1 atm... adaptation to other cases should be obvious!
436
SBasis compose_inverse(SBasis const &f, SBasis const &g, unsigned order, double zero){
437
SBasis result; //result
438
SBasis r=f; //remainder
439
SBasis Pk=Linear(1)-g,Qk=g,sg=Pk*Qk;
442
Pk.resize(order,Linear(0.));
443
Qk.resize(order,Linear(0.));
444
r.resize(order,Linear(0.));
446
int vs= valuation(sg,zero);
448
for (unsigned k=0; k<order; k+=vs){
449
double p10 = Pk.at(k)[0];// we have to solve the linear system:
450
double p01 = Pk.at(k)[1];//
451
double q10 = Qk.at(k)[0];// p10*a + q10*b = r10
452
double q01 = Qk.at(k)[1];// &
453
double r10 = r.at(k)[0];// p01*a + q01*b = r01
454
double r01 = r.at(k)[1];//
456
double det = p10*q01-p01*q10;
458
//TODO: handle det~0!!
463
a=( q01*r10-q10*r01)/det;
464
b=(-p01*r10+p10*r01)/det;
466
result.push_back(Linear(a,b));
484
c-file-style:"stroustrup"
485
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
490
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :