G+Smo  25.01.0
Geometry + Simulation Modules
 
Loading...
Searching...
No Matches
CheckMatrix.h
1
14#ifndef COMMONS_CHECKMATRIX_H
15#define COMMONS_CHECKMATRIX_H
16
17namespace UnitTest {
18
23template< class Derived >
24void CheckMatrixClose(TestResults& results, gsEigen::MatrixBase<Derived> const& expected, gsEigen::MatrixBase<Derived> const& actual,
25 typename gsEigen::MatrixBase<Derived>::Scalar const& tolerance, TestDetails const& details)
26{
27 bool equal = true;
28 const index_t rows = actual.rows();
29 const index_t cols = actual.cols();
30
31 equal &= (expected.rows() == rows);
32 equal &= (expected.cols() == cols);
33 for (index_t i=0; equal && i<rows; ++i)
34 {
35 for (index_t j=0; j<cols; ++j)
36 {
37 equal &= (gismo::math::abs( expected(i, j) - actual(i, j) )<= tolerance);
38 }
39 }
40
41 if (!equal)
42 {
43 UnitTest::MemoryOutStream stream;
44
45 if (expected.rows() != rows) {
46 stream << "Expected rows=" << expected.rows() << " but was " << rows <<"!";
47 results.OnTestFailure(details, stream.GetText());
48 return;
49 }
50 if (expected.cols() != cols) {
51 stream << "Expected cols=" << expected.cols() << " but was " << cols <<"!";
52 results.OnTestFailure(details, stream.GetText());
53 return;
54 }
55
56 stream << "Expected [ ";
57
58 for (int expectedRow = 0; expectedRow < rows; ++expectedRow)
59 {
60 stream << "[ ";
61 for (int expectedColumn = 0; expectedColumn < cols; ++expectedColumn)
62 stream << expected(expectedRow, expectedColumn) << " ";
63 stream << "] ";
64 }
65
66 stream << "] +/- " << tolerance << " but was [ ";
67
68 for (int actualRow = 0; actualRow < rows; ++actualRow)
69 {
70 stream << "[ ";
71 for (int actualColumn = 0; actualColumn < cols; ++actualColumn)
72 stream << actual(actualRow,actualColumn) << " ";
73 stream << "] ";
74 }
75
76 stream << "]";
77
78 results.OnTestFailure(details, stream.GetText());
79 }
80}
81
82
83template< typename T >
84void CheckMatrixPartitionOfUnitClose(TestResults& results, gismo::gsMatrix<T> const& actual,
85 T const& tolerance, TestDetails const& details)
86{
87 bool result = true;
88 const index_t cols = actual.cols();
89
90 for (index_t j=0; j<cols; ++j)
91 {
92 const real_t sum = actual.col(j).sum();
93 result &= (gismo::math::abs( sum - 1.0) <= tolerance);
94 }
95
96
97 if (!result)
98 {
99 UnitTest::MemoryOutStream stream;
100
101 stream << "expected for all columns that sum='" << 1.0 << "' +/- '" << tolerance<< "', but was [";
102
103 for (index_t j=0; j<cols; ++j)
104 {
105 const real_t sum = actual.col(j).sum();
106 stream << "[expected='" << 1.0 << ", actual.cols('";
107 stream << j << "').sum()='" << sum << "'']";
108 }
109
110 stream << "]";
111
112 results.OnTestFailure(details, stream.GetText());
113 }
114}
115
116
117}
118
119#endif // COMMONS_CHECKMATRIX_H
120
A matrix with arbitrary coefficient type and fixed or dynamic size.
Definition gsMatrix.h:41
#define index_t
Definition gsConfig.h:32