From 703198a1a6f1a4e22958852974dbf6e854610d71 Mon Sep 17 00:00:00 2001 From: Thomas Capricelli Date: Wed, 19 Aug 2009 18:32:37 +0200 Subject: [PATCH] wrapper for chkder() : this was the last wrapper missing --- .../Eigen/src/NonLinear/MathFunctions.h | 26 +++++++++ unsupported/test/NonLinear.cpp | 56 ++++++++----------- 2 files changed, 48 insertions(+), 34 deletions(-) diff --git a/unsupported/Eigen/src/NonLinear/MathFunctions.h b/unsupported/Eigen/src/NonLinear/MathFunctions.h index d3277505b..b4c23e704 100644 --- a/unsupported/Eigen/src/NonLinear/MathFunctions.h +++ b/unsupported/Eigen/src/NonLinear/MathFunctions.h @@ -356,5 +356,31 @@ int ei_lmdif1( ); } +template +void ei_chkder( + Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > &x, + Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > &fvec, + Eigen::Matrix< Scalar, Eigen::Dynamic, Eigen::Dynamic > &fjac, + Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > &xp, + Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > &fvecp, + int mode, + Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > &err + ) +{ + int ldfjac = fvec.size(); + if (mode==1) + xp.resize(ldfjac); + else + err.resize(ldfjac); + chkder( + fvec.size(), x.size(), x.data(), fvec.data(), + fjac.data(), ldfjac, + xp.data(), + fvecp.data(), + mode, + err.data() + ); +} + #endif // EIGEN_NONLINEAR_MATHFUNCTIONS_H diff --git a/unsupported/test/NonLinear.cpp b/unsupported/test/NonLinear.cpp index 87bbf5495..ddd8c3b66 100644 --- a/unsupported/test/NonLinear.cpp +++ b/unsupported/test/NonLinear.cpp @@ -57,59 +57,47 @@ int fcn_chkder(int /*m*/, int /*n*/, const double *x, double *fvec, double *fjac void testChkder() { - int i, m, n, ldfjac; - double x[3], fvec[15], fjac[15*3], xp[3], fvecp[15], - err[15]; - - m = 15; - n = 3; + int m=15, n=3; + Eigen::VectorXd x(n), fvec(m), xp, fvecp(m), err; + Eigen::MatrixXd fjac(m,n); + VectorXi ipvt; /* the following values should be suitable for */ /* checking the jacobian matrix. */ + x << 9.2e-1, 1.3e-1, 5.4e-1; - x[1-1] = 9.2e-1; - x[2-1] = 1.3e-1; - x[3-1] = 5.4e-1; + ei_chkder(x, fvec, fjac, xp, fvecp, 1, err); + fcn_chkder(m, n, x.data(), fvec.data(), fjac.data(), m, 1); + fcn_chkder(m, n, x.data(), fvec.data(), fjac.data(), m, 2); + fcn_chkder(m, n, xp.data(), fvecp.data(), fjac.data(), m, 1); + ei_chkder(x, fvec, fjac, xp, fvecp, 2, err); - ldfjac = 15; + fvecp -= fvec; - chkder(m, n, x, fvec, fjac, ldfjac, xp, fvecp, 1, err); - fcn_chkder(m, n, x, fvec, fjac, ldfjac, 1); - fcn_chkder(m, n, x, fvec, fjac, ldfjac, 2); - fcn_chkder(m, n, xp, fvecp, fjac, ldfjac, 1); - chkder(m, n, x, fvec, fjac, ldfjac, xp, fvecp, 2, err); - - for (i=1; i<=m; i++) - { - fvecp[i-1] = fvecp[i-1] - fvec[i-1]; - } - - - double fvec_ref[] = { + // check those + VectorXd fvec_ref(m), fvecp_ref(m), err_ref(m); + fvec_ref << -1.181606, -1.429655, -1.606344, -1.745269, -1.840654, -1.921586, -1.984141, -2.022537, -2.468977, -2.827562, -3.473582, -4.437612, - -6.047662, -9.267761, -18.91806 - }; - double fvecp_ref[] = { + -6.047662, -9.267761, -18.91806; + fvecp_ref << -7.724666e-09, -3.432406e-09, -2.034843e-10, 2.313685e-09, 4.331078e-09, 5.984096e-09, 7.363281e-09, 8.53147e-09, 1.488591e-08, 2.33585e-08, 3.522012e-08, 5.301255e-08, - 8.26666e-08, 1.419747e-07, 3.19899e-07 - }; - double err_ref[] = { + 8.26666e-08, 1.419747e-07, 3.19899e-07; + err_ref << 0.1141397, 0.09943516, 0.09674474, 0.09980447, 0.1073116, 0.1220445, 0.1526814, 1, 1, 1, 1, 1, - 1, 1, 1 - }; + 1, 1, 1; - for (i=1; i<=m; i++) VERIFY_IS_APPROX(fvec[i-1], fvec_ref[i-1]); - for (i=1; i<=m; i++) VERIFY_IS_APPROX(fvecp[i-1], fvecp_ref[i-1]); - for (i=1; i<=m; i++) VERIFY_IS_APPROX(err[i-1], err_ref[i-1]); + VERIFY_IS_APPROX(fvec, fvec_ref); + VERIFY_IS_APPROX(fvecp, fvecp_ref); + VERIFY_IS_APPROX(err, err_ref); }