Skip to content

Commit

Permalink
debug eigen tests
Browse files Browse the repository at this point in the history
match parentheses


Set active variables


still debugging


using


no typename


catch all typename
  • Loading branch information
wgledbetter committed Sep 1, 2020
1 parent 441024f commit 5c5fcc4
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 53 deletions.
76 changes: 44 additions & 32 deletions include/boost/math/differentiation/autodiff_eigen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,40 +20,52 @@ struct NumTraits<boost::math::differentiation::autodiff_v1::detail::
};
};

template <typename RealType, size_t Order, typename BinaryOp, typename A>
struct ScalarBinaryOpTraits<boost::math::differentiation::autodiff_v1::detail::
template fvar<RealType, Order>,
A, BinaryOp> {
typedef boost::math::differentiation::autodiff_v1::detail::template fvar<
RealType, Order>
ReturnType;
};
// #define BOOST_AUTODIFF_EIGEN_SCALAR_TRAITS(A) \
// template <class Scalar, size_t Order, typename BinaryOp> \
// struct ScalarBinaryOpTraits<boost::math::differentiation::autodiff_v1:: \
// detail::template fvar<Scalar, Order>, \
// A, BinaryOp> { \
// typedef boost::math::differentiation::autodiff_v1::detail::template fvar< \
// Scalar, Order> \
// ReturnType; \
// }; \
// template <class Scalar, size_t Order, typename BinaryOp> \
// struct ScalarBinaryOpTraits<A, \
// boost::math::differentiation::autodiff_v1:: \
// detail::template fvar<Scalar, Order>, \
// BinaryOp> { \
// typedef boost::math::differentiation::autodiff_v1::detail::template fvar< \
// Scalar, Order> \
// ReturnType; \
// };

template <typename RealType, size_t Order, typename BinaryOp, typename A>
struct ScalarBinaryOpTraits<A,
boost::math::differentiation::autodiff_v1::detail::
template fvar<RealType, Order>,
BinaryOp> {
typedef boost::math::differentiation::autodiff_v1::detail::template fvar<
RealType, Order>
ReturnType;
};
// BOOST_AUTODIFF_EIGEN_SCALAR_TRAITS(float);
// BOOST_AUTODIFF_EIGEN_SCALAR_TRAITS(double);
// BOOST_AUTODIFF_EIGEN_SCALAR_TRAITS(long double);
// BOOST_AUTODIFF_EIGEN_SCALAR_TRAITS(short);
// BOOST_AUTODIFF_EIGEN_SCALAR_TRAITS(unsigned short);
// BOOST_AUTODIFF_EIGEN_SCALAR_TRAITS(int);
// BOOST_AUTODIFF_EIGEN_SCALAR_TRAITS(unsigned int);
// BOOST_AUTODIFF_EIGEN_SCALAR_TRAITS(long);
// BOOST_AUTODIFF_EIGEN_SCALAR_TRAITS(unsigned long);

template <typename RealType, size_t Order, typename RealType2, size_t Order2,
typename BinaryOp>
struct ScalarBinaryOpTraits<
boost::math::differentiation::autodiff_v1::detail::template fvar<RealType,
Order>,
boost::math::differentiation::autodiff_v1::detail::template fvar<RealType2,
Order2>,
BinaryOp> {
typedef ScalarBinaryOpTraits<RealType, RealType2, BinaryOp>::ReturnType
RealReturn;
const size_t ReturnOrder = (Order > Order2) ? Order : Order2;
typedef boost::math::differentiation::autodiff_v1::detail::template fvar<
RealReturn, ReturnOrder>
ReturnType;
}
// template <typename RealType, size_t Order, typename RealType2, size_t Order2,
// typename BinaryOp>
// struct ScalarBinaryOpTraits<
// boost::math::differentiation::autodiff_v1::detail::template
// fvar<RealType,
// Order>,
// boost::math::differentiation::autodiff_v1::detail::template
// fvar<RealType2,
// Order2>,
// BinaryOp> {
// typedef ScalarBinaryOpTraits<RealType, RealType2, BinaryOp>::ReturnType
// RealReturn;
// const size_t ReturnOrder = (Order > Order2) ? Order : Order2;
// typedef boost::math::differentiation::autodiff_v1::detail::template fvar<
// RealReturn, ReturnOrder>
// ReturnType;
// };
} // namespace Eigen

#endif // BOOST_MATH_DIFFERENTIATION_AUTODIFF_EIGEN_HPP
70 changes: 49 additions & 21 deletions test/test_autodiff_9.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@

BOOST_AUTO_TEST_SUITE(test_autodiff_9)

using boost::math::differentiation::autodiff_v1::detail::fvar;

