]> www.ginac.de Git - ginac.git/blob - check/exam_lsolve.cpp
Change www.ginac.de urls to https.
[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-2020 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 "ginac.h"
24 using namespace GiNaC;
25
26 #include <iostream>
27 using namespace std;
28
29 static unsigned exam_lsolve1()
30 {
31         // A trivial example.
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 exam_lsolve2a()
48 {
49         // An example from the Maple online help.
50         unsigned result = 0;
51         symbol a("a"), b("b"), x("x"), y("y");
52         lst eqns, vars;
53         ex sol;
54         
55         // Create the linear system [a*x+b*y==3,x-y==b]...
56         eqns.append(a*x+b*y==3).append(x-y==b);
57         // ...to be solved for [x,y]...
58         vars.append(x).append(y);
59         // ...and solve it:
60         sol = lsolve(eqns, vars);
61         ex sol_x = sol.op(0).rhs();  // rhs of solution for first variable (x)
62         ex sol_y = sol.op(1).rhs();  // rhs of solution for second variable (y)
63         
64         // It should have returned [x==(3+b^2)/(a+b),y==(3-a*b)/(a+b)]
65         if (!normal(sol_x - (3+pow(b,2))/(a+b)).is_zero() ||
66                 !normal(sol_y - (3-a*b)/(a+b)).is_zero()) {
67                 ++result;
68                 clog << "solution of the system " << eqns << " for " << vars
69                      << " erroneously returned " << sol << endl;
70         }
71         
72         return result;
73 }
74
75 static unsigned exam_lsolve2b()
76 {
77         // A boring example from Mathematica's online help.
78         unsigned result = 0;
79         symbol x("x"), y("y");
80         lst eqns, vars;
81         ex sol;
82         
83         // Create the linear system [3*x+y==7,2*x-5*y==8]...
84         eqns.append(3*x+y==7).append(2*x-5*y==8);
85         // ...to be solved for [x,y]...
86         vars.append(x).append(y);
87         // ...and solve it:
88         sol = lsolve(eqns, vars);
89         ex sol_x = sol.op(0).rhs();  // rhs of solution for first variable (x)
90         ex sol_y = sol.op(1).rhs();  // rhs of solution for second variable (y)
91         
92         // It should have returned [x==43/17,y==-10/17]
93         if ((sol_x != numeric(43,17)) ||
94                 (sol_y != numeric(-10,17))) {
95                 ++result;
96                 clog << "solution of the system " << eqns << " for " << vars
97                      << " erroneously returned " << sol << endl;
98         }
99         
100         return result;
101 }
102
103 static unsigned exam_lsolve2c()
104 {
105         // A more interesting example from the Maple online help.
106         unsigned result = 0;
107         symbol x("x"), y("y");
108         lst eqns, vars;
109         ex sol;
110         
111         // Create the linear system [I*x+y==1,I*x-y==2]...
112         eqns.append(I*x+y==1).append(I*x-y==2);
113         // ...to be solved for [x,y]...
114         vars.append(x).append(y);
115         // ...and solve it:
116         sol = lsolve(eqns, vars);
117         ex sol_x = sol.op(0).rhs();  // rhs of solution for first variable (x)
118         ex sol_y = sol.op(1).rhs();  // rhs of solution for second variable (y)
119         
120         // It should have returned [x==-3/2*I,y==-1/2]
121         if ((sol_x != numeric(-3,2)*I) ||
122                 (sol_y != numeric(-1,2))) {
123                 ++result;
124                 clog << "solution of the system " << eqns << " for " << vars
125                      << " erroneously returned " << sol << endl;
126         }
127         
128         return result;
129 }
130
131 static unsigned exam_lsolve2S()
132 {
133         // A degenerate example that went wrong in GiNaC 0.6.2.
134         unsigned result = 0;
135         symbol x("x"), y("y"), t("t");
136         lst eqns, vars;
137         ex sol;
138         
139         // Create the linear system [0*x+0*y==0,0*x+1*y==t]...
140         eqns.append(0*x+0*y==0).append(0*x+1*y==t);
141         // ...to be solved for [x,y]...
142         vars.append(x).append(y);
143         // ...and solve it:
144         sol = lsolve(eqns, vars);
145         ex sol_x = sol.op(0).rhs();  // rhs of solution for first variable (x)
146         ex sol_y = sol.op(1).rhs();  // rhs of solution for second variable (y)
147         
148         // It should have returned [x==x,y==t]
149         if ((sol_x != x) ||
150                 (sol_y != t)) {
151                 ++result;
152                 clog << "solution of the system " << eqns << " for " << vars
153                      << " erroneously returned " << sol << endl;
154         }
155         
156         return result;
157 }
158
159 static unsigned exam_lsolve3S()
160 {
161         // A degenerate example that went wrong while trying to improve elimination
162         unsigned result = 0;
163         symbol b("b"), c("c");
164         symbol x("x"), y("y"), z("z");
165         lst eqns, vars;
166         ex sol;
167         
168         // Create the linear system [y+z==b,-y+z==c] with one additional row...
169         eqns.append(ex(0)==ex(0)).append(b==z+y).append(c==z-y);
170         // ...to be solved for [x,y,z]...
171         vars.append(x).append(y).append(z);
172         // ...and solve it:
173         sol = lsolve(eqns, vars);
174         ex sol_x = sol.op(0).rhs();  // rhs of solution for first variable (x)
175         ex sol_y = sol.op(1).rhs();  // rhs of solution for second variable (y)
176         ex sol_z = sol.op(2).rhs();  // rhs of solution for third variable (z)
177         
178         // It should have returned [x==x,y==t,]
179         if ((sol_x != x) ||
180                 (sol_y != (b-c)/2) ||
181                 (sol_z != (b+c)/2)) {
182                 ++result;
183                 clog << "solution of the system " << eqns << " for " << vars
184                      << " erroneously returned " << sol << endl;
185         }
186         
187         return result;
188 }
189
190 unsigned exam_lsolve()
191 {
192         unsigned result = 0;
193         
194         cout << "examining linear solve" << flush;
195         
196         result += exam_lsolve1();  cout << '.' << flush;
197         result += exam_lsolve2a();  cout << '.' << flush;
198         result += exam_lsolve2b();  cout << '.' << flush;
199         result += exam_lsolve2c();  cout << '.' << flush;
200         result += exam_lsolve2S();  cout << '.' << flush;
201         result += exam_lsolve3S();  cout << '.' << flush;
202         
203         return result;
204 }
205
206 int main(int argc, char** argv)
207 {
208         return exam_lsolve();
209 }