This bugzilla service is closed. All entries have been migrated to https://gitlab.com/libeigen/eigen
View | Details | Raw Unified | Return to bug 663
Collapse All | Expand All

(-)a/Eigen/src/Core/CwiseNullaryOp.h (+106 lines)
Lines 368-383 PlainObjectBase<Derived>::setConstant(In Link Here
368
template<typename Derived>
368
template<typename Derived>
369
EIGEN_STRONG_INLINE Derived&
369
EIGEN_STRONG_INLINE Derived&
370
PlainObjectBase<Derived>::setConstant(Index rows, Index cols, const Scalar& val)
370
PlainObjectBase<Derived>::setConstant(Index rows, Index cols, const Scalar& val)
371
{
371
{
372
  resize(rows, cols);
372
  resize(rows, cols);
373
  return setConstant(val);
373
  return setConstant(val);
374
}
374
}
375
375
376
/** 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.
377
  *
378
  * \param cols the new number of columns
379
  * \param val the value to which all coefficients are set
380
  *
381
  * Example: \include Matrix_setConstant_NoChange.cpp
382
  * Output: \verbinclude Matrix_setConstant_NoChange.out
383
  *
384
  * \sa MatrixBase::setConstant(const Scalar&), setConstant(Index,const Scalar&), class CwiseNullaryOp, MatrixBase::Constant(const Scalar&)
385
  */
386
template<typename Derived>
387
EIGEN_STRONG_INLINE Derived&
388
PlainObjectBase<Derived>::setConstant(NoChange_t, Index cols, const Scalar& val)
389
{
390
  resize(rows(), cols);
391
  return setConstant(val);
392
}
393
394
/** 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.
395
  *
396
  * \param rows the new number of rows
397
  * \param val the value to which all coefficients are set
398
  *
399
  * Example: \include Matrix_setConstant_NoChange.cpp
400
  * Output: \verbinclude Matrix_setConstant_NoChange.out
401
  *
402
  * \sa MatrixBase::setConstant(const Scalar&), setConstant(Index,const Scalar&), class CwiseNullaryOp, MatrixBase::Constant(const Scalar&)
403
  */
404
template<typename Derived>
405
EIGEN_STRONG_INLINE Derived&
406
PlainObjectBase<Derived>::setConstant(Index rows, NoChange_t, const Scalar& val)
407
{
408
  resize(rows, cols());
409
  return setConstant(val);
410
}
411
412
376
/**
413
/**
377
  * \brief Sets a linearly spaced vector.
414
  * \brief Sets a linearly spaced vector.
378
  *
415
  *
379
  * The function generates 'size' equally spaced values in the closed interval [low,high].
416
  * The function generates 'size' equally spaced values in the closed interval [low,high].
380
  * When size is set to 1, a vector of length 1 containing 'high' is returned.
417
  * When size is set to 1, a vector of length 1 containing 'high' is returned.
381
  *
418
  *
382
  * \only_for_vectors
419
  * \only_for_vectors
383
  *
420
  *
Lines 535-550 PlainObjectBase<Derived>::setZero(Index Link Here
535
template<typename Derived>
572
template<typename Derived>
536
EIGEN_STRONG_INLINE Derived&
573
EIGEN_STRONG_INLINE Derived&
537
PlainObjectBase<Derived>::setZero(Index rows, Index cols)
574
PlainObjectBase<Derived>::setZero(Index rows, Index cols)
538
{
575
{
539
  resize(rows, cols);
576
  resize(rows, cols);
540
  return setConstant(Scalar(0));
577
  return setConstant(Scalar(0));
541
}
578
}
542
579
580
/** Resizes to the given size while keeping number of rows constant and sets all coefficients in this expression to zero.
581
  *
582
  * \param cols the new number of columns
583
  *
584
  * Example: \include Matrix_setZero_NoChange.cpp
585
  * Output: \verbinclude Matrix_setZero_NoChange.out
586
  *
587
  * \sa DenseBase::setZero(), setZero(Index), class CwiseNullaryOp, DenseBase::Zero()
588
  */
589
template<typename Derived>
590
EIGEN_STRONG_INLINE Derived&
591
PlainObjectBase<Derived>::setZero(NoChange_t, Index cols)
592
{
593
  resize(rows(), cols);
594
  return setConstant(Scalar(0));
595
}
596
597
/** Resizes to the given size while keeping number of columns constant and sets all coefficients in this expression to zero.
598
  *
599
  * \param rows the new number of rows
600
  *
601
  * Example: \include Matrix_setZero_NoChange.cpp
602
  * Output: \verbinclude Matrix_setZero_NoChange.out
603
  *
604
  * \sa DenseBase::setZero(), setZero(Index), class CwiseNullaryOp, DenseBase::Zero()
605
  */
606
template<typename Derived>
607
EIGEN_STRONG_INLINE Derived&
608
PlainObjectBase<Derived>::setZero(Index rows, NoChange_t)
609
{
610
  resize(rows, cols());
611
  return setConstant(Scalar(0));
612
}
613
614
543
// ones:
615
// ones:
544
616
545
/** \returns an expression of a matrix where all coefficients equal one.
617
/** \returns an expression of a matrix where all coefficients equal one.
546
  *
618
  *
547
  * The parameters \a rows and \a cols are the number of rows and of columns of
619
  * The parameters \a rows and \a cols are the number of rows and of columns of
548
  * the returned matrix. Must be compatible with this MatrixBase type.
620
  * the returned matrix. Must be compatible with this MatrixBase type.
549
  *
621
  *
550
  * This variant is meant to be used for dynamic-size matrix types. For fixed-size types,
622
  * This variant is meant to be used for dynamic-size matrix types. For fixed-size types,
Lines 661-676 PlainObjectBase<Derived>::setOnes(Index Link Here
661
template<typename Derived>
733
template<typename Derived>
662
EIGEN_STRONG_INLINE Derived&
734
EIGEN_STRONG_INLINE Derived&
663
PlainObjectBase<Derived>::setOnes(Index rows, Index cols)
735
PlainObjectBase<Derived>::setOnes(Index rows, Index cols)
664
{
736
{
665
  resize(rows, cols);
737
  resize(rows, cols);
666
  return setConstant(Scalar(1));
738
  return setConstant(Scalar(1));
667
}
739
}
668
740
741
/** Resizes to the given size, and sets all coefficients in this expression to one.
742
  *
743
  * \param cols the new number of columns
744
  *
745
  * Example: \include Matrix_setOnes_NoChange.cpp
746
  * Output: \verbinclude Matrix_setOnes_NoChange.out
747
  *
748
  * \sa MatrixBase::setOnes(), setOnes(Index), class CwiseNullaryOp, MatrixBase::Ones()
749
  */
750
template<typename Derived>
751
EIGEN_STRONG_INLINE Derived&
752
PlainObjectBase<Derived>::setOnes(NoChange_t, Index cols)
753
{
754
  resize(rows(), cols);
755
  return setConstant(Scalar(1));
756
}
757
758
/** Resizes to the given size, and sets all coefficients in this expression to one.
759
  *
760
  * \param rows the new number of rows
761
  *
762
  * Example: \include Matrix_setOnes_NoChange.cpp
763
  * Output: \verbinclude Matrix_setOnes_NoChange.out
764
  *
765
  * \sa MatrixBase::setOnes(), setOnes(Index), class CwiseNullaryOp, MatrixBase::Ones()
766
  */
767
template<typename Derived>
768
EIGEN_STRONG_INLINE Derived&
769
PlainObjectBase<Derived>::setOnes(Index rows, NoChange_t)
770
{
771
  resize(rows, cols());
772
  return setConstant(Scalar(1));
773
}
774
669
// Identity:
775
// Identity:
670
776
671
/** \returns an expression of the identity matrix (not necessarily square).
777
/** \returns an expression of the identity matrix (not necessarily square).
672
  *
778
  *
673
  * The parameters \a rows and \a cols are the number of rows and of columns of
779
  * The parameters \a rows and \a cols are the number of rows and of columns of
674
  * the returned matrix. Must be compatible with this MatrixBase type.
780
  * the returned matrix. Must be compatible with this MatrixBase type.
675
  *
781
  *
676
  * This variant is meant to be used for dynamic-size matrix types. For fixed-size types,
782
  * This variant is meant to be used for dynamic-size matrix types. For fixed-size types,
(-)a/Eigen/src/Core/PlainObjectBase.h (+8 lines)
Lines 615-642 class PlainObjectBase : public internal: Link Here
615
    template<int Outer, int Inner>
615
    template<int Outer, int Inner>
616
    static inline typename StridedAlignedMapType<Stride<Outer, Inner> >::type MapAligned(Scalar* data, Index rows, Index cols, const Stride<Outer, Inner>& stride)
616
    static inline typename StridedAlignedMapType<Stride<Outer, Inner> >::type MapAligned(Scalar* data, Index rows, Index cols, const Stride<Outer, Inner>& stride)
617
    { return typename StridedAlignedMapType<Stride<Outer, Inner> >::type(data, rows, cols, stride); }
617
    { return typename StridedAlignedMapType<Stride<Outer, Inner> >::type(data, rows, cols, stride); }
618
    //@}
618
    //@}
619
619
620
    using Base::setConstant;
620
    using Base::setConstant;
621
    EIGEN_DEVICE_FUNC Derived& setConstant(Index size, const Scalar& val);
621
    EIGEN_DEVICE_FUNC Derived& setConstant(Index size, const Scalar& val);
622
    EIGEN_DEVICE_FUNC Derived& setConstant(Index rows, Index cols, const Scalar& val);
622
    EIGEN_DEVICE_FUNC Derived& setConstant(Index rows, Index cols, const Scalar& val);
623
    EIGEN_DEVICE_FUNC Derived& setConstant(NoChange_t, Index cols, const Scalar& val);
624
    EIGEN_DEVICE_FUNC Derived& setConstant(Index rows, NoChange_t, const Scalar& val);
623
625
624
    using Base::setZero;
626
    using Base::setZero;
625
    EIGEN_DEVICE_FUNC Derived& setZero(Index size);
627
    EIGEN_DEVICE_FUNC Derived& setZero(Index size);
626
    EIGEN_DEVICE_FUNC Derived& setZero(Index rows, Index cols);
628
    EIGEN_DEVICE_FUNC Derived& setZero(Index rows, Index cols);
629
    EIGEN_DEVICE_FUNC Derived& setZero(NoChange_t, Index cols);
630
    EIGEN_DEVICE_FUNC Derived& setZero(Index rows, NoChange_t);
627
631
628
    using Base::setOnes;
632
    using Base::setOnes;
629
    EIGEN_DEVICE_FUNC Derived& setOnes(Index size);
633
    EIGEN_DEVICE_FUNC Derived& setOnes(Index size);
630
    EIGEN_DEVICE_FUNC Derived& setOnes(Index rows, Index cols);
634
    EIGEN_DEVICE_FUNC Derived& setOnes(Index rows, Index cols);
635
    EIGEN_DEVICE_FUNC Derived& setOnes(NoChange_t, Index cols);
636
    EIGEN_DEVICE_FUNC Derived& setOnes(Index rows, NoChange_t);
631
637
632
    using Base::setRandom;
638
    using Base::setRandom;
633
    Derived& setRandom(Index size);
639
    Derived& setRandom(Index size);
634
    Derived& setRandom(Index rows, Index cols);
640
    Derived& setRandom(Index rows, Index cols);
641
    Derived& setRandom(NoChange_t, Index cols);
642
    Derived& setRandom(Index rows, NoChange_t);
635
643
636
    #ifdef EIGEN_PLAINOBJECTBASE_PLUGIN
644
    #ifdef EIGEN_PLAINOBJECTBASE_PLUGIN
637
    #include EIGEN_PLAINOBJECTBASE_PLUGIN
645
    #include EIGEN_PLAINOBJECTBASE_PLUGIN
638
    #endif
646
    #endif
639
647
640
  protected:
648
  protected:
641
    /** \internal Resizes *this in preparation for assigning \a other to it.
649
    /** \internal Resizes *this in preparation for assigning \a other to it.
642
      * Takes care of doing all the checking that's needed.
650
      * Takes care of doing all the checking that's needed.
(-)a/Eigen/src/Core/Random.h (+44 lines)
Lines 173-183 PlainObjectBase<Derived>::setRandom(Inde Link Here
173
template<typename Derived>
173
template<typename Derived>
174
EIGEN_STRONG_INLINE Derived&
174
EIGEN_STRONG_INLINE Derived&
175
PlainObjectBase<Derived>::setRandom(Index rows, Index cols)
175
PlainObjectBase<Derived>::setRandom(Index rows, Index cols)
176
{
176
{
177
  resize(rows, cols);
177
  resize(rows, cols);
178
  return setRandom();
178
  return setRandom();
179
}
179
}
180
180
181
/** Resizes to the given number of columns, and sets all coefficients in this expression to random values.
182
  *
183
  * Numbers are uniformly spread through their whole definition range for integer types,
184
  * and in the [-1:1] range for floating point scalar types.
185
  *
186
  * \not_reentrant
187
  *
188
  * \param cols the new number of columns
189
  *
190
  * Example: \include Matrix_setRandom_NoChange.cpp
191
  * Output: \verbinclude Matrix_setRandom_NoChange.out
192
  *
193
  * \sa DenseBase::setRandom(), setRandom(Index), class CwiseNullaryOp, DenseBase::Random()
194
  */
195
template<typename Derived>
196
EIGEN_STRONG_INLINE Derived&
197
PlainObjectBase<Derived>::setRandom(NoChange_t, Index cols)
198
{
199
  resize(rows(), cols);
200
  return setRandom();
201
}
202
203
/** Resizes to the given number of rows, and sets all coefficients in this expression to random values.
204
  *
205
  * Numbers are uniformly spread through their whole definition range for integer types,
206
  * and in the [-1:1] range for floating point scalar types.
207
  *
208
  * \not_reentrant
209
  *
210
  * \param rows the new number of rows
211
  *
212
  * Example: \include Matrix_setRandom_NoChange.cpp
213
  * Output: \verbinclude Matrix_setRandom_NoChange.out
214
  *
215
  * \sa DenseBase::setRandom(), setRandom(Index), class CwiseNullaryOp, DenseBase::Random()
216
  */
217
template<typename Derived>
218
EIGEN_STRONG_INLINE Derived&
219
PlainObjectBase<Derived>::setRandom(Index rows, NoChange_t)
220
{
221
  resize(rows, cols());
222
  return setRandom();
223
}
224
181
} // end namespace Eigen
225
} // end namespace Eigen
182
226
183
#endif // EIGEN_RANDOM_H
227
#endif // EIGEN_RANDOM_H
(-)a/doc/snippets/Matrix_setConstant_NoChange.cpp (+8 lines)
Line 0 Link Here
1
MatrixXf m;
2
m.resize(3,4);
3
m.setConstant(1);
4
cout << m << endl;
5
m.setConstant(5,NoChange, 2);
6
cout << m << endl;
7
m.setConstant(NoChange,5, 3);
8
cout << m << endl;
(-)a/doc/snippets/Matrix_setOnes_NoChange.cpp (+8 lines)
Line 0 Link Here
1
MatrixXf m;
2
m.resize(3,4);
3
m.setConstant(1);
4
cout << m << endl;
5
m.setOnes(5,NoChange);
6
cout << m << endl;
7
m.setOnes(NoChange,5);
8
cout << m << endl;
(-)a/doc/snippets/Matrix_setRandom_NoChange.cpp (+8 lines)
Line 0 Link Here
1
MatrixXf m;
2
m.resize(3,4);
3
m.setConstant(1);
4
cout << m << endl;
5
m.setRandom(5,NoChange);
6
cout << m << endl;
7
m.setRandom(NoChange,5);
8
cout << m << endl;
(-)a/doc/snippets/Matrix_setZero_NoChange.cpp (+8 lines)
Line 0 Link Here
1
MatrixXf m;
2
m.resize(3,4);
3
m.setConstant(1);
4
cout << m << endl;
5
m.setZero(5,NoChange);
6
cout << m << endl;
7
m.setZero(NoChange,5);
8
cout << m << endl;
(-)a/test/basicstuff.cpp (+60 lines)
Lines 249-264 void fixedSizeMatrixConstruction() Link Here
249
    VERIFY(m2(0) == DenseIndex(raw[0]));
