From aeda59adfae114a5372cdd9929c14e27405b0bdf Mon Sep 17 00:00:00 2001 From: Antonin Portelli Date: Fri, 13 Nov 2015 14:30:10 +0000 Subject: [PATCH] dimension resized automatically for solvers --- examples/exFit.cpp | 2 +- examples/exMin.cpp | 2 +- examples/exRootFinder.cpp | 2 +- lib/GslHybridRootFinder.cpp | 2 +- lib/GslHybridRootFinder.hpp | 3 ++- lib/Minimizer.cpp | 19 +++++++++++-------- lib/Minimizer.hpp | 3 +++ lib/MinuitMinimizer.cpp | 14 ++++++++++---- lib/MinuitMinimizer.hpp | 3 ++- lib/RootFinder.hpp | 3 ++- lib/Solver.cpp | 25 ++++++++++++++++--------- lib/Solver.hpp | 5 ++++- 12 files changed, 54 insertions(+), 29 deletions(-) diff --git a/examples/exFit.cpp b/examples/exFit.cpp index 053af3a..cb8c4ab 100644 --- a/examples/exFit.cpp +++ b/examples/exFit.cpp @@ -34,7 +34,7 @@ int main(void) // fit DVec init = DVec::Constant(2, 0.5); FitResult p; - MinuitMinimizer minimizer(2); + MinuitMinimizer minimizer; data.fitAllPoints(); p = data.fit(minimizer, init, f); diff --git a/examples/exMin.cpp b/examples/exMin.cpp index b044502..146b6fe 100644 --- a/examples/exMin.cpp +++ b/examples/exMin.cpp @@ -18,7 +18,7 @@ int main(int argc, char* argv[]) source = argv[1]; DoubleFunction f = compile(source, 1); - MinuitMinimizer minimize(1); + MinuitMinimizer minimize; DVec init(1); double min; diff --git a/examples/exRootFinder.cpp b/examples/exRootFinder.cpp index 249c4ed..4ae8a94 100644 --- a/examples/exRootFinder.cpp +++ b/examples/exRootFinder.cpp @@ -11,7 +11,7 @@ int main(void) 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); vector system = {&f1, &f2}; - GslHybridRootFinder solve(2); + GslHybridRootFinder solve; DVec init(2), x; solve.setVerbosity(Solver::Verbosity::Debug); diff --git a/lib/GslHybridRootFinder.cpp b/lib/GslHybridRootFinder.cpp index d21d4cb..1939ef2 100644 --- a/lib/GslHybridRootFinder.cpp +++ b/lib/GslHybridRootFinder.cpp @@ -95,7 +95,7 @@ GslHybridRootFinder::operator()(const vector &func) } if (res.size() != nArg) { - res.conservativeResize(nArg); + resize(nArg); } solver_ = gsl_multiroot_fsolver_alloc(gsl_multiroot_fsolver_hybrids, nFunc); x = gsl_vector_alloc(nFunc); diff --git a/lib/GslHybridRootFinder.hpp b/lib/GslHybridRootFinder.hpp index 81e7184..95ddb81 100644 --- a/lib/GslHybridRootFinder.hpp +++ b/lib/GslHybridRootFinder.hpp @@ -33,7 +33,8 @@ BEGIN_LATAN_NAMESPACE class GslHybridRootFinder: public RootFinder { public: - // constructor + // constructors + GslHybridRootFinder(void) = default; explicit GslHybridRootFinder(const Index dim); // destructor virtual ~GslHybridRootFinder(void) = default; diff --git a/lib/Minimizer.cpp b/lib/Minimizer.cpp index cf32506..1e06b46 100644 --- a/lib/Minimizer.cpp +++ b/lib/Minimizer.cpp @@ -26,15 +26,18 @@ using namespace Latan; // constructor ///////////////////////////////////////////////////////////////// Minimizer::Minimizer(const Index dim) : Solver(dim) -, highLimit_(dim) -, lowLimit_(dim) -, hasHighLimit_(dim) -, hasLowLimit_(dim) { - highLimit_.fill(0.); - lowLimit_.fill(0.); - hasHighLimit_.fill(false); - hasLowLimit_.fill(false); + resize(dim); +} + +// access ////////////////////////////////////////////////////////////////////// +void Minimizer::resize(const Index dim) +{ + Solver::resize(dim); + highLimit_.conservativeResize(dim); + lowLimit_.conservativeResize(dim); + hasHighLimit_.conservativeResize(dim); + hasLowLimit_.conservativeResize(dim); } // limits ////////////////////////////////////////////////////////////////////// diff --git a/lib/Minimizer.hpp b/lib/Minimizer.hpp index 900faef..c0fa8a0 100644 --- a/lib/Minimizer.hpp +++ b/lib/Minimizer.hpp @@ -35,9 +35,12 @@ class Minimizer: public Solver { public: // constructor + Minimizer(void) = default; explicit Minimizer(const Index dim); // destructor virtual ~Minimizer(void) = default; + // access + virtual void resize(const Index dim); // limits virtual double getHighLimit(const Index i) const ; virtual const DVec & getHighLimit(const PlaceHolder ph = _) const; diff --git a/lib/MinuitMinimizer.cpp b/lib/MinuitMinimizer.cpp index b2f2447..5964095 100644 --- a/lib/MinuitMinimizer.cpp +++ b/lib/MinuitMinimizer.cpp @@ -61,11 +61,17 @@ double MinuitMinimizer::MinuitFunction::Up(void) const return 1.; } -// constructor ///////////////////////////////////////////////////////////////// +// constructors //////////////////////////////////////////////////////////////// +MinuitMinimizer::MinuitMinimizer(const Algorithm algorithm) +{ + setAlgorithm(algorithm); +} + MinuitMinimizer::MinuitMinimizer(const Index dim, const Algorithm algorithm) : Minimizer(dim) -, algorithm_(algorithm) -{} +{ + setAlgorithm(algorithm); +} // access ////////////////////////////////////////////////////////////////////// MinuitMinimizer::Algorithm MinuitMinimizer::getAlgorithm(void) const @@ -100,7 +106,7 @@ const DVec & MinuitMinimizer::operator()(const DoubleFunction &f) if (f.getNArg() != x.size()) { - LATAN_ERROR(Size, "function to minimize number of arguments mismatch"); + resize(f.getNArg()); } // set parameters diff --git a/lib/MinuitMinimizer.hpp b/lib/MinuitMinimizer.hpp index 99153b8..f79873c 100644 --- a/lib/MinuitMinimizer.hpp +++ b/lib/MinuitMinimizer.hpp @@ -54,7 +54,8 @@ private: const DoubleFunction *f_; }; public: - // constructor + // constructors + MinuitMinimizer(const Algorithm algorithm = Algorithm::Migrad); explicit MinuitMinimizer(const Index dim, const Algorithm algorithm = Algorithm::Migrad); // destructor diff --git a/lib/RootFinder.hpp b/lib/RootFinder.hpp index 2d0197e..26e13bb 100644 --- a/lib/RootFinder.hpp +++ b/lib/RootFinder.hpp @@ -33,7 +33,8 @@ BEGIN_LATAN_NAMESPACE class RootFinder: public Solver { public: - // constructor + // constructors + RootFinder(void) = default; explicit RootFinder(const Index dim); // destructor virtual ~RootFinder(void) = default; diff --git a/lib/Solver.cpp b/lib/Solver.cpp index 54afb37..5e6b686 100644 --- a/lib/Solver.cpp +++ b/lib/Solver.cpp @@ -26,15 +26,20 @@ using namespace Latan; /****************************************************************************** * Solver implementation * ******************************************************************************/ -// constructor ///////////////////////////////////////////////////////////////// -Solver::Solver(const Index dim, const double precision, - const unsigned int maxIteration) -: x_(dim) +// constructors //////////////////////////////////////////////////////////////// +Solver::Solver(const double precision, const unsigned int maxIteration) { setMaxIteration(maxIteration); setPrecision(precision); } +Solver::Solver(const Index dim, const double precision, + const unsigned int maxIteration) +: Solver(precision, maxIteration) +{ + resize(dim); +} + // access ////////////////////////////////////////////////////////////////////// Index Solver::getDim(void) const { @@ -65,12 +70,9 @@ void Solver::setInit(const DVec &x0) { if (x0.size() != x_.size()) { - LATAN_ERROR(Size, "initial vector state with invalid size"); - } - else - { - x_ = x0; + resize(x0.size()); } + x_ = x0; } void Solver::setMaxIteration(const unsigned int maxIteration) @@ -87,3 +89,8 @@ void Solver::setVerbosity(const Verbosity verbosity) { verbosity_ = verbosity; } + +void Solver::resize(const Index dim) +{ + x_.resize(dim); +} diff --git a/lib/Solver.hpp b/lib/Solver.hpp index a52261c..23601c5 100644 --- a/lib/Solver.hpp +++ b/lib/Solver.hpp @@ -41,7 +41,9 @@ public: Debug = 2 }; public: - // constructor + // constructors + Solver(const double precision = defaultPrec, + const unsigned int maxIteration = defaultMaxIteration); explicit Solver(const Index dim, const double precision = defaultPrec, const unsigned int maxIteration = defaultMaxIteration); // destructor @@ -55,6 +57,7 @@ public: virtual void setPrecision(const double precision); virtual void setMaxIteration(const unsigned int maxIteration); void setVerbosity(const Verbosity verbosity); + virtual void resize(const Index dim); protected: // access DVec & getState(void);