Eigen: NonLinear functions

git-svn-id: svn://ultimatepp.org/upp/trunk@14514 f0d560ea-af0d-0410-9eb7-867de7ffcac7
This commit is contained in:
koldo 2020-05-30 09:28:07 +00:00
parent 75a2778769
commit d4ca363f30
4 changed files with 42 additions and 30 deletions

View file

@ -1,12 +1,11 @@
#include <Core/Core.h>
#include <Eigen.h>
namespace Upp {
using namespace Eigen;
bool NonLinearOptimization(VectorXd &y, int numData, Function <int(const VectorXd &b, VectorXd &residual)> Residual) {
bool NonLinearOptimization(VectorXd &y, Eigen::Index numData, Function <int(const VectorXd &b, VectorXd &residual)> Residual) {
Basic_functor functor(Residual);
functor.unknowns = y.size();
functor.datasetLen = numData;
@ -19,7 +18,7 @@ bool NonLinearOptimization(VectorXd &y, int numData, Function <int(const VectorX
return true;
}
bool NonLinearSolver(VectorXd &y, Function <int(const VectorXd &b, VectorXd &residual)> Residual) {
bool SolveNonLinearEquations(VectorXd &y, Function <int(const VectorXd &b, VectorXd &residual)> Residual) {
Basic_functor functor(Residual);
HybridNonLinearSolver<Basic_functor> solver(functor);
int ret = solver.solveNumericalDiff(y);

View file

@ -1,8 +1,6 @@
#ifndef _Eigen_Eigen_h
#define _Eigen_Eigen_h
#include <Core/Core.h>
#define EIGEN_MATRIX_PLUGIN <plugin/Eigen/ToStringPlugin.h>
#define EIGEN_DENSEBASE_PLUGIN <plugin/Eigen/ToStringPlugin.h>
#define EIGEN_TENSOR_PLUGIN <plugin/Eigen/ToStringPlugin.h>
@ -11,12 +9,10 @@
#define EIGEN_NO_DEBUG
#endif
#define EIGEN_EXCEPTIONS
#define eigen_assert(x) ASSERT(x)
#undef Success
#include "Eigen/Eigen"
#include "Eigen/Dense"
#include <plugin/Eigen/unsupported/Eigen/NonLinearOptimization>
#include <plugin/Eigen/unsupported/Eigen/FFT>
#include <plugin/Eigen/unsupported/Eigen/CXX11/Tensor>
@ -51,8 +47,8 @@ struct Basic_functor : NonLinearOptimizationFunctor<double> {
Function <int(const Eigen::VectorXd &b, Eigen::VectorXd &err)> function;
};
bool NonLinearOptimization(Eigen::VectorXd &y, int numData, Function <int(const Eigen::VectorXd &y, Eigen::VectorXd &residual)>residual);
bool NonLinearSolver(Eigen::VectorXd &y, Function <int(const Eigen::VectorXd &b, Eigen::VectorXd &residual)> Residual);
bool NonLinearOptimization(Eigen::VectorXd &y, Eigen::Index numData, Function <int(const Eigen::VectorXd &y, Eigen::VectorXd &residual)>residual);
bool SolveNonLinearEquations(Eigen::VectorXd &y, Function <int(const Eigen::VectorXd &b, Eigen::VectorXd &residual)> Residual);
template <class T>
void Xmlize(XmlIO &xml, Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> &mat) {

View file

@ -11,31 +11,40 @@ topic "";
and equation system solving.]&]
[s0;2 &]
[s1;%- &]
[s2;:Upp`:`:NonLinearOptimization`(Eigen`:`:VectorXd`&`,int`,Upp`:`:Function`<int`(const Eigen`:`:VectorXd`&`,Eigen`:`:VectorXd`&`)`>`):%- [@(0.0.255) b
[s2;:Upp`:`:SolveNonLinearEquations`(Eigen`:`:VectorXd`&`,Upp`:`:Function`<int`(const Eigen`:`:VectorXd`&b`,Eigen`:`:VectorXd`&residual`)`>`):%- [@(0.0.255) b
ool]_[* SolveNonLinearEquations]([_^Eigen`:`:VectorXd^ Eigen`::VectorXd]_`&[*@3 y],
[_^Upp`:`:Function^ Function]_<[@(0.0.255) int]([@(0.0.255) const]_[_^Eigen`:`:VectorXd^ E
igen`::VectorXd]_`&b, [_^Eigen`:`:VectorXd^ Eigen`::VectorXd]_`&residual)>_[*@3 Residua
l])&]
[s3; Given a system of non linear 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 zeroes the [%-*@3 Residual].&]
[s3; The dimension of [%-*@3 y] ([%-*@3 y.size()]) has to be equal to
the number of equations ([%-*@3 residual.size()]).&]
[s3; It uses a modification of the [^https`:`/`/en`.wikipedia`.org`/wiki`/Powell`%27s`_method^ P
owell`'s hybrid method] (`"dogleg`"). The Jacobian is approximated
using a forward`-difference method. &]
[s3; The algorithm was ported in [^http`:`/`/eigen`.tuxfamily`.org`/index`.php`?title`=Main`_Page^ E
igen] from [^https`:`/`/www`.math`.utah`.edu`/software`/minpack`/minpack`/hybrd`.html^ M
INPACK] library.&]
[s4; &]
[s1;%- &]
[s2;:Upp`:`:NonLinearOptimization`(Eigen`:`:VectorXd`&`,Eigen`:`:Index`,Upp`:`:Function`<int`(const Eigen`:`:VectorXd`&`,Eigen`:`:VectorXd`&`)`>`):%- [@(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])&]
[_^Eigen`:`:Index^ Eigen`::Index]_[*@3 numData], [_^Upp`:`:Function^ Function]_<[@(0.0.255) i
nt]([@(0.0.255) const]_[_^Eigen`:`:VectorXd^ Eigen`::VectorXd]_`&[*@3 y],
[_^Eigen`:`:VectorXd^ Eigen`::VectorXd]_`&[*@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.&]
[s3; [%- Residual ]Function has to fill in every call [%- residual]`[[%- numData]`]
Vector by applying the unknowns provisionally guessed values
in [%- y] to the model to be solved.&]
[s3; The algorithm was ported in [^http`:`/`/eigen`.tuxfamily`.org`/index`.php`?title`=Main`_Page^ E
igen] from [^https`:`/`/www`.math`.utah`.edu`/software`/minpack`/minpack`/lmder`.html^ M
INPACK] library.&]
[s4; &]
[s1;%- &]
[s2;:Upp`:`:NonLinearSolver`(Eigen`:`:VectorXd`&`,Upp`:`:Function`<int`(const Eigen`:`:VectorXd`&b`,Eigen`:`:VectorXd`&residual`)`>`):%- [@(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; ]]

View file

@ -0,0 +1,8 @@
TITLE("")
COMPRESSED
120,156,213,86,91,111,219,54,20,254,43,68,215,116,118,224,40,18,37,197,142,221,117,41,214,110,235,186,166,197,186,14,3,4,197,164,164,99,155,11,37,122,20,21,199,41,250,223,119,72,203,178,147,165,104,182,135,1,243,131,205,203,185,124,231,246,209,201,143,39,19,159,60,126,28,12,252,175,252,248,196,247,253,147,56,136,162,200,143,232,73,224,135,52,10,131,208,167,97,28,156,198,39,227,12,230,162,74,19,17,69,163,9,167,241,228,242,245,219,211,9,69,109,138,218,225,48,8,71,113,24,160,34,126,5,97,64,99,26,70,193,136,158,70,163,48,28,250,227,92,21,32,12,148,105,34,233,104,228,244,66,212,163,195,152,6,195,104,20,141,130,112,56,162,168,78,125,159,250,67,138,56,194,17,141,199,5,212,121,154,88,144,17,138,159,158,132,167,126,224,251,195,192,194,27,250,97,28,5,65,72,209,4,29,210,192,143,199,80,21,105,130,194,190,141,232,11,159,241,11,152,241,70,154,52,249,56,253,116,240,242,252,232,195,123,146,144,143,31,3,123,121,214,67,195,94,
68,61,191,79,146,218,159,144,228,240,140,78,34,114,174,42,41,42,224,154,204,154,42,55,66,85,117,154,126,250,244,36,181,66,135,103,33,138,108,214,36,161,228,189,42,97,39,71,22,252,10,72,6,80,17,81,229,178,41,128,24,69,128,215,64,170,206,170,90,26,81,138,27,110,53,8,175,10,2,127,54,155,77,189,174,49,127,164,86,242,74,84,115,47,221,184,161,206,93,48,57,56,114,11,58,25,127,88,46,217,152,141,223,163,28,32,218,159,157,221,151,173,149,154,245,94,138,57,84,86,226,55,200,141,210,191,23,236,9,27,180,74,223,183,88,217,83,81,25,214,203,81,193,144,123,20,50,54,184,231,84,67,45,138,134,75,214,103,207,88,127,140,144,146,179,158,239,249,30,141,227,62,201,148,146,233,52,57,36,159,65,150,246,146,233,197,223,172,94,108,221,119,39,233,148,61,193,98,132,100,157,14,8,170,220,129,126,65,182,171,116,250,116,223,63,70,132,46,246,14,92,116,136,232,193,94,51,231,239,129,194,219,100,244,159,77,29,218,95,218,125,218,183,117,10,39,
228,7,113,133,157,192,183,117,85,51,219,5,164,109,131,109,213,107,82,192,12,143,10,146,173,187,192,72,114,112,116,203,228,192,117,10,154,2,99,237,136,74,24,193,37,185,226,178,129,218,158,152,5,144,166,186,172,212,10,45,90,41,209,25,177,89,52,11,81,119,125,74,140,22,168,133,173,169,50,195,81,208,42,183,150,59,29,60,228,134,220,128,86,86,20,5,238,66,242,218,40,127,197,187,66,148,80,213,214,244,45,19,189,237,210,171,197,13,244,250,105,31,39,196,57,206,192,37,64,218,181,53,94,53,101,6,218,106,239,242,178,213,222,230,185,51,178,245,252,202,144,166,70,116,156,148,170,16,51,145,111,198,168,205,70,114,177,48,102,89,99,29,143,217,49,22,207,91,137,75,177,132,66,112,230,41,61,103,199,118,207,142,223,169,21,72,201,14,232,176,102,211,18,204,66,97,185,219,195,175,113,162,215,153,22,5,217,92,96,68,236,81,161,230,18,230,236,81,223,115,161,255,196,115,149,9,142,19,143,64,150,75,173,174,69,201,13,150,179,169,113,136,17,219,76,233,
21,215,5,59,66,136,51,208,80,229,208,154,243,200,94,10,185,156,43,45,204,162,36,43,76,209,82,105,107,195,22,209,133,209,70,225,186,208,51,205,245,140,151,66,174,219,64,68,85,192,53,243,150,139,37,251,214,8,35,129,125,243,6,203,202,166,239,248,28,218,222,77,201,76,171,242,118,82,86,171,21,243,16,236,130,121,141,225,248,13,69,195,142,107,53,51,8,24,216,113,41,170,37,207,47,247,22,54,27,5,243,22,166,148,23,228,205,171,243,119,207,191,123,157,98,71,103,154,235,181,43,75,52,249,28,93,117,124,240,118,143,0,63,67,86,221,225,43,23,218,63,37,175,123,185,235,11,156,117,47,188,127,203,88,183,241,239,228,221,54,221,208,5,182,252,11,110,248,127,77,113,247,194,124,128,138,238,216,237,217,157,253,29,182,219,141,114,59,191,219,64,81,37,87,186,168,7,110,60,85,246,7,186,64,29,59,56,59,42,18,246,77,36,248,63,66,115,119,185,226,235,125,246,65,82,105,89,14,35,135,25,206,188,128,202,212,27,178,194,54,181,149,3,244,32,10,
224,82,174,173,97,75,97,131,125,27,29,114,2,90,43,221,241,197,29,126,235,74,208,17,157,34,162,196,1,71,84,142,48,155,60,135,186,37,95,140,33,231,50,111,164,107,155,193,30,220,29,225,181,164,141,168,102,66,74,156,238,21,142,59,225,133,99,188,238,154,204,155,206,234,142,210,183,24,208,110,135,144,164,221,115,209,58,177,118,109,6,225,10,244,218,2,146,78,161,11,152,37,118,187,173,7,75,201,166,202,246,229,65,234,146,107,75,88,183,30,18,27,173,176,188,238,96,59,100,136,187,125,117,54,47,140,123,42,54,36,142,44,12,178,13,214,254,137,129,194,251,191,17,156,44,11,208,15,33,56,252,73,211,191,0,230,61,202,225,