File: exam_matrices.cpp

package info (click to toggle)
ginac 1.8.9-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 6,640 kB
  • sloc: cpp: 49,195; sh: 5,402; makefile: 448; python: 193
file content (405 lines) | stat: -rw-r--r-- 10,438 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405

/** @file exam_matrices.cpp
 *
 *  Here we examine manipulations on GiNaC's symbolic matrices. */

/*
 *  GiNaC Copyright (C) 1999-2025 Johannes Gutenberg University Mainz, Germany
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "ginac.h"
using namespace GiNaC;

#include <iostream>
#include <stdexcept>
using namespace std;

static unsigned matrix_determinants()
{
	unsigned result = 0;
	ex det;
	matrix m1(1,1), m2(2,2), m3(3,3), m4(4,4);
	symbol a("a"), b("b"), c("c");
	symbol d("d"), e("e"), f("f");
	symbol g("g"), h("h"), i("i");
	
	// check symbolic trivial matrix determinant
	m1 = matrix{{a}};
	det = m1.determinant();
	if (det != a) {
		clog << "determinant of 1x1 matrix " << m1
		     << " erroneously returned " << det << endl;
		++result;
	}
	
	// check generic dense symbolic 2x2 matrix determinant
	m2 = matrix{{a, b},
	            {c, d}};
	det = m2.determinant();
	if (det != (a*d-b*c)) {
		clog << "determinant of 2x2 matrix " << m2
		     << " erroneously returned " << det << endl;
		++result;
	}
	
	// check generic dense symbolic 3x3 matrix determinant
	m3 = matrix{{a, b, c},
	            {d, e, f},
	            {g, h, i}};
	det = m3.determinant();
	if (det != (a*e*i - a*f*h - d*b*i + d*c*h + g*b*f - g*c*e)) {
		clog << "determinant of 3x3 matrix " << m3
		     << " erroneously returned " << det << endl;
		++result;
	}
	
	// check dense numeric 3x3 matrix determinant
	m3 = matrix{{0, -1,  3},
	            {3, -2,  2},
	            {3,  4, -2}};
	det = m3.determinant();
	if (det != 42) {
		clog << "determinant of 3x3 matrix " << m3
		     << " erroneously returned " << det << endl;
		++result;
	}
	
	// check dense symbolic 2x2 matrix determinant
	m2 = matrix{{a/(a-b), 1},
	            {b/(a-b), 1}};
	det = m2.determinant();
	if (det != 1) {
		if (det.normal() == 1)  // only half wrong
			clog << "determinant of 2x2 matrix " << m2
			     << " was returned unnormalized as " << det << endl;
		else  // totally wrong
			clog << "determinant of 2x2 matrix " << m2
			     << " erroneously returned " << det << endl;
		++result;
	}
	
	// check sparse symbolic 4x4 matrix determinant
	m4.set(0,1,a).set(1,0,b).set(3,2,c).set(2,3,d);
	det = m4.determinant();
	if (det != a*b*c*d) {
		clog << "determinant of 4x4 matrix " << m4
		     << " erroneously returned " << det << endl;
		++result;
	}
	
	// check characteristic polynomial
	m3 = matrix{{a, -2,   2},
		    {3, a-1,  2},
		    {3,  4,  a-3}};
	ex p = m3.charpoly(a);
	if (p != 0) {
		clog << "charpoly of 3x3 matrix " << m3
		     << " erroneously returned " << p << endl;
		++result;
	}
	
	return result;
}

static unsigned matrix_invert1()
{
	unsigned result = 0;
	matrix m(1,1);
	symbol a("a");
	
	m.set(0,0,a);
	matrix m_i = m.inverse();
	
	if (m_i(0,0) != pow(a,-1)) {
		clog << "inversion of 1x1 matrix " << m
		     << " erroneously returned " << m_i << endl;
		++result;
	}
	
	return result;
}

static unsigned matrix_invert2()
{
	unsigned result = 0;
	symbol a("a"), b("b"), c("c"), d("d");
	matrix m = {{a, b},
	            {c, d}};
	matrix m_i = m.inverse();
	ex det = m.determinant();
	
	if ((normal(m_i(0,0)*det) != d) ||
	    (normal(m_i(0,1)*det) != -b) ||
	    (normal(m_i(1,0)*det) != -c) ||
	    (normal(m_i(1,1)*det) != a)) {
		clog << "inversion of 2x2 matrix " << m
		     << " erroneously returned " << m_i << endl;
		++result;
	}
	
	return result;
}

static unsigned matrix_invert3()
{
	unsigned result = 0;
	symbol a("a"), b("b"), c("c");
	symbol d("d"), e("e"), f("f");
	symbol g("g"), h("h"), i("i");
	matrix m = {{a, b, c},
	            {d, e, f},
	            {g, h, i}};
	matrix m_i = m.inverse();
	ex det = m.determinant();
	
	if ((normal(m_i(0,0)*det) != (e*i-f*h)) ||
	    (normal(m_i(0,1)*det) != (c*h-b*i)) ||
	    (normal(m_i(0,2)*det) != (b*f-c*e)) ||
	    (normal(m_i(1,0)*det) != (f*g-d*i)) ||
	    (normal(m_i(1,1)*det) != (a*i-c*g)) ||
	    (normal(m_i(1,2)*det) != (c*d-a*f)) ||
	    (normal(m_i(2,0)*det) != (d*h-e*g)) ||
	    (normal(m_i(2,1)*det) != (b*g-a*h)) ||
	    (normal(m_i(2,2)*det) != (a*e-b*d))) {
		clog << "inversion of 3x3 matrix " << m
		     << " erroneously returned " << m_i << endl;
		++result;
	}
	
	return result;
}

static unsigned matrix_solve2()
{
	// check the solution of the multiple system A*X = B:
	//       [ 1  2 -1 ] [ x0 y0 ]   [ 4 0 ]
	//       [ 1  4 -2 ]*[ x1 y1 ] = [ 7 0 ]
	//       [ a -2  2 ] [ x2 y2 ]   [ a 4 ]
	unsigned result = 0;
	symbol a("a");
	symbol x0("x0"), x1("x1"), x2("x2");
	symbol y0("y0"), y1("y1"), y2("y2");
	matrix A = {{1,  2, -1},
	            {1,  4, -2},
	            {a, -2,  2}};
	matrix B = {{4, 0},
	            {7, 0},
	            {a, 4}};
	matrix X = {{x0 ,y0},
	            {x1, y1},
	            {x2, y2}};
	matrix cmp = {{1, 0},
	              {3, 2},
	              {3, 4}};
	matrix sol(A.solve(X, B));
	if (cmp != sol) {
		clog << "Solving " << A << " * " << X << " == " << B << endl
		     << "erroneously returned " << sol << endl;
		result = 1;
	}

	return result;
}

static unsigned matrix_solve3()
{
	unsigned result = 0;
	symbol x("x");
	symbol t1("t1"), t2("t2"), t3("t3");
	matrix A = {
		{3+6*x, 6*(x+x*x)/(2+3*x), 0},
		{-(2+7*x+6*x*x)/x, -2-2*x, 0},
		{-2*(2+3*x)/(1+2*x), -6*x/(1+2*x), 1+4*x}
	};
	matrix B = {{0}, {0}, {0}};
	matrix X = {{t1}, {t2}, {t3}};
	for (auto algo : vector<int>({
		solve_algo::gauss, solve_algo::divfree, solve_algo::bareiss, solve_algo::markowitz
	})) {
		matrix sol(A.solve(X, B, algo));
		if (!normal((A*sol - B).evalm()).is_zero_matrix()) {
			clog << "Solving " << A << " * " << X << " == " << B << " with algo=" << algo << endl
			     << "erroneously returned " << sol << endl;
			result += 1;
		}
	}
	return result;
}

static unsigned matrix_evalm()
{
	unsigned result = 0;

	matrix S {{1, 2},
	          {3, 4}};
	matrix T {{1, 1},
	          {2, -1}};
	matrix R {{27, 14},
	          {36, 26}};

	ex e = ((S + T) * (S + 2*T));
	ex f = e.evalm();
	if (!f.is_equal(R)) {
		clog << "Evaluating " << e << " erroneously returned " << f << " instead of " << R << endl;
		result++;
	}

	return result;
}

static unsigned matrix_rank()
{
	unsigned result = 0;
	symbol x("x"), y("y");
	matrix m(3,3);

	// the zero matrix always has rank 0
	if (m.rank() != 0) {
		clog << "The rank of " << m << " was not computed correctly." << endl;
		++result;
	}

	// a trivial rank one example
	m = {{1, 0, 0},
	     {2, 0, 0},
	     {3, 0, 0}};
	if (m.rank() != 1) {
		clog << "The rank of " << m << " was not computed correctly." << endl;
		++result;
	}

	// an example from Maple's help with rank two
	m = {{x,   1,  0},
	     {0,   0,  1},
	     {x*y, y,  1}};
	if (m.rank() != 2) {
		clog << "The rank of " << m << " was not computed correctly." << endl;
		++result;
	}

	// the 3x3 unit matrix has rank 3
	m = ex_to<matrix>(unit_matrix(3,3));
	if (m.rank() != 3) {
		clog << "The rank of " << m << " was not computed correctly." << endl;
		++result;
	}

	return result;	
}

unsigned matrix_solve_nonnormal()
{
	symbol a("a"), b("b"), c("c"), x("x");
	// This matrix has a non-normal zero element!
	matrix mx {{1,0,0},
	           {0,1/(x+1)-(x-1)/(x*x-1),1},
	           {0,0,0}};
	matrix zero {{0}, {0}, {0}};
	matrix vars {{a}, {b}, {c}};
	try {
		matrix sol_gauss   = mx.solve(vars, zero, solve_algo::gauss);
		matrix sol_divfree = mx.solve(vars, zero, solve_algo::divfree);
		matrix sol_bareiss = mx.solve(vars, zero, solve_algo::bareiss);
		if (sol_gauss != sol_divfree  ||  sol_gauss != sol_bareiss) {
			clog << "different solutions while solving "
			     << mx << " * " << vars << " == " << zero << endl
			     << "gauss:   " << sol_gauss << endl
			     << "divfree: " << sol_divfree << endl
			     << "bareiss: " << sol_bareiss << endl;
			return 1;
		}
	} catch (const exception & e) {
		clog << "exception thrown while solving "
		     << mx << " * " << vars << " == " << zero << endl;
		return 1;
	}
	return 0;
}

static unsigned matrix_misc()
{
	unsigned result = 0;
	symbol a("a"), b("b"), c("c"), d("d"), e("e"), f("f");
	matrix m1 = {{a, b},
		     {c, d}};
	ex tr = trace(m1);
	
	// check a simple trace
	if (tr.compare(a+d)) {
		clog << "trace of 2x2 matrix " << m1
		     << " erroneously returned " << tr << endl;
		++result;
	}
	
	// and two simple transpositions
	matrix m2 = transpose(m1);
	if (m2(0,0) != a || m2(0,1) != c || m2(1,0) != b || m2(1,1) != d) {
		clog << "transpose of 2x2 matrix " << m1
			 << " erroneously returned " << m2 << endl;
		++result;
	}
	matrix m3 = {{a, b},
		     {c, d},
		     {e, f}};
	if (transpose(transpose(m3)) != m3) {
		clog << "transposing 3x2 matrix " << m3 << " twice"
		     << " erroneously returned " << transpose(transpose(m3)) << endl;
		++result;
	}
	
	// produce a runtime-error by inverting a singular matrix and catch it
	matrix m4(2,2);
	matrix m5;
	bool caught = false;
	try {
		m5 = inverse(m4);
	} catch (std::runtime_error &err) {
		caught = true;
	}
	if (!caught) {
		cerr << "singular 2x2 matrix " << m4
		     << " erroneously inverted to " << m5 << endl;
		++result;
	}
	
	return result;
}

unsigned exam_matrices()
{
	unsigned result = 0;
	
	cout << "examining symbolic matrix manipulations" << flush;
	
	result += matrix_determinants();  cout << '.' << flush;
	result += matrix_invert1();  cout << '.' << flush;
	result += matrix_invert2();  cout << '.' << flush;
	result += matrix_invert3();  cout << '.' << flush;
	result += matrix_solve2();  cout << '.' << flush;
	result += matrix_solve3();  cout << '.' << flush;
	result += matrix_evalm();  cout << "." << flush;
	result += matrix_rank();  cout << "." << flush;
	result += matrix_solve_nonnormal();  cout << "." << flush;
	result += matrix_misc();  cout << '.' << flush;
	
	return result;
}

int main(int argc, char** argv)
{
	return exam_matrices();
}