1
0
mirror of https://github.com/aportelli/LatAnalyze.git synced 2024-11-10 08:55:37 +00:00

dimension resized automatically for solvers

This commit is contained in:
Antonin Portelli 2015-11-13 14:30:10 +00:00
parent ae62a3225d
commit aeda59adfa
12 changed files with 54 additions and 29 deletions

View File

@ -34,7 +34,7 @@ int main(void)
// fit // fit
DVec init = DVec::Constant(2, 0.5); DVec init = DVec::Constant(2, 0.5);
FitResult p; FitResult p;
MinuitMinimizer minimizer(2); MinuitMinimizer minimizer;
data.fitAllPoints(); data.fitAllPoints();
p = data.fit(minimizer, init, f); p = data.fit(minimizer, init, f);

View File

@ -18,7 +18,7 @@ int main(int argc, char* argv[])
source = argv[1]; source = argv[1];
DoubleFunction f = compile(source, 1); DoubleFunction f = compile(source, 1);
MinuitMinimizer minimize(1); MinuitMinimizer minimize;
DVec init(1); DVec init(1);
double min; double min;

View File

@ -11,7 +11,7 @@ int main(void)
DoubleFunction f1([a](const double *x){return a*(1.-x[0]);}, 2); DoubleFunction f1([a](const double *x){return a*(1.-x[0]);}, 2);
DoubleFunction f2([b](const double *x){return b*(x[1]-x[0]*x[0]);}, 2); DoubleFunction f2([b](const double *x){return b*(x[1]-x[0]*x[0]);}, 2);
vector<DoubleFunction *> system = {&f1, &f2}; vector<DoubleFunction *> system = {&f1, &f2};
GslHybridRootFinder solve(2); GslHybridRootFinder solve;
DVec init(2), x; DVec init(2), x;
solve.setVerbosity(Solver::Verbosity::Debug); solve.setVerbosity(Solver::Verbosity::Debug);

View File

@ -95,7 +95,7 @@ GslHybridRootFinder::operator()(const vector<DoubleFunction *> &func)
} }
if (res.size() != nArg) if (res.size() != nArg)
{ {
res.conservativeResize(nArg); resize(nArg);
} }
solver_ = gsl_multiroot_fsolver_alloc(gsl_multiroot_fsolver_hybrids, nFunc); solver_ = gsl_multiroot_fsolver_alloc(gsl_multiroot_fsolver_hybrids, nFunc);
x = gsl_vector_alloc(nFunc); x = gsl_vector_alloc(nFunc);

View File

@ -33,7 +33,8 @@ BEGIN_LATAN_NAMESPACE
class GslHybridRootFinder: public RootFinder class GslHybridRootFinder: public RootFinder
{ {
public: public:
// constructor // constructors
GslHybridRootFinder(void) = default;
explicit GslHybridRootFinder(const Index dim); explicit GslHybridRootFinder(const Index dim);
// destructor // destructor
virtual ~GslHybridRootFinder(void) = default; virtual ~GslHybridRootFinder(void) = default;

View File

@ -26,15 +26,18 @@ using namespace Latan;
// constructor ///////////////////////////////////////////////////////////////// // constructor /////////////////////////////////////////////////////////////////
Minimizer::Minimizer(const Index dim) Minimizer::Minimizer(const Index dim)
: Solver(dim) : Solver(dim)
, highLimit_(dim)
, lowLimit_(dim)
, hasHighLimit_(dim)
, hasLowLimit_(dim)
{ {
highLimit_.fill(0.); resize(dim);
lowLimit_.fill(0.); }
hasHighLimit_.fill(false);
hasLowLimit_.fill(false); // access //////////////////////////////////////////////////////////////////////
void Minimizer::resize(const Index dim)
{
Solver::resize(dim);
highLimit_.conservativeResize(dim);
lowLimit_.conservativeResize(dim);
hasHighLimit_.conservativeResize(dim);
hasLowLimit_.conservativeResize(dim);
} }
// limits ////////////////////////////////////////////////////////////////////// // limits //////////////////////////////////////////////////////////////////////

View File

@ -35,9 +35,12 @@ class Minimizer: public Solver
{ {
public: public:
// constructor // constructor
Minimizer(void) = default;
explicit Minimizer(const Index dim); explicit Minimizer(const Index dim);
// destructor // destructor
virtual ~Minimizer(void) = default; virtual ~Minimizer(void) = default;
// access
virtual void resize(const Index dim);
// limits // limits
virtual double getHighLimit(const Index i) const ; virtual double getHighLimit(const Index i) const ;
virtual const DVec & getHighLimit(const PlaceHolder ph = _) const; virtual const DVec & getHighLimit(const PlaceHolder ph = _) const;

View File

@ -61,11 +61,17 @@ double MinuitMinimizer::MinuitFunction::Up(void) const
return 1.; return 1.;
} }
// constructor ///////////////////////////////////////////////////////////////// // constructors ////////////////////////////////////////////////////////////////
MinuitMinimizer::MinuitMinimizer(const Algorithm algorithm)
{
setAlgorithm(algorithm);
}
MinuitMinimizer::MinuitMinimizer(const Index dim, const Algorithm algorithm) MinuitMinimizer::MinuitMinimizer(const Index dim, const Algorithm algorithm)
: Minimizer(dim) : Minimizer(dim)
, algorithm_(algorithm) {
{} setAlgorithm(algorithm);
}
// access ////////////////////////////////////////////////////////////////////// // access //////////////////////////////////////////////////////////////////////
MinuitMinimizer::Algorithm MinuitMinimizer::getAlgorithm(void) const MinuitMinimizer::Algorithm MinuitMinimizer::getAlgorithm(void) const
@ -100,7 +106,7 @@ const DVec & MinuitMinimizer::operator()(const DoubleFunction &f)
if (f.getNArg() != x.size()) if (f.getNArg() != x.size())
{ {
LATAN_ERROR(Size, "function to minimize number of arguments mismatch"); resize(f.getNArg());
} }
// set parameters // set parameters

View File

@ -54,7 +54,8 @@ private:
const DoubleFunction *f_; const DoubleFunction *f_;
}; };
public: public:
// constructor // constructors
MinuitMinimizer(const Algorithm algorithm = Algorithm::Migrad);
explicit MinuitMinimizer(const Index dim, explicit MinuitMinimizer(const Index dim,
const Algorithm algorithm = Algorithm::Migrad); const Algorithm algorithm = Algorithm::Migrad);
// destructor // destructor

