File: check_matrices.cpp

package info (click to toggle)
ginac 1.0.8-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 3,544 kB
  • ctags: 3,232
  • sloc: cpp: 27,732; sh: 7,126; perl: 1,819; yacc: 763; lex: 345; makefile: 221; sed: 32
file content (217 lines) | stat: -rw-r--r-- 6,623 bytes parent folder | download
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
/** @file check_matrices.cpp
 *
 *  Here we test manipulations on GiNaC's symbolic matrices. */

/*
 *  GiNaC Copyright (C) 1999-2002 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "checks.h"

/* determinants of some sparse symbolic matrices with coefficients in
 * an integral domain. */
static unsigned integdom_matrix_determinants(void)
{
	unsigned result = 0;
	symbol a("a");
	
	for (unsigned size=3; size<20; ++size) {
		matrix A(size,size);
		// populate one element in each row:
		for (unsigned r=0; r<size-1; ++r)
			A.set(r,unsigned(rand()%size),dense_univariate_poly(a,5));
		// set the last row to a linear combination of two other lines
		// to guarantee that the determinant is zero:
		for (unsigned c=0; c<size; ++c)
			A.set(size-1,c,A(0,c)-A(size-2,c));
		if (!A.determinant().is_zero()) {
			clog << "Determinant of " << size << "x" << size << " matrix "
			     << endl << A << endl
			     << "was not found to vanish!" << endl;
			++result;
		}
	}
	
	return result;
}

/* determinants of some symbolic matrices with multivariate rational function
 * coefficients. */
static unsigned rational_matrix_determinants(void)
{
	unsigned result = 0;
	symbol a("a"), b("b"), c("c");
	
	for (unsigned size=3; size<8; ++size) {
		matrix A(size,size);
		for (unsigned r=0; r<size-1; ++r) {
			// populate one or two elements in each row:
			for (unsigned ec=0; ec<2; ++ec) {
				ex numer = sparse_tree(a, b, c, 1+rand()%4, false, false, false);
				ex denom;
				do {
					denom = sparse_tree(a, b, c, rand()%2, false, false, false);
				} while (denom.is_zero());
				A.set(r,unsigned(rand()%size),numer/denom);
			}
		}
		// set the last row to a linear combination of two other lines
		// to guarantee that the determinant is zero:
		for (unsigned co=0; co<size; ++co)
			A.set(size-1,co,A(0,co)-A(size-2,co));
		if (!A.determinant().is_zero()) {
			clog << "Determinant of " << size << "x" << size << " matrix "
			     << endl << A << endl
			     << "was not found to vanish!" << endl;
			++result;
		}
	}
	
	return result;
}

/* Some quite funny determinants with functions and stuff like that inside. */
static unsigned funny_matrix_determinants(void)
{
	unsigned result = 0;
	symbol a("a"), b("b"), c("c");
	
	for (unsigned size=3; size<7; ++size) {
		matrix A(size,size);
		for (unsigned co=0; co<size-1; ++co) {
			// populate one or two elements in each row:
			for (unsigned ec=0; ec<2; ++ec) {
				ex numer = sparse_tree(a, b, c, 1+rand()%3, true, true, false);
				ex denom;
				do {
					denom = sparse_tree(a, b, c, rand()%2, false, true, false);
				} while (denom.is_zero());
				A.set(unsigned(rand()%size),co,numer/denom);
			}
		}
		// set the last column to a linear combination of two other columns
		// to guarantee that the determinant is zero:
		for (unsigned ro=0; ro<size; ++ro)
			A.set(ro,size-1,A(ro,0)-A(ro,size-2));
		if (!A.determinant().is_zero()) {
			clog << "Determinant of " << size << "x" << size << " matrix "
			     << endl << A << endl
			     << "was not found to vanish!" << endl;
			++result;
		}
	}
	
	return result;
}

/* compare results from different determinant algorithms.*/
static unsigned compare_matrix_determinants(void)
{
	unsigned result = 0;
	symbol a("a");
	
	for (unsigned size=2; size<7; ++size) {
		matrix A(size,size);
		for (unsigned co=0; co<size; ++co) {
			for (unsigned ro=0; ro<size; ++ro) {
				// populate some elements
				ex elem = 0;
				if (rand()%(size/2) == 0)
					elem = sparse_tree(a, a, a, rand()%3, false, true, false);
				A.set(ro,co,elem);
			}
		}
		ex det_gauss = A.determinant(determinant_algo::gauss);
		ex det_laplace = A.determinant(determinant_algo::laplace);
		ex det_divfree = A.determinant(determinant_algo::divfree);
		ex det_bareiss = A.determinant(determinant_algo::bareiss);
		if ((det_gauss-det_laplace).normal() != 0 ||
			(det_bareiss-det_laplace).normal() != 0 ||
			(det_divfree-det_laplace).normal() != 0) {
			clog << "Determinant of " << size << "x" << size << " matrix "
			     << endl << A << endl
			     << "is inconsistent between different algorithms:" << endl
			     << "Gauss elimination:   " << det_gauss << endl
			     << "Minor elimination:   " << det_laplace << endl
			     << "Division-free elim.: " << det_divfree << endl
			     << "Fraction-free elim.: " << det_bareiss << endl;
			++result;
		}
	}
	
	return result;
}

static unsigned symbolic_matrix_inverse(void)
{
	unsigned result = 0;
	symbol a("a"), b("b"), c("c");
	
	for (unsigned size=2; size<5; ++size) {
		matrix A(size,size);
		do {
			for (unsigned co=0; co<size; ++co) {
				for (unsigned ro=0; ro<size; ++ro) {
					// populate some elements
					ex elem = 0;
					if (rand()%(size/2) == 0)
						elem = sparse_tree(a, b, c, rand()%2, false, true, false);
					A.set(ro,co,elem);
				}
			}
		} while (A.determinant() == 0);
		matrix B = A.inverse();
		matrix C = A.mul(B);
		bool ok = true;
		for (unsigned ro=0; ro<size; ++ro)
			for (unsigned co=0; co<size; ++co)
				if (C(ro,co).normal() != (ro==co?1:0))
					ok = false;
		if (!ok) {
			clog << "Inverse of " << size << "x" << size << " matrix "
			     << endl << A << endl
			     << "erroneously returned: "
			     << endl << B << endl;
			++result;
		}
	}
	
	return result;
}

unsigned check_matrices(void)
{
	unsigned result = 0;
	
	cout << "checking symbolic matrix manipulations" << flush;
	clog << "---------symbolic matrix manipulations:" << endl;
	
	result += integdom_matrix_determinants();  cout << '.' << flush;
	result += rational_matrix_determinants();  cout << '.' << flush;
	result += funny_matrix_determinants();  cout << '.' << flush;
	result += compare_matrix_determinants();  cout << '.' << flush;
	result += symbolic_matrix_inverse();  cout << '.' << flush;
	
	if (!result) {
		cout << " passed " << endl;
		clog << "(no output)" << endl;
	} else {
		cout << " failed " << endl;
	}
	
	return result;
}