Debian Codegraph @

dune/istl/test/matrixnormtest.cc

raw
1 // SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file LICENSE.md in module root
2 // SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
3 #include <complex>
4 #include <memory>
5
6 #include <dune/common/fmatrix.hh>
7 #include <dune/common/classname.hh>
8 #include <dune/istl/matrix.hh>
9
10 template <class V>
11 void checkNormNANVector(V const &v, int line) {
12 if (!std::isnan(v.infinity_norm())) {
13 std::cerr << "error: norm not NaN: infinity_norm() on line " << line
14 << " (type: " << Dune::className(v[0]) << ")" << std::endl;
15 std::exit(-1);
16 }
17 }
18
19 template <class M>
20 void checkNormNANMatrix(M const &v, int line) {
21 if (!std::isnan(v.frobenius_norm())) {
22 std::cerr << "error: norm not NaN: frobenius_norm() on line " << line
23 << " (type: " << Dune::className(v[0][0]) << ")" << std::endl;
24 std::exit(-1);
25 }
26 if (!std::isnan(v.infinity_norm())) {
27 std::cerr << "error: norm not NaN: infinity_norm() on line " << line
28 << " (type: " << Dune::className(v[0][0]) << ")" << std::endl;
29 std::exit(-1);
30 }
31 }
32
33 // Make sure that matrices with NaN entries have norm NaN.
34 // See also bug flyspray/FS#1147
35 template <typename T>
36 void test_nan(T const &mynan) {
37 using M = Dune::Matrix<Dune::FieldMatrix<T, 2, 2>>;
38 T n(0);
39 {
40 M m(2, 2);
41 m[0][0] = {{n, n}, {n, mynan}};
42 m[0][1] = n;
43 m[1][0] = n;
44 m[1][1] = n;
45 checkNormNANVector(m[0], __LINE__);
46 checkNormNANMatrix(m, __LINE__);
47 }
48 {
49 M m(2, 2);
50 m[0][0] = n;
51 m[0][1] = {{n, n}, {n, mynan}};
52 m[1][0] = n;
53 m[1][1] = n;
54 checkNormNANVector(m[0], __LINE__);
55 checkNormNANMatrix(m, __LINE__);
56 }
57 {
58 M m(2, 2);
59 m[0][0] = n;
60 m[0][1] = n;
61 m[1][0] = {{n, n}, {n, mynan}};
62 m[1][1] = n;
63 checkNormNANVector(m[1], __LINE__);
64 checkNormNANMatrix(m, __LINE__);
65 }
66 }
67
68 int main() {
69 {
70 double nan = std::nan("");
71 test_nan(nan);
72 }
73 {
74 std::complex<double> nan(std::nan(""), 17);
75 test_nan(nan);
76 }
77 }