2 /** @file exam_matrices.cpp
4 * Here we examine manipulations on GiNaC's symbolic matrices. */
7 * GiNaC Copyright (C) 1999-2018 Johannes Gutenberg University Mainz, Germany
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.
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.
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
25 using namespace GiNaC;
31 static unsigned matrix_determinants()
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");
40 // check symbolic trivial matrix determinant
42 det = m1.determinant();
44 clog << "determinant of 1x1 matrix " << m1
45 << " erroneously returned " << det << endl;
49 // check generic dense symbolic 2x2 matrix determinant
52 det = m2.determinant();
53 if (det != (a*d-b*c)) {
54 clog << "determinant of 2x2 matrix " << m2
55 << " erroneously returned " << det << endl;
59 // check generic dense symbolic 3x3 matrix determinant
60 m3 = matrix{{a, b, c},
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;
70 // check dense numeric 3x3 matrix determinant
71 m3 = matrix{{0, -1, 3},
74 det = m3.determinant();
76 clog << "determinant of 3x3 matrix " << m3
77 << " erroneously returned " << det << endl;
81 // check dense symbolic 2x2 matrix determinant
82 m2 = matrix{{a/(a-b), 1},
84 det = m2.determinant();
86 if (det.normal() == 1) // only half wrong
87 clog << "determinant of 2x2 matrix " << m2
88 << " was returned unnormalized as " << det << endl;
90 clog << "determinant of 2x2 matrix " << m2
91 << " erroneously returned " << det << endl;
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();
99 clog << "determinant of 4x4 matrix " << m4
100 << " erroneously returned " << det << endl;
104 // check characteristic polynomial
105 m3 = matrix{{a, -2, 2},
108 ex p = m3.charpoly(a);
110 clog << "charpoly of 3x3 matrix " << m3
111 << " erroneously returned " << p << endl;
118 static unsigned matrix_invert1()
125 matrix m_i = m.inverse();
127 if (m_i(0,0) != pow(a,-1)) {
128 clog << "inversion of 1x1 matrix " << m
129 << " erroneously returned " << m_i << endl;
136 static unsigned matrix_invert2()
139 symbol a("a"), b("b"), c("c"), d("d");
142 matrix m_i = m.inverse();
143 ex det = m.determinant();
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;
157 static unsigned matrix_invert3()
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},
166 matrix m_i = m.inverse();
167 ex det = m.determinant();
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;
186 static unsigned matrix_solve2()
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 ]
194 symbol x0("x0"), x1("x1"), x2("x2");
195 symbol y0("y0"), y1("y1"), y2("y2");
196 matrix A = {{1, 2, -1},
202 matrix X = {{x0 ,y0},
205 matrix cmp = {{1, 0},
208 matrix sol(A.solve(X, B));
210 clog << "Solving " << A << " * " << X << " == " << B << endl
211 << "erroneously returned " << sol << endl;
218 static unsigned matrix_evalm()
229 ex e = ((S + T) * (S + 2*T));
231 if (!f.is_equal(R)) {
232 clog << "Evaluating " << e << " erroneously returned " << f << " instead of " << R << endl;
239 static unsigned matrix_rank()
242 symbol x("x"), y("y");
245 // the zero matrix always has rank 0
247 clog << "The rank of " << m << " was not computed correctly." << endl;
251 // a trivial rank one example
256 clog << "The rank of " << m << " was not computed correctly." << endl;
260 // an example from Maple's help with rank two
265 clog << "The rank of " << m << " was not computed correctly." << endl;
269 // the 3x3 unit matrix has rank 3
270 m = ex_to<matrix>(unit_matrix(3,3));
272 clog << "The rank of " << m << " was not computed correctly." << endl;
279 unsigned matrix_solve_nonnormal()
281 symbol a("a"), b("b"), c("c"), x("x");
282 // This matrix has a non-normal zero element!
284 {0,1/(x+1)-(x-1)/(x*x-1),1},
286 matrix zero {{0}, {0}, {0}};
287 matrix vars {{a}, {b}, {c}};
289 matrix sol_gauss = mx.solve(vars, zero, solve_algo::gauss);
290 matrix sol_divfree = mx.solve(vars, zero, solve_algo::divfree);
291 matrix sol_bareiss = mx.solve(vars, zero, solve_algo::bareiss);
292 if (sol_gauss != sol_divfree || sol_gauss != sol_bareiss) {
293 clog << "different solutions while solving "
294 << mx << " * " << vars << " == " << zero << endl
295 << "gauss: " << sol_gauss << endl
296 << "divfree: " << sol_divfree << endl
297 << "bareiss: " << sol_bareiss << endl;
300 } catch (const exception & e) {
301 clog << "exception thrown while solving "
302 << mx << " * " << vars << " == " << zero << endl;
308 static unsigned matrix_misc()
311 symbol a("a"), b("b"), c("c"), d("d"), e("e"), f("f");
316 // check a simple trace
317 if (tr.compare(a+d)) {
318 clog << "trace of 2x2 matrix " << m1
319 << " erroneously returned " << tr << endl;
323 // and two simple transpositions
324 matrix m2 = transpose(m1);
325 if (m2(0,0) != a || m2(0,1) != c || m2(1,0) != b || m2(1,1) != d) {
326 clog << "transpose of 2x2 matrix " << m1
327 << " erroneously returned " << m2 << endl;
333 if (transpose(transpose(m3)) != m3) {
334 clog << "transposing 3x2 matrix " << m3 << " twice"
335 << " erroneously returned " << transpose(transpose(m3)) << endl;
339 // produce a runtime-error by inverting a singular matrix and catch it
345 } catch (std::runtime_error err) {
349 cerr << "singular 2x2 matrix " << m4
350 << " erroneously inverted to " << m5 << endl;
357 unsigned exam_matrices()
361 cout << "examining symbolic matrix manipulations" << flush;
363 result += matrix_determinants(); cout << '.' << flush;
364 result += matrix_invert1(); cout << '.' << flush;
365 result += matrix_invert2(); cout << '.' << flush;
366 result += matrix_invert3(); cout << '.' << flush;
367 result += matrix_solve2(); cout << '.' << flush;
368 result += matrix_evalm(); cout << "." << flush;
369 result += matrix_rank(); cout << "." << flush;
370 result += matrix_solve_nonnormal(); cout << "." << flush;
371 result += matrix_misc(); cout << '.' << flush;
376 int main(int argc, char** argv)
378 return exam_matrices();