File: exam_lsolve.cpp

package info (click to toggle)
ginac 1.0.8-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 3,544 kB
  • ctags: 3,232
  • sloc: cpp: 27,732; sh: 7,126; perl: 1,819; yacc: 763; lex: 345; makefile: 221; sed: 32
file content (208 lines) | stat: -rw-r--r-- 5,937 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/** @file exam_lsolve.cpp
 *
 *  These exams test solving small linear systems of symbolic equations. */

/*
 *  GiNaC Copyright (C) 1999-2002 Johannes Gutenberg University Mainz, Germany
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "exams.h"

static unsigned exam_lsolve1(void)
{
	// A trivial example.
	unsigned result = 0;
	symbol x("x");
	ex eq, aux;
	
	eq = (3*x+5 == numeric(8));
	aux = lsolve(eq, x);
	if (aux != 1) {
		++result;
		clog << "solution of 3*x+5==8 erroneously returned "
		     << aux << endl;
	}
	
	return result;
}

static unsigned exam_lsolve2a(void)
{
	// An example from the Maple online help.
	unsigned result = 0;
	symbol a("a"), b("b"), x("x"), y("y");
	lst eqns, vars;
	ex sol;
	
	// Create the linear system [a*x+b*y==3,x-y==b]...
	eqns.append(a*x+b*y==3).append(x-y==b);
	// ...to be solved for [x,y]...
	vars.append(x).append(y);
	// ...and solve it:
	sol = lsolve(eqns, vars);
	ex sol_x = sol.op(0).rhs();  // rhs of solution for first variable (x)
	ex sol_y = sol.op(1).rhs();  // rhs of solution for second variable (y)
	
	// It should have returned [x==(3+b^2)/(a+b),y==(3-a*b)/(a+b)]
	if (!normal(sol_x - (3+pow(b,2))/(a+b)).is_zero() ||
		!normal(sol_y - (3-a*b)/(a+b)).is_zero()) {
		++result;
		clog << "solution of the system " << eqns << " for " << vars
		     << " erroneously returned " << sol << endl;
	}
	
	return result;
}

static unsigned exam_lsolve2b(void)
{
	// A boring example from Mathematica's online help.
	unsigned result = 0;
	symbol x("x"), y("y");
	lst eqns, vars;
	ex sol;
	
	// Create the linear system [3*x+y==7,2*x-5*y==8]...
	eqns.append(3*x+y==7).append(2*x-5*y==8);
	// ...to be solved for [x,y]...
	vars.append(x).append(y);
	// ...and solve it:
	sol = lsolve(eqns, vars);
	ex sol_x = sol.op(0).rhs();  // rhs of solution for first variable (x)
	ex sol_y = sol.op(1).rhs();  // rhs of solution for second variable (y)
	
	// It should have returned [x==43/17,y==-10/17]
	if ((sol_x != numeric(43,17)) ||
		(sol_y != numeric(-10,17))) {
		++result;
		clog << "solution of the system " << eqns << " for " << vars
		     << " erroneously returned " << sol << endl;
	}
	
	return result;
}

static unsigned exam_lsolve2c(void)
{
	// A more interesting example from the Maple online help.
	unsigned result = 0;
	symbol x("x"), y("y");
	lst eqns, vars;
	ex sol;
	
	// Create the linear system [I*x+y==1,I*x-y==2]...
	eqns.append(I*x+y==1).append(I*x-y==2);
	// ...to be solved for [x,y]...
	vars.append(x).append(y);
	// ...and solve it:
	sol = lsolve(eqns, vars);
	ex sol_x = sol.op(0).rhs();  // rhs of solution for first variable (x)
	ex sol_y = sol.op(1).rhs();  // rhs of solution for second variable (y)
	
	// It should have returned [x==-3/2*I,y==-1/2]
	if ((sol_x != numeric(-3,2)*I) ||
		(sol_y != numeric(-1,2))) {
		++result;
		clog << "solution of the system " << eqns << " for " << vars
		     << " erroneously returned " << sol << endl;
	}
	
	return result;
}

static unsigned exam_lsolve2S(void)
{
	// A degenerate example that went wrong in GiNaC 0.6.2.
	unsigned result = 0;
	symbol x("x"), y("y"), t("t");
	lst eqns, vars;
	ex sol;
	
	// Create the linear system [0*x+0*y==0,0*x+1*y==t]...
	eqns.append(0*x+0*y==0).append(0*x+1*y==t);
	// ...to be solved for [x,y]...
	vars.append(x).append(y);
	// ...and solve it:
	sol = lsolve(eqns, vars);
	ex sol_x = sol.op(0).rhs();  // rhs of solution for first variable (x)
	ex sol_y = sol.op(1).rhs();  // rhs of solution for second variable (y)
	
	// It should have returned [x==x,y==t]
	if ((sol_x != x) ||
		(sol_y != t)) {
		++result;
		clog << "solution of the system " << eqns << " for " << vars
		     << " erroneously returned " << sol << endl;
	}
	
	return result;
}

static unsigned exam_lsolve3S(void)
{
	// A degenerate example that went wrong while trying to improve elimination
	unsigned result = 0;
	symbol b("b"), c("c");
	symbol x("x"), y("y"), z("z");
	lst eqns, vars;
	ex sol;
	
	// Create the linear system [y+z==b,-y+z==c] with one additional row...
	eqns.append(ex(0)==ex(0)).append(b==z+y).append(c==z-y);
	// ...to be solved for [x,y,z]...
	vars.append(x).append(y).append(z);
	// ...and solve it:
	sol = lsolve(eqns, vars);
	ex sol_x = sol.op(0).rhs();  // rhs of solution for first variable (x)
	ex sol_y = sol.op(1).rhs();  // rhs of solution for second variable (y)
	ex sol_z = sol.op(2).rhs();  // rhs of solution for third variable (z)
	
	// It should have returned [x==x,y==t,]
	if ((sol_x != x) ||
		(sol_y != (b-c)/2) ||
		(sol_z != (b+c)/2)) {
		++result;
		clog << "solution of the system " << eqns << " for " << vars
		     << " erroneously returned " << sol << endl;
	}
	
	return result;
}

unsigned exam_lsolve(void)
{
	unsigned result = 0;
	
	cout << "examining linear solve" << flush;
	clog << "----------linear solve:" << endl;
	
	result += exam_lsolve1();  cout << '.' << flush;
	result += exam_lsolve2a();  cout << '.' << flush;
	result += exam_lsolve2b();  cout << '.' << flush;
	result += exam_lsolve2c();  cout << '.' << flush;
	result += exam_lsolve2S();  cout << '.' << flush;
	result += exam_lsolve3S();  cout << '.' << flush;
	
	if (!result) {
		cout << " passed " << endl;
		clog << "(no output)" << endl;
	} else {
		cout << " failed " << endl;
	}
	
	return result;
}