diff options
-rw-r--r-- | Eigen/src/Core/Coeffs.h | 33 | ||||
-rw-r--r-- | Eigen/src/Core/MatrixBase.h | 2 | ||||
-rw-r--r-- | test/basicstuff.cpp | 13 |
3 files changed, 48 insertions, 0 deletions
diff --git a/Eigen/src/Core/Coeffs.h b/Eigen/src/Core/Coeffs.h index 8836998be..e9aa4de80 100644 --- a/Eigen/src/Core/Coeffs.h +++ b/Eigen/src/Core/Coeffs.h @@ -134,6 +134,23 @@ inline const typename ei_traits<Derived>::Scalar MatrixBase<Derived> return derived().coeff(index); } +/** \returns the coefficient at given index. + * + * This is synonymous to operator[](int) const. + * + * This method is allowed only for vector expressions, and for matrix expressions having the LinearAccessBit. + * + * \sa operator[](int), operator()(int,int) const, x() const, y() const, + * z() const, w() const + */ +template<typename Derived> +inline const typename ei_traits<Derived>::Scalar MatrixBase<Derived> + ::operator()(int index) const +{ + ei_assert(index >= 0 && index < size()); + return derived().coeff(index); +} + /** Short version: don't use this function, use * \link operator[](int) \endlink instead. * @@ -170,6 +187,22 @@ inline typename ei_traits<Derived>::Scalar& MatrixBase<Derived> return derived().coeffRef(index); } +/** \returns a reference to the coefficient at given index. + * + * This is synonymous to operator[](int). + * + * This method is allowed only for vector expressions, and for matrix expressions having the LinearAccessBit. + * + * \sa operator[](int) const, operator()(int,int), x(), y(), z(), w() + */ +template<typename Derived> +inline typename ei_traits<Derived>::Scalar& MatrixBase<Derived> + ::operator()(int index) +{ + ei_assert(index >= 0 && index < size()); + return derived().coeffRef(index); +} + /** equivalent to operator[](0). */ template<typename Derived> inline const typename ei_traits<Derived>::Scalar MatrixBase<Derived> diff --git a/Eigen/src/Core/MatrixBase.h b/Eigen/src/Core/MatrixBase.h index b50d2cdd5..b6bb83d13 100644 --- a/Eigen/src/Core/MatrixBase.h +++ b/Eigen/src/Core/MatrixBase.h @@ -249,9 +249,11 @@ template<typename Derived> class MatrixBase const Scalar coeff(int index) const; const Scalar operator[](int index) const; + const Scalar operator()(int index) const; Scalar& coeffRef(int index); Scalar& operator[](int index); + Scalar& operator()(int index); template<typename OtherDerived> void copyCoeff(int row, int col, const MatrixBase<OtherDerived>& other); diff --git a/test/basicstuff.cpp b/test/basicstuff.cpp index 9da5167e6..b48ebbe8e 100644 --- a/test/basicstuff.cpp +++ b/test/basicstuff.cpp @@ -46,9 +46,22 @@ template<typename MatrixType> void basicStuff(const MatrixType& m) v2 = VectorType::Random(rows), vzero = VectorType::Zero(rows); + Scalar x = ei_random<Scalar>(); + int r = ei_random<int>(0, rows-1), c = ei_random<int>(0, cols-1); + m1.coeffRef(r,c) = x; + VERIFY_IS_APPROX(x, m1.coeff(r,c)); + m1(r,c) = x; + VERIFY_IS_APPROX(x, m1(r,c)); + v1.coeffRef(r) = x; + VERIFY_IS_APPROX(x, v1.coeff(r)); + v1(r) = x; + VERIFY_IS_APPROX(x, v1(r)); + v1[r] = x; + VERIFY_IS_APPROX(x, v1[r]); + VERIFY_IS_APPROX( v1, v1); VERIFY_IS_NOT_APPROX( v1, 2*v1); VERIFY_IS_MUCH_SMALLER_THAN( vzero, v1); |