mirror of
https://gitlab.com/libeigen/eigen.git
synced 2025-09-23 14:53:13 +08:00
Fixed bug #702 and added unit test.
Thanks to Alexander Werner for the report.
This commit is contained in:
parent
76c230a84d
commit
ae83f5ede9
@ -599,12 +599,10 @@ atan2(const AutoDiffScalar<DerTypeA>& a, const AutoDiffScalar<DerTypeB>& b)
|
|||||||
PlainADS ret;
|
PlainADS ret;
|
||||||
ret.value() = atan2(a.value(), b.value());
|
ret.value() = atan2(a.value(), b.value());
|
||||||
|
|
||||||
Scalar tmp2 = a.value() * a.value();
|
Scalar squared_hypot = a.value() * a.value() + b.value() * b.value();
|
||||||
Scalar tmp3 = b.value() * b.value();
|
|
||||||
Scalar tmp4 = tmp3/(tmp2+tmp3);
|
|
||||||
|
|
||||||
if (tmp4!=0)
|
// if (squared_hypot==0) the derivation is undefined and the following results in a NaN:
|
||||||
ret.derivatives() = (a.derivatives() * b.value() - a.value() * b.derivatives()) * (tmp2+tmp3);
|
ret.derivatives() = (a.derivatives() * b.value() - a.value() * b.derivatives()) / squared_hypot;
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
@ -28,6 +28,7 @@ endif(ADOLC_FOUND)
|
|||||||
ei_add_test(NonLinearOptimization)
|
ei_add_test(NonLinearOptimization)
|
||||||
|
|
||||||
ei_add_test(NumericalDiff)
|
ei_add_test(NumericalDiff)
|
||||||
|
ei_add_test(autodiff_scalar)
|
||||||
ei_add_test(autodiff)
|
ei_add_test(autodiff)
|
||||||
|
|
||||||
if (NOT CMAKE_CXX_COMPILER MATCHES "clang\\+\\+$")
|
if (NOT CMAKE_CXX_COMPILER MATCHES "clang\\+\\+$")
|
||||||
|
44
unsupported/test/autodiff_scalar.cpp
Normal file
44
unsupported/test/autodiff_scalar.cpp
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
// 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());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void test_autodiff_scalar()
|
||||||
|
{
|
||||||
|
for(int i = 0; i < g_repeat; i++) {
|
||||||
|
CALL_SUBTEST_1( check_atan2<float>() );
|
||||||
|
CALL_SUBTEST_2( check_atan2<double>() );
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user