]> www.ginac.de Git - ginac.git/blob - check/exam_matrices.cpp
- renamed configure.in to configure.ac as suggested in the autoconf docs
[ginac.git] / check / exam_matrices.cpp
1 /** @file exam_matrices.cpp
2  *
3  *  Here we examine manipulations on GiNaC's symbolic matrices. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2001 Johannes Gutenberg University Mainz, Germany
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 #include <stdexcept>
24 #include "exams.h"
25
26 static unsigned matrix_determinants(void)
27 {
28         unsigned result = 0;
29         ex det;
30         matrix m1(1,1), m2(2,2), m3(3,3), m4(4,4);
31         symbol a("a"), b("b"), c("c");
32         symbol d("d"), e("e"), f("f");
33         symbol g("g"), h("h"), i("i");
34         
35         // check symbolic trivial matrix determinant
36         m1.set(0,0,a);
37         det = m1.determinant();
38         if (det != a) {
39                 clog << "determinant of 1x1 matrix " << m1
40                      << " erroneously returned " << det << endl;
41                 ++result;
42         }
43         
44         // check generic dense symbolic 2x2 matrix determinant
45         m2.set(0,0,a).set(0,1,b);
46         m2.set(1,0,c).set(1,1,d);
47         det = m2.determinant();
48         if (det != (a*d-b*c)) {
49                 clog << "determinant of 2x2 matrix " << m2
50                      << " erroneously returned " << det << endl;
51                 ++result;
52         }
53         
54         // check generic dense symbolic 3x3 matrix determinant
55         m3.set(0,0,a).set(0,1,b).set(0,2,c);
56         m3.set(1,0,d).set(1,1,e).set(1,2,f);
57         m3.set(2,0,g).set(2,1,h).set(2,2,i);
58         det = m3.determinant();
59         if (det != (a*e*i - a*f*h - d*b*i + d*c*h + g*b*f - g*c*e)) {
60                 clog << "determinant of 3x3 matrix " << m3
61                      << " erroneously returned " << det << endl;
62                 ++result;
63         }
64         
65         // check dense numeric 3x3 matrix determinant
66         m3.set(0,0,numeric(0)).set(0,1,numeric(-1)).set(0,2,numeric(3));
67         m3.set(1,0,numeric(3)).set(1,1,numeric(-2)).set(1,2,numeric(2));
68         m3.set(2,0,numeric(3)).set(2,1,numeric(4)).set(2,2,numeric(-2));
69         det = m3.determinant();
70         if (det != 42) {
71                 clog << "determinant of 3x3 matrix " << m3
72                      << " erroneously returned " << det << endl;
73                 ++result;
74         }
75         
76         // check dense symbolic 2x2 matrix determinant
77         m2.set(0,0,a/(a-b)).set(0,1,1);
78         m2.set(1,0,b/(a-b)).set(1,1,1);
79         det = m2.determinant();
80         if (det != 1) {
81                 if (det.normal() == 1)  // only half wrong
82                         clog << "determinant of 2x2 matrix " << m2
83                              << " was returned unnormalized as " << det << endl;
84                 else  // totally wrong
85                         clog << "determinant of 2x2 matrix " << m2
86                              << " erroneously returned " << det << endl;
87                 ++result;
88         }
89         
90         // check sparse symbolic 4x4 matrix determinant
91         m4.set(0,1,a).set(1,0,b).set(3,2,c).set(2,3,d);
92         det = m4.determinant();
93         if (det != a*b*c*d) {
94                 clog << "determinant of 4x4 matrix " << m4
95                      << " erroneously returned " << det << endl;
96                 ++result;
97         }
98         
99         // check characteristic polynomial
100         m3.set(0,0,a).set(0,1,-2).set(0,2,2);
101         m3.set(1,0,3).set(1,1,a-1).set(1,2,2);
102         m3.set(2,0,3).set(2,1,4).set(2,2,a-3);
103         ex p = m3.charpoly(a);
104         if (p != 0) {
105                 clog << "charpoly of 3x3 matrix " << m3
106                      << " erroneously returned " << p << endl;
107                 ++result;
108         }
109         
110         return result;
111 }
112
113 static unsigned matrix_invert1(void)
114 {
115         unsigned result = 0;
116         matrix m(1,1);
117         symbol a("a");
118         
119         m.set(0,0,a);
120         matrix m_i = m.inverse();
121         
122         if (m_i(0,0) != pow(a,-1)) {
123                 clog << "inversion of 1x1 matrix " << m
124                      << " erroneously returned " << m_i << endl;
125                 ++result;
126         }
127         
128         return result;
129 }
130
131 static unsigned matrix_invert2(void)
132 {
133         unsigned result = 0;
134         matrix m(2,2);
135         symbol a("a"), b("b"), c("c"), d("d");
136         m.set(0,0,a).set(0,1,b);
137         m.set(1,0,c).set(1,1,d);
138         matrix m_i = m.inverse();
139         ex det = m.determinant();
140         
141         if ((normal(m_i(0,0)*det) != d) ||
142                 (normal(m_i(0,1)*det) != -b) ||
143                 (normal(m_i(1,0)*det) != -c) ||
144                 (normal(m_i(1,1)*det) != a)) {
145                 clog << "inversion of 2x2 matrix " << m
146                      << " erroneously returned " << m_i << endl;
147                 ++result;
148         }
149         
150         return result;
151 }
152
153 static unsigned matrix_invert3(void)
154 {
155         unsigned result = 0;
156         matrix m(3,3);
157         symbol a("a"), b("b"), c("c");
158         symbol d("d"), e("e"), f("f");
159         symbol g("g"), h("h"), i("i");
160         m.set(0,0,a).set(0,1,b).set(0,2,c);
161         m.set(1,0,d).set(1,1,e).set(1,2,f);
162         m.set(2,0,g).set(2,1,h).set(2,2,i);
163         matrix m_i = m.inverse();
164         ex det = m.determinant();
165         
166         if ((normal(m_i(0,0)*det) != (e*i-f*h)) ||
167             (normal(m_i(0,1)*det) != (c*h-b*i)) ||
168             (normal(m_i(0,2)*det) != (b*f-c*e)) ||
169             (normal(m_i(1,0)*det) != (f*g-d*i)) ||
170             (normal(m_i(1,1)*det) != (a*i-c*g)) ||
171             (normal(m_i(1,2)*det) != (c*d-a*f)) ||
172             (normal(m_i(2,0)*det) != (d*h-e*g)) ||
173             (normal(m_i(2,1)*det) != (b*g-a*h)) ||
174             (normal(m_i(2,2)*det) != (a*e-b*d))) {
175                 clog << "inversion of 3x3 matrix " << m
176                      << " erroneously returned " << m_i << endl;
177                 ++result;
178         }
179         
180         return result;
181 }
182
183 static unsigned matrix_solve2(void)
184 {
185         // check the solution of the multiple system A*X = B:
186         //       [ 1  2 -1 ] [ x0 y0 ]   [ 4 0 ]
187         //       [ 1  4 -2 ]*[ x1 y1 ] = [ 7 0 ]
188         //       [ a -2  2 ] [ x2 y2 ]   [ a 4 ]
189         unsigned result = 0;
190         symbol a("a");
191         symbol x0("x0"), x1("x1"), x2("x2");
192         symbol y0("y0"), y1("y1"), y2("y2");
193         matrix A(3,3);
194         A.set(0,0,1).set(0,1,2).set(0,2,-1);
195         A.set(1,0,1).set(1,1,4).set(1,2,-2);
196         A.set(2,0,a).set(2,1,-2).set(2,2,2);
197         matrix B(3,2);
198         B.set(0,0,4).set(1,0,7).set(2,0,a);
199         B.set(0,1,0).set(1,1,0).set(2,1,4);
200         matrix X(3,2);
201         X.set(0,0,x0).set(1,0,x1).set(2,0,x2);
202         X.set(0,1,y0).set(1,1,y1).set(2,1,y2);
203         matrix cmp(3,2);
204         cmp.set(0,0,1).set(1,0,3).set(2,0,3);
205         cmp.set(0,1,0).set(1,1,2).set(2,1,4);
206         matrix sol(A.solve(X, B));
207         for (unsigned ro=0; ro<3; ++ro)
208                 for (unsigned co=0; co<2; ++co)
209                         if (cmp(ro,co) != sol(ro,co))
210                                 result = 1;
211         if (result) {
212                 clog << "Solving " << A << " * " << X << " == " << B << endl
213                      << "erroneously returned " << sol << endl;
214         }
215         
216         return result;
217 }
218
219 static unsigned matrix_evalm(void)
220 {
221         unsigned result = 0;
222
223         matrix S(2, 2, lst(
224                 1, 2,
225                 3, 4
226         )), T(2, 2, lst(
227                 1, 1,
228                 2, -1
229         )), R(2, 2, lst(
230                 27, 14,
231                 36, 26
232         ));
233
234         ex e = ((S + T) * (S + 2*T));
235         ex f = e.evalm();
236         if (!f.is_equal(R)) {
237                 clog << "Evaluating " << e << " erroneously returned " << f << " instead of " << R << endl;
238                 result++;
239         }
240
241         return result;
242 }
243
244 static unsigned matrix_misc(void)
245 {
246         unsigned result = 0;
247         matrix m1(2,2);
248         symbol a("a"), b("b"), c("c"), d("d"), e("e"), f("f");
249         m1.set(0,0,a).set(0,1,b);
250         m1.set(1,0,c).set(1,1,d);
251         ex tr = trace(m1);
252         
253         // check a simple trace
254         if (tr.compare(a+d)) {
255                 clog << "trace of 2x2 matrix " << m1
256                      << " erroneously returned " << tr << endl;
257                 ++result;
258         }
259         
260         // and two simple transpositions
261         matrix m2 = transpose(m1);
262         if (m2(0,0) != a || m2(0,1) != c || m2(1,0) != b || m2(1,1) != d) {
263                 clog << "transpose of 2x2 matrix " << m1
264                          << " erroneously returned " << m2 << endl;
265                 ++result;
266         }
267         matrix m3(3,2);
268         m3.set(0,0,a).set(0,1,b);
269         m3.set(1,0,c).set(1,1,d);
270         m3.set(2,0,e).set(2,1,f);
271         if (transpose(transpose(m3)) != m3) {
272                 clog << "transposing 3x2 matrix " << m3 << " twice"
273                      << " erroneously returned " << transpose(transpose(m3)) << endl;
274                 ++result;
275         }
276         
277         // produce a runtime-error by inverting a singular matrix and catch it
278         matrix m4(2,2);
279         matrix m5;
280         bool caught = false;
281         try {
282                 m5 = inverse(m4);
283         } catch (std::runtime_error err) {
284                 caught = true;
285         }
286         if (!caught) {
287                 cerr << "singular 2x2 matrix " << m4
288                      << " erroneously inverted to " << m5 << endl;
289                 ++result;
290         }
291         
292         return result;
293 }
294
295 unsigned exam_matrices(void)
296 {
297         unsigned result = 0;
298         
299         cout << "examining symbolic matrix manipulations" << flush;
300         clog << "----------symbolic matrix manipulations:" << endl;
301         
302         result += matrix_determinants();  cout << '.' << flush;
303         result += matrix_invert1();  cout << '.' << flush;
304         result += matrix_invert2();  cout << '.' << flush;
305         result += matrix_invert3();  cout << '.' << flush;
306         result += matrix_solve2();  cout << '.' << flush;
307         result += matrix_evalm();  cout << "." << flush;
308         result += matrix_misc();  cout << '.' << flush;
309         
310         if (!result) {
311                 cout << " passed " << endl;
312                 clog << "(no output)" << endl;
313         } else {
314                 cout << " failed " << endl;
315         }
316         
317         return result;
318 }