*add operator()(int) for vectors, synonymous to operator[](int).

I don't see any reason not to allow it, it doesn't add much code, and
 it makes porting from eigen1 easier.
*expand tests/basicstuff to first test coefficient access methods
This commit is contained in:
Benoit Jacob 2008-08-18 12:33:14 +00:00
parent 95a1283bf6
commit baf0cffedd
3 changed files with 48 additions and 0 deletions

View File

@ -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>

View File

@ -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);

View File

@ -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);