]> www.ginac.de Git - ginac.git/blob - check/linear_solve.cpp
- inserted correct date.
[ginac.git] / check / linear_solve.cpp
1 /** @file linear_solve.cpp
2  *
3  * These test routines do some simple checks on solving linear systems of
4  * symbolic equations. */
5
6 /*
7  *  GiNaC Copyright (C) 1999-2000 Johannes Gutenberg University Mainz, Germany
8  *
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.
13  *
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.
18  *
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
22  */
23
24 #include "ginac.h"
25
26 #ifndef NO_NAMESPACE_GINAC
27 using namespace GiNaC;
28 #endif // ndef NO_NAMESPACE_GINAC
29
30 static unsigned lsolve1(void)
31 {
32     unsigned result = 0;
33     symbol x("x");
34     ex eq, aux;
35     
36     eq = (3*x+5 == numeric(8));
37     aux = lsolve(eq, x);
38     if (aux != 1) {
39         result++;
40         clog << "solution of 3*x+5==8 erroneously returned "
41              << aux << endl;
42     }
43     
44     return result;
45 }
46
47 static unsigned lsolve2a(void)
48 {
49     unsigned result = 0;
50     symbol a("a"), b("b"), x("x"), y("y");
51     lst eqns, vars;
52     ex sol;
53     
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);
58     // ...and solve it:
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)
62     
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()) {
66         result++;
67         clog << "solution of the system " << eqns << " for " << vars
68              << " erroneously returned " << sol << endl;
69     }
70     
71     return result;
72 }
73
74 static unsigned lsolve2b(void)
75 {
76     unsigned result = 0;
77     symbol x("x"), y("y");
78     lst eqns, vars;
79     ex sol;
80     
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);
85     // ...and solve it:
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)
89
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()) {
93         result++;
94         clog << "solution of the system " << eqns << " for " << vars
95              << " erroneously returned " << sol << endl;
96     }
97     
98     return result;
99 }
100
101 unsigned linear_solve(void)
102 {
103     unsigned result = 0;
104     
105     cout << "checking linear solve..." << flush;
106     clog << "---------linear solve:" << endl;
107     
108     result += lsolve1();
109     result += lsolve2a();
110     result += lsolve2b();
111     
112     if (!result) {
113         cout << " passed ";
114         clog << "(no output)" << endl;
115     } else {
116         cout << " failed ";
117     }
118     
119     return result;
120 }