mirror of
https://gitlab.com/libeigen/eigen.git
synced 2025-08-14 12:46:00 +08:00
autodiff:
* fix namespace issue * simplify Jacobian code * fix issue with "Dynamic derivatives"
This commit is contained in:
parent
0927ba1fd3
commit
1503043981
@ -51,7 +51,8 @@ public:
|
|||||||
typedef typename Functor::ValueType ValueType;
|
typedef typename Functor::ValueType ValueType;
|
||||||
typedef typename Functor::JacobianType JacobianType;
|
typedef typename Functor::JacobianType JacobianType;
|
||||||
|
|
||||||
typedef AutoDiffScalar<Matrix<double,InputsAtCompileTime,1> > ActiveScalar;
|
typedef Matrix<double,InputsAtCompileTime,1> DerivativeType;
|
||||||
|
typedef AutoDiffScalar<DerivativeType> ActiveScalar;
|
||||||
|
|
||||||
typedef Matrix<ActiveScalar, InputsAtCompileTime, 1> ActiveInput;
|
typedef Matrix<ActiveScalar, InputsAtCompileTime, 1> ActiveInput;
|
||||||
typedef Matrix<ActiveScalar, ValuesAtCompileTime, 1> ActiveValue;
|
typedef Matrix<ActiveScalar, ValuesAtCompileTime, 1> ActiveValue;
|
||||||
@ -71,24 +72,18 @@ public:
|
|||||||
ActiveValue av(jac.rows());
|
ActiveValue av(jac.rows());
|
||||||
|
|
||||||
if(InputsAtCompileTime==Dynamic)
|
if(InputsAtCompileTime==Dynamic)
|
||||||
{
|
|
||||||
for (int j=0; j<jac.cols(); j++)
|
|
||||||
ax[j].derivatives().resize(this->inputs());
|
|
||||||
for (int j=0; j<jac.rows(); j++)
|
for (int j=0; j<jac.rows(); j++)
|
||||||
av[j].derivatives().resize(this->inputs());
|
av[j].derivatives().resize(this->inputs());
|
||||||
}
|
|
||||||
|
|
||||||
for (int j=0; j<jac.cols(); j++)
|
|
||||||
for (int i=0; i<jac.cols(); i++)
|
for (int i=0; i<jac.cols(); i++)
|
||||||
ax[i].derivatives().coeffRef(j) = i==j ? 1 : 0;
|
ax[i].derivatives() = DerivativeType::Unit(this->inputs(),i);
|
||||||
|
|
||||||
Functor::operator()(ax, &av);
|
Functor::operator()(ax, &av);
|
||||||
|
|
||||||
for (int i=0; i<jac.rows(); i++)
|
for (int i=0; i<jac.rows(); i++)
|
||||||
{
|
{
|
||||||
(*v)[i] = av[i].value();
|
(*v)[i] = av[i].value();
|
||||||
for (int j=0; j<jac.cols(); j++)
|
jac.row(i) = av[i].derivatives();
|
||||||
jac.coeffRef(i,j) = av[i].derivatives().coeff(j);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
protected:
|
protected:
|
||||||
|
@ -27,6 +27,18 @@
|
|||||||
|
|
||||||
namespace Eigen {
|
namespace Eigen {
|
||||||
|
|
||||||
|
template<typename A, typename B>
|
||||||
|
struct ei_make_coherent_impl {
|
||||||
|
static void run(A& a, B& b) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
// resize a to match b is a.size()==0, and conversely.
|
||||||
|
template<typename A, typename B>
|
||||||
|
void ei_make_coherent(const A& a, const B&b)
|
||||||
|
{
|
||||||
|
ei_make_coherent_impl<A,B>::run(a.const_cast_derived(), b.const_cast_derived());
|
||||||
|
}
|
||||||
|
|
||||||
/** \class AutoDiffScalar
|
/** \class AutoDiffScalar
|
||||||
* \brief A scalar type replacement with automatic differentation capability
|
* \brief A scalar type replacement with automatic differentation capability
|
||||||
*
|
*
|
||||||
@ -100,6 +112,7 @@ class AutoDiffScalar
|
|||||||
inline const AutoDiffScalar<CwiseBinaryOp<ei_scalar_sum_op<Scalar>,DerType,OtherDerType> >
|
inline const AutoDiffScalar<CwiseBinaryOp<ei_scalar_sum_op<Scalar>,DerType,OtherDerType> >
|
||||||
operator+(const AutoDiffScalar<OtherDerType>& other) const
|
operator+(const AutoDiffScalar<OtherDerType>& other) const
|
||||||
{
|
{
|
||||||
|
ei_make_coherent(m_derivatives, other.derivatives());
|
||||||
return AutoDiffScalar<CwiseBinaryOp<ei_scalar_sum_op<Scalar>,DerType,OtherDerType> >(
|
return AutoDiffScalar<CwiseBinaryOp<ei_scalar_sum_op<Scalar>,DerType,OtherDerType> >(
|
||||||
m_value + other.value(),
|
m_value + other.value(),
|
||||||
m_derivatives + other.derivatives());
|
m_derivatives + other.derivatives());
|
||||||
@ -117,6 +130,7 @@ class AutoDiffScalar
|
|||||||
inline const AutoDiffScalar<CwiseBinaryOp<ei_scalar_difference_op<Scalar>, DerType,OtherDerType> >
|
inline const AutoDiffScalar<CwiseBinaryOp<ei_scalar_difference_op<Scalar>, DerType,OtherDerType> >
|
||||||
operator-(const AutoDiffScalar<OtherDerType>& other) const
|
operator-(const AutoDiffScalar<OtherDerType>& other) const
|
||||||
{
|
{
|
||||||
|
ei_make_coherent(m_derivatives, other.derivatives());
|
||||||
return AutoDiffScalar<CwiseBinaryOp<ei_scalar_difference_op<Scalar>, DerType,OtherDerType> >(
|
return AutoDiffScalar<CwiseBinaryOp<ei_scalar_difference_op<Scalar>, DerType,OtherDerType> >(
|
||||||
m_value - other.value(),
|
m_value - other.value(),
|
||||||
m_derivatives - other.derivatives());
|
m_derivatives - other.derivatives());
|
||||||
@ -178,6 +192,7 @@ class AutoDiffScalar
|
|||||||
NestByValue<CwiseUnaryOp<ei_scalar_multiple_op<Scalar>, OtherDerType> > > > > >
|
NestByValue<CwiseUnaryOp<ei_scalar_multiple_op<Scalar>, OtherDerType> > > > > >
|
||||||
operator/(const AutoDiffScalar<OtherDerType>& other) const
|
operator/(const AutoDiffScalar<OtherDerType>& other) const
|
||||||
{
|
{
|
||||||
|
ei_make_coherent(m_derivatives, other.derivatives());
|
||||||
return AutoDiffScalar<CwiseUnaryOp<ei_scalar_multiple_op<Scalar>,
|
return AutoDiffScalar<CwiseUnaryOp<ei_scalar_multiple_op<Scalar>,
|
||||||
NestByValue<CwiseBinaryOp<ei_scalar_difference_op<Scalar>,
|
NestByValue<CwiseBinaryOp<ei_scalar_difference_op<Scalar>,
|
||||||
NestByValue<CwiseUnaryOp<ei_scalar_multiple_op<Scalar>, DerType> >,
|
NestByValue<CwiseUnaryOp<ei_scalar_multiple_op<Scalar>, DerType> >,
|
||||||
@ -193,6 +208,7 @@ class AutoDiffScalar
|
|||||||
NestByValue<CwiseUnaryOp<ei_scalar_multiple_op<Scalar>, OtherDerType> > > >
|
NestByValue<CwiseUnaryOp<ei_scalar_multiple_op<Scalar>, OtherDerType> > > >
|
||||||
operator*(const AutoDiffScalar<OtherDerType>& other) const
|
operator*(const AutoDiffScalar<OtherDerType>& other) const
|
||||||
{
|
{
|
||||||
|
ei_make_coherent(m_derivatives, other.derivatives());
|
||||||
return AutoDiffScalar<CwiseBinaryOp<ei_scalar_sum_op<Scalar>,
|
return AutoDiffScalar<CwiseBinaryOp<ei_scalar_sum_op<Scalar>,
|
||||||
NestByValue<CwiseUnaryOp<ei_scalar_multiple_op<Scalar>, DerType> >,
|
NestByValue<CwiseUnaryOp<ei_scalar_multiple_op<Scalar>, DerType> >,
|
||||||
NestByValue<CwiseUnaryOp<ei_scalar_multiple_op<Scalar>, OtherDerType> > > >(
|
NestByValue<CwiseUnaryOp<ei_scalar_multiple_op<Scalar>, OtherDerType> > > >(
|
||||||
@ -219,12 +235,57 @@ class AutoDiffScalar
|
|||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template<typename A_Scalar, int A_Rows, int A_Cols, int A_Options, int A_MaxRows, int A_MaxCols, typename B>
|
||||||
|
struct ei_make_coherent_impl<Matrix<A_Scalar, A_Rows, A_Cols, A_Options, A_MaxRows, A_MaxCols>, B> {
|
||||||
|
typedef Matrix<A_Scalar, A_Rows, A_Cols, A_Options, A_MaxRows, A_MaxCols> A;
|
||||||
|
static void run(A& a, B& b) {
|
||||||
|
if((A_Rows==Dynamic || A_Cols==Dynamic) && (a.size()==0))
|
||||||
|
{
|
||||||
|
a.resize(b.size());
|
||||||
|
a.setZero();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename A, typename B_Scalar, int B_Rows, int B_Cols, int B_Options, int B_MaxRows, int B_MaxCols>
|
||||||
|
struct ei_make_coherent_impl<A, Matrix<B_Scalar, B_Rows, B_Cols, B_Options, B_MaxRows, B_MaxCols> > {
|
||||||
|
typedef Matrix<B_Scalar, B_Rows, B_Cols, B_Options, B_MaxRows, B_MaxCols> B;
|
||||||
|
static void run(A& a, B& b) {
|
||||||
|
if((B_Rows==Dynamic || B_Cols==Dynamic) && (b.size()==0))
|
||||||
|
{
|
||||||
|
b.resize(a.size());
|
||||||
|
b.setZero();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename A_Scalar, int A_Rows, int A_Cols, int A_Options, int A_MaxRows, int A_MaxCols,
|
||||||
|
typename B_Scalar, int B_Rows, int B_Cols, int B_Options, int B_MaxRows, int B_MaxCols>
|
||||||
|
struct ei_make_coherent_impl<Matrix<A_Scalar, A_Rows, A_Cols, A_Options, A_MaxRows, A_MaxCols>,
|
||||||
|
Matrix<B_Scalar, B_Rows, B_Cols, B_Options, B_MaxRows, B_MaxCols> > {
|
||||||
|
typedef Matrix<A_Scalar, A_Rows, A_Cols, A_Options, A_MaxRows, A_MaxCols> A;
|
||||||
|
typedef Matrix<B_Scalar, B_Rows, B_Cols, B_Options, B_MaxRows, B_MaxCols> B;
|
||||||
|
static void run(A& a, B& b) {
|
||||||
|
if((A_Rows==Dynamic || A_Cols==Dynamic) && (a.size()==0))
|
||||||
|
{
|
||||||
|
a.resize(b.size());
|
||||||
|
a.setZero();
|
||||||
|
}
|
||||||
|
else if((B_Rows==Dynamic || B_Cols==Dynamic) && (b.size()==0))
|
||||||
|
{
|
||||||
|
b.resize(a.size());
|
||||||
|
b.setZero();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#define EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(FUNC,CODE) \
|
#define EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(FUNC,CODE) \
|
||||||
template<typename DerType> \
|
template<typename DerType> \
|
||||||
inline const AutoDiffScalar<CwiseUnaryOp<ei_scalar_multiple_op<typename ei_traits<DerType>::Scalar>, DerType> > \
|
inline const Eigen::AutoDiffScalar<Eigen::CwiseUnaryOp<Eigen::ei_scalar_multiple_op<typename Eigen::ei_traits<DerType>::Scalar>, DerType> > \
|
||||||
FUNC(const AutoDiffScalar<DerType>& x) { \
|
FUNC(const Eigen::AutoDiffScalar<DerType>& x) { \
|
||||||
|
using namespace Eigen; \
|
||||||
typedef typename ei_traits<DerType>::Scalar Scalar; \
|
typedef typename ei_traits<DerType>::Scalar Scalar; \
|
||||||
typedef AutoDiffScalar<CwiseUnaryOp<ei_scalar_multiple_op<Scalar>, DerType> > ReturnType; \
|
typedef AutoDiffScalar<CwiseUnaryOp<ei_scalar_multiple_op<Scalar>, DerType> > ReturnType; \
|
||||||
CODE; \
|
CODE; \
|
||||||
@ -253,9 +314,10 @@ namespace std
|
|||||||
return ReturnType(std::log(x.value),x.derivatives() * (Scalar(1).x.value()));)
|
return ReturnType(std::log(x.value),x.derivatives() * (Scalar(1).x.value()));)
|
||||||
|
|
||||||
template<typename DerType>
|
template<typename DerType>
|
||||||
inline const AutoDiffScalar<CwiseUnaryOp<ei_scalar_multiple_op<typename ei_traits<DerType>::Scalar>, DerType> >
|
inline const Eigen::AutoDiffScalar<Eigen::CwiseUnaryOp<Eigen::ei_scalar_multiple_op<typename Eigen::ei_traits<DerType>::Scalar>, DerType> >
|
||||||
pow(const AutoDiffScalar<DerType>& x, typename ei_traits<DerType>::Scalar y)
|
pow(const Eigen::AutoDiffScalar<DerType>& x, typename Eigen::ei_traits<DerType>::Scalar y)
|
||||||
{
|
{
|
||||||
|
using namespace Eigen;
|
||||||
typedef typename ei_traits<DerType>::Scalar Scalar;
|
typedef typename ei_traits<DerType>::Scalar Scalar;
|
||||||
return AutoDiffScalar<CwiseUnaryOp<ei_scalar_multiple_op<Scalar>, DerType> >(
|
return AutoDiffScalar<CwiseUnaryOp<ei_scalar_multiple_op<Scalar>, DerType> >(
|
||||||
std::pow(x.value(),y),
|
std::pow(x.value(),y),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user