mirror of
https://gitlab.com/libeigen/eigen.git
synced 2025-07-26 14:54:30 +08:00
improve a bit AutoDiffVector, but it still not working
This commit is contained in:
parent
6647a58847
commit
5dc02fe5e9
@ -35,7 +35,7 @@ namespace Eigen {
|
|||||||
* This class represents a scalar value while tracking its respective derivatives.
|
* This class represents a scalar value while tracking its respective derivatives.
|
||||||
*
|
*
|
||||||
* It supports the following list of global math function:
|
* It supports the following list of global math function:
|
||||||
* - std::abs, std::sqrt, std::pow, std::exp, std::log, std::sin, std::cos,
|
* - std::abs, std::sqrt, std::pow, std::exp, std::log, std::sin, std::cos,
|
||||||
* - ei_abs, ei_sqrt, ei_pow, ei_exp, ei_log, ei_sin, ei_cos,
|
* - ei_abs, ei_sqrt, ei_pow, ei_exp, ei_log, ei_sin, ei_cos,
|
||||||
* - ei_conj, ei_real, ei_imag, ei_abs2.
|
* - ei_conj, ei_real, ei_imag, ei_abs2.
|
||||||
*
|
*
|
||||||
@ -48,130 +48,150 @@ template<typename ValueType, typename JacobianType>
|
|||||||
class AutoDiffVector
|
class AutoDiffVector
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
typedef typename ei_traits<ValueType>::Scalar Scalar;
|
//typedef typename ei_traits<ValueType>::Scalar Scalar;
|
||||||
|
typedef typename ei_traits<ValueType>::Scalar BaseScalar;
|
||||||
|
typedef AutoDiffScalar<Matrix<BaseScalar,JacobianType::RowsAtCompileTime,1> > ActiveScalar;
|
||||||
|
typedef ActiveScalar Scalar;
|
||||||
|
typedef AutoDiffScalar<typename JacobianType::ColXpr> CoeffType;
|
||||||
|
|
||||||
inline AutoDiffVector() {}
|
inline AutoDiffVector() {}
|
||||||
|
|
||||||
inline AutoDiffVector(const ValueType& values)
|
inline AutoDiffVector(const ValueType& values)
|
||||||
: m_values(values)
|
: m_values(values)
|
||||||
{
|
{
|
||||||
m_jacobian.setZero();
|
m_jacobian.setZero();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
CoeffType operator[] (int i) { return CoeffType(m_values[i], m_jacobian.col(i)); }
|
||||||
|
const CoeffType operator[] (int i) const { return CoeffType(m_values[i], m_jacobian.col(i)); }
|
||||||
|
|
||||||
|
CoeffType operator() (int i) { return CoeffType(m_values[i], m_jacobian.col(i)); }
|
||||||
|
const CoeffType operator() (int i) const { return CoeffType(m_values[i], m_jacobian.col(i)); }
|
||||||
|
|
||||||
|
CoeffType coeffRef(int i) { return CoeffType(m_values[i], m_jacobian.col(i)); }
|
||||||
|
const CoeffType coeffRef(int i) const { return CoeffType(m_values[i], m_jacobian.col(i)); }
|
||||||
|
|
||||||
|
int size() const { return m_values.size(); }
|
||||||
|
|
||||||
|
// FIXME here we could return an expression of the sum
|
||||||
|
Scalar sum() const { /*std::cerr << "sum \n\n";*/ /*std::cerr << m_jacobian.rowwise().sum() << "\n\n";*/ return Scalar(m_values.sum(), m_jacobian.rowwise().sum()); }
|
||||||
|
|
||||||
|
|
||||||
inline AutoDiffVector(const ValueType& values, const JacobianType& jac)
|
inline AutoDiffVector(const ValueType& values, const JacobianType& jac)
|
||||||
: m_values(values), m_jacobian(jac)
|
: m_values(values), m_jacobian(jac)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
template<typename OtherValueType, typename OtherJacobianType>
|
template<typename OtherValueType, typename OtherJacobianType>
|
||||||
inline AutoDiffVector(const AutoDiffVector<OtherValueType, OtherJacobianType>& other)
|
inline AutoDiffVector(const AutoDiffVector<OtherValueType, OtherJacobianType>& other)
|
||||||
: m_values(other.values()), m_jacobian(other.jacobian())
|
: m_values(other.values()), m_jacobian(other.jacobian())
|
||||||
{}
|
{}
|
||||||
|
|
||||||
inline AutoDiffVector(const AutoDiffVector& other)
|
inline AutoDiffVector(const AutoDiffVector& other)
|
||||||
: m_values(other.values()), m_jacobian(other.jacobian())
|
: m_values(other.values()), m_jacobian(other.jacobian())
|
||||||
{}
|
{}
|
||||||
|
|
||||||
template<typename OtherValueType, typename OtherJacobianType>
|
template<typename OtherValueType, typename OtherJacobianType>
|
||||||
inline AutoDiffScalar& operator=(const AutoDiffVector<OtherValueType, OtherJacobianType>& other)
|
inline AutoDiffVector& operator=(const AutoDiffVector<OtherValueType, OtherJacobianType>& other)
|
||||||
{
|
{
|
||||||
m_values = other.values();
|
m_values = other.values();
|
||||||
m_jacobian = other.jacobian();
|
m_jacobian = other.jacobian();
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline AutoDiffVector& operator=(const AutoDiffVector& other)
|
inline AutoDiffVector& operator=(const AutoDiffVector& other)
|
||||||
{
|
{
|
||||||
m_values = other.values();
|
m_values = other.values();
|
||||||
m_jacobian = other.jacobian();
|
m_jacobian = other.jacobian();
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline const ValueType& values() const { return m_values; }
|
inline const ValueType& values() const { return m_values; }
|
||||||
inline ValueType& values() { return m_values; }
|
inline ValueType& values() { return m_values; }
|
||||||
|
|
||||||
inline const JacobianType& jacobian() const { return m_jacobian; }
|
inline const JacobianType& jacobian() const { return m_jacobian; }
|
||||||
inline JacobianType& jacobian() { return m_jacobian; }
|
inline JacobianType& jacobian() { return m_jacobian; }
|
||||||
|
|
||||||
template<typename OtherValueType,typename OtherJacobianType>
|
template<typename OtherValueType,typename OtherJacobianType>
|
||||||
inline const AutoDiffVector<
|
inline const AutoDiffVector<
|
||||||
CwiseBinaryOp<ei_scalar_sum_op<Scalar>,ValueType,OtherValueType> >
|
typename MakeCwiseBinaryOp<ei_scalar_sum_op<BaseScalar>,ValueType,OtherValueType>::Type,
|
||||||
CwiseBinaryOp<ei_scalar_sum_op<Scalar>,JacobianType,OtherJacobianType> >
|
typename MakeCwiseBinaryOp<ei_scalar_sum_op<BaseScalar>,JacobianType,OtherJacobianType>::Type >
|
||||||
operator+(const AutoDiffScalar<OtherDerType>& other) const
|
operator+(const AutoDiffVector<OtherValueType,OtherJacobianType>& other) const
|
||||||
{
|
{
|
||||||
return AutoDiffVector<
|
return AutoDiffVector<
|
||||||
CwiseBinaryOp<ei_scalar_sum_op<Scalar>,ValueType,OtherValueType> >
|
typename MakeCwiseBinaryOp<ei_scalar_sum_op<BaseScalar>,ValueType,OtherValueType>::Type,
|
||||||
CwiseBinaryOp<ei_scalar_sum_op<Scalar>,JacobianType,OtherJacobianType> >(
|
typename MakeCwiseBinaryOp<ei_scalar_sum_op<BaseScalar>,JacobianType,OtherJacobianType>::Type >(
|
||||||
m_values + other.values(),
|
m_values + other.values(),
|
||||||
m_jacobian + other.jacobian());
|
m_jacobian + other.jacobian());
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename OtherValueType, typename OtherJacobianType>
|
template<typename OtherValueType, typename OtherJacobianType>
|
||||||
inline AutoDiffVector&
|
inline AutoDiffVector&
|
||||||
operator+=(const AutoDiffVector<OtherValueType,OtherDerType>& other)
|
operator+=(const AutoDiffVector<OtherValueType,OtherJacobianType>& other)
|
||||||
{
|
{
|
||||||
m_values += other.values();
|
m_values += other.values();
|
||||||
m_jacobian += other.jacobian();
|
m_jacobian += other.jacobian();
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename OtherValueType,typename OtherJacobianType>
|
template<typename OtherValueType,typename OtherJacobianType>
|
||||||
inline const AutoDiffVector<
|
inline const AutoDiffVector<
|
||||||
CwiseBinaryOp<ei_scalar_difference_op<Scalar>,ValueType,OtherValueType> >
|
typename MakeCwiseBinaryOp<ei_scalar_difference_op<Scalar>,ValueType,OtherValueType>::Type,
|
||||||
CwiseBinaryOp<ei_scalar_difference_op<Scalar>,JacobianType,OtherJacobianType> >
|
typename MakeCwiseBinaryOp<ei_scalar_difference_op<Scalar>,JacobianType,OtherJacobianType>::Type >
|
||||||
operator-(const AutoDiffScalar<OtherDerType>& other) const
|
operator-(const AutoDiffVector<OtherValueType,OtherJacobianType>& other) const
|
||||||
{
|
{
|
||||||
return AutoDiffVector<
|
return AutoDiffVector<
|
||||||
CwiseBinaryOp<ei_scalar_difference_op<Scalar>,ValueType,OtherValueType> >
|
typename MakeCwiseBinaryOp<ei_scalar_difference_op<Scalar>,ValueType,OtherValueType>::Type,
|
||||||
CwiseBinaryOp<ei_scalar_difference_op<Scalar>,JacobianType,OtherJacobianType> >(
|
typename MakeCwiseBinaryOp<ei_scalar_difference_op<Scalar>,JacobianType,OtherJacobianType>::Type >(
|
||||||
m_values - other.values(),
|
m_values - other.values(),
|
||||||
m_jacobian - other.jacobian());
|
m_jacobian - other.jacobian());
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename OtherValueType, typename OtherJacobianType>
|
template<typename OtherValueType, typename OtherJacobianType>
|
||||||
inline AutoDiffVector&
|
inline AutoDiffVector&
|
||||||
operator-=(const AutoDiffVector<OtherValueType,OtherDerType>& other)
|
operator-=(const AutoDiffVector<OtherValueType,OtherJacobianType>& other)
|
||||||
{
|
{
|
||||||
m_values -= other.values();
|
m_values -= other.values();
|
||||||
m_jacobian -= other.jacobian();
|
m_jacobian -= other.jacobian();
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline const AutoDiffVector<
|
inline const AutoDiffVector<
|
||||||
CwiseUnaryOp<ei_scalar_opposite_op<Scalar>, ValueType>
|
typename MakeCwiseUnaryOp<ei_scalar_opposite_op<Scalar>, ValueType>::Type,
|
||||||
CwiseUnaryOp<ei_scalar_opposite_op<Scalar>, JacobianType> >
|
typename MakeCwiseUnaryOp<ei_scalar_opposite_op<Scalar>, JacobianType>::Type >
|
||||||
operator-() const
|
operator-() const
|
||||||
{
|
{
|
||||||
return AutoDiffVector<
|
return AutoDiffVector<
|
||||||
CwiseUnaryOp<ei_scalar_opposite_op<Scalar>, ValueType>
|
typename MakeCwiseUnaryOp<ei_scalar_opposite_op<Scalar>, ValueType>::Type,
|
||||||
CwiseUnaryOp<ei_scalar_opposite_op<Scalar>, JacobianType> >(
|
typename MakeCwiseUnaryOp<ei_scalar_opposite_op<Scalar>, JacobianType>::Type >(
|
||||||
-m_values,
|
-m_values,
|
||||||
-m_jacobian);
|
-m_jacobian);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline const AutoDiffVector<
|
inline const AutoDiffVector<
|
||||||
CwiseUnaryOp<ei_scalar_multiple_op<Scalar>, ValueType>
|
typename MakeCwiseUnaryOp<ei_scalar_multiple_op<Scalar>, ValueType>::Type,
|
||||||
CwiseUnaryOp<ei_scalar_multiple_op<Scalar>, JacobianType> >
|
typename MakeCwiseUnaryOp<ei_scalar_multiple_op<Scalar>, JacobianType>::Type>
|
||||||
operator*(const Scalar& other) const
|
operator*(const BaseScalar& other) const
|
||||||
{
|
{
|
||||||
return AutoDiffVector<
|
return AutoDiffVector<
|
||||||
CwiseUnaryOp<ei_scalar_multiple_op<Scalar>, ValueType>
|
typename MakeCwiseUnaryOp<ei_scalar_multiple_op<Scalar>, ValueType>::Type,
|
||||||
CwiseUnaryOp<ei_scalar_multiple_op<Scalar>, JacobianType> >(
|
typename MakeCwiseUnaryOp<ei_scalar_multiple_op<Scalar>, JacobianType>::Type >(
|
||||||
m_values * other,
|
m_values * other,
|
||||||
(m_jacobian * other));
|
m_jacobian * other);
|
||||||
}
|
}
|
||||||
|
|
||||||
friend inline const AutoDiffVector<
|
friend inline const AutoDiffVector<
|
||||||
CwiseUnaryOp<ei_scalar_multiple_op<Scalar>, ValueType>
|
typename MakeCwiseUnaryOp<ei_scalar_multiple_op<Scalar>, ValueType>::Type,
|
||||||
CwiseUnaryOp<ei_scalar_multiple_op<Scalar>, JacobianType> >
|
typename MakeCwiseUnaryOp<ei_scalar_multiple_op<Scalar>, JacobianType>::Type >
|
||||||
operator*(const Scalar& other, const AutoDiffVector& v)
|
operator*(const Scalar& other, const AutoDiffVector& v)
|
||||||
{
|
{
|
||||||
return AutoDiffVector<
|
return AutoDiffVector<
|
||||||
CwiseUnaryOp<ei_scalar_multiple_op<Scalar>, ValueType>
|
typename MakeCwiseUnaryOp<ei_scalar_multiple_op<Scalar>, ValueType>::Type,
|
||||||
CwiseUnaryOp<ei_scalar_multiple_op<Scalar>, JacobianType> >(
|
typename MakeCwiseUnaryOp<ei_scalar_multiple_op<Scalar>, JacobianType>::Type >(
|
||||||
v.values() * other,
|
v.values() * other,
|
||||||
v.jacobian() * other);
|
v.jacobian() * other);
|
||||||
}
|
}
|
||||||
|
|
||||||
// template<typename OtherValueType,typename OtherJacobianType>
|
// template<typename OtherValueType,typename OtherJacobianType>
|
||||||
// inline const AutoDiffVector<
|
// inline const AutoDiffVector<
|
||||||
// CwiseBinaryOp<ei_scalar_multiple_op<Scalar>, ValueType, OtherValueType>
|
// CwiseBinaryOp<ei_scalar_multiple_op<Scalar>, ValueType, OtherValueType>
|
||||||
@ -188,25 +208,25 @@ class AutoDiffVector
|
|||||||
// m_values.cwise() * other.values(),
|
// m_values.cwise() * other.values(),
|
||||||
// (m_jacobian * other.values()).nestByValue() + (m_values * other.jacobian()).nestByValue());
|
// (m_jacobian * other.values()).nestByValue() + (m_values * other.jacobian()).nestByValue());
|
||||||
// }
|
// }
|
||||||
|
|
||||||
inline AutoDiffVector& operator*=(const Scalar& other)
|
inline AutoDiffVector& operator*=(const Scalar& other)
|
||||||
{
|
{
|
||||||
m_values *= other;
|
m_values *= other;
|
||||||
m_jacobian *= other;
|
m_jacobian *= other;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename OtherValueType,typename OtherJacobianType>
|
template<typename OtherValueType,typename OtherJacobianType>
|
||||||
inline AutoDiffVector& operator*=(const AutoDiffVector<OtherValueType,OtherJacobianType>& other)
|
inline AutoDiffVector& operator*=(const AutoDiffVector<OtherValueType,OtherJacobianType>& other)
|
||||||
{
|
{
|
||||||
*this = *this * other;
|
*this = *this * other;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
ValueType m_values;
|
ValueType m_values;
|
||||||
JacobianType m_jacobian;
|
JacobianType m_jacobian;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user