1 /** @file exam_lsolve.cpp
3 * These exams test solving small linear systems of symbolic equations. */
6 * GiNaC Copyright (C) 1999-2000 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 static unsigned exam_lsolve1(void)
32 eq = (3*x+5 == numeric(8));
36 clog << "solution of 3*x+5==8 erroneously returned "
43 static unsigned exam_lsolve2a(void)
45 // An example from the Maple online help.
47 symbol a("a"), b("b"), x("x"), y("y");
51 // Create the linear system [a*x+b*y==3,x-y==b]...
52 eqns.append(a*x+b*y==3).append(x-y==b);
53 // ...to be solved for [x,y]...
54 vars.append(x).append(y);
56 sol = lsolve(eqns, vars);
57 ex sol_x = sol.op(0).rhs(); // rhs of solution for first variable (x)
58 ex sol_y = sol.op(1).rhs(); // rhs of solution for second variable (y)
60 // It should have returned [x==(3+b^2)/(a+b),y==(3-a*b)/(a+b)]
61 if (!normal(sol_x - (3+pow(b,2))/(a+b)).is_zero() ||
62 !normal(sol_y - (3-a*b)/(a+b)).is_zero()) {
64 clog << "solution of the system " << eqns << " for " << vars
65 << " erroneously returned " << sol << endl;
71 static unsigned exam_lsolve2b(void)
73 // A boring example from Mathematica's online help.
75 symbol x("x"), y("y");
79 // Create the linear system [3*x+y==7,2*x-5*y==8]...
80 eqns.append(3*x+y==7).append(2*x-5*y==8);
81 // ...to be solved for [x,y]...
82 vars.append(x).append(y);
84 sol = lsolve(eqns, vars);
85 ex sol_x = sol.op(0).rhs(); // rhs of solution for first variable (x)
86 ex sol_y = sol.op(1).rhs(); // rhs of solution for second variable (y)
88 // It should have returned [x==43/17,y==-10/17]
89 if (!(sol_x - numeric(43,17)).is_zero() ||
90 !(sol_y - numeric(-10,17)).is_zero()) {
92 clog << "solution of the system " << eqns << " for " << vars
93 << " erroneously returned " << sol << endl;
99 static unsigned exam_lsolve2c(void)
101 // A more interesting example from the Maple online help.
103 symbol x("x"), y("y");
107 // Create the linear system [I*x+y==1,I*x-y==2]...
108 eqns.append(I*x+y==1).append(I*x-y==2);
109 // ...to be solved for [x,y]...
110 vars.append(x).append(y);
112 sol = lsolve(eqns, vars);
113 ex sol_x = sol.op(0).rhs(); // rhs of solution for first variable (x)
114 ex sol_y = sol.op(1).rhs(); // rhs of solution for second variable (y)
116 // It should have returned [x==-3/2*I,y==-1/2]
117 if (!(sol_x - numeric(-3,2)*I).is_zero() ||
118 !(sol_y - numeric(-1,2)).is_zero()) {
120 clog << "solution of the system " << eqns << " for " << vars
121 << " erroneously returned " << sol << endl;
127 unsigned exam_lsolve(void)
131 cout << "examining linear solve" << flush;
132 clog << "----------linear solve:" << endl;
134 result += exam_lsolve1(); cout << '.' << flush;
135 result += exam_lsolve2a(); cout << '.' << flush;
136 result += exam_lsolve2b(); cout << '.' << flush;
137 result += exam_lsolve2c(); cout << '.' << flush;
140 cout << " passed " << endl;
141 clog << "(no output)" << endl;
143 cout << " failed " << endl;