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)
36 eq = (3*x+5 == numeric(8));
40 clog << "solution of 3*x+5==8 erroneously returned "
47 static unsigned lsolve2a(void)
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 (!(sol_x - (3+pow(b,2))/(a+b)).is_zero() ||
65 !(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 lsolve2b(void)
77 symbol x("x"), y("y");
81 // Create the linear system [I*x+y==1,I*x-y==2]...
82 eqns.append(I*x+y==1).append(I*x-y==2);
83 // ...to be solved for [x,y]...
84 vars.append(x).append(y);
86 sol = lsolve(eqns, vars);
87 ex sol_x = sol.op(0).rhs(); // rhs of solution for first variable (x)
88 ex sol_y = sol.op(1).rhs(); // rhs of solution for second variable (y)
90 // It should have returned [x==-3/2*I,y==-1/2]
91 if (!(sol_x - numeric(-3,2)*I).is_zero() ||
92 !(sol_y - numeric(-1,2)).is_zero()) {
94 clog << "solution of the system " << eqns << " for " << vars
95 << " erroneously returned " << sol << endl;
101 unsigned linear_solve(void)
105 cout << "checking linear solve..." << flush;
106 clog << "---------linear solve:" << endl;
109 result += lsolve2a();
110 result += lsolve2b();
114 clog << "(no output)" << endl;