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.

71 lines
2.2 KiB

  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra. Eigen itself is part of the KDE project.
  3. //
  4. // Copyright (C) 2008 Benoit Jacob <jacob.benoit.1@gmail.com>
  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> void matrixSum(const MatrixType& m)
  11. {
  12. typedef typename MatrixType::Scalar Scalar;
  13. int rows = m.rows();
  14. int cols = m.cols();
  15. MatrixType m1 = MatrixType::Random(rows, cols);
  16. VERIFY_IS_MUCH_SMALLER_THAN(MatrixType::Zero(rows, cols).sum(), Scalar(1));
  17. VERIFY_IS_APPROX(MatrixType::Ones(rows, cols).sum(), Scalar(float(rows*cols))); // the float() here to shut up excessive MSVC warning about int->complex conversion being lossy
  18. Scalar x = Scalar(0);
  19. for(int i = 0; i < rows; i++) for(int j = 0; j < cols; j++) x += m1(i,j);
  20. VERIFY_IS_APPROX(m1.sum(), x);
  21. }
  22. template<typename VectorType> void vectorSum(const VectorType& w)
  23. {
  24. typedef typename VectorType::Scalar Scalar;
  25. int size = w.size();
  26. VectorType v = VectorType::Random(size);
  27. for(int i = 1; i < size; i++)
  28. {
  29. Scalar s = Scalar(0);
  30. for(int j = 0; j < i; j++) s += v[j];
  31. VERIFY_IS_APPROX(s, v.start(i).sum());
  32. }
  33. for(int i = 0; i < size-1; i++)
  34. {
  35. Scalar s = Scalar(0);
  36. for(int j = i; j < size; j++) s += v[j];
  37. VERIFY_IS_APPROX(s, v.end(size-i).sum());
  38. }
  39. for(int i = 0; i < size/2; i++)
  40. {
  41. Scalar s = Scalar(0);
  42. for(int j = i; j < size-i; j++) s += v[j];
  43. VERIFY_IS_APPROX(s, v.segment(i, size-2*i).sum());
  44. }
  45. }
  46. void test_eigen2_sum()
  47. {
  48. for(int i = 0; i < g_repeat; i++) {
  49. CALL_SUBTEST_1( matrixSum(Matrix<float, 1, 1>()) );
  50. CALL_SUBTEST_2( matrixSum(Matrix2f()) );
  51. CALL_SUBTEST_3( matrixSum(Matrix4d()) );
  52. CALL_SUBTEST_4( matrixSum(MatrixXcf(3, 3)) );
  53. CALL_SUBTEST_5( matrixSum(MatrixXf(8, 12)) );
  54. CALL_SUBTEST_6( matrixSum(MatrixXi(8, 12)) );
  55. }
  56. for(int i = 0; i < g_repeat; i++) {
  57. CALL_SUBTEST_5( vectorSum(VectorXf(5)) );
  58. CALL_SUBTEST_7( vectorSum(VectorXd(10)) );
  59. CALL_SUBTEST_5( vectorSum(VectorXf(33)) );
  60. }
  61. }