]> www.ginac.de Git - ginac.git/blob - check/exam_lsolve.cpp
build: don't run any ${host} binaries while checking for readline.
[ginac.git] / check / exam_lsolve.cpp
1 /** @file exam_lsolve.cpp
2  *
3  *  These exams test solving small linear systems of symbolic equations. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2008 Johannes Gutenberg University Mainz, Germany
7  *
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.
12  *
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.
17  *
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
21  */
22
23 #include <iostream>
24 #include "ginac.h"
25 using namespace std;
26 using namespace GiNaC;
27
28 static unsigned exam_lsolve1()
29 {
30         // A trivial example.
31         unsigned result = 0;
32         symbol x("x");
33         ex eq, aux;
34         
35         eq = (3*x+5 == numeric(8));
36         aux = lsolve(eq, x);
37         if (aux != 1) {
38                 ++result;
39                 clog << "solution of 3*x+5==8 erroneously returned "
40                      << aux << endl;
41         }
42         
43         return result;
44 }
45
46 static unsigned exam_lsolve2a()
47 {
48         // An example from the Maple online help.
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 (!normal(sol_x - (3+pow(b,2))/(a+b)).is_zero() ||
65                 !normal(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 exam_lsolve2b()
75 {
76         // A boring example from Mathematica's online help.
77         unsigned result = 0;
78         symbol x("x"), y("y");
79         lst eqns, vars;
80         ex sol;
81         
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);
86         // ...and solve it:
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)
90         
91         // It should have returned [x==43/17,y==-10/17]
92         if ((sol_x != numeric(43,17)) ||
93                 (sol_y != numeric(-10,17))) {
94                 ++result;
95                 clog << "solution of the system " << eqns << " for " << vars
96                      << " erroneously returned " << sol << endl;
97         }
98         
99         return result;
100 }
101
102 static unsigned exam_lsolve2c()
103 {
104         // A more interesting example from the Maple online help.
105         unsigned result = 0;
106         symbol x("x"), y("y");
107         lst eqns, vars;
108         ex sol;
109         
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);
114         // ...and solve it:
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)
118         
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))) {
122                 ++result;
123                 clog << "solution of the system " << eqns << " for " << vars
124                      << " erroneously returned " << sol << endl;
125         }
126         
127         return result;
128 }
129
130 static unsigned exam_lsolve2S()
131 {
132         // A degenerate example that went wrong in GiNaC 0.6.2.
133         unsigned result = 0;
134         symbol x("x"), y("y"), t("t");
135         lst eqns, vars;
136         ex sol;
137         
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);
142         // ...and solve it:
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)
146         
147         // It should have returned [x==x,y==t]
148         if ((sol_x != x) ||
149                 (sol_y != t)) {
150                 ++result;
151                 clog << "solution of the system " << eqns << " for " << vars
152                      << " erroneously returned " << sol << endl;
153         }
154         
155         return result;
156 }
157
158 static unsigned exam_lsolve3S()
159 {
160         // A degenerate example that went wrong while trying to improve elimination
161         unsigned result = 0;
162         symbol b("b"), c("c");
163         symbol x("x"), y("y"), z("z");
164         lst eqns, vars;
165         ex sol;
166         
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);
171         // ...and solve it:
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)
176         
177         // It should have returned [x==x,y==t,]
178         if ((sol_x != x) ||
179                 (sol_y != (b-c)/2) ||
180                 (sol_z != (b+c)/2)) {
181                 ++result;
182                 clog << "solution of the system " << eqns << " for " << vars
183                      << " erroneously returned " << sol << endl;
184         }
185         
186         return result;
187 }
188
189 unsigned exam_lsolve()
190 {
191         unsigned result = 0;
192         
193         cout << "examining linear solve" << flush;
194         
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;
201         
202         return result;
203 }
204
205 int main(int argc, char** argv)
206 {
207         return exam_lsolve();
208 }