diff options
author | Benoit Jacob <jacob.benoit.1@gmail.com> | 2008-03-14 10:38:37 +0000 |
---|---|---|
committer | Benoit Jacob <jacob.benoit.1@gmail.com> | 2008-03-14 10:38:37 +0000 |
commit | fb3438e609be743c066af9167e6ed83cd17af394 (patch) | |
tree | 659a868f1c501ab4ddaf9b681ea8cb8d1ed4bdb0 | |
parent | fe569b060c367d34c83624061fff407738609402 (diff) |
- expand MathFunctions.h to provide more functions, like exp, log...
- add cwiseExp(), cwiseLog()...
--> for example, doing a gamma-correction on a bitmap image stored as
an array of floats is a simple matter of:
Eigen::Map<VectorXf> m = VectorXf::map(bitmap,size);
m = m.cwisePow(gamma);
- apidoc improvements, reorganization of the \name's
- remove obsolete examples
- remove EIGEN_ALWAYS_INLINE on lazyProduct(), it seems useless.
-rw-r--r-- | Eigen/src/Core/CwiseBinaryOp.h | 24 | ||||
-rw-r--r-- | Eigen/src/Core/CwiseUnaryOp.h | 180 | ||||
-rw-r--r-- | Eigen/src/Core/ForwardDeclarations.h | 7 | ||||
-rw-r--r-- | Eigen/src/Core/MathFunctions.h | 60 | ||||
-rw-r--r-- | Eigen/src/Core/MatrixBase.h | 410 | ||||
-rw-r--r-- | Eigen/src/Core/Product.h | 16 | ||||
-rw-r--r-- | doc/Doxyfile.in | 8 | ||||
-rw-r--r-- | doc/examples/class_Block.cpp | 4 | ||||
-rw-r--r-- | doc/examples/class_Cast.cpp | 27 | ||||
-rw-r--r-- | doc/examples/class_Column.cpp | 26 | ||||
-rw-r--r-- | doc/examples/class_FixedBlock.cpp | 4 | ||||
-rw-r--r-- | doc/examples/class_Row.cpp | 26 |
12 files changed, 435 insertions, 357 deletions
diff --git a/Eigen/src/Core/CwiseBinaryOp.h b/Eigen/src/Core/CwiseBinaryOp.h index 76e4f0659..131e98362 100644 --- a/Eigen/src/Core/CwiseBinaryOp.h +++ b/Eigen/src/Core/CwiseBinaryOp.h @@ -129,17 +129,16 @@ struct ei_scalar_quotient_op EIGEN_EMPTY_STRUCT { template<typename Scalar> Scalar operator() (const Scalar& a, const Scalar& b) const { return a / b; } }; -/** \relates MatrixBase - * - * \returns an expression of the difference of \a mat1 and \a mat2 +/**\returns an expression of the difference of \c *this and \a other * * \sa class CwiseBinaryOp, MatrixBase::operator-=() */ -template<typename Derived1, typename Derived2> -const CwiseBinaryOp<ei_scalar_difference_op, Derived1, Derived2> -operator-(const MatrixBase<Derived1> &mat1, const MatrixBase<Derived2> &mat2) +template<typename Derived> +template<typename OtherDerived> +const CwiseBinaryOp<ei_scalar_difference_op, Derived, OtherDerived> +MatrixBase<Derived>::operator-(const MatrixBase<OtherDerived> &other) const { - return CwiseBinaryOp<ei_scalar_difference_op, Derived1, Derived2>(mat1.derived(), mat2.derived()); + return CwiseBinaryOp<ei_scalar_difference_op, Derived, OtherDerived>(derived(), other.derived()); } /** replaces \c *this by \c *this - \a other. @@ -156,15 +155,16 @@ MatrixBase<Derived>::operator-=(const MatrixBase<OtherDerived> &other) /** \relates MatrixBase * - * \returns an expression of the sum of \a mat1 and \a mat2 + * \returns an expression of the sum of \c *this and \a other * * \sa class CwiseBinaryOp, MatrixBase::operator+=() */ -template<typename Derived1, typename Derived2> -const CwiseBinaryOp<ei_scalar_sum_op, Derived1, Derived2> -operator+(const MatrixBase<Derived1> &mat1, const MatrixBase<Derived2> &mat2) +template<typename Derived> +template<typename OtherDerived> +const CwiseBinaryOp<ei_scalar_sum_op, Derived, OtherDerived> +MatrixBase<Derived>::operator+(const MatrixBase<OtherDerived> &other) const { - return CwiseBinaryOp<ei_scalar_sum_op, Derived1, Derived2>(mat1.derived(), mat2.derived()); + return CwiseBinaryOp<ei_scalar_sum_op, Derived, OtherDerived>(derived(), other.derived()); } /** replaces \c *this by \c *this + \a other. diff --git a/Eigen/src/Core/CwiseUnaryOp.h b/Eigen/src/Core/CwiseUnaryOp.h index 2943f5826..6958ca248 100644 --- a/Eigen/src/Core/CwiseUnaryOp.h +++ b/Eigen/src/Core/CwiseUnaryOp.h @@ -79,6 +79,24 @@ class CwiseUnaryOp : ei_no_assignment_operator, const UnaryOp m_functor; }; +/** \returns an expression of a custom coefficient-wise unary operator \a func of *this + * + * The template parameter \a CustomUnaryOp is the type of the functor + * of the custom unary operator. + * + * Here is an example: + * \include class_CwiseUnaryOp.cpp + * + * \sa class CwiseUnaryOp, class CwiseBinarOp, MatrixBase::operator-, MatrixBase::cwiseAbs + */ +template<typename Derived> +template<typename CustomUnaryOp> +const CwiseUnaryOp<CustomUnaryOp, Derived> +MatrixBase<Derived>::cwise(const CustomUnaryOp& func) const +{ + return CwiseUnaryOp<CustomUnaryOp, Derived>(derived(), func); +} + /** \internal * \brief Template functor to compute the opposite of a scalar * @@ -88,16 +106,6 @@ struct ei_scalar_opposite_op EIGEN_EMPTY_STRUCT { template<typename Scalar> Scalar operator() (const Scalar& a) const { return -a; } }; -/** \internal - * \brief Template functor to compute the absolute value of a scalar - * - * \sa class CwiseUnaryOp, MatrixBase::cwiseAbs - */ -struct ei_scalar_abs_op EIGEN_EMPTY_STRUCT { - template<typename Scalar> Scalar operator() (const Scalar& a) const { return ei_abs(a); } -}; - - /** \returns an expression of the opposite of \c *this */ template<typename Derived> @@ -107,7 +115,16 @@ MatrixBase<Derived>::operator-() const return CwiseUnaryOp<ei_scalar_opposite_op, Derived>(derived()); } -/** \returns an expression of the opposite of \c *this +/** \internal + * \brief Template functor to compute the absolute value of a scalar + * + * \sa class CwiseUnaryOp, MatrixBase::cwiseAbs + */ +struct ei_scalar_abs_op EIGEN_EMPTY_STRUCT { + template<typename Scalar> Scalar operator() (const Scalar& a) const { return ei_abs(a); } +}; + +/** \returns an expression of the coefficient-wise absolute value of \c *this */ template<typename Derived> const CwiseUnaryOp<ei_scalar_abs_op,Derived> @@ -116,22 +133,22 @@ MatrixBase<Derived>::cwiseAbs() const return CwiseUnaryOp<ei_scalar_abs_op,Derived>(derived()); } -/** \returns an expression of a custom coefficient-wise unary operator \a func of *this - * - * The template parameter \a CustomUnaryOp is the type of the functor - * of the custom unary operator. - * - * Here is an example: - * \include class_CwiseUnaryOp.cpp +/** \internal + * \brief Template functor to compute the squared absolute value of a scalar * - * \sa class CwiseUnaryOp, class CwiseBinarOp, MatrixBase::operator-, MatrixBase::cwiseAbs + * \sa class CwiseUnaryOp, MatrixBase::cwiseAbs2 + */ +struct ei_scalar_abs2_op EIGEN_EMPTY_STRUCT { + template<typename Scalar> Scalar operator() (const Scalar& a) const { return ei_abs2(a); } +}; + +/** \returns an expression of the coefficient-wise squared absolute value of \c *this */ template<typename Derived> -template<typename CustomUnaryOp> -const CwiseUnaryOp<CustomUnaryOp, Derived> -MatrixBase<Derived>::cwise(const CustomUnaryOp& func) const +const CwiseUnaryOp<ei_scalar_abs2_op,Derived> +MatrixBase<Derived>::cwiseAbs2() const { - return CwiseUnaryOp<CustomUnaryOp, Derived>(derived(), func); + return CwiseUnaryOp<ei_scalar_abs2_op,Derived>(derived()); } /** \internal @@ -169,10 +186,7 @@ struct ei_scalar_cast_op EIGEN_EMPTY_STRUCT { * * The template parameter \a NewScalar is the type we are casting the scalars to. * - * Example: \include MatrixBase_cast.cpp - * Output: \verbinclude MatrixBase_cast.out - * - * \sa class CwiseUnaryOp, class ei_scalar_cast_op + * \sa class CwiseUnaryOp */ template<typename Derived> template<typename NewType> @@ -194,7 +208,7 @@ struct ei_scalar_multiple_op { const Scalar m_other; }; -/** \relates MatrixBase \sa class ei_scalar_multiple_op */ +/** \relates MatrixBase */ template<typename Derived> const CwiseUnaryOp<ei_scalar_multiple_op<typename ei_traits<Derived>::Scalar>, Derived> MatrixBase<Derived>::operator*(const Scalar& scalar) const @@ -203,7 +217,7 @@ MatrixBase<Derived>::operator*(const Scalar& scalar) const (derived(), ei_scalar_multiple_op<Scalar>(scalar)); } -/** \relates MatrixBase \sa class ei_scalar_multiple_op */ +/** \relates MatrixBase */ template<typename Derived> const CwiseUnaryOp<ei_scalar_multiple_op<typename ei_traits<Derived>::Scalar>, Derived> MatrixBase<Derived>::operator/(const Scalar& scalar) const @@ -213,7 +227,6 @@ MatrixBase<Derived>::operator/(const Scalar& scalar) const (derived(), ei_scalar_multiple_op<Scalar>(static_cast<Scalar>(1) / scalar)); } -/** \sa ei_scalar_multiple_op */ template<typename Derived> Derived& MatrixBase<Derived>::operator*=(const Scalar& other) @@ -221,7 +234,6 @@ MatrixBase<Derived>::operator*=(const Scalar& other) return *this = *this * other; } -/** \sa ei_scalar_multiple_op */ template<typename Derived> Derived& MatrixBase<Derived>::operator/=(const Scalar& other) @@ -229,4 +241,110 @@ MatrixBase<Derived>::operator/=(const Scalar& other) return *this = *this / other; } +/** \internal + * \brief Template functor to compute the square root of a scalar + * + * \sa class CwiseUnaryOp, MatrixBase::cwiseSqrt() + */ +struct ei_scalar_sqrt_op EIGEN_EMPTY_STRUCT { + template<typename Scalar> Scalar operator() (const Scalar& a) const { return ei_sqrt(a); } +}; + +/** \returns an expression of the coefficient-wise square root of *this. */ +template<typename Derived> +const CwiseUnaryOp<ei_scalar_sqrt_op, Derived> +MatrixBase<Derived>::cwiseSqrt() const +{ + return CwiseUnaryOp<ei_scalar_sqrt_op, Derived>(derived()); +} + +/** \internal + * \brief Template functor to compute the exponential of a scalar + * + * \sa class CwiseUnaryOp, MatrixBase::cwiseExp() + */ +struct ei_scalar_exp_op EIGEN_EMPTY_STRUCT { + template<typename Scalar> Scalar operator() (const Scalar& a) const { return ei_exp(a); } +}; + +/** \returns an expression of the coefficient-wise exponential of *this. */ +template<typename Derived> +const CwiseUnaryOp<ei_scalar_exp_op, Derived> +MatrixBase<Derived>::cwiseExp() const +{ + return CwiseUnaryOp<ei_scalar_exp_op, Derived>(derived()); +} + +/** \internal + * \brief Template functor to compute the logarithm of a scalar + * + * \sa class CwiseUnaryOp, MatrixBase::cwiseLog() + */ +struct ei_scalar_log_op EIGEN_EMPTY_STRUCT { + template<typename Scalar> Scalar operator() (const Scalar& a) const { return ei_log(a); } +}; + +/** \returns an expression of the coefficient-wise logarithm of *this. */ +template<typename Derived> +const CwiseUnaryOp<ei_scalar_log_op, Derived> +MatrixBase<Derived>::cwiseLog() const +{ + return CwiseUnaryOp<ei_scalar_log_op, Derived>(derived()); +} + +/** \internal + * \brief Template functor to compute the cosine of a scalar + * + * \sa class CwiseUnaryOp, MatrixBase::cwiseCos() + */ +struct ei_scalar_cos_op EIGEN_EMPTY_STRUCT { + template<typename Scalar> Scalar operator() (const Scalar& a) const { return ei_cos(a); } +}; + +/** \returns an expression of the coefficient-wise cosine of *this. */ +template<typename Derived> +const CwiseUnaryOp<ei_scalar_cos_op, Derived> +MatrixBase<Derived>::cwiseCos() const +{ + return CwiseUnaryOp<ei_scalar_cos_op, Derived>(derived()); +} + +/** \internal + * \brief Template functor to compute the sine of a scalar + * + * \sa class CwiseUnaryOp, MatrixBase::cwiseSin() + */ +struct ei_scalar_sin_op EIGEN_EMPTY_STRUCT { + template<typename Scalar> Scalar operator() (const Scalar& a) const { return ei_sin(a); } +}; + +/** \returns an expression of the coefficient-wise sine of *this. */ +template<typename Derived> +const CwiseUnaryOp<ei_scalar_sin_op, Derived> +MatrixBase<Derived>::cwiseSin() const +{ + return CwiseUnaryOp<ei_scalar_sin_op, Derived>(derived()); +} + +/** \internal + * \brief Template functor to raise a scalar to a power + * + * \sa class CwiseUnaryOp, MatrixBase::cwisePow + */ +template<typename Scalar> +struct ei_scalar_pow_op { + ei_scalar_pow_op(const Scalar& exponent) : m_exponent(exponent) {} + Scalar operator() (const Scalar& a) const { return ei_pow(a, m_exponent); } + const Scalar m_exponent; +}; + +/** \relates MatrixBase */ +template<typename Derived> +const CwiseUnaryOp<ei_scalar_pow_op<typename ei_traits<Derived>::Scalar>, Derived> +MatrixBase<Derived>::cwisePow(const Scalar& exponent) const +{ + return CwiseUnaryOp<ei_scalar_pow_op<Scalar>, Derived> + (derived(), ei_scalar_pow_op<Scalar>(exponent)); +} + #endif // EIGEN_CWISE_UNARY_OP_H diff --git a/Eigen/src/Core/ForwardDeclarations.h b/Eigen/src/Core/ForwardDeclarations.h index 114362784..69b8cab7b 100644 --- a/Eigen/src/Core/ForwardDeclarations.h +++ b/Eigen/src/Core/ForwardDeclarations.h @@ -53,6 +53,13 @@ struct ei_scalar_quotient_op; struct ei_scalar_opposite_op; struct ei_scalar_conjugate_op; struct ei_scalar_abs_op; +struct ei_scalar_abs2_op; +struct ei_scalar_sqrt_op; +struct ei_scalar_exp_op; +struct ei_scalar_log_op; +struct ei_scalar_cos_op; +struct ei_scalar_sin_op; +template<typename Scalar> struct ei_scalar_pow_op; template<typename NewType> struct ei_scalar_cast_op; template<typename Scalar> struct ei_scalar_multiple_op; diff --git a/Eigen/src/Core/MathFunctions.h b/Eigen/src/Core/MathFunctions.h index b320d228f..13bd549f4 100644 --- a/Eigen/src/Core/MathFunctions.h +++ b/Eigen/src/Core/MathFunctions.h @@ -35,14 +35,13 @@ inline int ei_imag(int) { return 0; } inline int ei_conj(int x) { return x; } inline int ei_abs(int x) { return abs(x); } inline int ei_abs2(int x) { return x*x; } -inline int ei_sqrt(int) -{ - // Taking the square root of integers is not allowed - // (the square root does not always exist within the integers). - // Please cast to a floating-point type. - assert(false); - return 0; -} +inline int ei_sqrt(int) { assert(false); return 0; } +inline int ei_exp(int) { assert(false); return 0; } +inline int ei_log(int) { assert(false); return 0; } +inline int ei_sin(int) { assert(false); return 0; } +inline int ei_cos(int) { assert(false); return 0; } +inline int ei_pow(int x, int y) { return std::pow(x, y); } + template<> inline int ei_random(int a, int b) { // We can't just do rand()%n as only the high-order bits are really random @@ -72,6 +71,12 @@ inline float ei_conj(float x) { return x; } inline float ei_abs(float x) { return std::abs(x); } inline float ei_abs2(float x) { return x*x; } inline float ei_sqrt(float x) { return std::sqrt(x); } +inline float ei_exp(float x) { return std::exp(x); } +inline float ei_log(float x) { return std::log(x); } +inline float ei_sin(float x) { return std::sin(x); } +inline float ei_cos(float x) { return std::cos(x); } +inline float ei_pow(float x, float y) { return std::pow(x, y); } + template<> inline float ei_random(float a, float b) { return a + (b-a) * std::rand() / RAND_MAX; @@ -100,6 +105,12 @@ inline double ei_conj(double x) { return x; } inline double ei_abs(double x) { return std::abs(x); } inline double ei_abs2(double x) { return x*x; } inline double ei_sqrt(double x) { return std::sqrt(x); } +inline double ei_exp(double x) { return std::exp(x); } +inline double ei_log(double x) { return std::log(x); } +inline double ei_sin(double x) { return std::sin(x); } +inline double ei_cos(double x) { return std::cos(x); } +inline double ei_pow(double x, double y) { return std::pow(x, y); } + template<> inline double ei_random(double a, double b) { return a + (b-a) * std::rand() / RAND_MAX; @@ -127,14 +138,10 @@ inline float ei_imag(const std::complex<float>& x) { return std::imag(x); } inline std::complex<float> ei_conj(const std::complex<float>& x) { return std::conj(x); } inline float ei_abs(const std::complex<float>& x) { return std::abs(x); } inline float ei_abs2(const std::complex<float>& x) { return std::norm(x); } -inline std::complex<float> ei_sqrt(const std::complex<float>&) -{ - // Taking the square roots of complex numbers is not allowed, - // as this is ambiguous (there are two square roots). - // What were you trying to do? - assert(false); - return 0; -} +inline std::complex<float> ei_exp(std::complex<float> x) { return std::exp(x); } +inline std::complex<float> ei_sin(std::complex<float> x) { return std::sin(x); } +inline std::complex<float> ei_cos(std::complex<float> x) { return std::cos(x); } + template<> inline std::complex<float> ei_random() { return std::complex<float>(ei_random<float>(), ei_random<float>()); @@ -160,6 +167,10 @@ inline double ei_imag(const std::complex<double>& x) { return std::imag(x); } inline std::complex<double> ei_conj(const std::complex<double>& x) { return std::conj(x); } inline double ei_abs(const std::complex<double>& x) { return std::abs(x); } inline double ei_abs2(const std::complex<double>& x) { return std::norm(x); } +inline std::complex<double> ei_exp(std::complex<double> x) { return std::exp(x); } +inline std::complex<double> ei_sin(std::complex<double> x) { return std::sin(x); } +inline std::complex<double> ei_cos(std::complex<double> x) { return std::cos(x); } + template<> inline std::complex<double> ei_random() { return std::complex<double>(ei_random<double>(), ei_random<double>()); @@ -179,21 +190,4 @@ inline bool ei_isApprox(const std::complex<double>& a, const std::complex<double } // ei_isApproxOrLessThan wouldn't make sense for complex numbers -#define EIGEN_MAKE_MORE_OVERLOADED_COMPLEX_OPERATOR_STAR(T,U) \ -inline std::complex<T> operator*(U a, const std::complex<T>& b) \ -{ \ - return std::complex<T>(static_cast<T>(a)*b.real(), \ - static_cast<T>(a)*b.imag()); \ -} \ -inline std::complex<T> operator*(const std::complex<T>& b, U a) \ -{ \ - return std::complex<T>(static_cast<T>(a)*b.real(), \ - static_cast<T>(a)*b.imag()); \ -} - -EIGEN_MAKE_MORE_OVERLOADED_COMPLEX_OPERATOR_STAR(int, float) -EIGEN_MAKE_MORE_OVERLOADED_COMPLEX_OPERATOR_STAR(int, double) -EIGEN_MAKE_MORE_OVERLOADED_COMPLEX_OPERATOR_STAR(float, double) -EIGEN_MAKE_MORE_OVERLOADED_COMPLEX_OPERATOR_STAR(double, float) - #endif // EIGEN_MATHFUNCTIONS_H diff --git a/Eigen/src/Core/MatrixBase.h b/Eigen/src/Core/MatrixBase.h index d5f8decee..f51c06d6c 100644 --- a/Eigen/src/Core/MatrixBase.h +++ b/Eigen/src/Core/MatrixBase.h @@ -55,80 +55,76 @@ template<typename Derived> class MatrixBase public: + /// \name Compile-time traits + //@{ typedef typename ei_traits<Derived>::Scalar Scalar; - const Derived& derived() const { return *static_cast<const Derived*>(this); } - Derived& derived() { return *static_cast<Derived*>(this); } - Derived& const_cast_derived() const - { return *static_cast<Derived*>(const_cast<MatrixBase*>(this)); } - - - /** The number of rows at compile-time. This is just a copy of the value provided - * by the \a Derived type. If a value is not known at compile-time, - * it is set to the \a Dynamic constant. - * \sa MatrixBase::rows(), MatrixBase::cols(), ColsAtCompileTime, SizeAtCompileTime */ - enum { RowsAtCompileTime = ei_traits<Derived>::RowsAtCompileTime }; - - /** The number of columns at compile-time. This is just a copy of the value provided - * by the \a Derived type. If a value is not known at compile-time, - * it is set to the \a Dynamic constant. - * \sa MatrixBase::rows(), MatrixBase::cols(), RowsAtCompileTime, SizeAtCompileTime */ - enum { ColsAtCompileTime = ei_traits<Derived>::ColsAtCompileTime }; - - /** This is equal to the number of coefficients, i.e. the number of - * rows times the number of columns, or to \a Dynamic if this is not - * known at compile-time. \sa RowsAtCompileTime, ColsAtCompileTime */ - enum { SizeAtCompileTime - = ei_traits<Derived>::RowsAtCompileTime == Dynamic - || ei_traits<Derived>::ColsAtCompileTime == Dynamic - ? Dynamic - : ei_traits<Derived>::RowsAtCompileTime * ei_traits<Derived>::ColsAtCompileTime - }; - - /** This value is equal to the maximum possible number of rows that this expression - * might have. If this expression might have an arbitrarily high number of rows, - * this value is set to \a Dynamic. - * - * This value is useful to know when evaluating an expression, in order to determine - * whether it is possible to avoid doing a dynamic memory allocation. - * - * \sa RowsAtCompileTime, MaxColsAtCompileTime, MaxSizeAtCompileTime - */ - enum { MaxRowsAtCompileTime = ei_traits<Derived>::MaxRowsAtCompileTime }; - - /** This value is equal to the maximum possible number of columns that this expression - * might have. If this expression might have an arbitrarily high number of columns, - * this value is set to \a Dynamic. - * - * This value is useful to know when evaluating an expression, in order to determine - * whether it is possible to avoid doing a dynamic memory allocation. - * - * \sa ColsAtCompileTime, MaxRowsAtCompileTime, MaxSizeAtCompileTime - */ - enum { MaxColsAtCompileTime = ei_traits<Derived>::MaxColsAtCompileTime }; - - /** This value is equal to the maximum possible number of coefficients that this expression - * might have. If this expression might have an arbitrarily high number of coefficients, - * this value is set to \a Dynamic. - * - * This value is useful to know when evaluating an expression, in order to determine - * whether it is possible to avoid doing a dynamic memory allocation. - * - * \sa SizeAtCompileTime, MaxRowsAtCompileTime, MaxColsAtCompileTime - */ - enum { MaxSizeAtCompileTime - = ei_traits<Derived>::MaxRowsAtCompileTime == Dynamic - || ei_traits<Derived>::MaxColsAtCompileTime == Dynamic - ? Dynamic - : ei_traits<Derived>::MaxRowsAtCompileTime * ei_traits<Derived>::MaxColsAtCompileTime - }; - - /** This is set to true if either the number of rows or the number of - * columns is known at compile-time to be equal to 1. Indeed, in that case, - * we are dealing with a column-vector (if there is only one column) or with - * a row-vector (if there is only one row). */ - enum { IsVectorAtCompileTime - = ei_traits<Derived>::RowsAtCompileTime == 1 || ei_traits<Derived>::ColsAtCompileTime == 1 + enum { + + RowsAtCompileTime = ei_traits<Derived>::RowsAtCompileTime, + /**< The number of rows at compile-time. This is just a copy of the value provided + * by the \a Derived type. If a value is not known at compile-time, + * it is set to the \a Dynamic constant. + * \sa MatrixBase::rows(), MatrixBase::cols(), ColsAtCompileTime, SizeAtCompileTime */ + + ColsAtCompileTime = ei_traits<Derived>::ColsAtCompileTime, + /**< The number of columns at compile-time. This is just a copy of the value provided + * by the \a Derived type. If a value is not known at compile-time, + * it is set to the \a Dynamic constant. + * \sa MatrixBase::rows(), MatrixBase::cols(), RowsAtCompileTime, SizeAtCompileTime */ + + SizeAtCompileTime + = ei_traits<Derived>::RowsAtCompileTime == Dynamic + || ei_traits<Derived>::ColsAtCompileTime == Dynamic + ? Dynamic + : ei_traits<Derived>::RowsAtCompileTime * ei_traits<Derived>::ColsAtCompileTime, + /**< This is equal to the number of coefficients, i.e. the number of + * rows times the number of columns, or to \a Dynamic if this is not + * known at compile-time. \sa RowsAtCompileTime, ColsAtCompileTime */ + + MaxRowsAtCompileTime = ei_traits<Derived>::MaxRowsAtCompileTime, + /**< This value is equal to the maximum possible number of rows that this expression + * might have. If this expression might have an arbitrarily high number of rows, + * this value is set to \a Dynamic. + * + * This value is useful to know when evaluating an expression, in order to determine + * whether it is possible to avoid doing a dynamic memory allocation. + * + * \sa RowsAtCompileTime, MaxColsAtCompileTime, MaxSizeAtCompileTime + */ + + MaxColsAtCompileTime = ei_traits<Derived>::MaxColsAtCompileTime, + /**< This value is equal to the maximum possible number of columns that this expression + * might have. If this expression might have an arbitrarily high number of columns, + * this value is set to \a Dynamic. + * + * This value is useful to know when evaluating an expression, in order to determine + * whether it is possible to avoid doing a dynamic memory allocation. + * + * \sa ColsAtCompileTime, MaxRowsAtCompileTime, MaxSizeAtCompileTime + */ + + MaxSizeAtCompileTime + = ei_traits<Derived>::MaxRowsAtCompileTime == Dynamic + || ei_traits<Derived>::MaxColsAtCompileTime == Dynamic + ? Dynamic + : ei_traits<Derived>::MaxRowsAtCompileTime * ei_traits<Derived>::MaxColsAtCompileTime, + /**< This value is equal to the maximum possible number of coefficients that this expression + * might have. If this expression might have an arbitrarily high number of coefficients, + * this value is set to \a Dynamic. + * + * This value is useful to know when evaluating an expression, in order to determine + * whether it is possible to avoid doing a dynamic memory allocation. + * + * \sa SizeAtCompileTime, MaxRowsAtCompileTime, MaxColsAtCompileTime + */ + + IsVectorAtCompileTime + = ei_traits<Derived>::RowsAtCompileTime == 1 || ei_traits<Derived>::ColsAtCompileTime == 1 + /**< This is set to true if either the number of rows or the number of + * columns is known at compile-time to be equal to 1. Indeed, in that case, + * we are dealing with a column-vector (if there is only one column) or with + * a row-vector (if there is only one row). */ }; /** This is the "real scalar" type; if the \a Scalar type is already real numbers @@ -141,13 +137,14 @@ template<typename Derived> class MatrixBase * \sa class NumTraits */ typedef typename NumTraits<Scalar>::Real RealScalar; + //@} - /// \name matrix properties + /// \name Run-time traits //@{ /** \returns the number of rows. \sa cols(), RowsAtCompileTime */ - int rows() const { return static_cast<const Derived *>(this)->_rows(); } + int rows() const { return derived()._rows(); } /** \returns the number of columns. \sa row(), ColsAtCompileTime*/ - int cols() const { return static_cast<const Derived *>(this)->_cols(); } + int cols() const { return derived()._cols(); } /** \returns the number of coefficients, which is \a rows()*cols(). * \sa rows(), cols(), SizeAtCompileTime. */ int size() const { return rows() * cols(); } @@ -158,6 +155,9 @@ template<typename Derived> class MatrixBase bool isVector() const { return rows()==1 || cols()==1; } //@} + /// \name Copying and initialization + //@{ + /** Copies \a other into *this. \returns a reference to *this. */ template<typename OtherDerived> Derived& operator=(const MatrixBase<OtherDerived>& other); @@ -174,17 +174,96 @@ template<typename Derived> class MatrixBase template<typename OtherDerived> CommaInitializer operator<< (const MatrixBase<OtherDerived>& other); + //@} - /** swaps *this with the expression \a other. - * - * \note \a other is only marked const because I couln't find another way - * to get g++ 4.2 to accept that template parameter resolution. It gets const_cast'd - * of course. TODO: get rid of const here. + /// \name Coefficient accessors + //@{ + Scalar coeff(int row, int col) const; + Scalar operator()(int row, int col) const; + + Scalar& coeffRef(int row, int col); + Scalar& operator()(int row, int col); + + Scalar coeff(int index) const; + Scalar operator[](int index) const; + + Scalar& coeffRef(int index); + Scalar& operator[](int index); + + Scalar x() const; + Scalar y() const; + Scalar z() const; + Scalar w() const; + Scalar& x(); + Scalar& y(); + Scalar& z(); + Scalar& w(); + //@} + + /** \name Linear structure + * sum, scalar multiple, ... */ + //@{ + const CwiseUnaryOp<ei_scalar_opposite_op,Derived> operator-() const; + template<typename OtherDerived> - void swap(const MatrixBase<OtherDerived>& other); + const CwiseBinaryOp<ei_scalar_sum_op, Derived, OtherDerived> + operator+(const MatrixBase<OtherDerived> &other) const; - /// \name sub-matrices + template<typename OtherDerived> + const CwiseBinaryOp<ei_scalar_difference_op, Derived, OtherDerived> + operator-(const MatrixBase<OtherDerived> &other) const; + + template<typename OtherDerived> + Derived& operator+=(const MatrixBase<OtherDerived>& other); + template<typename OtherDerived> + Derived& operator-=(const MatrixBase<OtherDerived>& other); + + Derived& operator*=(const Scalar& other); + Derived& operator/=(const Scalar& other); + + const CwiseUnaryOp<ei_scalar_multiple_op<Scalar>, Derived> operator*(const Scalar& scalar) const; + const CwiseUnaryOp<ei_scalar_multiple_op<Scalar>, Derived> operator/(const Scalar& scalar) const; + + friend const CwiseUnaryOp<ei_scalar_multiple_op<Scalar>, Derived> + operator*(const Scalar& scalar, const MatrixBase& matrix) + { return matrix*scalar; } + //@} + + /** \name Matrix product and related notions + * including the trace... + */ + //@{ + template<typename OtherDerived> + const Product<Derived, OtherDerived> + lazyProduct(const MatrixBase<OtherDerived>& other) const; + + template<typename OtherDerived> + const Eval<Product<Derived, OtherDerived> > + operator*(const MatrixBase<OtherDerived> &other) const; + + template<typename OtherDerived> + Derived& operator*=(const MatrixBase<OtherDerived>& other); + + Scalar trace() const; + //@} + + /** \name Dot product and related notions + * including vector norm, adjoint, transpose ... + */ + //@{ + template<typename OtherDerived> + Scalar dot(const MatrixBase<OtherDerived>& other) const; + RealScalar norm2() const; + RealScalar norm() const; + const CwiseUnaryOp<ei_scalar_multiple_op<Scalar>, Derived> normalized() const; + + Transpose<Derived> transpose(); + const Transpose<Derived> transpose() const; + const Transpose<CwiseUnaryOp<ei_scalar_conjugate_op, Derived> > adjoint() const; + //@} + + /// \name Sub-matrices //@{ Block<Derived, 1, ei_traits<Derived>::ColsAtCompileTime> row(int i); const Block<Derived, 1, ei_traits<Derived>::ColsAtCompileTime> row(int i) const; @@ -220,33 +299,8 @@ template<typename Derived> class MatrixBase const DiagonalCoeffs<Derived> diagonal() const; //@} - /// \name matrix transformation - //@{ - template<typename NewType> - const CwiseUnaryOp<ei_scalar_cast_op<NewType>, Derived> cast() const; - - const DiagonalMatrix<Derived> asDiagonal() const; - - Transpose<Derived> transpose(); - const Transpose<Derived> transpose() const; - - const CwiseUnaryOp<ei_scalar_conjugate_op, Derived> conjugate() const; - const Transpose<CwiseUnaryOp<ei_scalar_conjugate_op, Derived> > adjoint() const; - - const CwiseUnaryOp<ei_scalar_multiple_op<Scalar>, Derived> normalized() const; - //@} - - // FIXME not sure about the following name - /// \name metrics + /// \name Generating special matrices //@{ - Scalar trace() const; - - template<typename OtherDerived> - Scalar dot(const MatrixBase<OtherDerived>& other) const; - RealScalar norm2() const; - RealScalar norm() const; - //@} - static const Eval<Random<Derived> > random(int rows, int cols); static const Eval<Random<Derived> > random(int size); static const Eval<Random<Derived> > random(); @@ -259,23 +313,16 @@ template<typename Derived> class MatrixBase static const Identity<Derived> identity(); static const Identity<Derived> identity(int rows, int cols); + const DiagonalMatrix<Derived> asDiagonal() const; + Derived& setZero(); Derived& setOnes(); Derived& setRandom(); Derived& setIdentity(); + //@} - /// \name matrix diagnostic and comparison + /// \name Comparison and diagnostic //@{ - bool isZero(RealScalar prec = precision<Scalar>()) const; - bool isOnes(RealScalar prec = precision<Scalar>()) const; - bool isIdentity(RealScalar prec = precision<Scalar>()) const; - bool isDiagonal(RealScalar prec = precision<Scalar>()) const; - - template<typename OtherDerived> - bool isOrtho(const MatrixBase<OtherDerived>& other, - RealScalar prec = precision<Scalar>()) const; - bool isOrtho(RealScalar prec = precision<Scalar>()) const; - template<typename OtherDerived> bool isApprox(const OtherDerived& other, RealScalar prec = precision<Scalar>()) const; @@ -284,34 +331,58 @@ template<typename Derived> class MatrixBase template<typename OtherDerived> bool isMuchSmallerThan(const MatrixBase<OtherDerived>& other, RealScalar prec = precision<Scalar>()) const; - //@} - /// \name arithemetic operators - //@{ - const CwiseUnaryOp<ei_scalar_opposite_op,Derived> operator-() const; + bool isZero(RealScalar prec = precision<Scalar>()) const; + bool isOnes(RealScalar prec = precision<Scalar>()) const; + bool isIdentity(RealScalar prec = precision<Scalar>()) const; + bool isDiagonal(RealScalar prec = precision<Scalar>()) const; template<typename OtherDerived> - Derived& operator+=(const MatrixBase<OtherDerived>& other); - template<typename OtherDerived> - Derived& operator-=(const MatrixBase<OtherDerived>& other); - template<typename OtherDerived> - Derived& operator*=(const MatrixBase<OtherDerived>& other); + bool isOrtho(const MatrixBase<OtherDerived>& other, + RealScalar prec = precision<Scalar>()) const; + bool isOrtho(RealScalar prec = precision<Scalar>()) const; - Derived& operator*=(const Scalar& other); - Derived& operator/=(const Scalar& other); + /** puts in *row and *col the location of the coefficient of *this + * which has the biggest absolute value. + */ + void findBiggestCoeff(int *row, int *col) const + { + RealScalar biggest = 0; + for(int j = 0; j < cols(); j++) + for(int i = 0; i < rows(); i++) + { + RealScalar x = ei_abs(coeff(i,j)); + if(x > biggest) + { + biggest = x; + *row = i; + *col = j; + } + } + } + //@} - const CwiseUnaryOp<ei_scalar_multiple_op<Scalar>, Derived> operator*(const Scalar& scalar) const; - const CwiseUnaryOp<ei_scalar_multiple_op<Scalar>, Derived> operator/(const Scalar& scalar) const; + /// \name Special functions + //@{ + template<typename NewType> + const CwiseUnaryOp<ei_scalar_cast_op<NewType>, Derived> cast() const; - friend const CwiseUnaryOp<ei_scalar_multiple_op<Scalar>, Derived> - operator*(const Scalar& scalar, const MatrixBase& matrix) - { return matrix*scalar; } + const Eval<Derived> eval() const EIGEN_ALWAYS_INLINE; + const EvalOMP<Derived> evalOMP() const EIGEN_ALWAYS_INLINE; + /** swaps *this with the expression \a other. + * + * \note \a other is only marked const because I couln't find another way + * to get g++ 4.2 to accept that template parameter resolution. It gets const_cast'd + * of course. TODO: get rid of const here. + */ template<typename OtherDerived> - const Product<Derived, OtherDerived> - lazyProduct(const MatrixBase<OtherDerived>& other) const EIGEN_ALWAYS_INLINE; + void swap(const MatrixBase<OtherDerived>& other); + //@} - const CwiseUnaryOp<ei_scalar_abs_op,Derived> cwiseAbs() const; + /// \name Coefficient-wise operations + //@{ + const CwiseUnaryOp<ei_scalar_conjugate_op, Derived> conjugate() const; template<typename OtherDerived> const CwiseBinaryOp<ei_scalar_product_op, Derived, OtherDerived> @@ -320,36 +391,16 @@ template<typename Derived> class MatrixBase template<typename OtherDerived> const CwiseBinaryOp<ei_scalar_quotient_op, Derived, OtherDerived> cwiseQuotient(const MatrixBase<OtherDerived> &other) const; - //@} - /// \name coefficient accessors - //@{ - Scalar coeff(int row, int col) const; - Scalar operator()(int row, int col) const; - - Scalar& coeffRef(int row, int col); - Scalar& operator()(int row, int col); - - Scalar coeff(int index) const; - Scalar operator[](int index) const; - - Scalar& coeffRef(int index); - Scalar& operator[](int index); - - Scalar x() const; - Scalar y() const; - Scalar z() const; - Scalar w() const; - Scalar& x(); - Scalar& y(); - Scalar& z(); - Scalar& w(); - //@} - - /// \name special functions - //@{ - const Eval<Derived> eval() const EIGEN_ALWAYS_INLINE; - const EvalOMP<Derived> evalOMP() const EIGEN_ALWAYS_INLINE; + const CwiseUnaryOp<ei_scalar_abs_op, Derived> cwiseAbs() const; + const CwiseUnaryOp<ei_scalar_abs2_op, Derived> cwiseAbs2() const; + const CwiseUnaryOp<ei_scalar_sqrt_op, Derived> cwiseSqrt() const; + const CwiseUnaryOp<ei_scalar_exp_op, Derived> cwiseExp() const; + const CwiseUnaryOp<ei_scalar_log_op, Derived> cwiseLog() const; + const CwiseUnaryOp<ei_scalar_cos_op, Derived> cwiseCos() const; + const CwiseUnaryOp<ei_scalar_sin_op, Derived> cwiseSin() const; + const CwiseUnaryOp<ei_scalar_pow_op<typename ei_traits<Derived>::Scalar>, Derived> + cwisePow(const Scalar& exponent) const; template<typename CustomUnaryOp> const CwiseUnaryOp<CustomUnaryOp, Derived> cwise(const CustomUnaryOp& func = CustomUnaryOp()) const; @@ -359,24 +410,13 @@ template<typename Derived> class MatrixBase cwise(const MatrixBase<OtherDerived> &other, const CustomBinaryOp& func = CustomBinaryOp()) const; //@} - /** puts in *row and *col the location of the coefficient of *this - * which has the biggest absolute value. - */ - void findBiggestCoeff(int *row, int *col) const - { - RealScalar biggest = 0; - for(int j = 0; j < cols(); j++) - for(int i = 0; i < rows(); i++) - { - RealScalar x = ei_abs(coeff(i,j)); - if(x > biggest) - { - biggest = x; - *row = i; - *col = j; - } - } - } + /// \name Casting to the derived type + //@{ + const Derived& derived() const { return *static_cast<const Derived*>(this); } + Derived& derived() { return *static_cast<Derived*>(this); } + Derived& const_cast_derived() const + { return *static_cast<Derived*>(const_cast<MatrixBase*>(this)); } + //@} }; diff --git a/Eigen/src/Core/Product.h b/Eigen/src/Core/Product.h index cbc60f1ed..4999a1a4e 100644 --- a/Eigen/src/Core/Product.h +++ b/Eigen/src/Core/Product.h @@ -144,21 +144,19 @@ MatrixBase<Derived>::lazyProduct(const MatrixBase<OtherDerived> &other) const return Product<Derived, OtherDerived>(derived(), other.derived()); } -/** \relates MatrixBase - * - * \returns the matrix product of \a mat1 and \a mat2. More precisely, the return statement is: - * \code return mat1.lazyProduct(mat2).eval(); \endcode +/** \returns the matrix product of \c *this and \a other. * * \note This function causes an immediate evaluation. If you want to perform a matrix product * without immediate evaluation, use MatrixBase::lazyProduct() instead. * - * \sa MatrixBase::lazyProduct(), MatrixBase::operator*=(const MatrixBase&) + * \sa lazyProduct(), operator*=(const MatrixBase&) */ -template<typename Derived1, typename Derived2> -const Eval<Product<Derived1, Derived2> > -operator*(const MatrixBase<Derived1> &mat1, const MatrixBase<Derived2> &mat2) +template<typename Derived> +template<typename OtherDerived> +const Eval<Product<Derived, OtherDerived> > +MatrixBase<Derived>::operator*(const MatrixBase<OtherDerived> &other) const { - return mat1.lazyProduct(mat2).eval(); + return lazyProduct(other).eval(); } /** replaces \c *this by \c *this * \a other. diff --git a/doc/Doxyfile.in b/doc/Doxyfile.in index a8dc339c1..ec3841482 100644 --- a/doc/Doxyfile.in +++ b/doc/Doxyfile.in @@ -70,7 +70,7 @@ GENERATE_TESTLIST = YES GENERATE_BUGLIST = YES GENERATE_DEPRECATEDLIST= YES ENABLED_SECTIONS = -MAX_INITIALIZER_LINES = 30 +MAX_INITIALIZER_LINES = 0 SHOW_USED_FILES = YES SHOW_DIRECTORIES = NO FILE_VERSION_FILTER = @@ -91,9 +91,9 @@ INPUT = ${CMAKE_SOURCE_DIR}/doc ${CMAKE_SOURCE_DIR}/Eigen INPUT_ENCODING = UTF-8 FILE_PATTERNS = * RECURSIVE = NO -EXCLUDE = CMake* *.txt +EXCLUDE = EXCLUDE_SYMLINKS = NO -EXCLUDE_PATTERNS = +EXCLUDE_PATTERNS = CMake* *.txt *~ EXCLUDE_SYMBOLS = EXAMPLE_PATH = ${CMAKE_SOURCE_DIR}/doc/snippets \ ${CMAKE_BINARY_DIR}/doc/snippets \ @@ -140,7 +140,7 @@ GENERATE_CHI = NO BINARY_TOC = NO TOC_EXPAND = NO DISABLE_INDEX = NO -ENUM_VALUES_PER_LINE = 4 +ENUM_VALUES_PER_LINE = 1 GENERATE_TREEVIEW = NO TREEVIEW_WIDTH = 250 #--------------------------------------------------------------------------- diff --git a/doc/examples/class_Block.cpp b/doc/examples/class_Block.cpp index fd5ae816c..79cc314bf 100644 --- a/doc/examples/class_Block.cpp +++ b/doc/examples/class_Block.cpp @@ -6,14 +6,14 @@ template<typename Derived> Eigen::Block<Derived> topLeftCorner(MatrixBase<Derived>& m, int rows, int cols) { - return Eigen::Block<Derived>(m, 0, 0, rows, cols); + return Eigen::Block<Derived>(m.derived(), 0, 0, rows, cols); } template<typename Derived> const Eigen::Block<Derived> topLeftCorner(const MatrixBase<Derived>& m, int rows, int cols) { - return Eigen::Block<Derived>(m, 0, 0, rows, cols); + return Eigen::Block<Derived>(m.derived(), 0, 0, rows, cols); } int main(int, char**) diff --git a/doc/examples/class_Cast.cpp b/doc/examples/class_Cast.cpp deleted file mode 100644 index 7e752be1b..000000000 --- a/doc/examples/class_Cast.cpp +++ /dev/null @@ -1,27 +0,0 @@ -#include <Eigen/Core> -USING_PART_OF_NAMESPACE_EIGEN -using namespace std; - -template<typename Derived> -const Eigen::CwiseUnaryOp< - Eigen::ei_scalar_cast_op< - typename Eigen::ei_traits<typename Derived::Scalar>::FloatingPoint - >, Derived -> -castToFloatingPoint(const MatrixBase<Derived>& m) -{ - return m.template cast< - typename Eigen::ei_traits< - typename Derived::Scalar - >::FloatingPoint - >(); -} - -int main(int, char**) -{ - Matrix2i m = Matrix2i::random(); - cout << "Here's the matrix m. It has coefficients of type int." - << endl << m << endl; - cout << "Here's m/20:" << endl << castToFloatingPoint(m)/20 << endl; - return 0; -} diff --git a/doc/examples/class_Column.cpp b/doc/examples/class_Column.cpp deleted file mode 100644 index 1394324fa..000000000 --- a/doc/examples/class_Column.cpp +++ /dev/null @@ -1,26 +0,0 @@ -#include <Eigen/Core> -USING_PART_OF_NAMESPACE_EIGEN -using namespace std; - -template<typename Derived> -Eigen::Block<Derived,Derived::RowsAtCompileTime,1> -firstColumn(MatrixBase<Derived>& m) -{ - return typename Eigen::Block<Derived,Derived::RowsAtCompileTime,1>(m, 0); -} - -template<typename Derived> -const Eigen::Block<Derived,Derived::RowsAtCompileTime,1> -firstColumn(const MatrixBase<Derived>& m) -{ - return typename Eigen::Block<Derived,Derived::RowsAtCompileTime,1>(m, 0); -} - -int main(int, char**) -{ - Matrix4d m = Matrix4d::identity(); - cout << firstColumn(2*m) << endl; // calls the const version - firstColumn(m) *= 5; // calls the non-const version - cout << "Now the matrix m is:" << endl << m << endl; - return 0; -} diff --git a/doc/examples/class_FixedBlock.cpp b/doc/examples/class_FixedBlock.cpp index 644f420bd..e3e9532bc 100644 --- a/doc/examples/class_FixedBlock.cpp +++ b/doc/examples/class_FixedBlock.cpp @@ -6,14 +6,14 @@ template<typename Derived> Eigen::Block<Derived, 2, 2> topLeft2x2Corner(MatrixBase<Derived>& m) { - return Eigen::Block<Derived, 2, 2>(m, 0, 0); + return Eigen::Block<Derived, 2, 2>(m.derived(), 0, 0); } template<typename Derived> const Eigen::Block<Derived, 2, 2> topLeft2x2Corner(const MatrixBase<Derived>& m) { - return Eigen::Block<Derived, 2, 2>(m, 0, 0); + return Eigen::Block<Derived, 2, 2>(m.derived(), 0, 0); } int main(int, char**) diff --git a/doc/examples/class_Row.cpp b/doc/examples/class_Row.cpp deleted file mode 100644 index eed1dbdb4..000000000 --- a/doc/examples/class_Row.cpp +++ /dev/null @@ -1,26 +0,0 @@ -#include <Eigen/Core> -USING_PART_OF_NAMESPACE_EIGEN -using namespace std; - -template<typename Derived> -Eigen::Block<Derived,1,Derived::ColsAtCompileTime> -firstRow(MatrixBase<Derived>& m) -{ - return Eigen::Block<Derived,1,Derived::ColsAtCompileTime>(m, 0); -} - -template<typename Derived> -const Eigen::Block<Derived,1,Derived::ColsAtCompileTime> -firstRow(const MatrixBase<Derived>& m) -{ - return Eigen::Block<Derived,1,Derived::ColsAtCompileTime>(m, 0); -} - -int main(int, char**) -{ - Matrix4d m = Matrix4d::identity(); - cout << firstRow(2*m) << endl; // calls the const version - firstRow(m) *= 5; // calls the non-const version - cout << "Now the matrix m is:" << endl << m << endl; - return 0; -} |