// 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 // // 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 . #ifndef EIGEN_CWISE_NULLARY_OP_H #define EIGEN_CWISE_NULLARY_OP_H /** \class CwiseNullaryOp * * \brief Generic expression of a matrix where all coefficients are defined by a functor * * \param NullaryOp template functor implementing the operator * * This class represents an expression of a generic nullary operator. * It is the return type of the ones(), zero(), constant(), identity() and random() functions, * and most of the time this is the only way it is used. * * However, if you want to write a function returning such an expression, you * will need to use this class. * */ template struct ei_traits > { typedef typename MatrixType::Scalar Scalar; enum { RowsAtCompileTime = MatrixType::RowsAtCompileTime, ColsAtCompileTime = MatrixType::ColsAtCompileTime, MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime, MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime, Flags = (MatrixType::Flags & (DefaultLostFlagMask | Like1DArrayBit)) | ei_functor_traits::IsVectorizable | (ei_functor_traits::IsRepeatable ? 0 : EvalBeforeNestingBit), CoeffReadCost = ei_functor_traits::Cost }; }; template class CwiseNullaryOp : ei_no_assignment_operator, public MatrixBase > { public: EIGEN_GENERIC_PUBLIC_INTERFACE(CwiseNullaryOp) CwiseNullaryOp(int rows, int cols, const NullaryOp& func = NullaryOp()) : m_rows(rows), m_cols(cols), m_functor(func) { ei_assert(rows > 0 && (RowsAtCompileTime == Dynamic || RowsAtCompileTime == rows) && cols > 0 && (ColsAtCompileTime == Dynamic || ColsAtCompileTime == cols)); } private: int _rows() const { return m_rows.value(); } int _cols() const { return m_cols.value(); } const Scalar _coeff(int rows, int cols) const { return m_functor(rows, cols); } PacketScalar _packetCoeff(int, int) const { return m_functor.packetOp(); } protected: const ei_int_if_dynamic m_rows; const ei_int_if_dynamic m_cols; const NullaryOp m_functor; }; /** \returns an expression of a matrix defined by a custom functor \a func * * 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 zero() should be used * instead. * * The template parameter \a CustomNullaryOp is the type of the functor. * * \sa class CwiseNullaryOp */ template template const CwiseNullaryOp MatrixBase::create(int rows, int cols, const CustomNullaryOp& func) { return CwiseNullaryOp(rows, cols, func); } /** \returns an expression of a matrix defined by a custom functor \a func * * 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 zero() should be used * instead. * * The template parameter \a CustomNullaryOp is the type of the functor. * * \sa class CwiseNullaryOp */ template template const CwiseNullaryOp MatrixBase::create(int size, const CustomNullaryOp& func) { ei_assert(IsVectorAtCompileTime); if(RowsAtCompileTime == 1) return CwiseNullaryOp(1, size, func); else return CwiseNullaryOp(size, 1, func); } /** \returns an expression of a matrix defined by a custom functor \a func * * This variant is only for fixed-size MatrixBase types. For dynamic-size types, you * need to use the variants taking size arguments. * * The template parameter \a CustomNullaryOp is the type of the functor. * * \sa class CwiseNullaryOp */ template template const CwiseNullaryOp MatrixBase::create(const CustomNullaryOp& func) { return CwiseNullaryOp(rows(), cols(), func); } /** \returns an expression of a constant matrix of value \a value * * 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 zero() should be used * instead. * * The template parameter \a CustomNullaryOp is the type of the functor. * * \sa class CwiseNullaryOp */ template const CwiseNullaryOp::Scalar>, Derived> MatrixBase::constant(int rows, int cols, const Scalar& value) { return create(rows, cols, ei_scalar_constant_op(value)); } /** \returns an expression of a constant matrix of value \a value * * 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 zero() should be used * instead. * * The template parameter \a CustomNullaryOp is the type of the functor. * * \sa class CwiseNullaryOp */ template const CwiseNullaryOp::Scalar>, Derived> MatrixBase::constant(int size, const Scalar& value) { return create(size, ei_scalar_constant_op(value)); } /** \returns an expression of a constant matrix of value \a value * * This variant is only for fixed-size MatrixBase types. For dynamic-size types, you * need to use the variants taking size arguments. * * The template parameter \a CustomNullaryOp is the type of the functor. * * \sa class CwiseNullaryOp */ template const CwiseNullaryOp::Scalar>, Derived> MatrixBase::constant(const Scalar& value) { return create(RowsAtCompileTime, ColsAtCompileTime, ei_scalar_constant_op(value)); } template bool MatrixBase::isEqualToConstant (const Scalar& value, typename NumTraits::Real prec) const { for(int j = 0; j < cols(); j++) for(int i = 0; i < rows(); i++) if(!ei_isApprox(coeff(i, j), value, prec)) return false; return true; } /** Sets all coefficients in this expression to \a value. * * \sa class CwiseNullaryOp, zero(), ones() */ template Derived& MatrixBase::setConstant(const Scalar& value) { return *this = constant(rows(), cols(), value); } // zero: /** \returns an expression of a zero matrix. * * 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 zero() should be used * instead. * * Example: \include MatrixBase_zero_int_int.cpp * Output: \verbinclude MatrixBase_zero_int_int.out * * \sa zero(), zero(int) */ template const CwiseNullaryOp::Scalar>, Derived> MatrixBase::zero(int rows, int cols) { return constant(rows, cols, Scalar(0)); } /** \returns an expression of a zero vector. * * 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 zero() should be used * instead. * * Example: \include MatrixBase_zero_int.cpp * Output: \verbinclude MatrixBase_zero_int.out * * \sa zero(), zero(int,int) */ template const CwiseNullaryOp::Scalar>, Derived> MatrixBase::zero(int size) { return constant(size, Scalar(0)); } /** \returns an expression of a fixed-size zero matrix or vector. * * 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_zero.cpp * Output: \verbinclude MatrixBase_zero.out * * \sa zero(int), zero(int,int) */ template const CwiseNullaryOp::Scalar>, Derived> MatrixBase::zero() { return constant(Scalar(0)); } /** \returns true if *this is approximately equal to the zero matrix, * within the precision given by \a prec. * * Example: \include MatrixBase_isZero.cpp * Output: \verbinclude MatrixBase_isZero.out * * \sa class CwiseNullaryOp, zero() */ template bool MatrixBase::isZero (typename NumTraits::Real prec) const { for(int j = 0; j < cols(); j++) for(int i = 0; i < rows(); i++) if(!ei_isMuchSmallerThan(coeff(i, j), static_cast(1), prec)) return false; return true; } /** Sets all coefficients in this expression to zero. * * Example: \include MatrixBase_setZero.cpp * Output: \verbinclude MatrixBase_setZero.out * * \sa class CwiseNullaryOp, zero() */ template Derived& MatrixBase::setZero() { return setConstant(Scalar(0)); } // ones: /** \returns an expression of a matrix where all coefficients equal one. * * 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 ones() should be used * instead. * * Example: \include MatrixBase_ones_int_int.cpp * Output: \verbinclude MatrixBase_ones_int_int.out * * \sa ones(), ones(int), isOnes(), class Ones */ template const CwiseNullaryOp::Scalar>, Derived> MatrixBase::ones(int rows, int cols) { return constant(rows, cols, Scalar(1)); } /** \returns an expression of a vector where all coefficients equal one. * * 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 ones() should be used * instead. * * Example: \include MatrixBase_ones_int.cpp * Output: \verbinclude MatrixBase_ones_int.out * * \sa ones(), ones(int,int), isOnes(), class Ones */ template const CwiseNullaryOp::Scalar>, Derived> MatrixBase::ones(int size) { return constant(size, Scalar(1)); } /** \returns an expression of a fixed-size matrix or vector where all coefficients equal one. * * 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_ones.cpp * Output: \verbinclude MatrixBase_ones.out * * \sa ones(int), ones(int,int), isOnes(), class Ones */ template const CwiseNullaryOp::Scalar>, Derived> MatrixBase::ones() { return constant(Scalar(1)); } /** \returns true if *this is approximately equal to the matrix where all coefficients * are equal to 1, within the precision given by \a prec. * * Example: \include MatrixBase_isOnes.cpp * Output: \verbinclude MatrixBase_isOnes.out * * \sa class CwiseNullaryOp, ones() */ template bool MatrixBase::isOnes (typename NumTraits::Real prec) const { return isEqualToConstant(Scalar(1)); } /** Sets all coefficients in this expression to one. * * Example: \include MatrixBase_setOnes.cpp * Output: \verbinclude MatrixBase_setOnes.out * * \sa class CwiseNullaryOp, ones() */ template Derived& MatrixBase::setOnes() { return setConstant(Scalar(1)); } // random: /** \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 const CwiseNullaryOp::Scalar>, Derived> MatrixBase::random(int rows, int cols) { return create(rows, cols, ei_scalar_random_op()); } /** \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 const CwiseNullaryOp::Scalar>, Derived> MatrixBase::random(int size) { return create(size, ei_scalar_random_op()); } /** \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 const CwiseNullaryOp::Scalar>, Derived> MatrixBase::random() { return create(RowsAtCompileTime, ColsAtCompileTime, ei_scalar_random_op()); } /** 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 Derived& MatrixBase::setRandom() { return *this = random(rows(), cols()); } // Identity: /** \returns an expression of the identity matrix (not necessarily square). * * 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 identity() should be used * instead. * * Example: \include MatrixBase_identity_int_int.cpp * Output: \verbinclude MatrixBase_identity_int_int.out * * \sa identity(), setIdentity(), isIdentity() */ template const CwiseNullaryOp::Scalar>, Derived> MatrixBase::identity(int rows, int cols) { return create(rows, cols, ei_scalar_identity_op()); } /** \returns an expression of the identity matrix (not necessarily square). * * This variant is only for fixed-size MatrixBase types. For dynamic-size types, you * need to use the variant taking size arguments. * * Example: \include MatrixBase_identity.cpp * Output: \verbinclude MatrixBase_identity.out * * \sa identity(int,int), setIdentity(), isIdentity() */ template const CwiseNullaryOp::Scalar>, Derived> MatrixBase::identity() { return create(RowsAtCompileTime, ColsAtCompileTime, ei_scalar_identity_op()); } /** \returns true if *this is approximately equal to the identity matrix * (not necessarily square), * within the precision given by \a prec. * * Example: \include MatrixBase_isIdentity.cpp * Output: \verbinclude MatrixBase_isIdentity.out * * \sa class CwiseNullaryOp, identity(), identity(int,int), setIdentity() */ template bool MatrixBase::isIdentity (typename NumTraits::Real prec) const { for(int j = 0; j < cols(); j++) { for(int i = 0; i < rows(); i++) { if(i == j) { if(!ei_isApprox(coeff(i, j), static_cast(1), prec)) return false; } else { if(!ei_isMuchSmallerThan(coeff(i, j), static_cast(1), prec)) return false; } } } return true; } /** Writes the identity expression (not necessarily square) into *this. * * Example: \include MatrixBase_setIdentity.cpp * Output: \verbinclude MatrixBase_setIdentity.out * * \sa class CwiseNullaryOp, identity(), identity(int,int), isIdentity() */ template Derived& MatrixBase::setIdentity() { return *this = identity(rows(), cols()); } #endif // EIGEN_CWISE_NULLARY_OP_H