From 5dc02fe5e9388f088bb1d12d7c1247c817d10aa0 Mon Sep 17 00:00:00 2001 From: Gael Guennebaud Date: Fri, 6 Nov 2009 11:34:58 +0100 Subject: [PATCH] improve a bit AutoDiffVector, but it still not working --- .../Eigen/src/AutoDiff/AutoDiffVector.h | 128 ++++++++++-------- 1 file changed, 74 insertions(+), 54 deletions(-) diff --git a/unsupported/Eigen/src/AutoDiff/AutoDiffVector.h b/unsupported/Eigen/src/AutoDiff/AutoDiffVector.h index 69ea9144e..03c82b7e8 100644 --- a/unsupported/Eigen/src/AutoDiff/AutoDiffVector.h +++ b/unsupported/Eigen/src/AutoDiff/AutoDiffVector.h @@ -35,7 +35,7 @@ namespace Eigen { * This class represents a scalar value while tracking its respective derivatives. * * 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_conj, ei_real, ei_imag, ei_abs2. * @@ -48,130 +48,150 @@ template class AutoDiffVector { public: - typedef typename ei_traits::Scalar Scalar; - + //typedef typename ei_traits::Scalar Scalar; + typedef typename ei_traits::Scalar BaseScalar; + typedef AutoDiffScalar > ActiveScalar; + typedef ActiveScalar Scalar; + typedef AutoDiffScalar CoeffType; + inline AutoDiffVector() {} - + inline AutoDiffVector(const ValueType& values) : m_values(values) { 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) {} - + template inline AutoDiffVector(const AutoDiffVector& other) : m_values(other.values()), m_jacobian(other.jacobian()) {} - + inline AutoDiffVector(const AutoDiffVector& other) : m_values(other.values()), m_jacobian(other.jacobian()) {} - + template - inline AutoDiffScalar& operator=(const AutoDiffVector& other) + inline AutoDiffVector& operator=(const AutoDiffVector& other) { m_values = other.values(); m_jacobian = other.jacobian(); return *this; } - + inline AutoDiffVector& operator=(const AutoDiffVector& other) { m_values = other.values(); m_jacobian = other.jacobian(); return *this; } - + inline const ValueType& values() const { return m_values; } inline ValueType& values() { return m_values; } - + inline const JacobianType& jacobian() const { return m_jacobian; } inline JacobianType& jacobian() { return m_jacobian; } - + template inline const AutoDiffVector< - CwiseBinaryOp,ValueType,OtherValueType> > - CwiseBinaryOp,JacobianType,OtherJacobianType> > - operator+(const AutoDiffScalar& other) const + typename MakeCwiseBinaryOp,ValueType,OtherValueType>::Type, + typename MakeCwiseBinaryOp,JacobianType,OtherJacobianType>::Type > + operator+(const AutoDiffVector& other) const { return AutoDiffVector< - CwiseBinaryOp,ValueType,OtherValueType> > - CwiseBinaryOp,JacobianType,OtherJacobianType> >( + typename MakeCwiseBinaryOp,ValueType,OtherValueType>::Type, + typename MakeCwiseBinaryOp,JacobianType,OtherJacobianType>::Type >( m_values + other.values(), m_jacobian + other.jacobian()); } - + template inline AutoDiffVector& - operator+=(const AutoDiffVector& other) + operator+=(const AutoDiffVector& other) { m_values += other.values(); m_jacobian += other.jacobian(); return *this; } - + template inline const AutoDiffVector< - CwiseBinaryOp,ValueType,OtherValueType> > - CwiseBinaryOp,JacobianType,OtherJacobianType> > - operator-(const AutoDiffScalar& other) const + typename MakeCwiseBinaryOp,ValueType,OtherValueType>::Type, + typename MakeCwiseBinaryOp,JacobianType,OtherJacobianType>::Type > + operator-(const AutoDiffVector& other) const { return AutoDiffVector< - CwiseBinaryOp,ValueType,OtherValueType> > - CwiseBinaryOp,JacobianType,OtherJacobianType> >( - m_values - other.values(), - m_jacobian - other.jacobian()); + typename MakeCwiseBinaryOp,ValueType,OtherValueType>::Type, + typename MakeCwiseBinaryOp,JacobianType,OtherJacobianType>::Type >( + m_values - other.values(), + m_jacobian - other.jacobian()); } - + template inline AutoDiffVector& - operator-=(const AutoDiffVector& other) + operator-=(const AutoDiffVector& other) { m_values -= other.values(); m_jacobian -= other.jacobian(); return *this; } - + inline const AutoDiffVector< - CwiseUnaryOp, ValueType> - CwiseUnaryOp, JacobianType> > + typename MakeCwiseUnaryOp, ValueType>::Type, + typename MakeCwiseUnaryOp, JacobianType>::Type > operator-() const { return AutoDiffVector< - CwiseUnaryOp, ValueType> - CwiseUnaryOp, JacobianType> >( - -m_values, - -m_jacobian); + typename MakeCwiseUnaryOp, ValueType>::Type, + typename MakeCwiseUnaryOp, JacobianType>::Type >( + -m_values, + -m_jacobian); } - + inline const AutoDiffVector< - CwiseUnaryOp, ValueType> - CwiseUnaryOp, JacobianType> > - operator*(const Scalar& other) const + typename MakeCwiseUnaryOp, ValueType>::Type, + typename MakeCwiseUnaryOp, JacobianType>::Type> + operator*(const BaseScalar& other) const { return AutoDiffVector< - CwiseUnaryOp, ValueType> - CwiseUnaryOp, JacobianType> >( + typename MakeCwiseUnaryOp, ValueType>::Type, + typename MakeCwiseUnaryOp, JacobianType>::Type >( m_values * other, - (m_jacobian * other)); + m_jacobian * other); } - + friend inline const AutoDiffVector< - CwiseUnaryOp, ValueType> - CwiseUnaryOp, JacobianType> > + typename MakeCwiseUnaryOp, ValueType>::Type, + typename MakeCwiseUnaryOp, JacobianType>::Type > operator*(const Scalar& other, const AutoDiffVector& v) { return AutoDiffVector< - CwiseUnaryOp, ValueType> - CwiseUnaryOp, JacobianType> >( + typename MakeCwiseUnaryOp, ValueType>::Type, + typename MakeCwiseUnaryOp, JacobianType>::Type >( v.values() * other, v.jacobian() * other); } - + // template // inline const AutoDiffVector< // CwiseBinaryOp, ValueType, OtherValueType> @@ -188,25 +208,25 @@ class AutoDiffVector // m_values.cwise() * other.values(), // (m_jacobian * other.values()).nestByValue() + (m_values * other.jacobian()).nestByValue()); // } - + inline AutoDiffVector& operator*=(const Scalar& other) { m_values *= other; m_jacobian *= other; return *this; } - + template inline AutoDiffVector& operator*=(const AutoDiffVector& other) { *this = *this * other; return *this; } - + protected: ValueType m_values; JacobianType m_jacobian; - + }; }