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.

177 lines
7.1 KiB

  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2009-2014 Gael Guennebaud <gael.guennebaud@inria.fr>
  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 T> EIGEN_DONT_INLINE T copy(const T& x)
  11. {
  12. return x;
  13. }
  14. template<typename MatrixType> void stable_norm(const MatrixType& m)
  15. {
  16. /* this test covers the following files:
  17. StableNorm.h
  18. */
  19. using std::sqrt;
  20. using std::abs;
  21. typedef typename MatrixType::Index Index;
  22. typedef typename MatrixType::Scalar Scalar;
  23. typedef typename NumTraits<Scalar>::Real RealScalar;
  24. bool complex_real_product_ok = true;
  25. // Check the basic machine-dependent constants.
  26. {
  27. int ibeta, it, iemin, iemax;
  28. ibeta = std::numeric_limits<RealScalar>::radix; // base for floating-point numbers
  29. it = std::numeric_limits<RealScalar>::digits; // number of base-beta digits in mantissa
  30. iemin = std::numeric_limits<RealScalar>::min_exponent; // minimum exponent
  31. iemax = std::numeric_limits<RealScalar>::max_exponent; // maximum exponent
  32. VERIFY( (!(iemin > 1 - 2*it || 1+it>iemax || (it==2 && ibeta<5) || (it<=4 && ibeta <= 3 ) || it<2))
  33. && "the stable norm algorithm cannot be guaranteed on this computer");
  34. Scalar inf = std::numeric_limits<RealScalar>::infinity();
  35. if(NumTraits<Scalar>::IsComplex && (numext::isnan)(inf*RealScalar(1)) )
  36. {
  37. complex_real_product_ok = false;
  38. static bool first = true;
  39. if(first)
  40. std::cerr << "WARNING: compiler mess up complex*real product, " << inf << " * " << 1.0 << " = " << inf*RealScalar(1) << std::endl;
  41. first = false;
  42. }
  43. }
  44. Index rows = m.rows();
  45. Index cols = m.cols();
  46. // get a non-zero random factor
  47. Scalar factor = internal::random<Scalar>();
  48. while(numext::abs2(factor)<RealScalar(1e-4))
  49. factor = internal::random<Scalar>();
  50. Scalar big = factor * ((std::numeric_limits<RealScalar>::max)() * RealScalar(1e-4));
  51. factor = internal::random<Scalar>();
  52. while(numext::abs2(factor)<RealScalar(1e-4))
  53. factor = internal::random<Scalar>();
  54. Scalar small = factor * ((std::numeric_limits<RealScalar>::min)() * RealScalar(1e4));
  55. MatrixType vzero = MatrixType::Zero(rows, cols),
  56. vrand = MatrixType::Random(rows, cols),
  57. vbig(rows, cols),
  58. vsmall(rows,cols);
  59. vbig.fill(big);
  60. vsmall.fill(small);
  61. VERIFY_IS_MUCH_SMALLER_THAN(vzero.norm(), static_cast<RealScalar>(1));
  62. VERIFY_IS_APPROX(vrand.stableNorm(), vrand.norm());
  63. VERIFY_IS_APPROX(vrand.blueNorm(), vrand.norm());
  64. VERIFY_IS_APPROX(vrand.hypotNorm(), vrand.norm());
  65. RealScalar size = static_cast<RealScalar>(m.size());
  66. // test numext::isfinite
  67. VERIFY(!(numext::isfinite)( std::numeric_limits<RealScalar>::infinity()));
  68. VERIFY(!(numext::isfinite)(sqrt(-abs(big))));
  69. // test overflow
  70. VERIFY((numext::isfinite)(sqrt(size)*abs(big)));
  71. VERIFY_IS_NOT_APPROX(sqrt(copy(vbig.squaredNorm())), abs(sqrt(size)*big)); // here the default norm must fail
  72. VERIFY_IS_APPROX(vbig.stableNorm(), sqrt(size)*abs(big));
  73. VERIFY_IS_APPROX(vbig.blueNorm(), sqrt(size)*abs(big));
  74. VERIFY_IS_APPROX(vbig.hypotNorm(), sqrt(size)*abs(big));
  75. // test underflow
  76. VERIFY((numext::isfinite)(sqrt(size)*abs(small)));
  77. VERIFY_IS_NOT_APPROX(sqrt(copy(vsmall.squaredNorm())), abs(sqrt(size)*small)); // here the default norm must fail
  78. VERIFY_IS_APPROX(vsmall.stableNorm(), sqrt(size)*abs(small));
  79. VERIFY_IS_APPROX(vsmall.blueNorm(), sqrt(size)*abs(small));
  80. VERIFY_IS_APPROX(vsmall.hypotNorm(), sqrt(size)*abs(small));
  81. // Test compilation of cwise() version
  82. VERIFY_IS_APPROX(vrand.colwise().stableNorm(), vrand.colwise().norm());
  83. VERIFY_IS_APPROX(vrand.colwise().blueNorm(), vrand.colwise().norm());
  84. VERIFY_IS_APPROX(vrand.colwise().hypotNorm(), vrand.colwise().norm());
  85. VERIFY_IS_APPROX(vrand.rowwise().stableNorm(), vrand.rowwise().norm());
  86. VERIFY_IS_APPROX(vrand.rowwise().blueNorm(), vrand.rowwise().norm());
  87. VERIFY_IS_APPROX(vrand.rowwise().hypotNorm(), vrand.rowwise().norm());
  88. // test NaN, +inf, -inf
  89. MatrixType v;
  90. Index i = internal::random<Index>(0,rows-1);
  91. Index j = internal::random<Index>(0,cols-1);
  92. // NaN
  93. {
  94. v = vrand;
  95. v(i,j) = std::numeric_limits<RealScalar>::quiet_NaN();
  96. VERIFY(!(numext::isfinite)(v.squaredNorm())); VERIFY((numext::isnan)(v.squaredNorm()));
  97. VERIFY(!(numext::isfinite)(v.norm())); VERIFY((numext::isnan)(v.norm()));
  98. VERIFY(!(numext::isfinite)(v.stableNorm())); VERIFY((numext::isnan)(v.stableNorm()));
  99. VERIFY(!(numext::isfinite)(v.blueNorm())); VERIFY((numext::isnan)(v.blueNorm()));
  100. VERIFY(!(numext::isfinite)(v.hypotNorm())); VERIFY((numext::isnan)(v.hypotNorm()));
  101. }
  102. // +inf
  103. {
  104. v = vrand;
  105. v(i,j) = std::numeric_limits<RealScalar>::infinity();
  106. VERIFY(!(numext::isfinite)(v.squaredNorm())); VERIFY(isPlusInf(v.squaredNorm()));
  107. VERIFY(!(numext::isfinite)(v.norm())); VERIFY(isPlusInf(v.norm()));
  108. VERIFY(!(numext::isfinite)(v.stableNorm()));
  109. if(complex_real_product_ok){
  110. VERIFY(isPlusInf(v.stableNorm()));
  111. }
  112. VERIFY(!(numext::isfinite)(v.blueNorm())); VERIFY(isPlusInf(v.blueNorm()));
  113. VERIFY(!(numext::isfinite)(v.hypotNorm())); VERIFY(isPlusInf(v.hypotNorm()));
  114. }
  115. // -inf
  116. {
  117. v = vrand;
  118. v(i,j) = -std::numeric_limits<RealScalar>::infinity();
  119. VERIFY(!(numext::isfinite)(v.squaredNorm())); VERIFY(isPlusInf(v.squaredNorm()));
  120. VERIFY(!(numext::isfinite)(v.norm())); VERIFY(isPlusInf(v.norm()));
  121. VERIFY(!(numext::isfinite)(v.stableNorm()));
  122. if(complex_real_product_ok) {
  123. VERIFY(isPlusInf(v.stableNorm()));
  124. }
  125. VERIFY(!(numext::isfinite)(v.blueNorm())); VERIFY(isPlusInf(v.blueNorm()));
  126. VERIFY(!(numext::isfinite)(v.hypotNorm())); VERIFY(isPlusInf(v.hypotNorm()));
  127. }
  128. // mix
  129. {
  130. Index i2 = internal::random<Index>(0,rows-1);
  131. Index j2 = internal::random<Index>(0,cols-1);
  132. v = vrand;
  133. v(i,j) = -std::numeric_limits<RealScalar>::infinity();
  134. v(i2,j2) = std::numeric_limits<RealScalar>::quiet_NaN();
  135. VERIFY(!(numext::isfinite)(v.squaredNorm())); VERIFY((numext::isnan)(v.squaredNorm()));
  136. VERIFY(!(numext::isfinite)(v.norm())); VERIFY((numext::isnan)(v.norm()));
  137. VERIFY(!(numext::isfinite)(v.stableNorm())); VERIFY((numext::isnan)(v.stableNorm()));
  138. VERIFY(!(numext::isfinite)(v.blueNorm())); VERIFY((numext::isnan)(v.blueNorm()));
  139. VERIFY(!(numext::isfinite)(v.hypotNorm())); VERIFY((numext::isnan)(v.hypotNorm()));
  140. }
  141. }
  142. void test_stable_norm()
  143. {
  144. for(int i = 0; i < g_repeat; i++) {
  145. CALL_SUBTEST_1( stable_norm(Matrix<float, 1, 1>()) );
  146. CALL_SUBTEST_2( stable_norm(Vector4d()) );
  147. CALL_SUBTEST_3( stable_norm(VectorXd(internal::random<int>(10,2000))) );
  148. CALL_SUBTEST_4( stable_norm(VectorXf(internal::random<int>(10,2000))) );
  149. CALL_SUBTEST_5( stable_norm(VectorXcd(internal::random<int>(10,2000))) );
  150. }
  151. }