2013-11-08 01:32:24 +08:00
|
|
|
// This file is part of Eigen, a lightweight C++ template library
|
|
|
|
// for linear algebra.
|
|
|
|
//
|
|
|
|
// Copyright (C) 2013 Christoph Hertzberg <chtz@informatik.uni-bremen.de>
|
|
|
|
//
|
|
|
|
// 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/.
|
|
|
|
|
|
|
|
#include "main.h"
|
|
|
|
#include <unsupported/Eigen/AutoDiff>
|
|
|
|
|
|
|
|
/*
|
|
|
|
* In this file scalar derivations are tested for correctness.
|
|
|
|
* TODO add more tests!
|
|
|
|
*/
|
|
|
|
|
|
|
|
template<typename Scalar> void check_atan2()
|
|
|
|
{
|
|
|
|
typedef Matrix<Scalar, 1, 1> Deriv1;
|
|
|
|
typedef AutoDiffScalar<Deriv1> AD;
|
|
|
|
|
|
|
|
AD x(internal::random<Scalar>(-3.0, 3.0), Deriv1::UnitX());
|
|
|
|
|
|
|
|
using std::exp;
|
|
|
|
Scalar r = exp(internal::random<Scalar>(-10, 10));
|
|
|
|
|
|
|
|
AD s = sin(x), c = cos(x);
|
|
|
|
AD res = atan2(r*s, r*c);
|
|
|
|
|
|
|
|
VERIFY_IS_APPROX(res.value(), x.value());
|
|
|
|
VERIFY_IS_APPROX(res.derivatives(), x.derivatives());
|
2016-04-05 20:49:41 +08:00
|
|
|
|
|
|
|
res = atan2(r*s+0, r*c+0);
|
|
|
|
VERIFY_IS_APPROX(res.value(), x.value());
|
|
|
|
VERIFY_IS_APPROX(res.derivatives(), x.derivatives());
|
2013-11-08 01:32:24 +08:00
|
|
|
}
|
|
|
|
|
2016-06-16 14:33:19 +08:00
|
|
|
template<typename Scalar> void check_hyperbolic_functions()
|
|
|
|
{
|
|
|
|
using std::sinh;
|
|
|
|
using std::cosh;
|
|
|
|
using std::tanh;
|
|
|
|
typedef Matrix<Scalar, 1, 1> Deriv1;
|
|
|
|
typedef AutoDiffScalar<Deriv1> AD;
|
|
|
|
Deriv1 p = Deriv1::Random();
|
|
|
|
AD val(p.x(),Deriv1::UnitX());
|
|
|
|
|
|
|
|
Scalar cosh_px = std::cosh(p.x());
|
|
|
|
AD res1 = tanh(val);
|
|
|
|
VERIFY_IS_APPROX(res1.value(), std::tanh(p.x()));
|
|
|
|
VERIFY_IS_APPROX(res1.derivatives().x(), Scalar(1.0) / (cosh_px * cosh_px));
|
2013-11-08 01:32:24 +08:00
|
|
|
|
2016-06-16 14:33:19 +08:00
|
|
|
AD res2 = sinh(val);
|
|
|
|
VERIFY_IS_APPROX(res2.value(), std::sinh(p.x()));
|
|
|
|
VERIFY_IS_APPROX(res2.derivatives().x(), cosh_px);
|
2013-11-08 01:32:24 +08:00
|
|
|
|
2016-06-16 14:33:19 +08:00
|
|
|
AD res3 = cosh(val);
|
|
|
|
VERIFY_IS_APPROX(res3.value(), cosh_px);
|
|
|
|
VERIFY_IS_APPROX(res3.derivatives().x(), std::sinh(p.x()));
|
|
|
|
|
|
|
|
// Check constant values.
|
|
|
|
const Scalar sample_point = Scalar(1) / Scalar(3);
|
|
|
|
val = AD(sample_point,Deriv1::UnitX());
|
|
|
|
res1 = tanh(val);
|
|
|
|
VERIFY_IS_APPROX(res1.derivatives().x(), Scalar(0.896629559604914));
|
|
|
|
|
|
|
|
res2 = sinh(val);
|
|
|
|
VERIFY_IS_APPROX(res2.derivatives().x(), Scalar(1.056071867829939));
|
|
|
|
|
|
|
|
res3 = cosh(val);
|
|
|
|
VERIFY_IS_APPROX(res3.derivatives().x(), Scalar(0.339540557256150));
|
|
|
|
}
|
2013-11-08 01:32:24 +08:00
|
|
|
|
2017-05-24 05:12:36 +08:00
|
|
|
template <typename Scalar>
|
|
|
|
void check_limits_specialization()
|
|
|
|
{
|
|
|
|
typedef Eigen::Matrix<Scalar, 1, 1> Deriv;
|
|
|
|
typedef Eigen::AutoDiffScalar<Deriv> AD;
|
|
|
|
|
|
|
|
typedef std::numeric_limits<AD> A;
|
|
|
|
typedef std::numeric_limits<Scalar> B;
|
|
|
|
|
2018-06-07 20:43:02 +08:00
|
|
|
// workaround "unused typedef" warning:
|
2018-04-24 23:11:51 +08:00
|
|
|
VERIFY(!bool(internal::is_same<B, A>::value));
|
|
|
|
|
2017-06-09 18:45:01 +08:00
|
|
|
#if EIGEN_HAS_CXX11
|
|
|
|
VERIFY(bool(std::is_base_of<B, A>::value));
|
|
|
|
#endif
|
2017-05-24 05:12:36 +08:00
|
|
|
}
|
|
|
|
|
2018-07-17 20:46:15 +08:00
|
|
|
EIGEN_DECLARE_TEST(autodiff_scalar)
|
2013-11-08 01:32:24 +08:00
|
|
|
{
|
|
|
|
for(int i = 0; i < g_repeat; i++) {
|
|
|
|
CALL_SUBTEST_1( check_atan2<float>() );
|
|
|
|
CALL_SUBTEST_2( check_atan2<double>() );
|
2016-06-16 14:33:19 +08:00
|
|
|
CALL_SUBTEST_3( check_hyperbolic_functions<float>() );
|
|
|
|
CALL_SUBTEST_4( check_hyperbolic_functions<double>() );
|
2017-05-24 05:12:36 +08:00
|
|
|
CALL_SUBTEST_5( check_limits_specialization<double>());
|
2013-11-08 01:32:24 +08:00
|
|
|
}
|
|
|
|
}
|