2 /** @file exam_matrices.cpp
4 * Here we examine manipulations on GiNaC's symbolic matrices. */
7 * GiNaC Copyright (C) 1999-2020 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_solve3()
222 symbol t1("t1"), t2("t2"), t3("t3");
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}
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
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;
243 static unsigned matrix_evalm()
254 ex e = ((S + T) * (S + 2*T));
256 if (!f.is_equal(R)) {
257 clog << "Evaluating " << e << " erroneously returned " << f << " instead of " << R << endl;
264 static unsigned matrix_rank()
267 symbol x("x"), y("y");
270 // the zero matrix always has rank 0
272 clog << "The rank of " << m << " was not computed correctly." << endl;
276 // a trivial rank one example
281 clog << "The rank of " << m << " was not computed correctly." << endl;
285 // an example from Maple's help with rank two
290 clog << "The rank of " << m << " was not computed correctly." << endl;
294 // the 3x3 unit matrix has rank 3
295 m = ex_to<matrix>(unit_matrix(3,3));
297 clog << "The rank of " << m << " was not computed correctly." << endl;
304 unsigned matrix_solve_nonnormal()
306 symbol a("a"), b("b"), c("c"), x("x");
307 // This matrix has a non-normal zero element!
309 {0,1/(x+1)-(x-1)/(x*x-1),1},
311 matrix zero {{0}, {0}, {0}};
312 matrix vars {{a}, {b}, {c}};
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;
325 } catch (const exception & e) {
326 clog << "exception thrown while solving "
327 << mx << " * " << vars << " == " << zero << endl;
333 static unsigned matrix_misc()
336 symbol a("a"), b("b"), c("c"), d("d"), e("e"), f("f");
341 // check a simple trace
342 if (tr.compare(a+d)) {
343 clog << "trace of 2x2 matrix " << m1
344 << " erroneously returned " << tr << endl;
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;
358 if (transpose(transpose(m3)) != m3) {
359 clog << "transposing 3x2 matrix " << m3 << " twice"
360 << " erroneously returned " << transpose(transpose(m3)) << endl;
364 // produce a runtime-error by inverting a singular matrix and catch it
370 } catch (std::runtime_error err) {
374 cerr << "singular 2x2 matrix " << m4
375 << " erroneously inverted to " << m5 << endl;
382 unsigned exam_matrices()
386 cout << "examining symbolic matrix manipulations" << flush;
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;
402 int main(int argc, char** argv)
404 return exam_matrices();