BOOST_AUTO_TEST_CASE_TEMPLATE(eigen_init, T, all_float_types) {
constexpr int size = 5;

constexpr std::size_t n = 5;
typedef typename fvar<T, n> fTn;
typedef fvar<T, n> fTn;
Eigen::Matrix<fTn, size, 1> x;
x[0] = fTn(1.5);
x[1] = make_fvar<T, n - 1>(2.5);
Expand All @@ -18,7 +20,7 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(eigen_init, T, all_float_types) {
x[4] = 5.5;

constexpr std::size_t m = 2;
typedef typename fvar<T, n> fTm;
typedef fvar<T, n> fTm;
Eigen::Matrix<fTm, size, 1> y;
y = x.template cast<fTn>();
BOOST_CHECK_EQUAL(x[0].derivative(0), y[0].derivative(0));
Expand All @@ -28,7 +30,7 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(eigen_init, T, all_float_types) {
BOOST_CHECK_EQUAL(x[4].derivative(0), y[4].derivative(0));

constexpr std::size_t p = 3;
typedef typename fvar<T, p> fTp;
typedef fvar<T, p> fTp;
Eigen::Matrix<fTp, 1, size> z =
Eigen::Matrix<fTn, size, 1>::Random().transpose().template cast<fTp>();
}
Expand All @@ -44,12 +46,12 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(eigen_general, T, all_float_types) {
constexpr std::size_t n = 2;
constexpr double p = 3.456;

typedef typename fvar<T, n> fTn;
typedef fvar<T, n> fTn;
Eigen::Matrix<fTn, dim, 1> x;
x[0] = -1;
x[1] = 0;
x[2] = 1;
x[3] = 5;
x[0] = make_fvar<T, n>(-1);
x[1] = make_fvar<T, n>(0);
x[2] = make_fvar<T, n>(1);
x[3] = make_fvar<T, n>(5);

Eigen::Matrix<fTn, dim, 1> y1, y2, y3, y4, y5;
y1 = sin(x);
Expand Down Expand Up @@ -119,20 +121,20 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(eigen_general, T, all_float_types) {
(p - 1) * p * pow(x[3].derivative(0), p - 2));

// Check log
BOOST_CHECK_EQUAL(y5[2].derivative(0), log(x[0].derivative(0));
BOOST_CHECK_EQUAL(y5[3].derivative(0), log(x[3].derivative(0));
BOOST_CHECK_EQUAL(y5[2].derivative(1), 1/x[2].derivative(0));
BOOST_CHECK_EQUAL(y5[3].derivative(1), 1/x[3].derivative(0));
BOOST_CHECK_EQUAL(y5[2].derivative(2), -1/pow(x[2].derivative(0), 2));
BOOST_CHECK_EQUAL(y5[3].derivative(2), -1/pow(x[3].derivative(0), 2));
BOOST_CHECK_EQUAL(y5[2].derivative(0), log(x[0].derivative(0)));
BOOST_CHECK_EQUAL(y5[3].derivative(0), log(x[3].derivative(0)));
BOOST_CHECK_EQUAL(y5[2].derivative(1), 1 / x[2].derivative(0));
BOOST_CHECK_EQUAL(y5[3].derivative(1), 1 / x[3].derivative(0));
BOOST_CHECK_EQUAL(y5[2].derivative(2), -1 / pow(x[2].derivative(0), 2));
BOOST_CHECK_EQUAL(y5[3].derivative(2), -1 / pow(x[3].derivative(0), 2));
}

BOOST_AUTO_TEST_CASE_TEMPLATE(eigen_scalar, T, all_float_types) {
constexpr int dim = 4;
constexpr size_t n = 4;

typedef typename fvar<T, n> fTn;
fTn x = 4;
typedef fvar<T, n> fTn;
fTn x = make_fvar<T, n>(4);
Eigen::Matrix<fTn, dim, 1> X;
Eigen::Matrix<fTn, dim, 1> Z;
Eigen::Matrix<fTn, dim, dim> I = Eigen::Matrix<fTn, dim, dim>::Identity();
Expand Down Expand Up @@ -170,8 +172,8 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(eigen_vector, T, all_float_types) {
constexpr int dim = 3;
constexpr size_t n = 4;

typedef typename fvar<T, n> fTn;
fTn x = 5;
typedef fvar<T, n> fTn;
fTn x = make_fvar<T, n>(5);
T xD0 = x.derivative(0);
Eigen::Matrix<fTn, dim, 1> X;
X[0] = 1;
Expand Down Expand Up @@ -224,6 +226,32 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(eigen_vector, T, all_float_types) {
xD0 * sin(xD0) - 4 * cos(xD0));
}

// BOOST_AUTO_TEST_CASE_TEMPLATE(eigen_determinant, T, all_float_types) {
// constexpr int dim = 4;
// }
BOOST_AUTO_TEST_CASE_TEMPLATE(eigen_determinant, T, all_float_types) {
using std::cos;
using std::sin;
constexpr int dim = 4;
constexpr size_t n = 1;

typedef fvar<T, n> fTn;
fTn x = make_fvar<T, n>(3);
T xD0 = x.derivative(0);
Eigen::Matrix<fTn, dim, dim> M = 10 * Eigen::Matrix<fTn, dim, dim>::Random();
M(0, 3) = x;
M(1, 0) = 3 * x;
M(1, 2) = 1 / x;
M(2, 2) = x * x;
M(3, 1) = sin(x);
fTn y = M.determinant();

Eigen::Matrix<T, dim, dim> dMdx = Eigen::Matrix<T, dim, dim>::Zero();
dMdx(0, 3) = 1;
dMdx(1, 0) = 3;
dMdx(1, 2) = -1 / (xD0 * xD0);
dMdx(2, 2) = 2 * xD0;
dMdx(3, 1) = cos(xD0);

T ans = y.derivative(0) * (M.inverse() * dMdx).trace();
BOOST_CHECK_EQUAL(y.derivative(1), ans);
}

BOOST_AUTO_TEST_SUITE_END()

0 comments on commit 5c5fcc4

Please sign in to comment.