You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
4.0 KiB

  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2010-2011 Jitse Niesen <jitse@maths.leeds.ac.uk>
  5. //
  6. // This Source Code Form is subject to the terms of the Mozilla
  7. // Public License v. 2.0. If a copy of the MPL was not distributed
  8. // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. #include "main.h"
  10. template<typename MatrixType>
  11. bool equalsIdentity(const MatrixType& A)
  12. {
  13. typedef typename MatrixType::Index Index;
  14. typedef typename MatrixType::Scalar Scalar;
  15. Scalar zero = static_cast<Scalar>(0);
  16. bool offDiagOK = true;
  17. for (Index i = 0; i < A.rows(); ++i) {
  18. for (Index j = i+1; j < A.cols(); ++j) {
  19. offDiagOK = offDiagOK && (A(i,j) == zero);
  20. }
  21. }
  22. for (Index i = 0; i < A.rows(); ++i) {
  23. for (Index j = 0; j < (std::min)(i, A.cols()); ++j) {
  24. offDiagOK = offDiagOK && (A(i,j) == zero);
  25. }
  26. }
  27. bool diagOK = (A.diagonal().array() == 1).all();
  28. return offDiagOK && diagOK;
  29. }
  30. template<typename VectorType>
  31. void testVectorType(const VectorType& base)
  32. {
  33. typedef typename internal::traits<VectorType>::Index Index;
  34. typedef typename internal::traits<VectorType>::Scalar Scalar;
  35. const Index size = base.size();
  36. Scalar high = internal::random<Scalar>(-500,500);
  37. Scalar low = (size == 1 ? high : internal::random<Scalar>(-500,500));
  38. if (low>high) std::swap(low,high);
  39. const Scalar step = ((size == 1) ? 1 : (high-low)/(size-1));
  40. // check whether the result yields what we expect it to do
  41. VectorType m(base);
  42. m.setLinSpaced(size,low,high);
  43. VectorType n(size);
  44. for (int i=0; i<size; ++i)
  45. n(i) = low+i*step;
  46. VERIFY_IS_APPROX(m,n);
  47. // random access version
  48. m = VectorType::LinSpaced(size,low,high);
  49. VERIFY_IS_APPROX(m,n);
  50. // Assignment of a RowVectorXd to a MatrixXd (regression test for bug #79).
  51. VERIFY( (MatrixXd(RowVectorXd::LinSpaced(3, 0, 1)) - RowVector3d(0, 0.5, 1)).norm() < std::numeric_limits<Scalar>::epsilon() );
  52. // These guys sometimes fail! This is not good. Any ideas how to fix them!?
  53. //VERIFY( m(m.size()-1) == high );
  54. //VERIFY( m(0) == low );
  55. // sequential access version
  56. m = VectorType::LinSpaced(Sequential,size,low,high);
  57. VERIFY_IS_APPROX(m,n);
  58. // These guys sometimes fail! This is not good. Any ideas how to fix them!?
  59. //VERIFY( m(m.size()-1) == high );
  60. //VERIFY( m(0) == low );
  61. // check whether everything works with row and col major vectors
  62. Matrix<Scalar,Dynamic,1> row_vector(size);
  63. Matrix<Scalar,1,Dynamic> col_vector(size);
  64. row_vector.setLinSpaced(size,low,high);
  65. col_vector.setLinSpaced(size,low,high);
  66. VERIFY( row_vector.isApprox(col_vector.transpose(), NumTraits<Scalar>::epsilon()));
  67. Matrix<Scalar,Dynamic,1> size_changer(size+50);
  68. size_changer.setLinSpaced(size,low,high);
  69. VERIFY( size_changer.size() == size );
  70. typedef Matrix<Scalar,1,1> ScalarMatrix;
  71. ScalarMatrix scalar;
  72. scalar.setLinSpaced(1,low,high);
  73. VERIFY_IS_APPROX( scalar, ScalarMatrix::Constant(high) );
  74. VERIFY_IS_APPROX( ScalarMatrix::LinSpaced(1,low,high), ScalarMatrix::Constant(high) );
  75. }
  76. template<typename MatrixType>
  77. void testMatrixType(const MatrixType& m)
  78. {
  79. typedef typename MatrixType::Index Index;
  80. const Index rows = m.rows();
  81. const Index cols = m.cols();
  82. MatrixType A;
  83. A.setIdentity(rows, cols);
  84. VERIFY(equalsIdentity(A));
  85. VERIFY(equalsIdentity(MatrixType::Identity(rows, cols)));
  86. }
  87. void test_nullary()
  88. {
  89. CALL_SUBTEST_1( testMatrixType(Matrix2d()) );
  90. CALL_SUBTEST_2( testMatrixType(MatrixXcf(internal::random<int>(1,300),internal::random<int>(1,300))) );
  91. CALL_SUBTEST_3( testMatrixType(MatrixXf(internal::random<int>(1,300),internal::random<int>(1,300))) );
  92. for(int i = 0; i < g_repeat; i++) {
  93. CALL_SUBTEST_4( testVectorType(VectorXd(internal::random<int>(1,300))) );
  94. CALL_SUBTEST_5( testVectorType(Vector4d()) ); // regression test for bug 232
  95. CALL_SUBTEST_6( testVectorType(Vector3d()) );
  96. CALL_SUBTEST_7( testVectorType(VectorXf(internal::random<int>(1,300))) );
  97. CALL_SUBTEST_8( testVectorType(Vector3f()) );
  98. CALL_SUBTEST_8( testVectorType(Matrix<float,1,1>()) );
  99. }
  100. }