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

Working Nd

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