Bugzilla – Attachment 666 Details for
Bug 663
Permit NoChange in setZero, setOnes, setConstant, setRandom
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Requests
|
Help
|
Log In
[x]
|
Forgot Password
Login:
[x]
This bugzilla service is closed. All entries have been migrated to
https://gitlab.com/libeigen/eigen
[patch]
Patch for Bug 663
Bug663 (text/plain), 13.68 KB, created by
Philipp Wissmann
on 2016-03-11 15:43:21 UTC
(
hide
)
Description:
Patch for Bug 663
Filename:
MIME Type:
Creator:
Philipp Wissmann
Created:
2016-03-11 15:43:21 UTC
Size:
13.68 KB
patch
obsolete
># HG changeset patch ># User Philipp Wissmann <phwissmann@posteo.ch> ># 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<Derived>::setConstant(In > template<typename Derived> > EIGEN_STRONG_INLINE Derived& > PlainObjectBase<Derived>::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<typename Derived> >+EIGEN_STRONG_INLINE Derived& >+PlainObjectBase<Derived>::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<typename Derived> >+EIGEN_STRONG_INLINE Derived& >+PlainObjectBase<Derived>::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<Derived>::setZero(Index > template<typename Derived> > EIGEN_STRONG_INLINE Derived& > PlainObjectBase<Derived>::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<typename Derived> >+EIGEN_STRONG_INLINE Derived& >+PlainObjectBase<Derived>::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<typename Derived> >+EIGEN_STRONG_INLINE Derived& >+PlainObjectBase<Derived>::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<Derived>::setOnes(Index > template<typename Derived> > EIGEN_STRONG_INLINE Derived& > PlainObjectBase<Derived>::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<typename Derived> >+EIGEN_STRONG_INLINE Derived& >+PlainObjectBase<Derived>::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<typename Derived> >+EIGEN_STRONG_INLINE Derived& >+PlainObjectBase<Derived>::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<int Outer, int Inner> > static inline typename StridedAlignedMapType<Stride<Outer, Inner> >::type MapAligned(Scalar* data, Index rows, Index cols, const Stride<Outer, Inner>& stride) > { return typename StridedAlignedMapType<Stride<Outer, Inner> >::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<Derived>::setRandom(Inde > template<typename Derived> > EIGEN_STRONG_INLINE Derived& > PlainObjectBase<Derived>::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<typename Derived> >+EIGEN_STRONG_INLINE Derived& >+PlainObjectBase<Derived>::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<typename Derived> >+EIGEN_STRONG_INLINE Derived& >+PlainObjectBase<Derived>::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<Scalar,1,1>(raw[0]))); > VERIFY((a==Array<Scalar,1,1>(raw[0])).all()); > } > } > >+//#ifdef EIGEN_TEST_PART_1 >+void resizingNoChange() >+{ >+ int rows = 3; >+ int cols = 6; >+ MatrixXf m(rows, cols); >+ Array<float,Dynamic,Dynamic> 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<<m << std::endl;\ >+ VERIFY(m.rows() == rows);\ >+ VERIFY(a.rows() == rows);\ >+ VERIFY(m.cols() == cols);\ >+ VERIFY(a.cols() == cols);\ >+ rows = 4;\ >+ m.setConstant(rows, NoChange, 3);\ >+ a.setConstant(rows, NoChange, 3);\ >+ VERIFY(m.cols() == cols);\ >+ VERIFY(a.cols() == cols);\ >+ VERIFY(m.rows() == rows);\ >+ VERIFY(a.rows() == rows);\ >+} >+ >+//#endif >+ >+ > void test_basicstuff() > { > for(int i = 0; i < g_repeat; i++) { > CALL_SUBTEST_1( basicStuff(Matrix<float, 1, 1>()) ); > CALL_SUBTEST_2( basicStuff(Matrix4d()) ); > CALL_SUBTEST_3( basicStuff(MatrixXcf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) ); > CALL_SUBTEST_4( basicStuff(MatrixXi(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) ); > CALL_SUBTEST_5( basicStuff(MatrixXcd(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) ); >@@ -272,9 +329,12 @@ void test_basicstuff() > CALL_SUBTEST_1(fixedSizeMatrixConstruction<unsigned char>()); > CALL_SUBTEST_1(fixedSizeMatrixConstruction<float>()); > CALL_SUBTEST_1(fixedSizeMatrixConstruction<double>()); > CALL_SUBTEST_1(fixedSizeMatrixConstruction<int>()); > CALL_SUBTEST_1(fixedSizeMatrixConstruction<long int>()); > CALL_SUBTEST_1(fixedSizeMatrixConstruction<std::ptrdiff_t>()); > > CALL_SUBTEST_2(casting()); >+ >+ CALL_SUBTEST_1(resizingNoChange()); >+ > }
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
Attachments on
bug 663
: 666