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