// This file is part of Eigen, a lightweight C++ template library // for linear algebra. // // Copyright (C) 2009 Gael Guennebaud <g.gael@free.fr> // // This Source Code Form is subject to the terms of the Mozilla // Public License v. 2.0. If a copy of the MPL was not distributed // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
// TODO also check actual derivatives! template <int> void test_autodiff_scalar()
{
Vector2f p = Vector2f::Random(); typedef AutoDiffScalar<Vector2f> AD;
AD ax(p.x(),Vector2f::UnitX());
AD ay(p.y(),Vector2f::UnitY());
AD res = foo<AD>(ax,ay);
VERIFY_IS_APPROX(res.value(), foo(p.x(),p.y()));
}
// TODO also check actual derivatives! template <int> void test_autodiff_vector()
{
Vector2f p = Vector2f::Random(); typedef AutoDiffScalar<Vector2f> AD; typedef Matrix<AD,2,1> VectorAD;
VectorAD ap = p.cast<AD>();
ap.x().derivatives() = Vector2f::UnitX();
ap.y().derivatives() = Vector2f::UnitY();
AD res = foo<VectorAD>(ap);
VERIFY_IS_APPROX(res.value(), foo(p));
}
//set unit vectors for the derivative directions (partial derivatives of the input vector)
x(0).derivatives().resize(2);
x(0).derivatives().setZero();
x(0).derivatives()(0)= 1;
x(1).derivatives().resize(2);
x(1).derivatives().setZero();
x(1).derivatives()(1)=1;
//repeat partial derivatives for the inner AutoDiffScalar
x(0).value().derivatives() = VectorXd::Unit(2,0);
x(1).value().derivatives() = VectorXd::Unit(2,1);
//set the hessian matrix to zero for(int idx=0; idx<2; idx++) {
x(0).derivatives()(idx).derivatives() = VectorXd::Zero(2);
x(1).derivatives()(idx).derivatives() = VectorXd::Zero(2);
}
ADD z = x(0)*x(1);
VERIFY_IS_APPROX(z.derivatives()(0).derivatives(), Vector2d(0,1));
VERIFY_IS_APPROX(z.derivatives()(1).derivatives(), Vector2d(1,0));
}
double bug_1222() { typedef Eigen::AutoDiffScalar<Eigen::Vector3d> AD; constdouble _cv1_3 = 1.0; const AD chi_3 = 1.0; // this line did not work, because operator+ returns ADS<DerType&>, which then cannot be converted to ADS<DerType> const AD denom = chi_3 + _cv1_3; return denom.value();
}
#ifdef EIGEN_TEST_PART_5
double bug_1223() { using std::min; typedef Eigen::AutoDiffScalar<Eigen::Vector3d> AD;
constdouble _cv1_3 = 1.0; const AD chi_3 = 1.0; const AD denom = 1.0;
// failed because implementation of min attempts to construct ADS<DerType&> via constructor AutoDiffScalar(const Real& value) // without initializing m_derivatives (which is a reference in this case) #define EIGEN_TEST_SPACE const AD t = min EIGEN_TEST_SPACE (denom / chi_3, 1.0);
const AD t2 = min EIGEN_TEST_SPACE (denom / (chi_3 * _cv1_3), 1.0);
return t.value() + t2.value();
}
// regression test for some compilation issues with specializations of ScalarBinaryOpTraits void bug_1260() {
Matrix4d A = Matrix4d::Ones();
Vector4d v = Vector4d::Ones();
A*v;
}
// check a compilation issue with numext::max double bug_1261() { typedef AutoDiffScalar<Matrix2d> AD; typedef Matrix<AD,2,1> VectorAD;
VectorAD v(0.,0.); const AD maxVal = v.maxCoeff(); const AD minVal = v.minCoeff(); return maxVal.value() + minVal.value();
}
double bug_1264() { typedef AutoDiffScalar<Vector2d> AD; const AD s = 0.; const Matrix<AD, 3, 1> v1(0.,0.,0.); const Matrix<AD, 3, 1> v2 = (s + 3.0) * v1; return v2(0).value();
}
// check with expressions on constants double bug_1281() { int n = 2; typedef AutoDiffScalar<VectorXd> AD; const AD c = 1.;
AD x0(2,n,0);
AD y1 = (AD(c)+AD(c))*x0;
y1 = x0 * (AD(c)+AD(c));
AD y2 = (-AD(c))+x0;
y2 = x0+(-AD(c));
AD y3 = (AD(c)*(-AD(c))+AD(c))*x0;
y3 = x0 * (AD(c)*(-AD(c))+AD(c)); return (y1+y2+y3).value();
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.