1 /** @file check_lsolve.cpp
3 * These test routines do some simple checks on solving linear systems of
4 * symbolic equations. They are a well-tried resource for cross-checking
5 * the underlying symbolic manipulations. */
8 * GiNaC Copyright (C) 1999-2007 Johannes Gutenberg University Mainz, Germany
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 #include <cstdlib> // rand()
30 using namespace GiNaC;
33 dense_univariate_poly(const symbol & x, unsigned degree);
35 static unsigned check_matrix_solve(unsigned m, unsigned n, unsigned p,
41 // set the first min(m,n) rows of A and B
42 for (unsigned ro=0; (ro<m)&&(ro<n); ++ro) {
43 for (unsigned co=0; co<n; ++co)
44 A.set(ro,co,dense_univariate_poly(a,degree));
45 for (unsigned co=0; co<p; ++co)
46 B.set(ro,co,dense_univariate_poly(a,degree));
48 // repeat excessive rows of A and B to avoid excessive construction of
49 // overdetermined linear systems
50 for (unsigned ro=n; ro<m; ++ro) {
51 for (unsigned co=0; co<n; ++co)
52 A.set(ro,co,A(ro-1,co));
53 for (unsigned co=0; co<p; ++co)
54 B.set(ro,co,B(ro-1,co));
56 // create a vector of n*p symbols all named "xrc" where r and c are ints
59 for (unsigned i=0; i<n; ++i) {
60 for (unsigned j=0; j<p; ++j) {
62 buf << "x" << i << j << ends;
63 x.push_back(symbol(buf.str()));
68 // Solve the system A*X==B:
71 } catch (const exception & err) { // catch runtime_error
72 // Presumably, the coefficient matrix A was degenerate
73 string errwhat = err.what();
74 if (errwhat == "matrix::solve(): inconsistent linear system")
77 clog << "caught exception: " << errwhat << endl;
81 // check the result with our original matrix:
82 bool errorflag = false;
83 for (unsigned ro=0; ro<m; ++ro) {
84 for (unsigned pco=0; pco<p; ++pco) {
86 for (unsigned co=0; co<n; ++co)
87 e += A(ro,co)*sol(co,pco);
88 if (!(e-B(ro,pco)).normal().is_zero())
93 clog << "Our solve method claims that A*X==B, with matrices" << endl
94 << "A == " << A << endl
95 << "X == " << sol << endl
96 << "B == " << B << endl;
103 static unsigned check_inifcns_lsolve(unsigned n)
107 for (int repetition=0; repetition<200; ++repetition) {
108 // create two size n vectors of symbols, one for the coefficients
109 // a[0],..,a[n], one for indeterminates x[0]..x[n]:
112 for (unsigned i=0; i<n; ++i) {
115 a.push_back(symbol(string("a")+buf.str()));
116 x.push_back(symbol(string("x")+buf.str()));
118 lst eqns; // equation list
119 lst vars; // variable list
121 // Create a random linear system...
122 for (unsigned i=0; i<n; ++i) {
123 ex lhs = rand()%201-100;
124 ex rhs = rand()%201-100;
125 for (unsigned j=0; j<n; ++j) {
126 // ...with small coefficients to give degeneracy a chance...
127 lhs += a[j]*(rand()%21-10);
128 rhs += x[j]*(rand()%21-10);
130 eqns.append(lhs==rhs);
134 sol = lsolve(eqns, vars);
136 // ...and check the solution:
137 if (sol.nops() == 0) {
138 // no solution was found
139 // is the coefficient matrix really, really, really degenerate?
140 matrix coeffmat(n,n);
141 for (unsigned ro=0; ro<n; ++ro)
142 for (unsigned co=0; co<n; ++co)
143 coeffmat.set(ro,co,eqns.op(co).rhs().coeff(a[co],1));
144 if (!coeffmat.determinant().is_zero()) {
146 clog << "solution of the system " << eqns << " for " << vars
147 << " was not found" << endl;
150 // insert the solution into rhs of out equations
151 bool errorflag = false;
152 for (unsigned i=0; i<n; ++i)
153 if (eqns.op(i).rhs().subs(sol) != eqns.op(i).lhs())
157 clog << "solution of the system " << eqns << " for " << vars
158 << " erroneously returned " << sol << endl;
166 unsigned check_lsolve()
170 cout << "checking linear solve" << flush;
172 // solve some numeric linear systems
173 for (unsigned n=1; n<14; ++n)
174 result += check_matrix_solve(n, n, 1, 0);
175 cout << '.' << flush;
176 // solve some underdetermined numeric systems
177 for (unsigned n=1; n<14; ++n)
178 result += check_matrix_solve(n+1, n, 1, 0);
179 cout << '.' << flush;
180 // solve some overdetermined numeric systems
181 for (unsigned n=1; n<14; ++n)
182 result += check_matrix_solve(n, n+1, 1, 0);
183 cout << '.' << flush;
184 // solve some multiple numeric systems
185 for (unsigned n=1; n<14; ++n)
186 result += check_matrix_solve(n, n, n/3+1, 0);
187 cout << '.' << flush;
188 // solve some symbolic linear systems
189 for (unsigned n=1; n<8; ++n)
190 result += check_matrix_solve(n, n, 1, 2);
191 cout << '.' << flush;
193 // check lsolve, the wrapper function around matrix::solve()
194 result += check_inifcns_lsolve(2); cout << '.' << flush;
195 result += check_inifcns_lsolve(3); cout << '.' << flush;
196 result += check_inifcns_lsolve(4); cout << '.' << flush;
197 result += check_inifcns_lsolve(5); cout << '.' << flush;
198 result += check_inifcns_lsolve(6); cout << '.' << flush;
203 int main(int argc, char** argv)
205 return check_lsolve();