improve a bit AutoDiffVector, but it still not working

This commit is contained in:
Gael Guennebaud 2009-11-06 11:34:58 +01:00
parent 6647a58847
commit 5dc02fe5e9

View File

@ -48,7 +48,11 @@ template<typename ValueType, typename JacobianType>
class AutoDiffVector
{
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() {}
@ -58,6 +62,22 @@ class AutoDiffVector
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)
: m_values(values), m_jacobian(jac)
{}
@ -72,7 +92,7 @@ class AutoDiffVector
{}
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_jacobian = other.jacobian();
@ -94,20 +114,20 @@ class AutoDiffVector
template<typename OtherValueType,typename OtherJacobianType>
inline const AutoDiffVector<
CwiseBinaryOp<ei_scalar_sum_op<Scalar>,ValueType,OtherValueType> >
CwiseBinaryOp<ei_scalar_sum_op<Scalar>,JacobianType,OtherJacobianType> >
operator+(const AutoDiffScalar<OtherDerType>& other) const
typename MakeCwiseBinaryOp<ei_scalar_sum_op<BaseScalar>,ValueType,OtherValueType>::Type,
typename MakeCwiseBinaryOp<ei_scalar_sum_op<BaseScalar>,JacobianType,OtherJacobianType>::Type >
operator+(const AutoDiffVector<OtherValueType,OtherJacobianType>& other) const
{
return AutoDiffVector<
CwiseBinaryOp<ei_scalar_sum_op<Scalar>,ValueType,OtherValueType> >
CwiseBinaryOp<ei_scalar_sum_op<Scalar>,JacobianType,OtherJacobianType> >(
typename MakeCwiseBinaryOp<ei_scalar_sum_op<BaseScalar>,ValueType,OtherValueType>::Type,
typename MakeCwiseBinaryOp<ei_scalar_sum_op<BaseScalar>,JacobianType,OtherJacobianType>::Type >(
m_values + other.values(),
m_jacobian + other.jacobian());
}
template<typename OtherValueType, typename OtherJacobianType>
inline AutoDiffVector&
operator+=(const AutoDiffVector<OtherValueType,OtherDerType>& other)
operator+=(const AutoDiffVector<OtherValueType,OtherJacobianType>& other)
{
m_values += other.values();
m_jacobian += other.jacobian();
@ -116,20 +136,20 @@ class AutoDiffVector
template<typename OtherValueType,typename OtherJacobianType>
inline const AutoDiffVector<
CwiseBinaryOp<ei_scalar_difference_op<Scalar>,ValueType,OtherValueType> >
CwiseBinaryOp<ei_scalar_difference_op<Scalar>,JacobianType,OtherJacobianType> >
operator-(const AutoDiffScalar<OtherDerType>& other) const
typename MakeCwiseBinaryOp<ei_scalar_difference_op<Scalar>,ValueType,OtherValueType>::Type,
typename MakeCwiseBinaryOp<ei_scalar_difference_op<Scalar>,JacobianType,OtherJacobianType>::Type >
operator-(const AutoDiffVector<OtherValueType,OtherJacobianType>& other) const
{
return AutoDiffVector<
CwiseBinaryOp<ei_scalar_difference_op<Scalar>,ValueType,OtherValueType> >
CwiseBinaryOp<ei_scalar_difference_op<Scalar>,JacobianType,OtherJacobianType> >(
typename MakeCwiseBinaryOp<ei_scalar_difference_op<Scalar>,ValueType,OtherValueType>::Type,
typename MakeCwiseBinaryOp<ei_scalar_difference_op<Scalar>,JacobianType,OtherJacobianType>::Type >(
m_values - other.values(),
m_jacobian - other.jacobian());
}
template<typename OtherValueType, typename OtherJacobianType>
inline AutoDiffVector&
operator-=(const AutoDiffVector<OtherValueType,OtherDerType>& other)
operator-=(const AutoDiffVector<OtherValueType,OtherJacobianType>& other)
{
m_values -= other.values();
m_jacobian -= other.jacobian();
@ -137,37 +157,37 @@ class AutoDiffVector
}
inline const AutoDiffVector<
CwiseUnaryOp<ei_scalar_opposite_op<Scalar>, ValueType>
CwiseUnaryOp<ei_scalar_opposite_op<Scalar>, JacobianType> >
typename MakeCwiseUnaryOp<ei_scalar_opposite_op<Scalar>, ValueType>::Type,
typename MakeCwiseUnaryOp<ei_scalar_opposite_op<Scalar>, JacobianType>::Type >
operator-() const
{
return AutoDiffVector<
CwiseUnaryOp<ei_scalar_opposite_op<Scalar>, ValueType>
CwiseUnaryOp<ei_scalar_opposite_op<Scalar>, JacobianType> >(
typename MakeCwiseUnaryOp<ei_scalar_opposite_op<Scalar>, ValueType>::Type,
typename MakeCwiseUnaryOp<ei_scalar_opposite_op<Scalar>, JacobianType>::Type >(
-m_values,
-m_jacobian);
}
inline const AutoDiffVector<
CwiseUnaryOp<ei_scalar_multiple_op<Scalar>, ValueType>
CwiseUnaryOp<ei_scalar_multiple_op<Scalar>, JacobianType> >
operator*(const Scalar& other) const
typename MakeCwiseUnaryOp<ei_scalar_multiple_op<Scalar>, ValueType>::Type,
typename MakeCwiseUnaryOp<ei_scalar_multiple_op<Scalar>, JacobianType>::Type>
operator*(const BaseScalar& other) const
{
return AutoDiffVector<
CwiseUnaryOp<ei_scalar_multiple_op<Scalar>, ValueType>
CwiseUnaryOp<ei_scalar_multiple_op<Scalar>, JacobianType> >(
typename MakeCwiseUnaryOp<ei_scalar_multiple_op<Scalar>, ValueType>::Type,
typename MakeCwiseUnaryOp<ei_scalar_multiple_op<Scalar>, JacobianType>::Type >(
m_values * other,
(m_jacobian * other));
m_jacobian * other);
}
friend inline const AutoDiffVector<
CwiseUnaryOp<ei_scalar_multiple_op<Scalar>, ValueType>
CwiseUnaryOp<ei_scalar_multiple_op<Scalar>, JacobianType> >
typename MakeCwiseUnaryOp<ei_scalar_multiple_op<Scalar>, ValueType>::Type,
typename MakeCwiseUnaryOp<ei_scalar_multiple_op<Scalar>, JacobianType>::Type >
operator*(const Scalar& other, const AutoDiffVector& v)
{
return AutoDiffVector<
CwiseUnaryOp<ei_scalar_multiple_op<Scalar>, ValueType>
CwiseUnaryOp<ei_scalar_multiple_op<Scalar>, JacobianType> >(
typename MakeCwiseUnaryOp<ei_scalar_multiple_op<Scalar>, ValueType>::Type,
typename MakeCwiseUnaryOp<ei_scalar_multiple_op<Scalar>, JacobianType>::Type >(
v.values() * other,
v.jacobian() * other);
}