/**
* @file
* @brief Root finding for sbasis functions.
*//*
* Authors:
* Nathan Hurst <njh@njhurst.com>
* JF Barraud
* Copyright 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.
*
*/
/*
* It is more efficient to find roots of f(t) = c_0, c_1, ... all at once, rather than iterating.
*
* multi-roots using bernstein method, one approach would be:
sort c
take median and find roots of that
whenever a segment lies entirely on one side of the median,
find the median of the half and recurse.
in essence we are implementing quicksort on a continuous function
* the gsl poly roots finder is faster than bernstein too, but we don't use it for 3 reasons:
a) it requires convertion to poly, which is numerically unstable
b) it requires gsl (which is currently not a dependency, and would bring in a whole slew of unrelated stuff)
c) it finds all roots, even complex ones. We don't want to accidently treat a nearly real root as a real root
From memory gsl poly roots was about 10 times faster than bernstein in the case where all the roots
are in [0,1] for polys of order 5. I spent some time working out whether eigenvalue root finding
could be done directly in sbasis space, but the maths was too hard for me. -- njh
jfbarraud: eigenvalue root finding could be done directly in sbasis space ?
njh: I don't know, I think it should. You would make a matrix whose characteristic polynomial was
correct, but do it by putting the sbasis terms in the right spots in the matrix. normal eigenvalue
root finding makes a matrix that is a diagonal + a row along the top. This matrix has the property
that its characteristic poly is just the poly whose coefficients are along the top row.
Now an sbasis function is a linear combination of the poly coeffs. So it seems to me that you
should be able to put the sbasis coeffs directly into a matrix in the right spots so that the
characteristic poly is the sbasis. You'll still have problems b) and c).
We might be able to lift an eigenvalue solver and include that directly into 2geom. Eigenvalues
also allow you to find intersections of multiple curves but require solving n*m x n*m matrices.
**/
#include <cmath>
#include <map>
using namespace std;
namespace Geom{
/** Find the smallest interval that bounds a
\param a sbasis function
\returns inteval
*/
#ifdef USE_SBASIS_OF
}
return result;
}
#else
}
return result;
}
#endif
/** Find a small interval that bounds a
\param a sbasis function
\returns inteval
*/
// I have no idea how this works, some clever bounding argument by jfb.
#ifdef USE_SBASIS_OF
#else
#endif
double a=sb[j][0];
double b=sb[j][1];
double v, t = 0;
if (v<0) t = ((b-a)/v+1)*0.5;
if (v>=0 || t<0 || t>1) {
} else {
}
if (v>0) t = ((b-a)/v+1)*0.5;
if (v<=0 || t<0 || t>1) {
}else{
}
}
return res;
}
/** Find a small interval that bounds a(t) for t in i to order order
\param sb sbasis function
\param i domain interval
\param order number of terms
\return interval
*/
#ifdef USE_SBASIS_OF
#else
#endif
double a=sb[j][0];
double b=sb[j][1];
double t = 0;
}else{
}
}else{
}
}
return res;
}
//-- multi_roots ------------------------------------
// goal: solve f(t)=c for several c at once.
/* algo: -compute f at both ends of the given segment [a,b].
-compute bounds m<df(t)<M for df on the segment.
let c and C be the levels below and above f(a):
going from f(a) down to c with slope m takes at least time (f(a)-c)/m
going from f(a) up to C with slope M takes at least time (C-f(a))/M
From this we conclude there are no roots before a'=a+min((f(a)-c)/m,(C-f(a))/M).
Do the same for b: compute some b' such that there are no roots in (b',b].
-if [a',b'] is not empty, repeat the process with [a',(a'+b')/2] and [(a'+b')/2,b'].
unfortunately, extra care is needed about rounding errors, and also to avoid the repetition of roots,
making things tricky and unpleasant...
*/
//TODO: Make sure the code is "rounding-errors proof" and take care about repetition of roots!
}
#ifdef USE_SBASIS_OF
#else
static void multi_roots_internal(SBasis const &f,
#endif
double htol,
double vtol,
double a,
double fa,
double b,
double fb){
if (f.isZero(0)){
int idx;
}
return;
}
////usefull?
// if (f.size()==1){
// int idxa=upper_level(levels,fa);
// int idxb=upper_level(levels,fb);
// if (fa==fb){
// if (fa==levels[idxa]){
// roots[a]=idxa;
// roots[b]=idxa;
// }
// return;
// }
// int idx_min=std::min(idxa,idxb);
// int idx_max=std::max(idxa,idxb);
// if (idx_max==levels.size()) idx_max-=1;
// for(int i=idx_min;i<=idx_max; i++){
// double t=a+(b-a)*(levels[i]-fa)/(fb-fa);
// if(a<t&&t<b) roots[t]=i;
// }
// return;
// }
if ((b-a)<htol){
//TODO: use different tol for t and f ?
//TODO: unsigned idx ? (remove int casts when fixed)
}
return;
}
//first times when a level (higher or lower) can be reached from a or b.
//ta_hi=ta_lo=a;
}else{
}
//tb_hi=tb_lo=b;
}else{
}
//hum, rounding errors frighten me! so I add this +tol...
}else{
//we do not want to count it twice (from the left and from the right)
}
}
}
/** Solve f(t)=c for several c at once.
\param f sbasis function
\param levels vector of 'y' values
\param htol, vtol
\param a, b left and right bounds
\returns a vector of vectors, one for each y giving roots
Effectively computes:
results = roots(f(y_i)) for all y_i
* algo: -compute f at both ends of the given segment [a,b].
-compute bounds m<df(t)<M for df on the segment.
let c and C be the levels below and above f(a):
going from f(a) down to c with slope m takes at least time (f(a)-c)/m
going from f(a) up to C with slope M takes at least time (C-f(a))/M
From this we conclude there are no roots before a'=a+min((f(a)-c)/m,(C-f(a))/M).
Do the same for b: compute some b' such that there are no roots in (b',b].
-if [a',b'] is not empty, repeat the process with [a',(a'+b')/2] and [(a'+b')/2,b'].
unfortunately, extra care is needed about rounding errors, and also to avoid the repetition of roots,
making things tricky and unpleasant...
TODO: Make sure the code is "rounding-errors proof" and take care about repetition of roots!
*/
double htol,
double vtol,
double a,
double b){
return(roots);
}
}
}
//find the first interval whose max is >= x
return( lower_bound( levels.begin(), levels.end(), Interval(x,x), compareIntervalMax) - levels.begin() );
}
}else{
}
}
return result;
}
/** level_sets internal method.
* algorithm: (~adaptation of Newton method versus 'accroissements finis')
-compute f at both ends of the given segment [a,b].
-compute bounds m<df(t)<M for df on the segment.
Suppose f(a) is between two 'levels' c and C. Then
f wont enter c before a + (f(a)-c.max())/m
f wont enter C before a + (C.min()-f(a))/M
From this we conclude nothing happens before a'=a+min((f(a)-c.max())/m,(C.min()-f(a))/M).
We do the same for b: compute some b' such that nothing happens in (b',b].
-if [a',b'] is not empty, repeat the process with [a',(a'+b')/2] and [(a'+b')/2,b'].
If f(a) or f(b) belongs to some 'level' C, then use the same argument to find a' or b' such
that f remains in C on [a,a'] or [b',b]. In case f is monotonic, we also know f won't enter another
level before or after some time, allowing us to restrict the search a little more.
unfortunately, extra care is needed about rounding errors, and also to avoid the repetition of roots,
making things tricky and unpleasant...
*/
double a,
double fa,
double b,
double fb,
double tol=1e-5){
if (f.isZero(0)){
unsigned idx;
}
return;
}
//first times when a level (higher or lower) can be reached from a or b.
//--- if f(a) belongs to a level.-------
//find the first time when we may exit this level.
//move to that time for the next iteration.
}else{
//--- if f(b) does not belong to a level.-------
if ( idxa == 0 ){
ta_lo = b;
}else{
}
ta_hi = b;
}else{
}
}
//--- if f(b) belongs to a level.-------
//find the first time from b when we may exit this level.
//move to that time for the next iteration.
}else{
//--- if f(b) does not belong to a level.-------
if ( idxb == 0 ){
tb_lo = a;
}else{
}
tb_hi = a;
}else{
}
}
//let [t0,t1] be the next interval where to search.
//if the interval is smaller than our resolution:
//pretend f simultaneously meets all the levels between f(t0) and f(t1)...
//push [t0,t1] into all crossed level. Cheat to avoid overlapping intervals on different levels?
}
}
}
return;
}
//To make sure we finally exit the level jump at least by tol:
double ft=f(t);
}
double a, double b, double tol){
// Fuse overlapping intervals...
}
return solsets;
}
std::vector<Interval> level_set (SBasis const &f, double level, double vtol, double a, double b, double tol){
}
std::vector<Interval> level_set (SBasis const &f, Interval const &level, double a, double b, double tol){
}
std::vector<std::vector<Interval> > level_sets (SBasis const &f, std::vector<double> const &levels, double vtol, double a, double b, double tol){
}
}
//-------------------------------------
//-------------------------------------
return; // no roots here
double t = s[0][0] / (s[0][0] - s[0][1]);
return;
}
}
// It is faster to use the bernstein root finder for small degree polynomials (<100?.
double d = s[0][0] - s[0][1];
if(d != 0) {
double r = s[0][0] / d;
if(0 <= r && r <= 1)
}
return res;
}
double d = s[0][0] - s[0][1];
if(d != 0) {
double r = s[0][0] / d;
}
return res;
}
/** Find all t s.t s(t) = 0
\param a sbasis function
\see Bezier::roots
\returns vector of zeros (roots)
*/
switch(s.size()) {
case 0:
assert(false);
case 1:
return roots1(s);
default:
{
sbasis_to_bezier(bz, s);
}
}
}
switch(s.size()) {
case 0:
assert(false);
case 1:
default:
{
sbasis_to_bezier(bz, s);
}
}
}
};
/*
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 :