sbasis.cpp revision a41e1396ee67d054e39c9ea60fc05a521a8d3785
/*
* sbasis.cpp - S-power basis function class + supporting classes
*
* Authors:
* Nathan Hurst <njh@mail.csse.monash.edu.au>
* Michael Sloan <mgsloan@gmail.com>
*
* Copyright (C) 2006-2007 authors
*
* modify it either under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation
* (the "LGPL") or, at your option, under the terms of the Mozilla
* Public License Version 1.1 (the "MPL"). If you do not alter this
* notice, a recipient may use your version of this file under either
* the MPL or the LGPL.
*
* You should have received a copy of the LGPL along with this library
* in the file COPYING-LGPL-2.1; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* You should have received a copy of the MPL along with this library
* in the file COPYING-MPL-1.1
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
* OF ANY KIND, either express or implied. See the LGPL or the MPL for
* the specific language governing rights and limitations.
*/
#include <cmath>
namespace Geom{
/** bound the error from term truncation
\param tail first term to chop
\returns the largest possible error this truncation could give
*/
}
/** test all coefficients are finite
*/
for(unsigned i = 0; i < size(); i++) {
if(!(*this)[i].isFinite())
return false;
}
return true;
}
/** Compute the value and the first n derivatives
\param t position to evaluate
\param n number of derivatives (not counting value)
\returns a vector with the value and the n derivative evaluations
There is an elegant way to compute the value and n derivatives for a polynomial using a variant of horner's rule. Someone will someday work out how for sbasis.
*/
for(unsigned i = 1; i < n+1; i++) {
}
return ret;
}
/** Compute the pointwise sum of a and b (Exact)
\param a,b sbasis functions
\returns sbasis equal to a+b
*/
for(unsigned i = 0; i < min_size; i++) {
result[i] = a[i] + b[i];
}
result[i] = a[i];
result[i] = b[i];
return result;
}
/** Compute the pointwise difference of a and b (Exact)
\param a,b sbasis functions
\returns sbasis equal to a-b
*/
for(unsigned i = 0; i < min_size; i++) {
result[i] = a[i] - b[i];
}
result[i] = a[i];
result[i] = -b[i];
return result;
}
/** Compute the pointwise sum of a and b and store in a (Exact)
\param a,b sbasis functions
\returns sbasis equal to a+b
*/
for(unsigned i = 0; i < min_size; i++)
a[i] += b[i];
a[i] = b[i];
return a;
}
/** Compute the pointwise difference of a and b and store in a (Exact)
\param a,b sbasis functions
\returns sbasis equal to a-b
*/
for(unsigned i = 0; i < min_size; i++)
a[i] -= b[i];
a[i] = -b[i];
return a;
}
/** Compute the pointwise product of a and b (Exact)
\param a,b sbasis functions
\returns sbasis equal to a*b
*/
for(unsigned i = 0; i < a.size(); i++)
c[i] = a[i] * k;
return c;
}
/** Compute the pointwise product of a and b and store the value in a (Exact)
\param a,b sbasis functions
\returns sbasis equal to a*b
*/
if (a.isZero()) return a;
if (b == 0)
a.clear();
else
for(unsigned i = 0; i < a.size(); i++)
a[i] *= b;
return a;
}
/** multiply a by x^sh in place (Exact)
\param a sbasis function
\param sh power
\returns a
*/
for(int i = 0; i < sh; i++)
c[i] = Linear(0,0);
c[i] = a[j];
return c;
}
/** multiply a by x^sh (Exact)
\param a linear function
\param sh power
\returns a* x^sh
*/
for(int i = 0; i < sh; i++)
c[i] = Linear(0,0);
if(sh >= 0)
c[sh] = a;
return c;
}
#if 0
// c = {a0*b0 - shift(1, a.Tri*b.Tri), a1*b1 - shift(1, a.Tri*b.Tri)}
// shift(1, a.Tri*b.Tri)
return c;
for(unsigned j = 0; j < b.size(); j++) {
for(unsigned i = j; i < a.size()+j; i++) {
}
}
for(unsigned j = 0; j < b.size(); j++) {
for(unsigned i = j; i < a.size()+j; i++) {
}
}
c.normalize();
//assert(!(0 == c.back()[0] && 0 == c.back()[1]));
return c;
}
#else
/** Compute the pointwise product of a and b adding c (Exact)
\param a,b,c sbasis functions
\returns sbasis equal to a*b+c
The added term is almost free
*/
return c;
for(unsigned j = 0; j < b.size(); j++) {
for(unsigned i = j; i < a.size()+j; i++) {
}
}
for(unsigned j = 0; j < b.size(); j++) {
for(unsigned i = j; i < a.size()+j; i++) {
}
}
c.normalize();
//assert(!(0 == c.back()[0] && 0 == c.back()[1]));
return c;
}
/** Compute the pointwise product of a and b (Exact)
\param a,b sbasis functions
\returns sbasis equal to a*b
*/
return c;
return multiply_add(a, b, c);
}
#endif
/** Compute the integral of a (Exact)
\param a sbasis functions
\returns sbasis integral(a)
*/
SBasis a;
a[0] = Linear(0,0);
a[k][0] = a[k][1] = ahat;
}
double aTri = 0;
for(int k = c.size()-1; k >= 0; k--) {
a[k][0] -= aTri/2;
}
a.normalize();
return a;
}
/** Compute the derivative of a (Exact)
\param a sbasis functions
*/
SBasis c;
if(a.isZero())
return c;
for(unsigned k = 0; k < a.size()-1; k++) {
double d = (2*k+1)*(a[k][1] - a[k][0]);
c[k][0] = d + (k+1)*a[k+1][0];
c[k][1] = d - (k+1)*a[k+1][1];
}
int k = a.size()-1;
double d = (2*k+1)*(a[k][1] - a[k][0]);
if(d == 0)
c.pop_back();
else {
c[k][0] = d;
c[k][1] = d;
}
return c;
}
/** Compute the derivative of this inplace (Exact)
*/
if(isZero()) return;
for(unsigned k = 0; k < size()-1; k++) {
double d = (2*k+1)*((*this)[k][1] - (*this)[k][0]);
(*this)[k][0] = d + (k+1)*(*this)[k+1][0];
(*this)[k][1] = d - (k+1)*(*this)[k+1][1];
}
int k = size()-1;
double d = (2*k+1)*((*this)[k][1] - (*this)[k][0]);
if(d == 0)
pop_back();
else {
(*this)[k][0] = d;
(*this)[k][1] = d;
}
}
/** Compute the sqrt of a
\param a sbasis functions
\returns sbasis \f[ \sqrt{a} \f]
It is recommended to use the piecewise version unless you have good reason.
TODO: convert int k to unsigned k, and remove cast
*/
SBasis c;
if(a.isZero() || k == 0)
return c;
for(unsigned i = 1; i <= (unsigned)k && i<r.size(); i++) {
r.truncate(k+1);
c += cisi;
if(r.tailError(i) == 0) // if exact
break;
}
return c;
}
/** Compute the recpirocal of a
\param a sbasis functions
\returns sbasis 1/a
It is recommended to use the piecewise version unless you have good reason.
*/
SBasis c;
double r_s0k = 1;
for(unsigned i = 0; i < (unsigned)k; i++) {
}
return c;
}
/** Compute a / b to k terms
\param a,b sbasis functions
\returns sbasis a/b
It is recommended to use the piecewise version unless you have good reason.
*/
SBasis c;
SBasis r = a; // remainder
k++;
for(unsigned i = 0; i < (unsigned)k; i++) {
c[i] += ci;
r.truncate(k+1);
if(r.tailError(i) == 0) // if exact
break;
}
return c;
}
/** Compute a composed with b
\param a,b sbasis functions
\returns sbasis a(b(t))
return a0 + s(a1 + s(a2 +... where s = (1-u)u; ak =(1 - u)a^0_k + ua^1_k
*/
SBasis r;
for(int i = a.size()-1; i >= 0; i--) {
}
return r;
}
/** Compute a composed with b to k terms
\param a,b sbasis functions
\returns sbasis a(b(t))
return a0 + s(a1 + s(a2 +... where s = (1-u)u; ak =(1 - u)a^0_k + ua^1_k
*/
SBasis r;
for(int i = a.size()-1; i >= 0; i--) {
}
r.truncate(k);
return r;
}
/*
Inversion algorithm. The notation is certainly very misleading. The
pseudocode should say:
c(v) := 0
r(u) := r_0(u) := u
for i:=0 to k do
c_i(v) := H_0(r_i(u)/(t_1)^i; u)
c(v) := c(v) + c_i(v)*t^i
r(u) := r(u) ? c_i(u)*(t(u))^i
endfor
*/
//#define DEBUG_INVERSION 1
/** find the function a^-1 such that a^-1 composed with a to k terms is the identity function
\param a sbasis function
\returns sbasis a^-1 s.t. a^-1(a(t)) = 1
The function must have 'unit range'("a00 = 0 and a01 = 1") and be monotonic.
*/
double a0 = a[0][0];
if(a0 != 0) {
a -= a0;
}
double a1 = a[0][1];
if(a1 != 1) {
a /= a1;
}
c[0] = Linear(0,1);
#ifdef DEBUG_INVERSION
//assert(t1 == t[1]);
#endif
//c.resize(k+1, Linear(0,0));
for(unsigned i = 0; i < (unsigned)k; i++) { // for i:=0 to k do
#ifdef DEBUG_INVERSION
#endif
if(r.size() <= i) // ensure enough space in the remainder, probably not needed
#ifdef DEBUG_INVERSION
#endif
c[i] = ci; // c(v) := c(v) + c_i(v)*t^i
// change from v to u parameterisation
// r(u) := r(u) - c_i(u)*(t(u))^i
// We can truncate this to the number of final terms, as no following terms can
// contribute to the result.
r.truncate(k);
if(r.tailError(i) == 0)
break; // yay!
}
#ifdef DEBUG_INVERSION
#endif
} else
c -= a0; // invert the offset
c /= a1; // invert the slope
return c;
}
/** Compute the sine of a to k terms
\param b linear function
\returns sbasis sin(a)
It is recommended to use the piecewise version unless you have good reason.
*/
for(int i = 0; i < k; i++) {
-2*s[i+1][0] + 4*(i+1)*s[i+1][1]);
}
return s;
}
/** Compute the cosine of a
\param b linear function
\returns sbasis cos(a)
It is recommended to use the piecewise version unless you have good reason.
*/
k);
}
/** compute fog^-1.
\param f,g sbasis functions
\returns sbasis f(g^-1(t)).
("zero" = double comparison threshold. *!*we might divide by "zero"*!*)
TODO: compute order according to tol?
TODO: requires g(0)=0 & g(1)=1 atm... adaptation to other cases should be obvious!
*/
SBasis r=f; //remainder
if (vs == 0) { // to prevent infinite loop
return result;
}
double a,b;
//TODO: handle det~0!!
a=b=0;
}else{
}
}
return result;
}
}
/*
Local Variables:
mode:c++
c-file-style:"stroustrup"
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
indent-tabs-mode:nil
fill-column:99
End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :