1 /** @file exam_lsolve.cpp
3 * These exams test solving small linear systems of symbolic equations. */
6 * GiNaC Copyright (C) 1999-2007 Johannes Gutenberg University Mainz, Germany
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 using namespace GiNaC;
28 static unsigned exam_lsolve1()
35 eq = (3*x+5 == numeric(8));
39 clog << "solution of 3*x+5==8 erroneously returned "
46 static unsigned exam_lsolve2a()
48 // An example from the Maple online help.
50 symbol a("a"), b("b"), x("x"), y("y");
54 // Create the linear system [a*x+b*y==3,x-y==b]...
55 eqns.append(a*x+b*y==3).append(x-y==b);
56 // ...to be solved for [x,y]...
57 vars.append(x).append(y);
59 sol = lsolve(eqns, vars);
60 ex sol_x = sol.op(0).rhs(); // rhs of solution for first variable (x)
61 ex sol_y = sol.op(1).rhs(); // rhs of solution for second variable (y)
63 // It should have returned [x==(3+b^2)/(a+b),y==(3-a*b)/(a+b)]
64 if (!normal(sol_x - (3+pow(b,2))/(a+b)).is_zero() ||
65 !normal(sol_y - (3-a*b)/(a+b)).is_zero()) {
67 clog << "solution of the system " << eqns << " for " << vars
68 << " erroneously returned " << sol << endl;
74 static unsigned exam_lsolve2b()
76 // A boring example from Mathematica's online help.
78 symbol x("x"), y("y");
82 // Create the linear system [3*x+y==7,2*x-5*y==8]...
83 eqns.append(3*x+y==7).append(2*x-5*y==8);
84 // ...to be solved for [x,y]...
85 vars.append(x).append(y);
87 sol = lsolve(eqns, vars);
88 ex sol_x = sol.op(0).rhs(); // rhs of solution for first variable (x)
89 ex sol_y = sol.op(1).rhs(); // rhs of solution for second variable (y)
91 // It should have returned [x==43/17,y==-10/17]
92 if ((sol_x != numeric(43,17)) ||
93 (sol_y != numeric(-10,17))) {
95 clog << "solution of the system " << eqns << " for " << vars
96 << " erroneously returned " << sol << endl;
102 static unsigned exam_lsolve2c()
104 // A more interesting example from the Maple online help.
106 symbol x("x"), y("y");
110 // Create the linear system [I*x+y==1,I*x-y==2]...
111 eqns.append(I*x+y==1).append(I*x-y==2);
112 // ...to be solved for [x,y]...
113 vars.append(x).append(y);
115 sol = lsolve(eqns, vars);
116 ex sol_x = sol.op(0).rhs(); // rhs of solution for first variable (x)
117 ex sol_y = sol.op(1).rhs(); // rhs of solution for second variable (y)
119 // It should have returned [x==-3/2*I,y==-1/2]
120 if ((sol_x != numeric(-3,2)*I) ||
121 (sol_y != numeric(-1,2))) {
123 clog << "solution of the system " << eqns << " for " << vars
124 << " erroneously returned " << sol << endl;
130 static unsigned exam_lsolve2S()
132 // A degenerate example that went wrong in GiNaC 0.6.2.
134 symbol x("x"), y("y"), t("t");
138 // Create the linear system [0*x+0*y==0,0*x+1*y==t]...
139 eqns.append(0*x+0*y==0).append(0*x+1*y==t);
140 // ...to be solved for [x,y]...
141 vars.append(x).append(y);
143 sol = lsolve(eqns, vars);
144 ex sol_x = sol.op(0).rhs(); // rhs of solution for first variable (x)
145 ex sol_y = sol.op(1).rhs(); // rhs of solution for second variable (y)
147 // It should have returned [x==x,y==t]
151 clog << "solution of the system " << eqns << " for " << vars
152 << " erroneously returned " << sol << endl;
158 static unsigned exam_lsolve3S()
160 // A degenerate example that went wrong while trying to improve elimination
162 symbol b("b"), c("c");
163 symbol x("x"), y("y"), z("z");
167 // Create the linear system [y+z==b,-y+z==c] with one additional row...
168 eqns.append(ex(0)==ex(0)).append(b==z+y).append(c==z-y);
169 // ...to be solved for [x,y,z]...
170 vars.append(x).append(y).append(z);
172 sol = lsolve(eqns, vars);
173 ex sol_x = sol.op(0).rhs(); // rhs of solution for first variable (x)
174 ex sol_y = sol.op(1).rhs(); // rhs of solution for second variable (y)
175 ex sol_z = sol.op(2).rhs(); // rhs of solution for third variable (z)
177 // It should have returned [x==x,y==t,]
179 (sol_y != (b-c)/2) ||
180 (sol_z != (b+c)/2)) {
182 clog << "solution of the system " << eqns << " for " << vars
183 << " erroneously returned " << sol << endl;
189 unsigned exam_lsolve()
193 cout << "examining linear solve" << flush;
195 result += exam_lsolve1(); cout << '.' << flush;
196 result += exam_lsolve2a(); cout << '.' << flush;
197 result += exam_lsolve2b(); cout << '.' << flush;
198 result += exam_lsolve2c(); cout << '.' << flush;
199 result += exam_lsolve2S(); cout << '.' << flush;
200 result += exam_lsolve3S(); cout << '.' << flush;
205 int main(int argc, char** argv)
207 return exam_lsolve();