View File

@ -33,7 +33,8 @@ BEGIN_LATAN_NAMESPACE
class RootFinder: public Solver class RootFinder: public Solver
{ {
public: public:
// constructor // constructors
RootFinder(void) = default;
explicit RootFinder(const Index dim); explicit RootFinder(const Index dim);
// destructor // destructor
virtual ~RootFinder(void) = default; virtual ~RootFinder(void) = default;

View File

@ -26,15 +26,20 @@ using namespace Latan;
/****************************************************************************** /******************************************************************************
* Solver implementation * * Solver implementation *
******************************************************************************/ ******************************************************************************/
// constructor ///////////////////////////////////////////////////////////////// // constructors ////////////////////////////////////////////////////////////////
Solver::Solver(const Index dim, const double precision, Solver::Solver(const double precision, const unsigned int maxIteration)
const unsigned int maxIteration)
: x_(dim)
{ {
setMaxIteration(maxIteration); setMaxIteration(maxIteration);
setPrecision(precision); setPrecision(precision);
} }
Solver::Solver(const Index dim, const double precision,
const unsigned int maxIteration)
: Solver(precision, maxIteration)
{
resize(dim);
}
// access ////////////////////////////////////////////////////////////////////// // access //////////////////////////////////////////////////////////////////////
Index Solver::getDim(void) const Index Solver::getDim(void) const
{ {
@ -65,12 +70,9 @@ void Solver::setInit(const DVec &x0)
{ {
if (x0.size() != x_.size()) if (x0.size() != x_.size())
{ {
LATAN_ERROR(Size, "initial vector state with invalid size"); resize(x0.size());
}
else
{
x_ = x0;
} }
x_ = x0;
} }
void Solver::setMaxIteration(const unsigned int maxIteration) void Solver::setMaxIteration(const unsigned int maxIteration)
@ -87,3 +89,8 @@ void Solver::setVerbosity(const Verbosity verbosity)
{ {
verbosity_ = verbosity; verbosity_ = verbosity;
} }
void Solver::resize(const Index dim)
{
x_.resize(dim);
}

View File

@ -41,7 +41,9 @@ public:
Debug = 2 Debug = 2
}; };
public: public:
// constructor // constructors
Solver(const double precision = defaultPrec,
const unsigned int maxIteration = defaultMaxIteration);
explicit Solver(const Index dim, const double precision = defaultPrec, explicit Solver(const Index dim, const double precision = defaultPrec,
const unsigned int maxIteration = defaultMaxIteration); const unsigned int maxIteration = defaultMaxIteration);
// destructor // destructor
@ -55,6 +57,7 @@ public:
virtual void setPrecision(const double precision); virtual void setPrecision(const double precision);
virtual void setMaxIteration(const unsigned int maxIteration); virtual void setMaxIteration(const unsigned int maxIteration);
void setVerbosity(const Verbosity verbosity); void setVerbosity(const Verbosity verbosity);
virtual void resize(const Index dim);
protected: protected:
// access // access
DVec & getState(void); DVec & getState(void);