249
    VERIFY(m2(0) == DenseIndex(raw[0]));
250
    VERIFY(a2(0) == DenseIndex(raw[0]));
250
    VERIFY(a2(0) == DenseIndex(raw[0]));
251
    VERIFY(m3(0) == int(raw[0]));
251
    VERIFY(m3(0) == int(raw[0]));
252
    VERIFY_IS_EQUAL(m,(Matrix<Scalar,1,1>(raw[0])));
252
    VERIFY_IS_EQUAL(m,(Matrix<Scalar,1,1>(raw[0])));
253
    VERIFY((a==Array<Scalar,1,1>(raw[0])).all());
253
    VERIFY((a==Array<Scalar,1,1>(raw[0])).all());
254
  }
254
  }
255
}
255
}
256
256
257
//#ifdef EIGEN_TEST_PART_1
258
void resizingNoChange()
259
{
260
  int rows = 3;
261
  int cols = 6;
262
  MatrixXf m(rows, cols);
263
  Array<float,Dynamic,Dynamic> a(rows, cols);
264
265
  // macro to generate tests for setZero, setOnes, SetRandom
266
#define setResizeTest( what )\
267
  cols = 7;\
268
  m.set##what(NoChange, cols);\
269
  a.set##what(NoChange, cols);\
270
  VERIFY(m.rows() == rows);\
271
  VERIFY(a.rows() == rows);\
272
  VERIFY(m.cols() == cols);\
273
  VERIFY(a.cols() == cols);\
274
  rows = 4;\
275
  m.set##what(rows, NoChange);\
276
  a.set##what(rows, NoChange);\
277
  VERIFY(m.cols() == cols);\
278
  VERIFY(a.cols() == cols);\
279
  VERIFY(m.rows() == rows);\
280
  VERIFY(a.rows() == rows);\
281
  rows = 3;\
282
  cols = 6;\
283
  m.resize(rows, cols);\
284
  a.resize(rows, cols);\
285
286
  setResizeTest(Zero);
287
  setResizeTest(Ones);
288
  setResizeTest(Random);
289
290
291
#undef setResizeTest
292
293
  // setConstant needs additional input
294
  cols = 7;\
295
  m.setConstant(NoChange, cols, 2);\
296
  a.setConstant(NoChange, cols, 2);\
297
  std::cout<<m << std::endl;\
298
  VERIFY(m.rows() == rows);\
299
  VERIFY(a.rows() == rows);\
300
  VERIFY(m.cols() == cols);\
301
  VERIFY(a.cols() == cols);\
302
  rows = 4;\
303
  m.setConstant(rows, NoChange, 3);\
304
  a.setConstant(rows, NoChange, 3);\
305
  VERIFY(m.cols() == cols);\
306
  VERIFY(a.cols() == cols);\
307
  VERIFY(m.rows() == rows);\
308
  VERIFY(a.rows() == rows);\
309
}
310
311
//#endif
312
313
257
void test_basicstuff()
314
void test_basicstuff()
258
{
315
{
259
  for(int i = 0; i < g_repeat; i++) {
316
  for(int i = 0; i < g_repeat; i++) {
260
    CALL_SUBTEST_1( basicStuff(Matrix<float, 1, 1>()) );
317
    CALL_SUBTEST_1( basicStuff(Matrix<float, 1, 1>()) );
261
    CALL_SUBTEST_2( basicStuff(Matrix4d()) );
318
    CALL_SUBTEST_2( basicStuff(Matrix4d()) );
262
    CALL_SUBTEST_3( basicStuff(MatrixXcf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
319
    CALL_SUBTEST_3( basicStuff(MatrixXcf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
263
    CALL_SUBTEST_4( basicStuff(MatrixXi(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
320
    CALL_SUBTEST_4( basicStuff(MatrixXi(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
264
    CALL_SUBTEST_5( basicStuff(MatrixXcd(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
321
    CALL_SUBTEST_5( basicStuff(MatrixXcd(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
Lines 272-280 void test_basicstuff() Link Here
272
  CALL_SUBTEST_1(fixedSizeMatrixConstruction<unsigned char>());
329
  CALL_SUBTEST_1(fixedSizeMatrixConstruction<unsigned char>());
273
  CALL_SUBTEST_1(fixedSizeMatrixConstruction<float>());
330
  CALL_SUBTEST_1(fixedSizeMatrixConstruction<float>());
274
  CALL_SUBTEST_1(fixedSizeMatrixConstruction<double>());
331
  CALL_SUBTEST_1(fixedSizeMatrixConstruction<double>());
275
  CALL_SUBTEST_1(fixedSizeMatrixConstruction<int>());
332
  CALL_SUBTEST_1(fixedSizeMatrixConstruction<int>());
276
  CALL_SUBTEST_1(fixedSizeMatrixConstruction<long int>());
333
  CALL_SUBTEST_1(fixedSizeMatrixConstruction<long int>());
277
  CALL_SUBTEST_1(fixedSizeMatrixConstruction<std::ptrdiff_t>());
334
  CALL_SUBTEST_1(fixedSizeMatrixConstruction<std::ptrdiff_t>());
278
335
279
  CALL_SUBTEST_2(casting());
336
  CALL_SUBTEST_2(casting());
337
338
  CALL_SUBTEST_1(resizingNoChange());
339
280
}
340
}

Return to bug 663