Commit 76cbec45 authored by Laurent Belcour's avatar Laurent Belcour

Got stable fitting of 1D function using Legendre

parent 27538f91
......@@ -17,20 +17,38 @@ class function
// Overload the function operator
virtual vec operator()(const vec& x) const = 0 ;
virtual vec value(const vec& x) const = 0 ;
// IO function to text files
virtual void load(const std::string& filename) = 0 ;
virtual void save(const std::string& filename, const arguments& args) const = 0 ;
virtual int dimX() const { return _nX ; }
virtual int dimY() const { return _nY ; }
virtual int dimX() const { return _nX ; }
virtual int dimY() const { return _nY ; }
virtual void setDimX(int nX) { _nX = nX ; }
virtual void setDimY(int nY) { _nY = nY ; }
// Acces to the domain of definition of the function
virtual void setMin(const vec& min)
{
assert(min.size() == _nX) ;
_min = min ;
}
virtual void setMax(const vec& max)
{
assert(max.size() == _nX) ;
_max = max ;
}
virtual vec getMin() const { return _min ; }
virtual vec getMax() const { return _max ; }
virtual void setDimX(int nX) { _nX = nX ; }
virtual void setDimY(int nY) { _nY = nY ; }
protected: //data
protected:
// Dimension of the function
// Dimension of the function & domain of
// definition.
int _nX, _nY ;
vec _min, _max ;
} ;
//Q_DECLARE_INTERFACE(function, "Fitter.Function")
......@@ -120,7 +120,7 @@ double legendre(double x, int i)
}
}
#define POLYNOMIALS
//#define POLYNOMIALS
// Get the p_i and q_j function
double rational_function::p(const vec& x, int i) const
......@@ -132,7 +132,7 @@ double rational_function::p(const vec& x, int i) const
#ifdef POLYNOMIALS
res *= pow(x[k], deg[k]) ;
#else // LEGENDRE
res *= legendre(x[k], deg[k]);
res *= legendre(2.0*((x[k] - _min[k]) / _max[k] - 0.5), deg[k]);
#endif
}
......@@ -147,7 +147,7 @@ double rational_function::q(const vec& x, int i) const
#ifdef POLYNOMIALS
res *= pow(x[k], deg[k]) ;
#else // LEGENDRE
res *= legendre(x[k], deg[k]);
res *= legendre(2.0*((x[k] - _min[k]) / _max[k] - 0.5), deg[k]);
#endif
}
......
......@@ -52,6 +52,8 @@ bool rational_fitter_cgal::fit_data(const data* dat, function* fit)
// to the dimension of my fitting problem
r->setDimX(d->dimX()) ;
r->setDimY(d->dimY()) ;
r->setMin(d->min()) ;
r->setMax(d->max()) ;
std::cout << "<<INFO>> np in [" << _min_np << ", " << _max_np
<< "] & nq in [" << _min_nq << ", " << _max_nq << "]" << std::endl ;
......@@ -151,11 +153,12 @@ bool rational_fitter_cgal::fit_data(const vertical_segment* d, int np, int nq, i
double a1_norm = 0.0 ;
vec xi = d->get(i) ;
/*
for(int k=0; k<d->dimX(); ++k)
{
xi[k] /= dmax[k] ;
}
*/
// A row of the constraint matrix has this
// form: [p_{0}(x_i), .., p_{np}(x_i), -f(x_i) q_{0}(x_i), .., -f(x_i) q_{nq}(x_i)]
// For the lower constraint and negated for
......@@ -318,11 +321,11 @@ bool rational_fitter_cgal::fit_data(const vertical_segment* d, int np, int nq, i
if(i < np)
{
p[i] = v / r->p(dmax, i) ;
p[i] = v ;
}
else
{
q[i-np] = v / r->q(dmax, i-np) ;
q[i-np] = v ;
}
}
r->update(p, q) ;
......
......@@ -43,6 +43,8 @@ bool rational_fitter_eigen::fit_data(const data* dat, function* fit)
// to the dimension of my fitting problem
r->setDimX(d->dimX()) ;
r->setDimY(d->dimY()) ;
r->setMin(d->min()) ;
r->setMax(d->max()) ;
std::cout << "<<INFO>> np =" << _np << "& nq =" << _nq << std::endl ;
......
......@@ -53,6 +53,8 @@ bool rational_fitter_matlab::fit_data(const data* dat, function* fit)
// to the dimension of my fitting problem
r->setDimX(d->dimX()) ;
r->setDimY(d->dimY()) ;
r->setMin(d->min()) ;
r->setMax(d->max()) ;
std::cout << "<<INFO>> np in [" << _min_np << ", " << _max_np
<< "] & nq in [" << _min_nq << ", " << _max_nq << "]" << std::endl ;
......@@ -165,11 +167,11 @@ bool rational_fitter_matlab::fit_data(const rational_data* d, int np, int nq, in
double a1_norm = 0.0 ;
vec xi = d->get(i) ;
for(int k=0; k<d->dimX(); ++k)
/* for(int k=0; k<d->dimX(); ++k)
{
xi[k] /= dmax[k] ;
}
*/
// A row of the constraint matrix has this
// form: [p_{0}(x_i), .., p_{np}(x_i), -f(x_i) q_{0}(x_i), .., -f(x_i) q_{nq}(x_i)]
// For the lower constraint and negated for
......@@ -330,11 +332,11 @@ bool rational_fitter_matlab::fit_data(const rational_data* d, int np, int nq, in
total += val[i]*val[i] ;
if(i < np)
{
a.push_back(val[i] / r->p(dmax, i)) ;
a.push_back(val[i]) ;
}
else
{
b.push_back(val[i] / r->q(dmax, i-np)) ;
b.push_back(val[i]) ;
}
}
r->update(a, b) ;
......
......@@ -47,6 +47,8 @@ bool rational_fitter_quadprog::fit_data(const data* dat, function* fit)
// to the dimension of my fitting problem
r->setDimX(d->dimX()) ;
r->setDimY(d->dimY()) ;
r->setMin(d->min()) ;
r->setMax(d->max()) ;
std::cout << "<<INFO>> np in [" << _min_np << ", " << _max_np
<< "] & nq in [" << _min_nq << ", " << _max_nq << "]" << std::endl ;
......@@ -129,11 +131,6 @@ bool rational_fitter_quadprog::fit_data(const rational_data* dat, int np, int nq
return false ;
}
// I need to set the dimension of the resulting function to be equal
// to the dimension of my fitting problem
r->setDimX(d->dimX()) ;
r->setDimY(d->dimY()) ;
// Get the maximum value in data to scale the input parameter space
// so that it reduces the values of the polynomial
vec dmax = d->max() ;
......@@ -165,11 +162,11 @@ bool rational_fitter_quadprog::fit_data(const rational_data* dat, int np, int nq
double a1_norm = 0.0 ;
vec xi = d->get(i) ;
for(int k=0; k<d->dimX(); ++k)
/* for(int k=0; k<d->dimX(); ++k)
{
xi[k] /= dmax[k] ;
}
*/
// A row of the constraint matrix has this
// form: [p_{0}(x_i), .., p_{np}(x_i), -f(x_i) q_{0}(x_i), .., -f(x_i) q_{nq}(x_i)]
// For the lower constraint and negated for
......@@ -288,11 +285,11 @@ bool rational_fitter_quadprog::fit_data(const rational_data* dat, int np, int nq
norm += v*v ;
if(i < np)
{
p.push_back(v / r->p(dmax, i)) ;
p.push_back(v) ;
}
else
{
q.push_back(v / r->q(dmax, i-np)) ;
q.push_back(v) ;
}
}
......
......@@ -47,6 +47,8 @@ bool rational_fitter_quadprog::fit_data(const data* dat, function* fit)
// to the dimension of my fitting problem
r->setDimX(d->dimX()) ;
r->setDimY(d->dimY()) ;
r->setMin(d->min()) ;
r->setMax(d->max()) ;
std::cout << "<<INFO>> np in [" << _min_np << ", " << _max_np
<< "] & nq in [" << _min_nq << ", " << _max_nq << "]" << std::endl ;
......@@ -174,11 +176,11 @@ bool rational_fitter_quadprog::fit_data(const rational_data* dat, int np, int nq
double a1_norm = 0.0 ;
vec xi = d->get(i) ;
for(int k=0; k<d->dimX(); ++k)
/* for(int k=0; k<d->dimX(); ++k)
{
xi[k] /= dmax[k] ;
}
*/
// A row of the constraint matrix has this
// form: [p_{0}(x_i), .., p_{np}(x_i), -f(x_i) q_{0}(x_i), .., -f(x_i) q_{nq}(x_i)]
// For the lower constraint and negated for
......@@ -294,11 +296,11 @@ bool rational_fitter_quadprog::fit_data(const rational_data* dat, int np, int nq
norm += v*v ;
if(i < np)
{
p.push_back(v / r->p(dmax, i)) ;
p.push_back(v) ;
}
else
{
q.push_back(v / r->q(dmax, i-np)) ;
q.push_back(v) ;
}
}
......
......@@ -4,4 +4,5 @@ SUBDIRS = core \
tests
plugins.depends = core
tests.depends = core
tests.depends = plugins
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment