1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2009 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
10#include "main.h"
11
12template<typename T> bool isNotNaN(const T& x)
13{
14  return x==x;
15}
16
17// workaround aggressive optimization in ICC
18template<typename T> EIGEN_DONT_INLINE  T sub(T a, T b) { return a - b; }
19
20template<typename T> bool isFinite(const T& x)
21{
22  return isNotNaN(sub(x,x));
23}
24
25template<typename T> EIGEN_DONT_INLINE T copy(const T& x)
26{
27  return x;
28}
29
30template<typename MatrixType> void stable_norm(const MatrixType& m)
31{
32  /* this test covers the following files:
33     StableNorm.h
34  */
35  using std::sqrt;
36  using std::abs;
37  typedef typename MatrixType::Index Index;
38  typedef typename MatrixType::Scalar Scalar;
39  typedef typename NumTraits<Scalar>::Real RealScalar;
40
41  // Check the basic machine-dependent constants.
42  {
43    int ibeta, it, iemin, iemax;
44
45    ibeta = std::numeric_limits<RealScalar>::radix;         // base for floating-point numbers
46    it    = std::numeric_limits<RealScalar>::digits;        // number of base-beta digits in mantissa
47    iemin = std::numeric_limits<RealScalar>::min_exponent;  // minimum exponent
48    iemax = std::numeric_limits<RealScalar>::max_exponent;  // maximum exponent
49
50    VERIFY( (!(iemin > 1 - 2*it || 1+it>iemax || (it==2 && ibeta<5) || (it<=4 && ibeta <= 3 ) || it<2))
51           && "the stable norm algorithm cannot be guaranteed on this computer");
52  }
53
54
55  Index rows = m.rows();
56  Index cols = m.cols();
57
58  // get a non-zero random factor
59  Scalar factor = internal::random<Scalar>();
60  while(numext::abs2(factor)<RealScalar(1e-4))
61    factor = internal::random<Scalar>();
62  Scalar big = factor * ((std::numeric_limits<RealScalar>::max)() * RealScalar(1e-4));
63
64  factor = internal::random<Scalar>();
65  while(numext::abs2(factor)<RealScalar(1e-4))
66    factor = internal::random<Scalar>();
67  Scalar small = factor * ((std::numeric_limits<RealScalar>::min)() * RealScalar(1e4));
68
69  MatrixType  vzero = MatrixType::Zero(rows, cols),
70              vrand = MatrixType::Random(rows, cols),
71              vbig(rows, cols),
72              vsmall(rows,cols);
73
74  vbig.fill(big);
75  vsmall.fill(small);
76
77  VERIFY_IS_MUCH_SMALLER_THAN(vzero.norm(), static_cast<RealScalar>(1));
78  VERIFY_IS_APPROX(vrand.stableNorm(),      vrand.norm());
79  VERIFY_IS_APPROX(vrand.blueNorm(),        vrand.norm());
80  VERIFY_IS_APPROX(vrand.hypotNorm(),       vrand.norm());
81
82  RealScalar size = static_cast<RealScalar>(m.size());
83
84  // test isFinite
85  VERIFY(!isFinite( std::numeric_limits<RealScalar>::infinity()));
86  VERIFY(!isFinite(sqrt(-abs(big))));
87
88  // test overflow
89  VERIFY(isFinite(sqrt(size)*abs(big)));
90  VERIFY_IS_NOT_APPROX(sqrt(copy(vbig.squaredNorm())), abs(sqrt(size)*big)); // here the default norm must fail
91  VERIFY_IS_APPROX(vbig.stableNorm(), sqrt(size)*abs(big));
92  VERIFY_IS_APPROX(vbig.blueNorm(),   sqrt(size)*abs(big));
93  VERIFY_IS_APPROX(vbig.hypotNorm(),  sqrt(size)*abs(big));
94
95  // test underflow
96  VERIFY(isFinite(sqrt(size)*abs(small)));
97  VERIFY_IS_NOT_APPROX(sqrt(copy(vsmall.squaredNorm())),   abs(sqrt(size)*small)); // here the default norm must fail
98  VERIFY_IS_APPROX(vsmall.stableNorm(), sqrt(size)*abs(small));
99  VERIFY_IS_APPROX(vsmall.blueNorm(),   sqrt(size)*abs(small));
100  VERIFY_IS_APPROX(vsmall.hypotNorm(),  sqrt(size)*abs(small));
101
102  // Test compilation of cwise() version
103  VERIFY_IS_APPROX(vrand.colwise().stableNorm(),      vrand.colwise().norm());
104  VERIFY_IS_APPROX(vrand.colwise().blueNorm(),        vrand.colwise().norm());
105  VERIFY_IS_APPROX(vrand.colwise().hypotNorm(),       vrand.colwise().norm());
106  VERIFY_IS_APPROX(vrand.rowwise().stableNorm(),      vrand.rowwise().norm());
107  VERIFY_IS_APPROX(vrand.rowwise().blueNorm(),        vrand.rowwise().norm());
108  VERIFY_IS_APPROX(vrand.rowwise().hypotNorm(),       vrand.rowwise().norm());
109}
110
111void test_stable_norm()
112{
113  for(int i = 0; i < g_repeat; i++) {
114    CALL_SUBTEST_1( stable_norm(Matrix<float, 1, 1>()) );
115    CALL_SUBTEST_2( stable_norm(Vector4d()) );
116    CALL_SUBTEST_3( stable_norm(VectorXd(internal::random<int>(10,2000))) );
117    CALL_SUBTEST_4( stable_norm(VectorXf(internal::random<int>(10,2000))) );
118    CALL_SUBTEST_5( stable_norm(VectorXcd(internal::random<int>(10,2000))) );
119  }
120}
121