]> www.ginac.de Git - ginac.git/blob - check/exam_misc.cpp
2d0f29e152c21f80a6e8a52480dbec6acf61b1ed
[ginac.git] / check / exam_misc.cpp
1 /** @file exam_misc.cpp
2  *
3  */
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 #define VECSIZE 30
29 static unsigned exam_expand_subs()
30 {
31         unsigned result = 0;
32         symbol a[VECSIZE];
33         ex e, aux;
34         
35         for (unsigned i=0; i<VECSIZE; ++i)
36                 e = e + a[i];
37         
38         // prepare aux so it will swallow anything but a1^2:
39         aux = -e + a[0] + a[1];
40         e = expand(subs(expand(pow(e, 2)), a[0] == aux));
41         
42         if (e != pow(a[1],2)) {
43                 clog << "Denny Fliegner's quick consistency check erroneously returned "
44                      << e << "." << endl;
45                 ++result;
46         }
47         
48         return result;
49 }
50
51 /*  A simple modification of Denny Fliegner's three step consistency test:
52  *  1)  e = (a0 + a1)^200
53  *  2)  expand e
54  *  3)  substitute a0 by -a1 in e
55  *  after which e should return 0 (without expanding). */
56 static unsigned exam_expand_subs2()
57 {
58         unsigned result = 0;
59         symbol a("a"), b("b");
60         ex e, f;
61         
62         e = pow(a+b,200).expand();
63         f = e.subs(a == -b);
64         
65         if (f != 0) {
66                 clog << "e = pow(a+b,200).expand(); f = e.subs(a == -b); erroneously returned "
67                      << f << " instead of simplifying to 0." << endl;
68                 ++result;
69         }
70         
71         return result;
72 }
73
74 static unsigned exam_expand_power()
75 {
76         unsigned result = 0;
77         symbol x("x"), a("a"), b("b");
78         ex e;
79         
80         e = pow(x,pow(a+b,2)-pow(a,2)-pow(b,2)-a*b*2).expand();
81         
82         if (e != 1) {
83                 clog << "e = pow(x,pow(a+b,2)-pow(a,2)-pow(b,2)-a*b*2).expand(); erroneously returned "
84                      << e << " instead of simplifying to 1." << endl;
85                 ++result;
86         }
87         
88         return result;
89 }
90
91 static unsigned exam_sqrfree()
92 {
93         unsigned result = 0;
94         symbol x("x"), y("y");
95         ex e1, e2;
96         
97         e1 = (1+x)*pow((2+x),2)*pow((3+x),3)*pow((4+x),4);
98         e2 = sqrfree(expand(e1),lst(x));
99         if (e1 != e2) {
100                 clog << "sqrfree(expand(" << e1 << ")) erroneously returned "
101                      << e2 << endl;
102                 ++result;
103         }
104         
105         e1 = (x+y)*pow((x+2*y),2)*pow((x+3*y),3)*pow((x+4*y),4);
106         e2 = sqrfree(expand(e1));
107         if (e1 != e2) {
108                 clog << "sqrfree(expand(" << e1 << ")) erroneously returned "
109                      << e2 << endl;
110                 ++result;
111         }
112         e2 = sqrfree(expand(e1),lst(x));
113         if (e1 != e2) {
114                 clog << "sqrfree(expand(" << e1 << "),[x]) erroneously returned "
115                      << e2 << endl;
116                 ++result;
117         }
118         e2 = sqrfree(expand(e1),lst(y));
119         if (e1 != e2) {
120                 clog << "sqrfree(expand(" << e1 << "),[y]) erroneously returned "
121                      << e2 << endl;
122                 ++result;
123         }
124         e2 = sqrfree(expand(e1),lst(x,y));
125         if (e1 != e2) {
126                 clog << "sqrfree(expand(" << e1 << "),[x,y]) erroneously returned "
127                      << e2 << endl;
128                 ++result;
129         }
130         
131         return result;
132 }
133
134 /* Arithmetic Operators should behave just as one expects from built-in types.
135  * When somebody screws up the operators this routine will most probably fail
136  * to compile.  Unfortunately we can only test the stuff that is allowed, not
137  * what is forbidden (e.g. e1+e2 = 42) since that must not compile.  :-(   */
138 static unsigned exam_operator_semantics()
139 {
140         unsigned result = 0;
141         ex e1, e2;
142         int i1, i2;
143         
144         // Assignment should not return const ex though it may be obfuscated:
145         e1 = 7; e2 = 4;
146         i1 = 7; i2 = 4;
147         (e1 = e2) = 2;
148         (i1 = i2) = 2;
149         if (e1!=i1 || e2!=i2) {
150                 clog << "Semantics of ex::operator=() screwed." << endl;
151                 ++result;
152         }
153         (e1 += e2) = 2;
154         (i1 += i2) = 2;
155         if (e1!=i1 || e2!=i2) {
156                 clog << "Semantics of ex::operator=() screwed." << endl;
157                 ++result;
158         }
159         (e1 -= e2) = 2;
160         (i1 -= i2) = 2;
161         if (e1!=i1 || e2!=i2) {
162                 clog << "Semantics of ex::operator=() screwed." << endl;
163                 ++result;
164         }
165         
166         // Prefix/postfix increment/decrement behaviour:
167         e1 = 7; e2 = 4;
168         i1 = 7; i2 = 4;
169         e1 = (--e2 = 2)++;
170         i1 = (--i2 = 2)++;
171         if (e1!=i1 || e2!=i2) {
172                 clog << "Semantics of increment/decrement operators screwed." << endl;
173                 ++result;
174         }
175         e1 = (++e2 = 2)--;
176         i1 = (++i2 = 2)--;
177         if (e1!=i1 || e2!=i2) {
178                 clog << "Semantics of increment/decrement operators screwed." << endl;
179                 ++result;
180         }
181         
182         // prefix increment/decrement must return an lvalue (contrary to postfix):
183         e1 = 7; e2 = 4;
184         i1 = 7; i2 = 4;
185         --++----e1;  ++(++++++++(++++e2));
186         --++----i1;  ++(++++++++(++++i2));
187         if (e1!=i1 || e2!=i2) {
188                 clog << "Semantics of prefix increment/decrement operators screwed." << endl;
189                 ++result;
190         }
191         
192         // This one has a good chance of detecting problems in self-assignment:
193         // (which incidentally was severely broken from version 0.7.3 to 0.8.2).
194         ex selfprobe = numeric("65536");
195         selfprobe = selfprobe;
196         if (!is_exactly_a<numeric>(selfprobe)) {
197                 clog << "ex (of numeric) after self-assignment became " << selfprobe << endl;
198                 ++result;
199         }
200         
201         return result;
202 }
203
204 /* This checks whether subs() works as intended in some special cases. */
205 static unsigned exam_subs()
206 {
207         unsigned result = 0;
208         symbol x("x");
209         ex e1, e2;
210
211         // This used to fail in GiNaC 1.0.5 because it first substituted
212         // x+1 -> (x-1)+1 -> x, and then substituted again x -> x-1, giving
213         // the wrong result
214         e1 = x+1;
215         e2 = e1.subs(x == x-1);
216         if (!e2.is_equal(x)) {
217                 clog << "(x+1).subs(x==x-1) erroneously returned " << e2 << " instead of x" << endl;
218                 ++result;
219         }
220
221         e1 = sin(1+sin(x));
222         e2 = e1.subs(sin(wild()) == cos(wild()));
223         if (!e2.is_equal(cos(1+cos(x)))) {
224                 clog << "sin(1+sin(x)).subs(sin($1)==cos($1)) erroneously returned " << e2 << " instead of cos(1+cos(x))" << endl;
225                 ++result;
226         }
227
228         return result;
229 }
230
231 /* Joris van der Hoeven (he of TeXmacs fame) is a funny guy.  He has his own
232  * ideas what a symbolic system should do.  Let's make sure we won't disappoint
233  * him some day.  Incidentally, this seems to always have worked. */
234 static unsigned exam_joris()
235 {
236         unsigned result = 0;
237         symbol x("x");
238
239         ex e = expand(pow(x, x-1) * x);
240         if (e != pow(x, x)) {
241                 clog << "x^(x-1)*x did not expand to x^x.  Please call Joris!" << endl;
242                 ++result;
243         }
244
245         return result;
246 }
247
248 /* Test Chris Dams' algebraic substitutions. */
249 static unsigned exam_subs_algebraic()
250 {
251         unsigned result = 0;
252         symbol x("x"), y("y");
253
254         ex e = ex(x*x*x*y*y).subs(x*y==2, subs_options::algebraic);
255         if (e != 4*x) {
256                 clog << "(x^3*y^2).subs(x*y==2,subs_options::algebraic) erroneously returned " << e << endl;
257                 ++result;
258         }
259
260         e = ex(x*x*x*x*x).subs(x*x==y, subs_options::algebraic);
261         if (e != y*y*x) {
262                 clog << "x^5.subs(x^2==y,subs_options::algebraic) erroneously returned " << e << endl;
263                 ++result;
264         }
265
266         e=x*x*y;
267         if (!e.has(x*y, has_options::algebraic))
268         {       clog << "(x^2*y).has(x*y, has_options::algebraic) erroneously returned false." << endl;
269                 ++result;
270         }
271
272         if (e.has(x*y*y, has_options::algebraic))
273         {       clog << "(x^2*y).has(x*y*y, has_options::algebraic) erroneously returned true." << endl;
274                 ++result;
275         }
276
277         e=x*x*x*y;
278         if (!e.has(x*x, has_options::algebraic))
279         {       clog << "(x^3*y).has(x*x, has_options::algebraic) erroneously returned false." << endl;
280                 ++result;
281         }
282
283         if (e.has(y*y, has_options::algebraic))
284         {       clog << "(x^3*y).has(y*y, has_options::algebraic) erroneously returned true." << endl;
285                 ++result;
286         }
287
288         return result;
289 }
290
291 unsigned exam_misc()
292 {
293         unsigned result = 0;
294         
295         cout << "examining miscellaneous other things" << flush;
296         
297         result += exam_expand_subs();  cout << '.' << flush;
298         result += exam_expand_subs2();  cout << '.' << flush;
299         result += exam_expand_power(); cout << '.' << flush;
300         result += exam_sqrfree(); cout << '.' << flush;
301         result += exam_operator_semantics(); cout << '.' << flush;
302         result += exam_subs(); cout << '.' << flush;
303         result += exam_joris(); cout << '.' << flush;
304         result += exam_subs_algebraic(); cout << '.' << flush;
305         
306         return result;
307 }
308
309 int main(int argc, char** argv)
310 {
311         return exam_misc();
312 }