Debian Codegraph @

dune/istl/test/scaledidmatrixtest.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 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
4 // vi: set et ts=4 sw=2 sts=2:
5
6 // include this first to see whether it includes all necessary headers itself
7 #include <dune/istl/scaledidmatrix.hh>
8
9 #include <iostream>
10 #include <algorithm>
11
12 #include <dune/common/diagonalmatrix.hh>
13 #include <dune/common/fvector.hh>
14 #include <dune/common/exceptions.hh>
15 #include <dune/common/test/testsuite.hh>
16
17 using namespace Dune;
18
19
20 template<class K, int n>
21 TestSuite test_matrix()
22 {
23 TestSuite test("test_matrix");
24
25 ScaledIdentityMatrix<K,n> A(1);
26
27 // Test for proper matrix sizes, both statically and dynamically
28 static_assert(A.rows==n, "Incorrect number of rows");
29 static_assert(A.cols==n, "Incorrect number of columns");
30
31 test.check(A.rows==n) << "Incorrect number of rows";
32 test.check(A.cols==n) << "Incorrect number of columns";
33
34 test.check(A.rows()==n) << "Incorrect number of rows";
35 test.check(A.cols()==n) << "Incorrect number of columns";
36
37 FieldVector<K,n> f;
38 FieldVector<K,n> v;
39
40 // assign matrix
41 A=2;
42
43 // assign vector
44 f = 1;
45 v = 2;
46
47 // matrix vector product
48 A.umv(v,f);
49
50 // matrix times scalar
51 [[maybe_unused]] auto r1 = A * 23.;
52 [[maybe_unused]] auto r2 = 42. * A;
53
54 // test norms
55 A.frobenius_norm();
56 A.frobenius_norm2();
57 A.infinity_norm();
58 A.infinity_norm_real();
59
60 // print matrix
61 std::cout << A << std::endl;
62 // print vector
63 std::cout << f << std::endl;
64
65 // Construction of FieldMatrix from ScaledIdentityMatrix
66 FieldMatrix<K,n,n> AFM = FieldMatrix<K,n,n>(A);
67
68 // Test whether we can add a ScaledIdentityMatrix to a FieldMatrix,
69 // and vice versa
70 const auto sum = A + AFM + A;
71
72 // Is the sum correctly computed?
73 for (std::size_t i=0; i<sum.N(); ++i)
74 for (std::size_t j=0; j<sum.M(); ++j)
75 if (i==j)
76 test.check(sum[i][j] == A[i][j] + AFM[i][j] + A[i][j]) << "sum diagonal";
77 else // Off-diagonal entries of a ScaledIdentityMatrix cannot be accessed.
78 test.check(sum[i][j] == AFM[i][j]) << "sum off-diagonal";
79
80 // Construct a number type different from K
81 using OtherScalar = std::conditional_t<std::is_same_v<K,float>, double, float>;
82
83 // Construction of FieldMatrix from ScaledIdentityMatrix
84 auto AFMOtherScalar = FieldMatrix<OtherScalar,n,n>(A);
85
86 // Test whether we can add a ScaledIdentityMatrix to a FieldMatrix
87 // with a different number type, and vice versa
88 const auto sum2 = A + AFMOtherScalar + A;
89 AFMOtherScalar += A;
90
91 // Is the sum correctly computed?
92 for (std::size_t i=0; i<sum2.N(); ++i)
93 {
94 for (std::size_t j=0; j<sum2.M(); ++j)
95 if (i==j)
96 test.check(sum2[i][j] == A[i][j] + AFMOtherScalar[i][j]) << "sum2 diagonal";
97 else // Off-diagonal entries of a ScaledIdentityMatrix cannot be accessed.
98 test.check(sum2[i][j] == AFMOtherScalar[i][j]) << "sum2 off-diagonal";
99 }
100
101 // Construction of DiagonalMatrix from ScaledIdentityMatrix
102 auto ADMOtherScalar = DiagonalMatrix<OtherScalar,n>(42.0);
103
104 // Test whether we can add a ScaledIdentityMatrix to a DiagonalMatrix
105 // with a different number type, and vice versa
106 const auto sum3 = A + ADMOtherScalar + A;
107 ADMOtherScalar += A;
108
109 // Is the sum correctly computed?
110 for (std::size_t i=0; i<sum3.N(); ++i)
111 test.check(sum3[i][i] == A[i][i] + ADMOtherScalar[i][i]) << "sum3 diagonal";
112
113 return test;
114 }
115
116 int main()
117 {
118 TestSuite test("ScaledIdentityMatrix");
119
120 test.subTest(test_matrix<float, 1>());
121 test.subTest(test_matrix<double, 1>());
122 //test.subTest(test_matrix<int, 10>()); Does not compile with icc because there is no std::sqrt(int) std::fabs(int)
123 test.subTest(test_matrix<double, 5>());
124
125 return test.exit();
126 }