Commit 8ba2f0a2 authored by Laurent Belcour's avatar Laurent Belcour

Working Nd

parent 63c3f6be
......@@ -10,6 +10,9 @@ class arguments
public: // functions
// Constructor and destructor
arguments()
{
} ;
arguments(int argc, char** const argv)
{
std::string key ;
......
......@@ -9,7 +9,7 @@ class vec : public std::vector<double>
friend std::ostream& operator<< (std::ostream& out, const vec& v)
{
for(int i=0; i<v.size(); ++i)
for(unsigned int i=0; i<v.size(); ++i)
{
if(i == 0) out << "[" ; else out << ", " ;
out << v[i] ;
......
......@@ -13,7 +13,7 @@ class data
// Load data from a file
virtual void load(const std::string& filename) = 0 ;
virtual void load(const std::string& filename, double min, double max) = 0 ;
virtual void load(const std::string& filename, const arguments& args) = 0 ;
// Acces to data
// virtual bool get(int i, double& x, double& y, double& t) const = 0 ;
......
......@@ -8,12 +8,13 @@
#include "common.h"
#include "args.h"
class function //: public std::function<double(double)>
class function
{
public: // methods
// 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 ;
......
......@@ -10,11 +10,11 @@
void rational_data::load(const std::string& filename)
{
load(filename, -std::numeric_limits<double>::max(), std::numeric_limits<double>::max()) ;
arguments args ;
load(filename, args) ;
}
void rational_data::load(const std::string& filename, double min, double max)
void rational_data::load(const std::string& filename, const arguments& args)
{
std::ifstream file(filename.c_str()) ;
if(!file.is_open())
......@@ -22,8 +22,9 @@ void rational_data::load(const std::string& filename, double min, double max)
std::cerr << "<<ERROR>> unable to open file \"" << filename << "\"" << std::endl ;
}
// N-Floats regexp
// boost::regex e ("^([0-9]*\.?[0-9]+[\\t ]?)");
double min, max ;
min = args.get_float("min", -std::numeric_limits<double>::max()) ;
max = args.get_float("max", std::numeric_limits<double>::max()) ;
_nX = 0 ; _nY = 0 ;
std::vector<int> vs ; int current_vs = 0 ;
......@@ -102,8 +103,8 @@ void rational_data::load(const std::string& filename, double min, double max)
{
// TODO Specify the delta in case
// Handle multiple dim
v[dimX() + dimY()+i] = v[dimX() + i] - 0.01 ;
v[dimX() + 2*dimY()+i] = v[dimX() + i] + 0.01 ;
v[dimX() + dimY()+i] = v[dimX() + i] - args.get_float("dt", 0.1) ;
v[dimX() + 2*dimY()+i] = v[dimX() + i] + args.get_float("dt", 0.1) ;
}
}
......
......@@ -20,7 +20,7 @@ class rational_data : public QObject, public data
// Load data from a file
virtual void load(const std::string& filename) ;
virtual void load(const std::string& filename, double min, double max) ;
virtual void load(const std::string& filename, const arguments& args) ;
// Acces to data
virtual bool get(int i, double& x, double& yl, double& yu) const ;
......
......@@ -23,28 +23,26 @@ rational_function::~rational_function()
}
// Overload the function operator
vec rational_function::operator()(const vec& x) const
vec rational_function::value(const vec& x) const
{
vec res ;
res.resize(dimY()) ;
res.reserve(_nY) ;
for(int k=0; k<_nY; ++k)
{
double p = 0.0f ;
double q = 0.0f ;
for(int l=0; l<_nX; ++l)
for(int i=a.size()-1; i>=0; --i)
{
p += a[i]*this->p(x, i) ;
}
for(int i=a.size()-1; i>=0; --i)
{
p = x[l]*p + a[i] ;
}
for(int i=b.size()-1; i>=0; --i)
{
q = x[l]*q + b[i] ;
}
for(int i=b.size()-1; i>=0; --i)
{
q += b[i]*this->q(x, i) ;
}
res[k] = p/q ;
}
return res ;
......@@ -133,16 +131,16 @@ void rational_function::save(const std::string& filename, const arguments& args)
std::ofstream file(filename.c_str(), std::ios_base::trunc);
for(double x=min[0]; x<=max[0]; x+=dt)
{
vec vx ; vx.push_back(x) ;
file << x << "\t" << ((*this)(vx))[0] << std::endl ;
std::cout << x << "\t" << ((*this)(vx))[0] << std::endl ;
vec vx ; for(int i=0;i<_nX; ++i) { vx.push_back(x) ; }
file << x << "\t" << value(vx)[0] << std::endl ;
std::cout << x << "\t" << value(vx)[0] << std::endl ;
}
}
std::ostream& operator<< (std::ostream& out, const rational_function& r)
{
std::cout << "p = [" ;
for(int i=0; i<r.a.size(); ++i)
for(unsigned int i=0; i<r.a.size(); ++i)
{
if(i != 0)
{
......@@ -153,7 +151,7 @@ std::ostream& operator<< (std::ostream& out, const rational_function& r)
std::cout << "]" << std::endl ;
std::cout << "q = [" ;
for(int i=0; i<r.b.size(); ++i)
for(unsigned int i=0; i<r.b.size(); ++i)
{
if(i != 0)
{
......@@ -163,6 +161,7 @@ std::ostream& operator<< (std::ostream& out, const rational_function& r)
}
std::cout << "]" << std::endl ;
return out ;
}
......
......@@ -24,7 +24,8 @@ class rational_function : public QObject, public function
virtual ~rational_function() ;
// Overload the function operator
virtual vec operator()(const vec& x) const ;
virtual vec value(const vec& x) const ;
virtual vec operator()(const vec& x) const { return value(x) ; } ;
// Get the p_i and q_j function
virtual double p(const vec& x, int i) const ;
......
......@@ -8,5 +8,7 @@ INCLUDEPATH += ../..
HEADERS = rational_function.h
SOURCES = rational_function.cpp
QMAKE_CXXFLAGS += -frounding-math -fPIC
QMAKE_CXXFLAGS += -frounding-math \
-fPIC \
-g
......@@ -86,29 +86,31 @@ int main(int argc, char** argv)
function* f = functions[0] ;
data* d = datas[0] ;
if(args.is_defined("min") && args.is_defined("max"))
{
d->load(args["input"], args.get_float("min", 0.0f), args.get_float("max", 1.0f));
}
else if(args.is_defined("min") && !args.is_defined("max"))
{
d->load(args["input"], args.get_float("min", 0.0f), std::numeric_limits<double>::max());
}
else if(args.is_defined("min") && !args.is_defined("max"))
{
d->load(args["input"], -std::numeric_limits<double>::max(), args.get_float("min", 0.0f));
}
else
{
d->load(args["input"]);
}
d->load(args["input"], args);
bool is_fitted = fitters[0]->fit_data(d, f) ;
// Display the result
if(is_fitted)
{
functions[0]->save(args["output"], args) ;
/*
vec min, max ;
min.assign(2, args.get_float("min", 0.0f)) ;
max.assign(2, args.get_float("max", 1.5f)) ;
int nb_samples = args.get_int("nb_samples", 100) ;
double dt = (max[0]-min[0]) / nb_samples ;
std::ofstream file(args["output"].c_str(), std::ios_base::trunc);
for(double x=min[0]; x<=max[0]; x+=dt)
{
vec vx ; for(int i=0;i<2; ++i) { vx.push_back(x) ; }
file << x << "\t" << f->value(vx)[0] << std::endl ;
std::cout << x << "\t" << f->value(vx)[0] << std::endl ;
}
/*/
f->save(args["output"], args) ;
//*/
}
else
{
......
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