1 /** @file linear_solve.cpp
3 * These test routines do some simple checks on solving linear systems of
4 * symbolic equations. */
7 * GiNaC Copyright (C) 1999-2000 Johannes Gutenberg University Mainz, Germany
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #ifndef NO_NAMESPACE_GINAC
27 using namespace GiNaC;
28 #endif // ndef NO_NAMESPACE_GINAC
30 static unsigned lsolve1(void)
37 eq = (3*x+5 == numeric(8));
41 clog << "solution of 3*x+5==8 erroneously returned "
48 static unsigned lsolve2a(void)
50 // An example from the Maple online-help.
52 symbol a("a"), b("b"), x("x"), y("y");
56 // Create the linear system [a*x+b*y==3,x-y==b]...
57 eqns.append(a*x+b*y==3).append(x-y==b);
58 // ...to be solved for [x,y]...
59 vars.append(x).append(y);
61 sol = lsolve(eqns, vars);
62 ex sol_x = sol.op(0).rhs(); // rhs of solution for first variable (x)
63 ex sol_y = sol.op(1).rhs(); // rhs of solution for second variable (y)
65 // It should have returned [x==(3+b^2)/(a+b),y==(3-a*b)/(a+b)]
66 if (!normal(sol_x - (3+pow(b,2))/(a+b)).is_zero() ||
67 !normal(sol_y - (3-a*b)/(a+b)).is_zero()) {
69 clog << "solution of the system " << eqns << " for " << vars
70 << " erroneously returned " << sol << endl;
76 static unsigned lsolve2b(void)
78 // A boring example from Mathematica's online-help.
80 symbol x("x"), y("y");
84 // Create the linear system [3*x+y==7,2*x-5*y==8]...
85 eqns.append(3*x+y==7).append(2*x-5*y==8);
86 // ...to be solved for [x,y]...
87 vars.append(x).append(y);
89 sol = lsolve(eqns, vars);
90 ex sol_x = sol.op(0).rhs(); // rhs of solution for first variable (x)
91 ex sol_y = sol.op(1).rhs(); // rhs of solution for second variable (y)
93 // It should have returned [x==43/17,y==-10/17]
94 if (!(sol_x - numeric(43,17)).is_zero() ||
95 !(sol_y - numeric(-10,17)).is_zero()) {
97 clog << "solution of the system " << eqns << " for " << vars
98 << " erroneously returned " << sol << endl;
104 static unsigned lsolve2c(void)
106 // An example from the Maple online-help.
108 symbol x("x"), y("y");
112 // Create the linear system [I*x+y==1,I*x-y==2]...
113 eqns.append(I*x+y==1).append(I*x-y==2);
114 // ...to be solved for [x,y]...
115 vars.append(x).append(y);
117 sol = lsolve(eqns, vars);
118 ex sol_x = sol.op(0).rhs(); // rhs of solution for first variable (x)
119 ex sol_y = sol.op(1).rhs(); // rhs of solution for second variable (y)
121 // It should have returned [x==-3/2*I,y==-1/2]
122 if (!(sol_x - numeric(-3,2)*I).is_zero() ||
123 !(sol_y - numeric(-1,2)).is_zero()) {
125 clog << "solution of the system " << eqns << " for " << vars
126 << " erroneously returned " << sol << endl;
132 unsigned linear_solve(void)
136 cout << "checking linear solve..." << flush;
137 clog << "---------linear solve:" << endl;
140 result += lsolve2a();
141 result += lsolve2b();
142 result += lsolve2c();
146 clog << "(no output)" << endl;