Commit dcdaffac authored by Laurent Belcour's avatar Laurent Belcour
Browse files

Movingn libs to plugins, it makes more sense

parent c0951e87
......@@ -3,6 +3,8 @@
#include <string>
#include <utility>
#include <QtPlugin>
class data
{
public: // methods
......@@ -13,12 +15,21 @@ class data
// Acces to data
virtual bool get(int i, double& x, double& y, double& t) const = 0 ;
virtual const std::vector<double>& get(int i) const = 0 ;
virtual const std::vector<double>& operator[](int i) const = 0 ;
// Get data size
// Get data size, e.g. the number of samples to fit
virtual int size() const = 0 ;
// Get min and max input parameters
// Get the dimension of the input and output space
// WARNING: this dimension is defined after loading
// the data!
virtual int input_dimension() const = 0;
virtual int output_dimension() const = 0;
// Get min and max input space values
virtual double min() const = 0 ;
virtual double max() const = 0 ;
} ;
Q_DECLARE_INTERFACE(data, "Fitter.Data")
......@@ -3,6 +3,8 @@
#include "function.h"
#include "data.h"
#include <QtPlugin>
/*
* Fitting interface for generic fitting algorithms
*
......@@ -15,6 +17,8 @@ class fitter
// underling function class. Return the best
// fit (along with fitting information ?)
//
virtual bool fit_data(const data* d, function* f) = 0 ;
virtual bool fit_data(const data* d, function*& f) = 0 ;
} ;
Q_DECLARE_INTERFACE(fitter, "Fitter.Fitter")
......@@ -3,6 +3,8 @@
#include <functional>
#include <string>
#include <QtPlugin>
class function : public std::function<double(double)>
{
public: // methods
......@@ -15,3 +17,5 @@ class function : public std::function<double(double)>
virtual void save() const = 0 ;
} ;
Q_DECLARE_INTERFACE(function, "Fitter.Function")
......@@ -170,6 +170,15 @@ bool rational_1d_data::get(int i, double& x, double& yl, double& yu) const
return true ;
}
int rational_1d_data::input_dimension() const
{
return 1 ;
}
int rational_1d_data::output_dimension() const
{
return 1 ;
}
const std::vector<double>& rational_1d_data::operator[](int i) const
{
......@@ -191,7 +200,7 @@ double rational_1d_data::max() const
return _max ;
}
bool rational_1d_fitter::fit_data(const rational_1d_data& data, rational_1d& fit)
bool rational_1d_fitter::fit_data(const rational_1d_data* data, rational_1d*& fit)
{
std::cout << "<<INFO>> np in [" << _min_np << ", " << _max_np << "] & nq in [" << _min_nq << ", " << _max_nq << "]" << std::endl ;
int temp_np = _min_np, temp_nq = _min_nq ;
......
......@@ -7,6 +7,7 @@
#include <tuple>
// Interface
#include <QObject>
#include <core/function.h>
#include <core/data.h>
#include <core/fitter.h>
......@@ -60,6 +61,12 @@ class rational_1d_data : public data
// Get min and max input parameters
virtual double min() const ;
virtual double max() const ;
// Get the dimension of the input and output space
// WARNING: this dimension is defined after loading
// the data!
virtual int input_dimension() const ;
virtual int output_dimension() const ;
private: // data
......@@ -72,16 +79,16 @@ class rational_1d_data : public data
double _min, _max ;
} ;
class rational_1d_fitter //: public fitter
class rational_1d_fitter : public fitter
{
public: // methods
// Fitting a data object
virtual bool fit_data(const rational_1d_data& data, rational_1d& fit) ;
virtual bool fit_data(const rational_1d_data* data, rational_1d*& fit) ;
// Fitting a data object using np elements in the numerator and nq
// elements in the denominator
virtual bool fit_data(const rational_1d_data& data, int np, int nq, rational_1d& fit) = 0 ;
virtual bool fit_data(const rational_1d_data* data, int np, int nq, rational_1d*& fit) = 0 ;
virtual void set_parameters(const arguments& args) ;
......
......@@ -21,7 +21,7 @@ typedef CGAL::Quadratic_program_solution<ET> Solution ;
typedef CGAL::Quadratic_program_options Options ;
bool rational_1d_fitter_cgal::fit_data(const rational_1d_data& data, int np, int nq, rational_1d& r)
bool rational_1d_fitter_cgal::fit_data(const rational_1d_data* data, int np, int nq, rational_1d*& r)
{
// by default, we have a nonnegative QP with Ax - b >= 0
Program qp (CGAL::LARGER, false, 0, false, 0) ;
......@@ -36,9 +36,9 @@ bool rational_1d_fitter_cgal::fit_data(const rational_1d_data& data, int np, int
// Each constraint (fitting interval or point
// add another dimension to the constraint
// matrix
Eigen::MatrixXd CI(np+nq, 2*data.size()) ;
Eigen::VectorXd ci(2*data.size()) ;
for(int i=0; i<data.size(); ++i)
Eigen::MatrixXd CI(np+nq, 2*data->size()) ;
Eigen::VectorXd ci(2*data->size()) ;
for(int i=0; i<data->size(); ++i)
{
// Norm of the row vector
double a0_norm = 0.0 ;
......@@ -53,43 +53,43 @@ bool rational_1d_fitter_cgal::fit_data(const rational_1d_data& data, int np, int
// Filling the p part
if(j<np)
{
const double pi = r.p(data[i][0], j) ;
const double pi = r->p((*data)[i][0], j) ;
a0_norm += pi*pi ;
a1_norm += pi*pi ;
qp.set_a(j, i, ET(pi)) ;
qp.set_a(j, i+data.size(), ET(-pi)) ;
qp.set_a(j, i+data->size(), ET(-pi)) ;
CI(j, i) = pi ;
CI(j, i+data.size()) = -pi ;
CI(j, i+data->size()) = -pi ;
}
// Filling the q part
else
{
const double qi = r.q(data[i][0], j-np) ;
a0_norm += qi*qi * (data[i][1]*data[i][1]) ;
qp.set_a(j, i, ET(-data[i][1] * qi)) ;
CI(j, i) = -data[i][1] * qi ;
const double qi = r->q((*data)[i][0], j-np) ;
a0_norm += qi*qi * ((*data)[i][1]*(*data)[i][1]) ;
qp.set_a(j, i, ET(-(*data)[i][1] * qi)) ;
CI(j, i) = -(*data)[i][1] * qi ;
a1_norm += qi*qi * (data[i][2]*data[i][2]) ;
qp.set_a(j, i+data.size(), ET(data[i][2] * qi)) ;
CI(j, i+data.size()) = data[i][2] * qi ;
a1_norm += qi*qi * ((*data)[i][2]*(*data)[i][2]) ;
qp.set_a(j, i+data->size(), ET((*data)[i][2] * qi)) ;
CI(j, i+data->size()) = (*data)[i][2] * qi ;
}
}
// Set the c vector, will later be updated using the
// delta parameter.
ci(i) = sqrt(a0_norm) ;
ci(i+data.size()) = sqrt(a1_norm) ;
ci(i+data->size()) = sqrt(a1_norm) ;
}
// Update the ci column with the delta parameter
// (See Celis et al. 2007 p.12)
Eigen::JacobiSVD<Eigen::MatrixXd> svd(CI);
const double sigma_m = svd.singularValues()(std::min(2*data.size(), np+nq)-1) ;
const double sigma_m = svd.singularValues()(std::min(2*data->size(), np+nq)-1) ;
const double sigma_M = svd.singularValues()(0) ;
#ifdef DEBUG
std::cout << "<<DEBUG>> SVD = [ " ;
for(int i=0; i<std::min(2*data.size(), np+nq); ++i)
for(int i=0; i<std::min(2*data->size(), np+nq); ++i)
{
std::cout << svd.singularValues()(i) << ", " ;
}
......@@ -108,7 +108,7 @@ bool rational_1d_fitter_cgal::fit_data(const rational_1d_data& data, int np, int
#ifdef DEBUG
std::cout << "<<DEBUG>> delta factor: " << sigma_m << " / " << sigma_M << " = " << delta << std::endl ;
#endif
for(int i=0; i<2*data.size(); ++i)
for(int i=0; i<2*data->size(); ++i)
{
qp.set_b(i, ET(delta * ci(i))) ;
}
......@@ -198,7 +198,11 @@ bool rational_1d_fitter_cgal::fit_data(const rational_1d_data& data, int np, int
}
}
r = rational_1d(p, q);
if(r != nullptr)
{
delete r ;
}
r = new rational_1d(p, q);
return true;
}
else
......
......@@ -16,6 +16,6 @@ class rational_1d_fitter_cgal : public rational_1d_fitter
// Fitting a data object using np elements in the numerator and nq
// elements in the denominator.
virtual bool fit_data(const rational_1d_data& data, int np, int nq, rational_1d& fit) ;
virtual bool fit_data(const rational_1d_data* data, int np, int nq, rational_1d*& fit) ;
} ;
......@@ -7,7 +7,7 @@
#include <iostream>
#include <cfenv>
bool rational_1d_fitter_eigen::fit_data(const rational_1d_data& data, int np, int nq, rational_1d& r)
bool rational_1d_fitter_eigen::fit_data(const rational_1d_data* data, int np, int nq, rational_1d*& r)
{
// Select the size of the result vector to be equal to the dimension
// of p + q.
......@@ -24,9 +24,9 @@ bool rational_1d_fitter_eigen::fit_data(const rational_1d_data& data, int np, in
// Each constraint (fitting interval or point
// add another dimension to the constraint
// matrix
Eigen::MatrixXd CI(np+nq, 2*data.size()) ;
Eigen::VectorXd ci(2*data.size()) ;
for(int i=0; i<data.size(); ++i)
Eigen::MatrixXd CI(np+nq, 2*data->size()) ;
Eigen::VectorXd ci(2*data->size()) ;
for(int i=0; i<data->size(); ++i)
{
// Norm of the row vector
double a0_norm = 0.0 ;
......@@ -41,39 +41,39 @@ bool rational_1d_fitter_eigen::fit_data(const rational_1d_data& data, int np, in
// Filling the p part
if(j<np)
{
const double pi = r.p(data[i][0], j) ;
const double pi = r->p((*data)[i][0], j) ;
a0_norm += pi*pi ;
a1_norm += pi*pi ;
CI(j, i) = pi ;
CI(j, i+data.size()) = - pi ;
CI(j, i+data->size()) = - pi ;
}
// Filling the q part
else
{
const double qi = r.q(data[i][0], j-np) ;
a0_norm += qi*qi * (data[i][1]*data[i][1]) ;
CI(j, i) = - data[i][1] * qi ;
const double qi = r->q((*data)[i][0], j-np) ;
a0_norm += qi*qi * ((*data)[i][1]*(*data)[i][1]) ;
CI(j, i) = - (*data)[i][1] * qi ;
a1_norm += qi*qi * (data[i][2]*data[i][2]) ;
CI(j, i+data.size()) = data[i][2] * qi ;
a1_norm += qi*qi * ((*data)[i][2]*(*data)[i][2]) ;
CI(j, i+data->size()) = (*data)[i][2] * qi ;
}
}
// Set the c vector, will later be updated using the
// delta parameter.
ci(i) = -sqrt(a0_norm) ;
ci(i+data.size()) = -sqrt(a1_norm) ;
ci(i+data->size()) = -sqrt(a1_norm) ;
}
// Update the ci column with the delta parameter
// (See Celis et al. 2007 p.12)
Eigen::JacobiSVD<Eigen::MatrixXd> svd(CI);
const double sigma_m = svd.singularValues()(std::min(2*data.size(), np+nq)-1) ;
const double sigma_m = svd.singularValues()(std::min(2*data->size(), np+nq)-1) ;
const double sigma_M = svd.singularValues()(0) ;
#ifdef DEBUG
std::cout << "<<DEBUG>> SVD = [ " ;
for(int i=0; i<std::min(2*data.size(), np+nq); ++i)
for(int i=0; i<std::min(2*data->size(), np+nq); ++i)
{
std::cout << svd.singularValues()(i) << ", " ;
}
......@@ -92,15 +92,15 @@ bool rational_1d_fitter_eigen::fit_data(const rational_1d_data& data, int np, in
#ifdef DEBUG
std::cout << "<<DEBUG>> delta factor: " << sigma_M << " / " << sigma_m << " = " << delta << std::endl ;
#endif
for(int i=0; i<2*data.size(); ++i)
for(int i=0; i<2*data->size(); ++i)
{
ci(i) *= delta ;
}
// solve the program, using ET as the exact type
Eigen::VectorXd g0(np+nq) ;
Eigen::MatrixXd CE(np+nq, 2*data.size()) ;
Eigen::VectorXd ce(2*data.size()) ;
Eigen::MatrixXd CE(np+nq, 2*data->size()) ;
Eigen::VectorXd ce(2*data->size()) ;
Eigen::VectorXd x(np+nq) ;
double cost = solve_quadprog(G, g0, CE, ce, CI, ci, x) ;
......@@ -134,7 +134,12 @@ bool rational_1d_fitter_eigen::fit_data(const rational_1d_data& data, int np, in
}
}
r = rational_1d(p, q);
if(r != nullptr)
{
delete r ;
}
r = new rational_1d(p, q);
return true ;
}
else
......
......@@ -9,7 +9,7 @@ class rational_1d_fitter_eigen : public rational_1d_fitter
// Fitting a data object using np elements
// in the numerator and nq elements in the
// denominator
virtual bool fit_data(const rational_1d_data& data, int np, int nq, rational_1d& fit) ;
virtual bool fit_data(const rational_1d_data* data, int np, int nq, rational_1d*& fit) ;
private:
} ;
DEST_DIR = ../bin
CONFIG += debug
CONFIG += debug plugin
QT +=
INCLUDEPATH += ../../ ../../libs/rational_1d /home/belcour/Sources/Eigen/include/eigen3
SOURCES += main.cpp \
......
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