From 16cc08d07c2684b53a0c8abb900f2a9d046202bb Mon Sep 17 00:00:00 2001 From: koldo Date: Wed, 13 May 2020 12:10:14 +0000 Subject: [PATCH] Eigen: Added simpler nonlinear functions git-svn-id: svn://ultimatepp.org/upp/trunk@14465 f0d560ea-af0d-0410-9eb7-867de7ffcac7 --- uppsrc/plugin/Eigen/Eigen.cpp | 35 +++++++++++++++- uppsrc/plugin/Eigen/Eigen.h | 16 +------- uppsrc/plugin/Eigen/Eigen.upp | 1 + .../plugin/Eigen/src.tpp/NonLinear_en-us.tpp | 41 +++++++++++++++++++ .../plugin/Eigen/src.tpp/NonLinear_en-us.tppi | 0 uppsrc/plugin/Eigen/src.tpp/all.i | 4 ++ .../plugin/Eigen/srcdoc.tpp/Eigen_en-us.tpp | 4 +- .../Eigen/srcdoc.tpp/NonLinear_en-us.tpp | 4 -- 8 files changed, 85 insertions(+), 20 deletions(-) create mode 100644 uppsrc/plugin/Eigen/src.tpp/NonLinear_en-us.tpp create mode 100644 uppsrc/plugin/Eigen/src.tpp/NonLinear_en-us.tppi create mode 100644 uppsrc/plugin/Eigen/src.tpp/all.i delete mode 100644 uppsrc/plugin/Eigen/srcdoc.tpp/NonLinear_en-us.tpp diff --git a/uppsrc/plugin/Eigen/Eigen.cpp b/uppsrc/plugin/Eigen/Eigen.cpp index 0c399d16d..b23ae0de7 100644 --- a/uppsrc/plugin/Eigen/Eigen.cpp +++ b/uppsrc/plugin/Eigen/Eigen.cpp @@ -1 +1,34 @@ -// Dummy file \ No newline at end of file +#include +#include + + +namespace Upp { + +using namespace Eigen; + +bool NonLinearOptimization(VectorXd &y, int numData, Function Residual) { + Basic_functor functor(Residual); + functor.unknowns = y.size(); + functor.datasetLen = numData; + Eigen::NumericalDiff numDiff(functor); + Eigen::LevenbergMarquardt > lm(numDiff); + int ret = lm.minimize(y); + if (ret == Eigen::LevenbergMarquardtSpace::ImproperInputParameters || + ret == Eigen::LevenbergMarquardtSpace::TooManyFunctionEvaluation) + return false; + return true; +} + +bool NonLinearSolver(VectorXd &y, Function Residual) { + Basic_functor functor(Residual); + HybridNonLinearSolver solver(functor); + int ret = solver.solveNumericalDiff(y); + if (ret == HybridNonLinearSolverSpace::ImproperInputParameters || + ret == HybridNonLinearSolverSpace::TooManyFunctionEvaluation || + ret == HybridNonLinearSolverSpace::NotMakingProgressJacobian || + ret == HybridNonLinearSolverSpace::NotMakingProgressIterations) + return false; + return true; +} + +} \ No newline at end of file diff --git a/uppsrc/plugin/Eigen/Eigen.h b/uppsrc/plugin/Eigen/Eigen.h index ca8b9d128..ea9fd1729 100644 --- a/uppsrc/plugin/Eigen/Eigen.h +++ b/uppsrc/plugin/Eigen/Eigen.h @@ -51,20 +51,8 @@ struct Basic_functor : NonLinearOptimizationFunctor { Function function; }; -template -bool NonLinearOptimization(T &coeff, int numData, Function function) { - Basic_functor functor(function); - functor.unknowns = coeff.size(); - functor.datasetLen = numData; - Eigen::NumericalDiff numDiff(functor); - Eigen::LevenbergMarquardt > lm(numDiff); - int ret = lm.minimize(coeff); - if (ret == Eigen::LevenbergMarquardtSpace::ImproperInputParameters || - ret == Eigen::LevenbergMarquardtSpace::TooManyFunctionEvaluation) - return false; - return true; -} - +bool NonLinearOptimization(Eigen::VectorXd &y, int numData, Function residual); +bool NonLinearSolver(Eigen::VectorXd &y, Function Residual); template void Xmlize(XmlIO &xml, Eigen::Matrix &mat) { diff --git a/uppsrc/plugin/Eigen/Eigen.upp b/uppsrc/plugin/Eigen/Eigen.upp index 3ede2985d..70daae5c1 100644 --- a/uppsrc/plugin/Eigen/Eigen.upp +++ b/uppsrc/plugin/Eigen/Eigen.upp @@ -13,6 +13,7 @@ file Eigen.cpp, Eigen.h, ToStringPlugin.h, + src.tpp, srcdoc.tpp, Copying, Eigen readonly separator, diff --git a/uppsrc/plugin/Eigen/src.tpp/NonLinear_en-us.tpp b/uppsrc/plugin/Eigen/src.tpp/NonLinear_en-us.tpp new file mode 100644 index 000000000..aac8d46da --- /dev/null +++ b/uppsrc/plugin/Eigen/src.tpp/NonLinear_en-us.tpp @@ -0,0 +1,41 @@ +topic ""; +[H6;0 $$1,0#05600065144404261032431302351956:begin] +[i448;a25;kKO9;2 $$2,0#37138531426314131252341829483370:codeitem] +[l288;2 $$3,0#27521748481378242620020725143825:desc] +[0 $$4,0#96390100711032703541132217272105:end] +[ $$0,0#00000000000000000000000000000000:Default] +[{_}%EN-US +[ {{10000@(113.42.0) [s0; [*@2;4 Nonlinear functions]]}}&] +[s0;*@3;4 &] +[s0; [2 Some functions have been include to ease nonlinear optimization +and equation system solving.]&] +[s0;2 &] +[s1;%- &] +[s2;:Upp`:`:NonLinearOptimization`(Eigen`:`:VectorXd`&`,int`,Upp`:`:Function``):%- [@(0.0.255) b +ool]_[* NonLinearOptimization]([_^Eigen`:`:VectorXd^ Eigen`::VectorXd]_`&[*@3 y], +[@(0.0.255) int]_[*@3 numData], [_^Upp`:`:Function^ Function]_<[@(0.0.255) int]([@(0.0.255) c +onst]_[_^Eigen`:`:VectorXd^ Eigen`::VectorXd]_`&[*@3 y], [_^Eigen`:`:VectorXd^ Eigen`::Ve +ctorXd]_`&[*@3 residual])>[*@3 Residual])&] +[s3; Given a number of [%-*@3 numData] records, the objective is to +obtain in an iterative way the [%-*@3 y] unknown coefficients that +minimizes, ideally to zero, the [%-*@3 residual] error of the [%-*@3 Residual] +Function.&] +[s3; To improve the success of this calculation, [%-*@3 y] has to be +initially filled with adequate initial guess of the unknowns.&] +[s3; [%-*@3 Residual ]Function has to fill in every call [%-*@3 residual]`[[%-*@3 numData]`] + Vector by applying the unknowns provisionally guessed values +in [%-*@3 y] to the model to be solved.&] +[s4; &] +[s1;%- &] +[s2;:Upp`:`:NonLinearSolver`(Eigen`:`:VectorXd`&`,Upp`:`:Function``):%- [@(0.0.255) b +ool]_[* NonLinearSolver]([_^Eigen`:`:VectorXd^ VectorXd]_`&[*@3 y], +[_^Upp`:`:Function^ Function]_<[@(0.0.255) int]([@(0.0.255) const]_[_^Eigen`:`:VectorXd^ E +igen`::VectorXd]_`&[*@3 y], [_^Eigen`:`:VectorXd^ Eigen`::VectorXd]_`&[*@3 residual])>_[*@3 R +esidual])&] +[s3; Given a system of equations defined by Function [%-*@3 Residual], +and a set of initial values of the unknowns set in [%-*@3 y], this +function tries to obtain the set of [%-*@3 y] that best comply +with [%-*@3 Residual].&] +[s3; The dimension of [%-*@3 y] ([%-*@3 y.size()]) has to be equal to +the number of equations ([%-*@3 residual.size()]).&] +[s4; ]] \ No newline at end of file diff --git a/uppsrc/plugin/Eigen/src.tpp/NonLinear_en-us.tppi b/uppsrc/plugin/Eigen/src.tpp/NonLinear_en-us.tppi new file mode 100644 index 000000000..e69de29bb diff --git a/uppsrc/plugin/Eigen/src.tpp/all.i b/uppsrc/plugin/Eigen/src.tpp/all.i new file mode 100644 index 000000000..4107fc7a2 --- /dev/null +++ b/uppsrc/plugin/Eigen/src.tpp/all.i @@ -0,0 +1,4 @@ +TOPIC("NonLinear_en-us") +#include "NonLinear_en-us.tppi" +END_TOPIC + diff --git a/uppsrc/plugin/Eigen/srcdoc.tpp/Eigen_en-us.tpp b/uppsrc/plugin/Eigen/srcdoc.tpp/Eigen_en-us.tpp index 69f005e44..8b935cdde 100644 --- a/uppsrc/plugin/Eigen/srcdoc.tpp/Eigen_en-us.tpp +++ b/uppsrc/plugin/Eigen/srcdoc.tpp/Eigen_en-us.tpp @@ -31,7 +31,9 @@ to ease its use for U`+`+ users. It has many samples from Eigen library and nonlinear equation solving and optimization like ][^http`:`/`/www`.itl`.nist`.gov`/div898`/strd`/nls`/data`/eckerle4`.shtml^2 Eckerl e4][2 and ][^http`:`/`/www`.itl`.nist`.gov`/div898`/strd`/nls`/data`/thurber`.shtml^2 T -hurber][2 (see the graphs).]&] +hurber][2 (see the graphs). To simplify access to these features, +][^topic`:`/`/plugin`/Eigen`/src`/NonLinear`_en`-us^2 simple functions][2 +have been added.]&] [s0;#2 &] [s0;= [2 @@image:1272&1104 diff --git a/uppsrc/plugin/Eigen/srcdoc.tpp/NonLinear_en-us.tpp b/uppsrc/plugin/Eigen/srcdoc.tpp/NonLinear_en-us.tpp deleted file mode 100644 index 380a63795..000000000 --- a/uppsrc/plugin/Eigen/srcdoc.tpp/NonLinear_en-us.tpp +++ /dev/null @@ -1,4 +0,0 @@ -topic ""; -[ $$0,0#00000000000000000000000000000000:Default] -[{_}%EN-US -[s0; ] \ No newline at end of file