# HG changeset patch # User Philipp Wissmann # Date 1457710206 -3600 # Fri Mar 11 16:30:06 2016 +0100 # Node ID 92d4c6ccf5080123c3fd98788727f72682d22b54 # Parent a545378bd83a0ba47cc2093bcd1cf58bdc92f595 Bug 663: Added support for NoChange in setZero, setConstant, setOnes and setRandom. Tests and documentation added. diff --git a/Eigen/src/Core/CwiseNullaryOp.h b/Eigen/src/Core/CwiseNullaryOp.h --- a/Eigen/src/Core/CwiseNullaryOp.h +++ b/Eigen/src/Core/CwiseNullaryOp.h @@ -368,16 +368,53 @@ PlainObjectBase::setConstant(In template EIGEN_STRONG_INLINE Derived& PlainObjectBase::setConstant(Index rows, Index cols, const Scalar& val) { resize(rows, cols); return setConstant(val); } +/** Resizes to the given number of columns, leaves the number of rows the same, and sets all coefficients in this expression to the given value \a val. + * + * \param cols the new number of columns + * \param val the value to which all coefficients are set + * + * Example: \include Matrix_setConstant_NoChange.cpp + * Output: \verbinclude Matrix_setConstant_NoChange.out + * + * \sa MatrixBase::setConstant(const Scalar&), setConstant(Index,const Scalar&), class CwiseNullaryOp, MatrixBase::Constant(const Scalar&) + */ +template +EIGEN_STRONG_INLINE Derived& +PlainObjectBase::setConstant(NoChange_t, Index cols, const Scalar& val) +{ + resize(rows(), cols); + return setConstant(val); +} + +/** Resizes to the given number of rows, leaves the number of columns the same, and sets all coefficients in this expression to the given value \a val. + * + * \param rows the new number of rows + * \param val the value to which all coefficients are set + * + * Example: \include Matrix_setConstant_NoChange.cpp + * Output: \verbinclude Matrix_setConstant_NoChange.out + * + * \sa MatrixBase::setConstant(const Scalar&), setConstant(Index,const Scalar&), class CwiseNullaryOp, MatrixBase::Constant(const Scalar&) + */ +template +EIGEN_STRONG_INLINE Derived& +PlainObjectBase::setConstant(Index rows, NoChange_t, const Scalar& val) +{ + resize(rows, cols()); + return setConstant(val); +} + + /** * \brief Sets a linearly spaced vector. * * The function generates 'size' equally spaced values in the closed interval [low,high]. * When size is set to 1, a vector of length 1 containing 'high' is returned. * * \only_for_vectors * @@ -535,16 +572,51 @@ PlainObjectBase::setZero(Index template EIGEN_STRONG_INLINE Derived& PlainObjectBase::setZero(Index rows, Index cols) { resize(rows, cols); return setConstant(Scalar(0)); } +/** Resizes to the given size while keeping number of rows constant and sets all coefficients in this expression to zero. + * + * \param cols the new number of columns + * + * Example: \include Matrix_setZero_NoChange.cpp + * Output: \verbinclude Matrix_setZero_NoChange.out + * + * \sa DenseBase::setZero(), setZero(Index), class CwiseNullaryOp, DenseBase::Zero() + */ +template +EIGEN_STRONG_INLINE Derived& +PlainObjectBase::setZero(NoChange_t, Index cols) +{ + resize(rows(), cols); + return setConstant(Scalar(0)); +} + +/** Resizes to the given size while keeping number of columns constant and sets all coefficients in this expression to zero. + * + * \param rows the new number of rows + * + * Example: \include Matrix_setZero_NoChange.cpp + * Output: \verbinclude Matrix_setZero_NoChange.out + * + * \sa DenseBase::setZero(), setZero(Index), class CwiseNullaryOp, DenseBase::Zero() + */ +template +EIGEN_STRONG_INLINE Derived& +PlainObjectBase::setZero(Index rows, NoChange_t) +{ + resize(rows, cols()); + 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, @@ -661,16 +733,50 @@ PlainObjectBase::setOnes(Index template EIGEN_STRONG_INLINE Derived& PlainObjectBase::setOnes(Index rows, Index cols) { resize(rows, cols); return setConstant(Scalar(1)); } +/** Resizes to the given size, and sets all coefficients in this expression to one. + * + * \param cols the new number of columns + * + * Example: \include Matrix_setOnes_NoChange.cpp + * Output: \verbinclude Matrix_setOnes_NoChange.out + * + * \sa MatrixBase::setOnes(), setOnes(Index), class CwiseNullaryOp, MatrixBase::Ones() + */ +template +EIGEN_STRONG_INLINE Derived& +PlainObjectBase::setOnes(NoChange_t, Index cols) +{ + resize(rows(), cols); + return setConstant(Scalar(1)); +} + +/** Resizes to the given size, and sets all coefficients in this expression to one. + * + * \param rows the new number of rows + * + * Example: \include Matrix_setOnes_NoChange.cpp + * Output: \verbinclude Matrix_setOnes_NoChange.out + * + * \sa MatrixBase::setOnes(), setOnes(Index), class CwiseNullaryOp, MatrixBase::Ones() + */ +template +EIGEN_STRONG_INLINE Derived& +PlainObjectBase::setOnes(Index rows, NoChange_t) +{ + resize(rows, cols()); + return setConstant(Scalar(1)); +} + // 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, diff --git a/Eigen/src/Core/PlainObjectBase.h b/Eigen/src/Core/PlainObjectBase.h --- a/Eigen/src/Core/PlainObjectBase.h +++ b/Eigen/src/Core/PlainObjectBase.h @@ -615,28 +615,36 @@ class PlainObjectBase : public internal: template static inline typename StridedAlignedMapType >::type MapAligned(Scalar* data, Index rows, Index cols, const Stride& stride) { return typename StridedAlignedMapType >::type(data, rows, cols, stride); } //@} using Base::setConstant; EIGEN_DEVICE_FUNC Derived& setConstant(Index size, const Scalar& val); EIGEN_DEVICE_FUNC Derived& setConstant(Index rows, Index cols, const Scalar& val); + EIGEN_DEVICE_FUNC Derived& setConstant(NoChange_t, Index cols, const Scalar& val); + EIGEN_DEVICE_FUNC Derived& setConstant(Index rows, NoChange_t, const Scalar& val); using Base::setZero; EIGEN_DEVICE_FUNC Derived& setZero(Index size); EIGEN_DEVICE_FUNC Derived& setZero(Index rows, Index cols); + EIGEN_DEVICE_FUNC Derived& setZero(NoChange_t, Index cols); + EIGEN_DEVICE_FUNC Derived& setZero(Index rows, NoChange_t); using Base::setOnes; EIGEN_DEVICE_FUNC Derived& setOnes(Index size); EIGEN_DEVICE_FUNC Derived& setOnes(Index rows, Index cols); + EIGEN_DEVICE_FUNC Derived& setOnes(NoChange_t, Index cols); + EIGEN_DEVICE_FUNC Derived& setOnes(Index rows, NoChange_t); using Base::setRandom; Derived& setRandom(Index size); Derived& setRandom(Index rows, Index cols); + Derived& setRandom(NoChange_t, Index cols); + Derived& setRandom(Index rows, NoChange_t); #ifdef EIGEN_PLAINOBJECTBASE_PLUGIN #include EIGEN_PLAINOBJECTBASE_PLUGIN #endif protected: /** \internal Resizes *this in preparation for assigning \a other to it. * Takes care of doing all the checking that's needed. diff --git a/Eigen/src/Core/Random.h b/Eigen/src/Core/Random.h --- a/Eigen/src/Core/Random.h +++ b/Eigen/src/Core/Random.h @@ -173,11 +173,55 @@ PlainObjectBase::setRandom(Inde template EIGEN_STRONG_INLINE Derived& PlainObjectBase::setRandom(Index rows, Index cols) { resize(rows, cols); return setRandom(); } +/** Resizes to the given number of columns, and sets all coefficients in this expression to random values. + * + * Numbers are uniformly spread through their whole definition range for integer types, + * and in the [-1:1] range for floating point scalar types. + * + * \not_reentrant + * + * \param cols the new number of columns + * + * Example: \include Matrix_setRandom_NoChange.cpp + * Output: \verbinclude Matrix_setRandom_NoChange.out + * + * \sa DenseBase::setRandom(), setRandom(Index), class CwiseNullaryOp, DenseBase::Random() + */ +template +EIGEN_STRONG_INLINE Derived& +PlainObjectBase::setRandom(NoChange_t, Index cols) +{ + resize(rows(), cols); + return setRandom(); +} + +/** Resizes to the given number of rows, and sets all coefficients in this expression to random values. + * + * Numbers are uniformly spread through their whole definition range for integer types, + * and in the [-1:1] range for floating point scalar types. + * + * \not_reentrant + * + * \param rows the new number of rows + * + * Example: \include Matrix_setRandom_NoChange.cpp + * Output: \verbinclude Matrix_setRandom_NoChange.out + * + * \sa DenseBase::setRandom(), setRandom(Index), class CwiseNullaryOp, DenseBase::Random() + */ +template +EIGEN_STRONG_INLINE Derived& +PlainObjectBase::setRandom(Index rows, NoChange_t) +{ + resize(rows, cols()); + return setRandom(); +} + } // end namespace Eigen #endif // EIGEN_RANDOM_H diff --git a/doc/snippets/Matrix_setConstant_NoChange.cpp b/doc/snippets/Matrix_setConstant_NoChange.cpp new file mode 100644 --- /dev/null +++ b/doc/snippets/Matrix_setConstant_NoChange.cpp @@ -0,0 +1,8 @@ +MatrixXf m; +m.resize(3,4); +m.setConstant(1); +cout << m << endl; +m.setConstant(5,NoChange, 2); +cout << m << endl; +m.setConstant(NoChange,5, 3); +cout << m << endl; \ No newline at end of file diff --git a/doc/snippets/Matrix_setOnes_NoChange.cpp b/doc/snippets/Matrix_setOnes_NoChange.cpp new file mode 100644 --- /dev/null +++ b/doc/snippets/Matrix_setOnes_NoChange.cpp @@ -0,0 +1,8 @@ +MatrixXf m; +m.resize(3,4); +m.setConstant(1); +cout << m << endl; +m.setOnes(5,NoChange); +cout << m << endl; +m.setOnes(NoChange,5); +cout << m << endl; \ No newline at end of file diff --git a/doc/snippets/Matrix_setRandom_NoChange.cpp b/doc/snippets/Matrix_setRandom_NoChange.cpp new file mode 100644 --- /dev/null +++ b/doc/snippets/Matrix_setRandom_NoChange.cpp @@ -0,0 +1,8 @@ +MatrixXf m; +m.resize(3,4); +m.setConstant(1); +cout << m << endl; +m.setRandom(5,NoChange); +cout << m << endl; +m.setRandom(NoChange,5); +cout << m << endl; \ No newline at end of file diff --git a/doc/snippets/Matrix_setZero_NoChange.cpp b/doc/snippets/Matrix_setZero_NoChange.cpp new file mode 100644 --- /dev/null +++ b/doc/snippets/Matrix_setZero_NoChange.cpp @@ -0,0 +1,8 @@ +MatrixXf m; +m.resize(3,4); +m.setConstant(1); +cout << m << endl; +m.setZero(5,NoChange); +cout << m << endl; +m.setZero(NoChange,5); +cout << m << endl; \ No newline at end of file diff --git a/test/basicstuff.cpp b/test/basicstuff.cpp --- a/test/basicstuff.cpp +++ b/test/basicstuff.cpp @@ -249,16 +249,73 @@ void fixedSizeMatrixConstruction() VERIFY(m2(0) == DenseIndex(raw[0])); VERIFY(a2(0) == DenseIndex(raw[0])); VERIFY(m3(0) == int(raw[0])); VERIFY_IS_EQUAL(m,(Matrix(raw[0]))); VERIFY((a==Array(raw[0])).all()); } } +//#ifdef EIGEN_TEST_PART_1 +void resizingNoChange() +{ + int rows = 3; + int cols = 6; + MatrixXf m(rows, cols); + Array a(rows, cols); + + // macro to generate tests for setZero, setOnes, SetRandom +#define setResizeTest( what )\ + cols = 7;\ + m.set##what(NoChange, cols);\ + a.set##what(NoChange, cols);\ + VERIFY(m.rows() == rows);\ + VERIFY(a.rows() == rows);\ + VERIFY(m.cols() == cols);\ + VERIFY(a.cols() == cols);\ + rows = 4;\ + m.set##what(rows, NoChange);\ + a.set##what(rows, NoChange);\ + VERIFY(m.cols() == cols);\ + VERIFY(a.cols() == cols);\ + VERIFY(m.rows() == rows);\ + VERIFY(a.rows() == rows);\ + rows = 3;\ + cols = 6;\ + m.resize(rows, cols);\ + a.resize(rows, cols);\ + + setResizeTest(Zero); + setResizeTest(Ones); + setResizeTest(Random); + + +#undef setResizeTest + + // setConstant needs additional input + cols = 7;\ + m.setConstant(NoChange, cols, 2);\ + a.setConstant(NoChange, cols, 2);\ + std::cout<()) ); CALL_SUBTEST_2( basicStuff(Matrix4d()) ); CALL_SUBTEST_3( basicStuff(MatrixXcf(internal::random(1,EIGEN_TEST_MAX_SIZE), internal::random(1,EIGEN_TEST_MAX_SIZE))) ); CALL_SUBTEST_4( basicStuff(MatrixXi(internal::random(1,EIGEN_TEST_MAX_SIZE), internal::random(1,EIGEN_TEST_MAX_SIZE))) ); CALL_SUBTEST_5( basicStuff(MatrixXcd(internal::random(1,EIGEN_TEST_MAX_SIZE), internal::random(1,EIGEN_TEST_MAX_SIZE))) ); @@ -272,9 +329,12 @@ void test_basicstuff() CALL_SUBTEST_1(fixedSizeMatrixConstruction()); CALL_SUBTEST_1(fixedSizeMatrixConstruction()); CALL_SUBTEST_1(fixedSizeMatrixConstruction()); CALL_SUBTEST_1(fixedSizeMatrixConstruction()); CALL_SUBTEST_1(fixedSizeMatrixConstruction()); CALL_SUBTEST_1(fixedSizeMatrixConstruction()); CALL_SUBTEST_2(casting()); + + CALL_SUBTEST_1(resizingNoChange()); + }