3655 |
13 Jul 17 |
peter |
// $Id$ |
3655 |
13 Jul 17 |
peter |
2 |
|
3655 |
13 Jul 17 |
peter |
3 |
/* |
4359 |
23 Aug 23 |
peter |
Copyright (C) 2017, 2021, 2023 Peter Johansson |
3655 |
13 Jul 17 |
peter |
5 |
|
3655 |
13 Jul 17 |
peter |
This file is part of the yat library, http://dev.thep.lu.se/yat |
3655 |
13 Jul 17 |
peter |
7 |
|
3655 |
13 Jul 17 |
peter |
The yat library is free software; you can redistribute it and/or |
3655 |
13 Jul 17 |
peter |
modify it under the terms of the GNU General Public License as |
3655 |
13 Jul 17 |
peter |
published by the Free Software Foundation; either version 3 of the |
3655 |
13 Jul 17 |
peter |
License, or (at your option) any later version. |
3655 |
13 Jul 17 |
peter |
12 |
|
3655 |
13 Jul 17 |
peter |
The yat library is distributed in the hope that it will be useful, |
3655 |
13 Jul 17 |
peter |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
3655 |
13 Jul 17 |
peter |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
3655 |
13 Jul 17 |
peter |
General Public License for more details. |
3655 |
13 Jul 17 |
peter |
17 |
|
3655 |
13 Jul 17 |
peter |
You should have received a copy of the GNU General Public License |
3655 |
13 Jul 17 |
peter |
along with yat. If not, see <http://www.gnu.org/licenses/>. |
3655 |
13 Jul 17 |
peter |
20 |
*/ |
3655 |
13 Jul 17 |
peter |
21 |
|
3655 |
13 Jul 17 |
peter |
22 |
#include <config.h> |
3655 |
13 Jul 17 |
peter |
23 |
|
3655 |
13 Jul 17 |
peter |
24 |
#include "Suite.h" |
3655 |
13 Jul 17 |
peter |
25 |
|
3655 |
13 Jul 17 |
peter |
26 |
#include "yat/utility/DiagonalMatrix.h" |
3655 |
13 Jul 17 |
peter |
27 |
#include "yat/utility/Matrix.h" |
3655 |
13 Jul 17 |
peter |
28 |
|
3660 |
14 Jul 17 |
peter |
29 |
#include <cmath> |
3660 |
14 Jul 17 |
peter |
30 |
|
3655 |
13 Jul 17 |
peter |
31 |
using namespace theplu::yat; |
3655 |
13 Jul 17 |
peter |
32 |
using namespace utility; |
3655 |
13 Jul 17 |
peter |
33 |
|
3655 |
13 Jul 17 |
peter |
34 |
bool check_vec(const Vector& lhs, const Vector& rhs, test::Suite& suite, |
3655 |
13 Jul 17 |
peter |
35 |
unsigned int N=1) |
3655 |
13 Jul 17 |
peter |
36 |
{ |
3655 |
13 Jul 17 |
peter |
37 |
if (lhs.size() != rhs.size()) { |
3655 |
13 Jul 17 |
peter |
38 |
suite.err() << "error: wrong dimensions: comparing " |
3655 |
13 Jul 17 |
peter |
39 |
<< lhs.size() << " with " |
3655 |
13 Jul 17 |
peter |
40 |
<< rhs.size() << "\n"; |
3655 |
13 Jul 17 |
peter |
41 |
suite.add(false); |
3655 |
13 Jul 17 |
peter |
42 |
return false; |
3655 |
13 Jul 17 |
peter |
43 |
} |
3655 |
13 Jul 17 |
peter |
44 |
bool result = true; |
3655 |
13 Jul 17 |
peter |
45 |
for (size_t i=0; i<lhs.size(); ++i) |
3655 |
13 Jul 17 |
peter |
46 |
if (!suite.equal(lhs(i), rhs(i), N)) { |
3655 |
13 Jul 17 |
peter |
47 |
suite.err() << "comparing element " << i << "\n"; |
3655 |
13 Jul 17 |
peter |
48 |
result = false; |
3655 |
13 Jul 17 |
peter |
49 |
} |
3655 |
13 Jul 17 |
peter |
50 |
suite.add(result); |
3655 |
13 Jul 17 |
peter |
51 |
return result; |
3655 |
13 Jul 17 |
peter |
52 |
} |
3655 |
13 Jul 17 |
peter |
53 |
|
3655 |
13 Jul 17 |
peter |
54 |
|
3655 |
13 Jul 17 |
peter |
55 |
template<class M1, class M2> |
3655 |
13 Jul 17 |
peter |
56 |
bool check(const M1& lhs, const M2& rhs, test::Suite& suite, |
3655 |
13 Jul 17 |
peter |
57 |
unsigned int N=1) |
3655 |
13 Jul 17 |
peter |
58 |
{ |
3655 |
13 Jul 17 |
peter |
59 |
if (lhs.rows() != rhs.rows() || lhs.columns() != rhs.columns()) { |
3655 |
13 Jul 17 |
peter |
60 |
suite.err() << "error: wrong dimensions: comparing " |
3655 |
13 Jul 17 |
peter |
61 |
<< lhs.rows() << " x " << lhs.columns() << " with " |
3655 |
13 Jul 17 |
peter |
62 |
<< rhs.rows() << " x " << rhs.columns() << "\n"; |
3655 |
13 Jul 17 |
peter |
63 |
suite.add(false); |
3655 |
13 Jul 17 |
peter |
64 |
return false; |
3655 |
13 Jul 17 |
peter |
65 |
} |
3655 |
13 Jul 17 |
peter |
66 |
bool result = true; |
3655 |
13 Jul 17 |
peter |
67 |
for (size_t row=0; row<lhs.rows(); ++row) |
3655 |
13 Jul 17 |
peter |
68 |
for (size_t col=0; col<lhs.columns(); ++col) { |
3655 |
13 Jul 17 |
peter |
69 |
if (!suite.equal(lhs(row, col), rhs(row, col), N)) { |
3655 |
13 Jul 17 |
peter |
70 |
suite.err() << "comparing row " << row << " column " << col << "\n"; |
3655 |
13 Jul 17 |
peter |
71 |
result = false; |
3655 |
13 Jul 17 |
peter |
72 |
} |
3655 |
13 Jul 17 |
peter |
73 |
} |
3655 |
13 Jul 17 |
peter |
74 |
suite.add(result); |
3655 |
13 Jul 17 |
peter |
75 |
return result; |
3655 |
13 Jul 17 |
peter |
76 |
} |
3655 |
13 Jul 17 |
peter |
77 |
|
3655 |
13 Jul 17 |
peter |
78 |
|
3655 |
13 Jul 17 |
peter |
79 |
int main(int argc, char* argv[]) |
3655 |
13 Jul 17 |
peter |
80 |
{ |
3655 |
13 Jul 17 |
peter |
81 |
test::Suite suite(argc, argv); |
3655 |
13 Jul 17 |
peter |
82 |
|
3655 |
13 Jul 17 |
peter |
// constructors |
3655 |
13 Jul 17 |
peter |
84 |
{ |
3655 |
13 Jul 17 |
peter |
85 |
DiagonalMatrix d1; |
3655 |
13 Jul 17 |
peter |
86 |
DiagonalMatrix d2(3,3,3.14); |
3655 |
13 Jul 17 |
peter |
87 |
Vector vec(3, 3.14); |
3655 |
13 Jul 17 |
peter |
88 |
DiagonalMatrix d3(vec); |
3655 |
13 Jul 17 |
peter |
89 |
check(d2, d3, suite); |
3655 |
13 Jul 17 |
peter |
90 |
} |
3655 |
13 Jul 17 |
peter |
91 |
|
4280 |
27 Jan 23 |
peter |
92 |
Matrix A = test::generate_Matrix(10, 10, 0); |
3655 |
13 Jul 17 |
peter |
93 |
DiagonalMatrix D1(A); |
3655 |
13 Jul 17 |
peter |
94 |
Matrix D2(A); |
3655 |
13 Jul 17 |
peter |
95 |
for (size_t i=0; i<D2.rows(); ++i) |
3655 |
13 Jul 17 |
peter |
96 |
for (size_t j=0; j<D2.columns(); ++j) |
3655 |
13 Jul 17 |
peter |
97 |
if (i!=j) |
3655 |
13 Jul 17 |
peter |
98 |
D2(i,j) = 0.0; |
3655 |
13 Jul 17 |
peter |
99 |
check(D1, D2, suite); |
3655 |
13 Jul 17 |
peter |
100 |
|
4280 |
27 Jan 23 |
peter |
101 |
Matrix B = test::generate_Matrix(A.rows(), A.columns(), 0.5); |
3655 |
13 Jul 17 |
peter |
102 |
|
3655 |
13 Jul 17 |
peter |
103 |
suite.out() << "DiagonalMatrix * DiagnoalMatrix\n"; |
3655 |
13 Jul 17 |
peter |
104 |
check(D1 * D1, D2 * D2, suite); |
4105 |
24 Sep 21 |
peter |
// test different dimension cases square x square; square x portrait etc |
4105 |
24 Sep 21 |
peter |
106 |
for (size_t i=1; i<4; ++i) |
4105 |
24 Sep 21 |
peter |
107 |
for (size_t j=1; j<4; ++j) { |
4105 |
24 Sep 21 |
peter |
108 |
DiagonalMatrix X1(i,j); |
4105 |
24 Sep 21 |
peter |
109 |
Matrix M1(i,j); |
4105 |
24 Sep 21 |
peter |
110 |
for (size_t k1=0; k1<X1.rows() && k1<X1.columns(); ++k1) { |
4105 |
24 Sep 21 |
peter |
111 |
X1(k1) = 10+k1; |
4105 |
24 Sep 21 |
peter |
112 |
M1(k1, k1) = X1(k1, k1); |
4105 |
24 Sep 21 |
peter |
113 |
} |
4105 |
24 Sep 21 |
peter |
114 |
for (size_t k=1; k<4; ++k) { |
4105 |
24 Sep 21 |
peter |
115 |
DiagonalMatrix X2(j,k); |
4105 |
24 Sep 21 |
peter |
116 |
Matrix M2(j,k); |
4105 |
24 Sep 21 |
peter |
117 |
for (size_t k2=0; k2<X2.rows() && k2<X2.columns(); ++k2) { |
4105 |
24 Sep 21 |
peter |
118 |
X2(k2) = 2+k2; |
4105 |
24 Sep 21 |
peter |
119 |
M2(k2, k2) = X2(k2, k2); |
4105 |
24 Sep 21 |
peter |
120 |
} |
4105 |
24 Sep 21 |
peter |
121 |
check(X1*X2, M1*M2, suite); |
4105 |
24 Sep 21 |
peter |
122 |
} |
4105 |
24 Sep 21 |
peter |
123 |
} |
3655 |
13 Jul 17 |
peter |
124 |
suite.out() << "DiagonalMatrix * Matrix\n"; |
3655 |
13 Jul 17 |
peter |
125 |
check(D1 * B, D2 * B, suite); |
3655 |
13 Jul 17 |
peter |
126 |
|
3655 |
13 Jul 17 |
peter |
127 |
suite.out() << "Matrix * DiagonalMatrix\n"; |
3655 |
13 Jul 17 |
peter |
128 |
check(D1 * B, D2 * B, suite); |
3655 |
13 Jul 17 |
peter |
129 |
|
3655 |
13 Jul 17 |
peter |
130 |
suite.out() << "Matrix + DiagonalMatrix\n"; |
3655 |
13 Jul 17 |
peter |
131 |
check(B + D1, B + D2, suite); |
3655 |
13 Jul 17 |
peter |
132 |
|
3655 |
13 Jul 17 |
peter |
133 |
suite.out() << "Matrix - DiagonalMatrix\n"; |
3655 |
13 Jul 17 |
peter |
134 |
check(B - D1, B - D2, suite); |
3655 |
13 Jul 17 |
peter |
135 |
|
3655 |
13 Jul 17 |
peter |
136 |
suite.out() << "DiagonalMatrix + Matrix\n"; |
3655 |
13 Jul 17 |
peter |
137 |
check(D1 + B, D2 + B, suite); |
3655 |
13 Jul 17 |
peter |
138 |
|
3655 |
13 Jul 17 |
peter |
139 |
suite.out() << "DiagonalMatrix - Matrix\n"; |
3655 |
13 Jul 17 |
peter |
140 |
check(D1 - B, D2 - B, suite); |
3655 |
13 Jul 17 |
peter |
141 |
|
3655 |
13 Jul 17 |
peter |
142 |
Vector v(B.rows()); |
3655 |
13 Jul 17 |
peter |
143 |
for (size_t i=0; i<v.size(); ++i) |
3655 |
13 Jul 17 |
peter |
144 |
v(i) = B(i,i); |
3655 |
13 Jul 17 |
peter |
145 |
|
3655 |
13 Jul 17 |
peter |
146 |
check_vec(D1 * v, D2 * v, suite); |
3655 |
13 Jul 17 |
peter |
147 |
check_vec(v * D1, v * D2, suite); |
3655 |
13 Jul 17 |
peter |
148 |
|
3655 |
13 Jul 17 |
peter |
149 |
return suite.return_value(); |
3655 |
13 Jul 17 |
peter |
150 |
} |