diff options
author | Gael Guennebaud <g.gael@free.fr> | 2008-05-31 18:11:48 +0000 |
---|---|---|
committer | Gael Guennebaud <g.gael@free.fr> | 2008-05-31 18:11:48 +0000 |
commit | 310f7aa09622e8e549b4e14cc3659e22bb02d915 (patch) | |
tree | 1d1c22bf1801a731b8d4bf7cb84443376660ea08 /Eigen/src/Array | |
parent | a2f71f9d7e1e443814fe60726d99a4b0508baefa (diff) |
moved purely "array" related stuff to a new module Array.
This include:
- cwise Pow,Sin,Cos,Exp...
- cwise Greater and other comparison operators
- .any(), .all() and partial reduction
- random
Diffstat (limited to 'Eigen/src/Array')
-rw-r--r-- | Eigen/src/Array/AllAndAny.h | 124 | ||||
-rw-r--r-- | Eigen/src/Array/ArrayBase.h | 43 | ||||
-rw-r--r-- | Eigen/src/Array/CMakeLists.txt | 6 | ||||
-rw-r--r-- | Eigen/src/Array/CwiseOperators.h | 153 | ||||
-rw-r--r-- | Eigen/src/Array/Functors.h | 205 | ||||
-rw-r--r-- | Eigen/src/Array/PartialRedux.h | 126 | ||||
-rw-r--r-- | Eigen/src/Array/Random.h | 111 |
7 files changed, 768 insertions, 0 deletions
diff --git a/Eigen/src/Array/AllAndAny.h b/Eigen/src/Array/AllAndAny.h new file mode 100644 index 000000000..74fbf38c9 --- /dev/null +++ b/Eigen/src/Array/AllAndAny.h @@ -0,0 +1,124 @@ +// This file is part of Eigen, a lightweight C++ template library +// for linear algebra. Eigen itself is part of the KDE project. +// +// Copyright (C) 2008 Gael Guennebaud <g.gael@free.fr> +// +// Eigen is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 3 of the License, or (at your option) any later version. +// +// Alternatively, you can redistribute it and/or +// modify it under the terms of the GNU General Public License as +// published by the Free Software Foundation; either version 2 of +// the License, or (at your option) any later version. +// +// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License and a copy of the GNU General Public License along with +// Eigen. If not, see <http://www.gnu.org/licenses/>. + +#ifndef EIGEN_ALLANDANY_H +#define EIGEN_ALLANDANY_H + +template<typename Derived, int UnrollCount> +struct ei_all_unroller +{ + enum { + col = (UnrollCount-1) / Derived::RowsAtCompileTime, + row = (UnrollCount-1) % Derived::RowsAtCompileTime + }; + + inline static bool run(const Derived &mat) + { + return ei_all_unroller<Derived, UnrollCount-1>::run(mat) && mat.coeff(row, col); + } +}; + +template<typename Derived> +struct ei_all_unroller<Derived, 1> +{ + inline static bool run(const Derived &mat) { return mat.coeff(0, 0); } +}; + +template<typename Derived> +struct ei_all_unroller<Derived, Dynamic> +{ + inline static bool run(const Derived &) { return false; } +}; + +template<typename Derived, int UnrollCount> +struct ei_any_unroller +{ + enum { + col = (UnrollCount-1) / Derived::RowsAtCompileTime, + row = (UnrollCount-1) % Derived::RowsAtCompileTime + }; + + inline static bool run(const Derived &mat) + { + return ei_any_unroller<Derived, UnrollCount-1>::run(mat) || mat.coeff(row, col); + } +}; + +template<typename Derived> +struct ei_any_unroller<Derived, 1> +{ + inline static bool run(const Derived &mat) { return mat.coeff(0, 0); } +}; + +template<typename Derived> +struct ei_any_unroller<Derived, Dynamic> +{ + inline static bool run(const Derived &) { return false; } +}; + +/** \returns true if all coefficients are true + * + * \sa MatrixBase::any() + */ +template<typename Derived> +bool MatrixBase<Derived>::all(void) const +{ + const bool unroll = SizeAtCompileTime * (CoeffReadCost + NumTraits<Scalar>::AddCost) + <= EIGEN_UNROLLING_LIMIT; + if(unroll) + return ei_all_unroller<Derived, + unroll ? int(SizeAtCompileTime) : Dynamic + >::run(derived()); + else + { + for(int j = 0; j < cols(); j++) + for(int i = 0; i < rows(); i++) + if (!coeff(i, j)) return false; + return true; + } +} + +/** \returns true if at least one coefficient is true + * + * \sa MatrixBase::any() + */ +template<typename Derived> +bool MatrixBase<Derived>::any(void) const +{ + const bool unroll = SizeAtCompileTime * (CoeffReadCost + NumTraits<Scalar>::AddCost) + <= EIGEN_UNROLLING_LIMIT; + if(unroll) + return ei_any_unroller<Derived, + unroll ? int(SizeAtCompileTime) : Dynamic + >::run(derived()); + else + { + for(int j = 0; j < cols(); j++) + for(int i = 0; i < rows(); i++) + if (coeff(i, j)) return true; + return false; + } +} + +#endif // EIGEN_ALLANDANY_H diff --git a/Eigen/src/Array/ArrayBase.h b/Eigen/src/Array/ArrayBase.h new file mode 100644 index 000000000..43d30f51d --- /dev/null +++ b/Eigen/src/Array/ArrayBase.h @@ -0,0 +1,43 @@ +// This file is part of Eigen, a lightweight C++ template library +// for linear algebra. Eigen itself is part of the KDE project. +// +// Copyright (C) 2006-2008 Benoit Jacob <jacob@math.jussieu.fr> +// +// Eigen is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 3 of the License, or (at your option) any later version. +// +// Alternatively, you can redistribute it and/or +// modify it under the terms of the GNU General Public License as +// published by the Free Software Foundation; either version 2 of +// the License, or (at your option) any later version. +// +// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License and a copy of the GNU General Public License along with +// Eigen. If not, see <http://www.gnu.org/licenses/>. + +#ifndef EIGEN_ARRAYBASE_H +#define EIGEN_ARRAYBASE_H + +template<typename Derived> class ArrayBase<Derived,true> +{ + inline const Derived& derived() const { return *static_cast<const Derived*>(this); } + inline Derived& derived() { return *static_cast<Derived*>(this); } + inline Derived& const_cast_derived() const + { return *static_cast<Derived*>(const_cast<ArrayBase*>(this)); } +public: + template<typename OtherDerived> + const Product<Derived,OtherDerived> + matrixProduct(const MatrixBase<OtherDerived> &other) const + { + return Product<Derived,OtherDerived>(derived(), other.derived()); + } +}; + +#endif // EIGEN_ARRAYBASE_H diff --git a/Eigen/src/Array/CMakeLists.txt b/Eigen/src/Array/CMakeLists.txt new file mode 100644 index 000000000..1b974a069 --- /dev/null +++ b/Eigen/src/Array/CMakeLists.txt @@ -0,0 +1,6 @@ +FILE(GLOB Eigen_ARRAY_SRCS "*.h") + +INSTALL(FILES + ${Eigen_ARRAY_SRCS} + DESTINATION ${INCLUDE_INSTALL_DIR}/Eigen/src/ARRAY + ) diff --git a/Eigen/src/Array/CwiseOperators.h b/Eigen/src/Array/CwiseOperators.h new file mode 100644 index 000000000..82a18d11c --- /dev/null +++ b/Eigen/src/Array/CwiseOperators.h @@ -0,0 +1,153 @@ +// This file is part of Eigen, a lightweight C++ template library +// for linear algebra. Eigen itself is part of the KDE project. +// +// Copyright (C) 2008 Gael Guennebaud <g.gael@free.fr> +// +// Eigen is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 3 of the License, or (at your option) any later version. +// +// Alternatively, you can redistribute it and/or +// modify it under the terms of the GNU General Public License as +// published by the Free Software Foundation; either version 2 of +// the License, or (at your option) any later version. +// +// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License and a copy of the GNU General Public License along with +// Eigen. If not, see <http://www.gnu.org/licenses/>. + +#ifndef EIGEN_ARRAY_CWISE_OPERATORS_H +#define EIGEN_ARRAY_CWISE_OPERATORS_H + +// -- unary operators -- + +/** \returns an expression of the coefficient-wise square root of *this. */ +template<typename Derived> +inline const CwiseUnaryOp<ei_scalar_sqrt_op<typename ei_traits<Derived>::Scalar>, Derived> +MatrixBase<Derived>::cwiseSqrt() const +{ + return derived(); +} + +/** \returns an expression of the coefficient-wise exponential of *this. */ +template<typename Derived> +inline const CwiseUnaryOp<ei_scalar_exp_op<typename ei_traits<Derived>::Scalar>, Derived> +MatrixBase<Derived>::cwiseExp() const +{ + return derived(); +} + +/** \returns an expression of the coefficient-wise logarithm of *this. */ +template<typename Derived> +inline const CwiseUnaryOp<ei_scalar_log_op<typename ei_traits<Derived>::Scalar>, Derived> +MatrixBase<Derived>::cwiseLog() const +{ + return derived(); +} + +/** \returns an expression of the coefficient-wise cosine of *this. */ +template<typename Derived> +inline const CwiseUnaryOp<ei_scalar_cos_op<typename ei_traits<Derived>::Scalar>, Derived> +MatrixBase<Derived>::cwiseCos() const +{ + return derived(); +} + +/** \returns an expression of the coefficient-wise sine of *this. */ +template<typename Derived> +inline const CwiseUnaryOp<ei_scalar_sin_op<typename ei_traits<Derived>::Scalar>, Derived> +MatrixBase<Derived>::cwiseSin() const +{ + return derived(); +} + +/** \returns an expression of the coefficient-wise power of *this to the given exponent. */ +template<typename Derived> +inline 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)); +} + +// -- binary operators -- + +/** \returns an expression of the coefficient-wise \< operator of *this and \a other + * + * \sa class CwiseBinaryOp + */ +template<typename Derived> +template<typename OtherDerived> +inline const CwiseBinaryOp<std::less<typename ei_traits<Derived>::Scalar>, Derived, OtherDerived> +MatrixBase<Derived>::cwiseLessThan(const MatrixBase<OtherDerived> &other) const +{ + return cwise(other, std::less<Scalar>()); +} + +/** \returns an expression of the coefficient-wise \<= operator of *this and \a other + * + * \sa class CwiseBinaryOp + */ +template<typename Derived> +template<typename OtherDerived> +inline const CwiseBinaryOp<std::less_equal<typename ei_traits<Derived>::Scalar>, Derived, OtherDerived> +MatrixBase<Derived>::cwiseLessEqual(const MatrixBase<OtherDerived> &other) const +{ + return cwise(other, std::less_equal<Scalar>()); +} + +/** \returns an expression of the coefficient-wise \> operator of *this and \a other + * + * \sa class CwiseBinaryOp + */ +template<typename Derived> +template<typename OtherDerived> +inline const CwiseBinaryOp<std::greater<typename ei_traits<Derived>::Scalar>, Derived, OtherDerived> +MatrixBase<Derived>::cwiseGreaterThan(const MatrixBase<OtherDerived> &other) const +{ + return cwise(other, std::greater<Scalar>()); +} + +/** \returns an expression of the coefficient-wise \>= operator of *this and \a other + * + * \sa class CwiseBinaryOp + */ +template<typename Derived> +template<typename OtherDerived> +inline const CwiseBinaryOp<std::greater_equal<typename ei_traits<Derived>::Scalar>, Derived, OtherDerived> +MatrixBase<Derived>::cwiseGreaterEqual(const MatrixBase<OtherDerived> &other) const +{ + return cwise(other, std::greater_equal<Scalar>()); +} + +/** \returns an expression of the coefficient-wise == operator of *this and \a other + * + * \sa class CwiseBinaryOp + */ +template<typename Derived> +template<typename OtherDerived> +inline const CwiseBinaryOp<std::equal_to<typename ei_traits<Derived>::Scalar>, Derived, OtherDerived> +MatrixBase<Derived>::cwiseEqualTo(const MatrixBase<OtherDerived> &other) const +{ + return cwise(other, std::equal_to<Scalar>()); +} + +/** \returns an expression of the coefficient-wise != operator of *this and \a other + * + * \sa class CwiseBinaryOp + */ +template<typename Derived> +template<typename OtherDerived> +inline const CwiseBinaryOp<std::not_equal_to<typename ei_traits<Derived>::Scalar>, Derived, OtherDerived> +MatrixBase<Derived>::cwiseNotEqualTo(const MatrixBase<OtherDerived> &other) const +{ + return cwise(other, std::not_equal_to<Scalar>()); +} + +#endif // EIGEN_ARRAY_CWISE_OPERATORS_H diff --git a/Eigen/src/Array/Functors.h b/Eigen/src/Array/Functors.h new file mode 100644 index 000000000..805f2dcad --- /dev/null +++ b/Eigen/src/Array/Functors.h @@ -0,0 +1,205 @@ +// This file is part of Eigen, a lightweight C++ template library +// for linear algebra. Eigen itself is part of the KDE project. +// +// Copyright (C) 2008 Gael Guennebaud <g.gael@free.fr> +// +// Eigen is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 3 of the License, or (at your option) any later version. +// +// Alternatively, you can redistribute it and/or +// modify it under the terms of the GNU General Public License as +// published by the Free Software Foundation; either version 2 of +// the License, or (at your option) any later version. +// +// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License and a copy of the GNU General Public License along with +// Eigen. If not, see <http://www.gnu.org/licenses/>. + +#ifndef EIGEN_ARRAY_FUNCTORS_H +#define EIGEN_ARRAY_FUNCTORS_H + +/** \internal + * \brief Template functor to compute the square root of a scalar + * + * \sa class CwiseUnaryOp, MatrixBase::cwiseSqrt() + */ +template<typename Scalar> struct ei_scalar_sqrt_op EIGEN_EMPTY_STRUCT { + inline const Scalar operator() (const Scalar& a) const { return ei_sqrt(a); } +}; +template<typename Scalar> +struct ei_functor_traits<ei_scalar_sqrt_op<Scalar> > +{ enum { Cost = 5 * NumTraits<Scalar>::MulCost, IsVectorizable = false }; }; + +/** \internal + * \brief Template functor to compute the exponential of a scalar + * + * \sa class CwiseUnaryOp, MatrixBase::cwiseExp() + */ +template<typename Scalar> struct ei_scalar_exp_op EIGEN_EMPTY_STRUCT { + inline const Scalar operator() (const Scalar& a) const { return ei_exp(a); } +}; +template<typename Scalar> +struct ei_functor_traits<ei_scalar_exp_op<Scalar> > +{ enum { Cost = 5 * NumTraits<Scalar>::MulCost, IsVectorizable = false }; }; + +/** \internal + * \brief Template functor to compute the logarithm of a scalar + * + * \sa class CwiseUnaryOp, MatrixBase::cwiseLog() + */ +template<typename Scalar> struct ei_scalar_log_op EIGEN_EMPTY_STRUCT { + inline const Scalar operator() (const Scalar& a) const { return ei_log(a); } +}; +template<typename Scalar> +struct ei_functor_traits<ei_scalar_log_op<Scalar> > +{ enum { Cost = 5 * NumTraits<Scalar>::MulCost, IsVectorizable = false }; }; + +/** \internal + * \brief Template functor to compute the cosine of a scalar + * + * \sa class CwiseUnaryOp, MatrixBase::cwiseCos() + */ +template<typename Scalar> struct ei_scalar_cos_op EIGEN_EMPTY_STRUCT { + inline const Scalar operator() (const Scalar& a) const { return ei_cos(a); } +}; +template<typename Scalar> +struct ei_functor_traits<ei_scalar_cos_op<Scalar> > +{ enum { Cost = 5 * NumTraits<Scalar>::MulCost, IsVectorizable = false }; }; + +/** \internal + * \brief Template functor to compute the sine of a scalar + * + * \sa class CwiseUnaryOp, MatrixBase::cwiseSin() + */ +template<typename Scalar> struct ei_scalar_sin_op EIGEN_EMPTY_STRUCT { + inline const Scalar operator() (const Scalar& a) const { return ei_sin(a); } +}; +template<typename Scalar> +struct ei_functor_traits<ei_scalar_sin_op<Scalar> > +{ enum { Cost = 5 * NumTraits<Scalar>::MulCost, IsVectorizable = false }; }; + +/** \internal + * \brief Template functor to raise a scalar to a power + * + * \sa class CwiseUnaryOp, MatrixBase::cwisePow + */ +template<typename Scalar> +struct ei_scalar_pow_op { + inline ei_scalar_pow_op(const Scalar& exponent) : m_exponent(exponent) {} + inline Scalar operator() (const Scalar& a) const { return ei_pow(a, m_exponent); } + const Scalar m_exponent; +}; +template<typename Scalar> +struct ei_functor_traits<ei_scalar_pow_op<Scalar> > +{ enum { Cost = 5 * NumTraits<Scalar>::MulCost, IsVectorizable = false }; }; + +// default ei_functor_traits for STL functors: + +template<typename T> +struct ei_functor_traits<std::multiplies<T> > +{ enum { Cost = NumTraits<T>::MulCost, IsVectorizable = false }; }; + +template<typename T> +struct ei_functor_traits<std::divides<T> > +{ enum { Cost = NumTraits<T>::MulCost, IsVectorizable = false }; }; + +template<typename T> +struct ei_functor_traits<std::plus<T> > +{ enum { Cost = NumTraits<T>::AddCost, IsVectorizable = false }; }; + +template<typename T> +struct ei_functor_traits<std::minus<T> > +{ enum { Cost = NumTraits<T>::AddCost, IsVectorizable = false }; }; + +template<typename T> +struct ei_functor_traits<std::negate<T> > +{ enum { Cost = NumTraits<T>::AddCost, IsVectorizable = false }; }; + +template<typename T> +struct ei_functor_traits<std::logical_or<T> > +{ enum { Cost = 1, IsVectorizable = false }; }; + +template<typename T> +struct ei_functor_traits<std::logical_and<T> > +{ enum { Cost = 1, IsVectorizable = false }; }; + +template<typename T> +struct ei_functor_traits<std::logical_not<T> > +{ enum { Cost = 1, IsVectorizable = false }; }; + +template<typename T> +struct ei_functor_traits<std::greater<T> > +{ enum { Cost = 1, IsVectorizable = false }; }; + +template<typename T> +struct ei_functor_traits<std::less<T> > +{ enum { Cost = 1, IsVectorizable = false }; }; + +template<typename T> +struct ei_functor_traits<std::greater_equal<T> > +{ enum { Cost = 1, IsVectorizable = false }; }; + +template<typename T> +struct ei_functor_traits<std::less_equal<T> > +{ enum { Cost = 1, IsVectorizable = false }; }; + +template<typename T> +struct ei_functor_traits<std::equal_to<T> > +{ enum { Cost = 1, IsVectorizable = false }; }; + +template<typename T> +struct ei_functor_traits<std::not_equal_to<T> > +{ enum { Cost = 1, IsVectorizable = false }; }; + +template<typename T> +struct ei_functor_traits<std::binder2nd<T> > +{ enum { Cost = ei_functor_traits<T>::Cost, IsVectorizable = false }; }; + +template<typename T> +struct ei_functor_traits<std::binder1st<T> > +{ enum { Cost = ei_functor_traits<T>::Cost, IsVectorizable = false }; }; + +template<typename T> +struct ei_functor_traits<std::unary_negate<T> > +{ enum { Cost = 1 + ei_functor_traits<T>::Cost, IsVectorizable = false }; }; + +template<typename T> +struct ei_functor_traits<std::binary_negate<T> > +{ enum { Cost = 1 + ei_functor_traits<T>::Cost, IsVectorizable = false }; }; + +#ifdef EIGEN_STDEXT_SUPPORT + +template<typename T0,typename T1> +struct ei_functor_traits<std::project1st<T0,T1> > +{ enum { Cost = 0, IsVectorizable = false }; }; + +template<typename T0,typename T1> +struct ei_functor_traits<std::project2nd<T0,T1> > +{ enum { Cost = 0, IsVectorizable = false }; }; + +template<typename T0,typename T1> +struct ei_functor_traits<std::select2nd<std::pair<T0,T1> > > +{ enum { Cost = 0, IsVectorizable = false }; }; + +template<typename T0,typename T1> +struct ei_functor_traits<std::select1st<std::pair<T0,T1> > > +{ enum { Cost = 0, IsVectorizable = false }; }; + +template<typename T0,typename T1> +struct ei_functor_traits<std::unary_compose<T0,T1> > +{ enum { Cost = ei_functor_traits<T0>::Cost + ei_functor_traits<T1>::Cost, IsVectorizable = false }; }; + +template<typename T0,typename T1,typename T2> +struct ei_functor_traits<std::binary_compose<T0,T1,T2> > +{ enum { Cost = ei_functor_traits<T0>::Cost + ei_functor_traits<T1>::Cost + ei_functor_traits<T2>::Cost, IsVectorizable = false }; }; + +#endif // EIGEN_STDEXT_SUPPORT + +#endif // EIGEN_ARRAY_FUNCTORS_H diff --git a/Eigen/src/Array/PartialRedux.h b/Eigen/src/Array/PartialRedux.h new file mode 100644 index 000000000..6b33caaa9 --- /dev/null +++ b/Eigen/src/Array/PartialRedux.h @@ -0,0 +1,126 @@ +// This file is part of Eigen, a lightweight C++ template library +// for linear algebra. Eigen itself is part of the KDE project. +// +// Copyright (C) 2008 Gael Guennebaud <g.gael@free.fr> +// Copyright (C) 2006-2008 Benoit Jacob <jacob@math.jussieu.fr> +// +// Eigen is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 3 of the License, or (at your option) any later version. +// +// Alternatively, you can redistribute it and/or +// modify it under the terms of the GNU General Public License as +// published by the Free Software Foundation; either version 2 of +// the License, or (at your option) any later version. +// +// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License and a copy of the GNU General Public License along with +// Eigen. If not, see <http://www.gnu.org/licenses/>. + +#ifndef EIGEN_PARTIAL_REDUX_H +#define EIGEN_PARTIAL_REDUX_H + +/** \class PartialRedux + * + * \brief Generic expression of a partially reduxed matrix + * + * \param Direction indicates the direction of the redux (Vertical or Horizontal) + * \param BinaryOp type of the binary functor implementing the operator (must be associative) + * \param MatrixType the type of the matrix we are applying the redux operation + * + * This class represents an expression of a partial redux operator of a matrix. + * It is the return type of MatrixBase::verticalRedux(), MatrixBase::horizontalRedux(), + * and most of the time this is the only way it is used. + * + * \sa class CwiseBinaryOp + */ +template<int Direction, typename BinaryOp, typename MatrixType> +struct ei_traits<PartialRedux<Direction, BinaryOp, MatrixType> > +{ + typedef typename ei_result_of< + BinaryOp(typename MatrixType::Scalar) + >::type Scalar; + typedef typename ei_nested<MatrixType>::type MatrixTypeNested; + typedef typename ei_unref<MatrixTypeNested>::type _MatrixTypeNested; + enum { + RowsAtCompileTime = Direction==Vertical ? 1 : MatrixType::RowsAtCompileTime, + ColsAtCompileTime = Direction==Horizontal ? 1 : MatrixType::ColsAtCompileTime, + MaxRowsAtCompileTime = Direction==Vertical ? 1 : MatrixType::MaxRowsAtCompileTime, + MaxColsAtCompileTime = Direction==Horizontal ? 1 : MatrixType::MaxColsAtCompileTime, + Flags = ((int(RowsAtCompileTime) == Dynamic || int(ColsAtCompileTime) == Dynamic) + ? (unsigned int)_MatrixTypeNested::Flags + : (unsigned int)_MatrixTypeNested::Flags & ~LargeBit) & HereditaryBits, + TraversalSize = Direction==Vertical ? RowsAtCompileTime : ColsAtCompileTime, + CoeffReadCost = TraversalSize * _MatrixTypeNested::CoeffReadCost + + (TraversalSize - 1) * ei_functor_traits<BinaryOp>::Cost + }; +}; + +template<int Direction, typename BinaryOp, typename MatrixType> +class PartialRedux : ei_no_assignment_operator, + public MatrixBase<PartialRedux<Direction, BinaryOp, MatrixType> > +{ + public: + + EIGEN_GENERIC_PUBLIC_INTERFACE(PartialRedux) + typedef typename ei_traits<PartialRedux>::MatrixTypeNested MatrixTypeNested; + typedef typename ei_traits<PartialRedux>::_MatrixTypeNested _MatrixTypeNested; + + PartialRedux(const MatrixType& mat, const BinaryOp& func = BinaryOp()) + : m_matrix(mat), m_functor(func) {} + + private: + + int _rows() const { return (Direction==Vertical ? 1 : m_matrix.rows()); } + int _cols() const { return (Direction==Horizontal ? 1 : m_matrix.cols()); } + + const Scalar _coeff(int i, int j) const + { + if (Direction==Vertical) + return m_matrix.col(j).redux(m_functor); + else + return m_matrix.row(i).redux(m_functor); + } + + protected: + const MatrixTypeNested m_matrix; + const BinaryOp m_functor; +}; + +/** \returns a row vector expression of *this vertically reduxed by \a func + * + * The template parameter \a BinaryOp is the type of the functor + * of the custom redux operator. Note that func must be an associative operator. + * + * \sa class PartialRedux, MatrixBase::horizontalRedux() + */ +template<typename Derived> +template<typename BinaryOp> +const PartialRedux<Vertical, BinaryOp, Derived> +MatrixBase<Derived>::verticalRedux(const BinaryOp& func) const +{ + return PartialRedux<Vertical, BinaryOp, Derived>(derived(), func); +} + +/** \returns a row vector expression of *this horizontally reduxed by \a func + * + * The template parameter \a BinaryOp is the type of the functor + * of the custom redux operator. Note that func must be an associative operator. + * + * \sa class PartialRedux, MatrixBase::verticalRedux() + */ +template<typename Derived> +template<typename BinaryOp> +const PartialRedux<Horizontal, BinaryOp, Derived> +MatrixBase<Derived>::horizontalRedux(const BinaryOp& func) const +{ + return PartialRedux<Horizontal, BinaryOp, Derived>(derived(), func); +} + +#endif // EIGEN_PARTIAL_REDUX_H diff --git a/Eigen/src/Array/Random.h b/Eigen/src/Array/Random.h new file mode 100644 index 000000000..2e19e1ff0 --- /dev/null +++ b/Eigen/src/Array/Random.h @@ -0,0 +1,111 @@ +// This file is part of Eigen, a lightweight C++ template library +// for linear algebra. Eigen itself is part of the KDE project. +// +// Copyright (C) 2008 Gael Guennebaud <g.gael@free.fr> +// +// Eigen is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 3 of the License, or (at your option) any later version. +// +// Alternatively, you can redistribute it and/or +// modify it under the terms of the GNU General Public License as +// published by the Free Software Foundation; either version 2 of +// the License, or (at your option) any later version. +// +// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License and a copy of the GNU General Public License along with +// Eigen. If not, see <http://www.gnu.org/licenses/>. + +#ifndef EIGEN_RANDOM_H +#define EIGEN_RANDOM_H + +template<typename Scalar> struct ei_scalar_random_op EIGEN_EMPTY_STRUCT { + inline ei_scalar_random_op(void) {} + inline const Scalar operator() (int, int) const { return ei_random<Scalar>(); } +}; +template<typename Scalar> +struct ei_functor_traits<ei_scalar_random_op<Scalar> > +{ enum { Cost = 5 * NumTraits<Scalar>::MulCost, IsVectorizable = false, IsRepeatable = false }; }; + +/** \returns a random matrix (not an expression, the matrix is immediately evaluated). + * + * The parameters \a rows and \a cols are the number of rows and of columns of + * the returned matrix. Must be compatible with this MatrixBase type. + * + * This variant is meant to be used for dynamic-size matrix types. For fixed-size types, + * it is redundant to pass \a rows and \a cols as arguments, so ei_random() should be used + * instead. + * + * Example: \include MatrixBase_random_int_int.cpp + * Output: \verbinclude MatrixBase_random_int_int.out + * + * \sa ei_random(), ei_random(int) + */ +template<typename Derived> +inline const CwiseNullaryOp<ei_scalar_random_op<typename ei_traits<Derived>::Scalar>, Derived> +MatrixBase<Derived>::random(int rows, int cols) +{ + return create(rows, cols, ei_scalar_random_op<Scalar>()); +} + +/** \returns a random vector (not an expression, the vector is immediately evaluated). + * + * The parameter \a size is the size of the returned vector. + * Must be compatible with this MatrixBase type. + * + * \only_for_vectors + * + * This variant is meant to be used for dynamic-size vector types. For fixed-size types, + * it is redundant to pass \a size as argument, so ei_random() should be used + * instead. + * + * Example: \include MatrixBase_random_int.cpp + * Output: \verbinclude MatrixBase_random_int.out + * + * \sa ei_random(), ei_random(int,int) + */ +template<typename Derived> +inline const CwiseNullaryOp<ei_scalar_random_op<typename ei_traits<Derived>::Scalar>, Derived> +MatrixBase<Derived>::random(int size) +{ + return create(size, ei_scalar_random_op<Scalar>()); +} + +/** \returns a fixed-size random matrix or vector + * (not an expression, the matrix is immediately evaluated). + * + * This variant is only for fixed-size MatrixBase types. For dynamic-size types, you + * need to use the variants taking size arguments. + * + * Example: \include MatrixBase_random.cpp + * Output: \verbinclude MatrixBase_random.out + * + * \sa ei_random(int), ei_random(int,int) + */ +template<typename Derived> +inline const CwiseNullaryOp<ei_scalar_random_op<typename ei_traits<Derived>::Scalar>, Derived> +MatrixBase<Derived>::random() +{ + return create(RowsAtCompileTime, ColsAtCompileTime, ei_scalar_random_op<Scalar>()); +} + +/** Sets all coefficients in this expression to random values. + * + * Example: \include MatrixBase_setRandom.cpp + * Output: \verbinclude MatrixBase_setRandom.out + * + * \sa class CwiseNullaryOp, ei_random() + */ +template<typename Derived> +inline Derived& MatrixBase<Derived>::setRandom() +{ + return *this = random(rows(), cols()); +} + +#endif // EIGEN_RANDOM_H |