]> www.ginac.de Git - ginac.git/blob - check/exam_matrices.cpp
Add simple exam test for releational objects.
[ginac.git] / check / exam_matrices.cpp
1
2 /** @file exam_matrices.cpp
3  *
4  *  Here we examine manipulations on GiNaC's symbolic matrices. */
5
6 /*
7  *  GiNaC Copyright (C) 1999-2021 Johannes Gutenberg University Mainz, Germany
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 2 of the License, or
12  *  (at your option) any later version.
13  *
14  *  This program is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *  GNU General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License
20  *  along with this program; if not, write to the Free Software
21  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22  */
23
24 #include "ginac.h"
25 using namespace GiNaC;
26
27 #include <iostream>
28 #include <stdexcept>
29 using namespace std;
30
31 static unsigned matrix_determinants()
32 {
33         unsigned result = 0;
34         ex det;
35         matrix m1(1,1), m2(2,2), m3(3,3), m4(4,4);
36         symbol a("a"), b("b"), c("c");
37         symbol d("d"), e("e"), f("f");
38         symbol g("g"), h("h"), i("i");
39         
40         // check symbolic trivial matrix determinant
41         m1 = matrix{{a}};
42         det = m1.determinant();
43         if (det != a) {
44                 clog << "determinant of 1x1 matrix " << m1
45                      << " erroneously returned " << det << endl;
46                 ++result;
47         }
48         
49         // check generic dense symbolic 2x2 matrix determinant
50         m2 = matrix{{a, b},
51                     {c, d}};
52         det = m2.determinant();
53         if (det != (a*d-b*c)) {
54                 clog << "determinant of 2x2 matrix " << m2
55                      << " erroneously returned " << det << endl;
56                 ++result;
57         }
58         
59         // check generic dense symbolic 3x3 matrix determinant
60         m3 = matrix{{a, b, c},
61                     {d, e, f},
62                     {g, h, i}};
63         det = m3.determinant();
64         if (det != (a*e*i - a*f*h - d*b*i + d*c*h + g*b*f - g*c*e)) {
65                 clog << "determinant of 3x3 matrix " << m3
66                      << " erroneously returned " << det << endl;
67                 ++result;
68         }
69         
70         // check dense numeric 3x3 matrix determinant
71         m3 = matrix{{0, -1,  3},
72                     {3, -2,  2},
73                     {3,  4, -2}};
74         det = m3.determinant();
75         if (det != 42) {
76                 clog << "determinant of 3x3 matrix " << m3
77                      << " erroneously returned " << det << endl;
78                 ++result;
79         }
80         
81         // check dense symbolic 2x2 matrix determinant
82         m2 = matrix{{a/(a-b), 1},
83                     {b/(a-b), 1}};
84         det = m2.determinant();
85         if (det != 1) {
86                 if (det.normal() == 1)  // only half wrong
87                         clog << "determinant of 2x2 matrix " << m2
88                              << " was returned unnormalized as " << det << endl;
89                 else  // totally wrong
90                         clog << "determinant of 2x2 matrix " << m2
91                              << " erroneously returned " << det << endl;
92                 ++result;
93         }
94         
95         // check sparse symbolic 4x4 matrix determinant
96         m4.set(0,1,a).set(1,0,b).set(3,2,c).set(2,3,d);
97         det = m4.determinant();
98         if (det != a*b*c*d) {
99                 clog << "determinant of 4x4 matrix " << m4
100                      << " erroneously returned " << det << endl;
101                 ++result;
102         }
103         
104         // check characteristic polynomial
105         m3 = matrix{{a, -2,   2},
106                     {3, a-1,  2},
107                     {3,  4,  a-3}};
108         ex p = m3.charpoly(a);
109         if (p != 0) {
110                 clog << "charpoly of 3x3 matrix " << m3
111                      << " erroneously returned " << p << endl;
112                 ++result;
113         }
114         
115         return result;
116 }
117
118 static unsigned matrix_invert1()
119 {
120         unsigned result = 0;
121         matrix m(1,1);
122         symbol a("a");
123         
124         m.set(0,0,a);
125         matrix m_i = m.inverse();
126         
127         if (m_i(0,0) != pow(a,-1)) {
128                 clog << "inversion of 1x1 matrix " << m
129                      << " erroneously returned " << m_i << endl;
130                 ++result;
131         }
132         
133         return result;
134 }
135
136 static unsigned matrix_invert2()
137 {
138         unsigned result = 0;
139         symbol a("a"), b("b"), c("c"), d("d");
140         matrix m = {{a, b},
141                     {c, d}};
142         matrix m_i = m.inverse();
143         ex det = m.determinant();
144         
145         if ((normal(m_i(0,0)*det) != d) ||
146             (normal(m_i(0,1)*det) != -b) ||
147             (normal(m_i(1,0)*det) != -c) ||
148             (normal(m_i(1,1)*det) != a)) {
149                 clog << "inversion of 2x2 matrix " << m
150                      << " erroneously returned " << m_i << endl;
151                 ++result;
152         }
153         
154         return result;
155 }
156
157 static unsigned matrix_invert3()
158 {
159         unsigned result = 0;
160         symbol a("a"), b("b"), c("c");
161         symbol d("d"), e("e"), f("f");
162         symbol g("g"), h("h"), i("i");
163         matrix m = {{a, b, c},
164                     {d, e, f},
165                     {g, h, i}};
166         matrix m_i = m.inverse();
167         ex det = m.determinant();
168         
169         if ((normal(m_i(0,0)*det) != (e*i-f*h)) ||
170             (normal(m_i(0,1)*det) != (c*h-b*i)) ||
171             (normal(m_i(0,2)*det) != (b*f-c*e)) ||
172             (normal(m_i(1,0)*det) != (f*g-d*i)) ||
173             (normal(m_i(1,1)*det) != (a*i-c*g)) ||
174             (normal(m_i(1,2)*det) != (c*d-a*f)) ||
175             (normal(m_i(2,0)*det) != (d*h-e*g)) ||
176             (normal(m_i(2,1)*det) != (b*g-a*h)) ||
177             (normal(m_i(2,2)*det) != (a*e-b*d))) {
178                 clog << "inversion of 3x3 matrix " << m
179                      << " erroneously returned " << m_i << endl;
180                 ++result;
181         }
182         
183         return result;
184 }
185
186 static unsigned matrix_solve2()
187 {
188         // check the solution of the multiple system A*X = B:
189         //       [ 1  2 -1 ] [ x0 y0 ]   [ 4 0 ]
190         //       [ 1  4 -2 ]*[ x1 y1 ] = [ 7 0 ]
191         //       [ a -2  2 ] [ x2 y2 ]   [ a 4 ]
192         unsigned result = 0;
193         symbol a("a");
194         symbol x0("x0"), x1("x1"), x2("x2");
195         symbol y0("y0"), y1("y1"), y2("y2");
196         matrix A = {{1,  2, -1},
197                     {1,  4, -2},
198                     {a, -2,  2}};
199         matrix B = {{4, 0},
200                     {7, 0},
201                     {a, 4}};
202         matrix X = {{x0 ,y0},
203                     {x1, y1},
204                     {x2, y2}};
205         matrix cmp = {{1, 0},
206                       {3, 2},
207                       {3, 4}};
208         matrix sol(A.solve(X, B));
209         if (cmp != sol) {
210                 clog << "Solving " << A << " * " << X << " == " << B << endl
211                      << "erroneously returned " << sol << endl;
212                 result = 1;
213         }
214
215         return result;
216 }
217
218 static unsigned matrix_solve3()
219 {
220         unsigned result = 0;
221         symbol x("x");
222         symbol t1("t1"), t2("t2"), t3("t3");
223         matrix A = {
224                 {3+6*x, 6*(x+x*x)/(2+3*x), 0},
225                 {-(2+7*x+6*x*x)/x, -2-2*x, 0},
226                 {-2*(2+3*x)/(1+2*x), -6*x/(1+2*x), 1+4*x}
227         };
228         matrix B = {{0}, {0}, {0}};
229         matrix X = {{t1}, {t2}, {t3}};
230         for (auto algo : vector<int>({
231                 solve_algo::gauss, solve_algo::divfree, solve_algo::bareiss, solve_algo::markowitz
232         })) {
233                 matrix sol(A.solve(X, B, algo));
234                 if (!normal((A*sol - B).evalm()).is_zero_matrix()) {
235                         clog << "Solving " << A << " * " << X << " == " << B << " with algo=" << algo << endl
236                              << "erroneously returned " << sol << endl;
237                         result += 1;
238                 }
239         }
240         return result;
241 }
242
243 static unsigned matrix_evalm()
244 {
245         unsigned result = 0;
246
247         matrix S {{1, 2},
248                   {3, 4}};
249         matrix T {{1, 1},
250                   {2, -1}};
251         matrix R {{27, 14},
252                   {36, 26}};
253
254         ex e = ((S + T) * (S + 2*T));
255         ex f = e.evalm();
256         if (!f.is_equal(R)) {
257                 clog << "Evaluating " << e << " erroneously returned " << f << " instead of " << R << endl;
258                 result++;
259         }
260
261         return result;
262 }
263
264 static unsigned matrix_rank()
265 {
266         unsigned result = 0;
267         symbol x("x"), y("y");
268         matrix m(3,3);
269
270         // the zero matrix always has rank 0
271         if (m.rank() != 0) {
272                 clog << "The rank of " << m << " was not computed correctly." << endl;
273                 ++result;
274         }
275
276         // a trivial rank one example
277         m = {{1, 0, 0},
278              {2, 0, 0},
279              {3, 0, 0}};
280         if (m.rank() != 1) {
281                 clog << "The rank of " << m << " was not computed correctly." << endl;
282                 ++result;
283         }
284
285         // an example from Maple's help with rank two
286         m = {{x,   1,  0},
287              {0,   0,  1},
288              {x*y, y,  1}};
289         if (m.rank() != 2) {
290                 clog << "The rank of " << m << " was not computed correctly." << endl;
291                 ++result;
292         }
293
294         // the 3x3 unit matrix has rank 3
295         m = ex_to<matrix>(unit_matrix(3,3));
296         if (m.rank() != 3) {
297                 clog << "The rank of " << m << " was not computed correctly." << endl;
298                 ++result;
299         }
300
301         return result;  
302 }
303
304 unsigned matrix_solve_nonnormal()
305 {
306         symbol a("a"), b("b"), c("c"), x("x");
307         // This matrix has a non-normal zero element!
308         matrix mx {{1,0,0},
309                    {0,1/(x+1)-(x-1)/(x*x-1),1},
310                    {0,0,0}};
311         matrix zero {{0}, {0}, {0}};
312         matrix vars {{a}, {b}, {c}};
313         try {
314                 matrix sol_gauss   = mx.solve(vars, zero, solve_algo::gauss);
315                 matrix sol_divfree = mx.solve(vars, zero, solve_algo::divfree);
316                 matrix sol_bareiss = mx.solve(vars, zero, solve_algo::bareiss);
317                 if (sol_gauss != sol_divfree  ||  sol_gauss != sol_bareiss) {
318                         clog << "different solutions while solving "
319                              << mx << " * " << vars << " == " << zero << endl
320                              << "gauss:   " << sol_gauss << endl
321                              << "divfree: " << sol_divfree << endl
322                              << "bareiss: " << sol_bareiss << endl;
323                         return 1;
324                 }
325         } catch (const exception & e) {
326                 clog << "exception thrown while solving "
327                      << mx << " * " << vars << " == " << zero << endl;
328                 return 1;
329         }
330         return 0;
331 }
332
333 static unsigned matrix_misc()
334 {
335         unsigned result = 0;
336         symbol a("a"), b("b"), c("c"), d("d"), e("e"), f("f");
337         matrix m1 = {{a, b},
338                      {c, d}};
339         ex tr = trace(m1);
340         
341         // check a simple trace
342         if (tr.compare(a+d)) {
343                 clog << "trace of 2x2 matrix " << m1
344                      << " erroneously returned " << tr << endl;
345                 ++result;
346         }
347         
348         // and two simple transpositions
349         matrix m2 = transpose(m1);
350         if (m2(0,0) != a || m2(0,1) != c || m2(1,0) != b || m2(1,1) != d) {
351                 clog << "transpose of 2x2 matrix " << m1
352                          << " erroneously returned " << m2 << endl;
353                 ++result;
354         }
355         matrix m3 = {{a, b},
356                      {c, d},
357                      {e, f}};
358         if (transpose(transpose(m3)) != m3) {
359                 clog << "transposing 3x2 matrix " << m3 << " twice"
360                      << " erroneously returned " << transpose(transpose(m3)) << endl;
361                 ++result;
362         }
363         
364         // produce a runtime-error by inverting a singular matrix and catch it
365         matrix m4(2,2);
366         matrix m5;
367         bool caught = false;
368         try {
369                 m5 = inverse(m4);
370         } catch (std::runtime_error err) {
371                 caught = true;
372         }
373         if (!caught) {
374                 cerr << "singular 2x2 matrix " << m4
375                      << " erroneously inverted to " << m5 << endl;
376                 ++result;
377         }
378         
379         return result;
380 }
381
382 unsigned exam_matrices()
383 {
384         unsigned result = 0;
385         
386         cout << "examining symbolic matrix manipulations" << flush;
387         
388         result += matrix_determinants();  cout << '.' << flush;
389         result += matrix_invert1();  cout << '.' << flush;
390         result += matrix_invert2();  cout << '.' << flush;
391         result += matrix_invert3();  cout << '.' << flush;
392         result += matrix_solve2();  cout << '.' << flush;
393         result += matrix_solve3();  cout << '.' << flush;
394         result += matrix_evalm();  cout << "." << flush;
395         result += matrix_rank();  cout << "." << flush;
396         result += matrix_solve_nonnormal();  cout << "." << flush;
397         result += matrix_misc();  cout << '.' << flush;
398         
399         return result;
400 }
401
402 int main(int argc, char** argv)
403 {
404         return exam_matrices();
405 }