]> www.ginac.de Git - ginac.git/blob - check/linear_solve.cpp
- Patch for static libreadline w/ libncurses problem, submitted by B.Haible.
[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     // A trivial example.
33     unsigned result = 0;
34     symbol x("x");
35     ex eq, aux;
36     
37     eq = (3*x+5 == numeric(8));
38     aux = lsolve(eq, x);
39     if (aux != 1) {
40         result++;
41         clog << "solution of 3*x+5==8 erroneously returned "
42              << aux << endl;
43     }
44     
45     return result;
46 }
47
48 static unsigned lsolve2a(void)
49 {
50     // An example from the Maple online-help.
51     unsigned result = 0;
52     symbol a("a"), b("b"), x("x"), y("y");
53     lst eqns, vars;
54     ex sol;
55     
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);
60     // ...and solve it:
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)
64     
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()) {
68         result++;
69         clog << "solution of the system " << eqns << " for " << vars
70              << " erroneously returned " << sol << endl;
71     }
72     
73     return result;
74 }
75
76 static unsigned lsolve2b(void)
77 {
78     // A boring example from Mathematica's online-help.
79     unsigned result = 0;
80     symbol x("x"), y("y");
81     lst eqns, vars;
82     ex sol;
83     
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);
88     // ...and solve it:
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)
92     
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()) {
96         result++;
97         clog << "solution of the system " << eqns << " for " << vars
98              << " erroneously returned " << sol << endl;
99     }
100     
101     return result;
102 }
103
104 static unsigned lsolve2c(void)
105 {
106     // An example from the Maple online-help.
107     unsigned result = 0;
108     symbol x("x"), y("y");
109     lst eqns, vars;
110     ex sol;
111     
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);
116     // ...and solve it:
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)
120     
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()) {
124         result++;
125         clog << "solution of the system " << eqns << " for " << vars
126              << " erroneously returned " << sol << endl;
127     }
128     
129     return result;
130 }
131
132 unsigned linear_solve(void)
133 {
134     unsigned result = 0;
135     
136     cout << "checking linear solve..." << flush;
137     clog << "---------linear solve:" << endl;
138     
139     result += lsolve1();
140     result += lsolve2a();
141     result += lsolve2b();
142     result += lsolve2c();
143     
144     if (!result) {
145         cout << " passed ";
146         clog << "(no output)" << endl;
147     } else {
148         cout << " failed ";
149     }
150     
151     return result;
152 }