]> www.ginac.de Git - ginac.git/blob - check/exam_misc.cpp
3595c9a2e817d9f5aba0b24006b4432a4298ac64
[ginac.git] / check / exam_misc.cpp
1 /** @file exam_misc.cpp
2  *
3  */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2022 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 /* Arithmetic Operators should behave just as one expects from built-in types.
93  * When somebody screws up the operators this routine will most probably fail
94  * to compile.  Unfortunately we can only test the stuff that is allowed, not
95  * what is forbidden (e.g. e1+e2 = 42) since that must not compile.  :-(   */
96 static unsigned exam_operator_semantics()
97 {
98         unsigned result = 0;
99         ex e1, e2;
100         int i1, i2;
101         
102         // Assignment should not return const ex though it may be obfuscated:
103         e1 = 7; e2 = 4;
104         i1 = 7; i2 = 4;
105         (e1 = e2) = 2;
106         (i1 = i2) = 2;
107         if (e1!=i1 || e2!=i2) {
108                 clog << "Semantics of ex::operator=() screwed." << endl;
109                 ++result;
110         }
111         (e1 += e2) = 2;
112         (i1 += i2) = 2;
113         if (e1!=i1 || e2!=i2) {
114                 clog << "Semantics of ex::operator=() screwed." << endl;
115                 ++result;
116         }
117         (e1 -= e2) = 2;
118         (i1 -= i2) = 2;
119         if (e1!=i1 || e2!=i2) {
120                 clog << "Semantics of ex::operator=() screwed." << endl;
121                 ++result;
122         }
123         
124         // Prefix/postfix increment/decrement behavior:
125         e1 = 7; e2 = 4;
126         i1 = 7; i2 = 4;
127         e1 = (--e2 = 2)++;
128         i1 = (--i2 = 2)++;
129         if (e1!=i1 || e2!=i2) {
130                 clog << "Semantics of increment/decrement operators screwed." << endl;
131                 ++result;
132         }
133         e1 = (++e2 = 2)--;
134         i1 = (++i2 = 2)--;
135         if (e1!=i1 || e2!=i2) {
136                 clog << "Semantics of increment/decrement operators screwed." << endl;
137                 ++result;
138         }
139         
140         // prefix increment/decrement must return an lvalue (contrary to postfix):
141         e1 = 7; e2 = 4;
142         i1 = 7; i2 = 4;
143         --++----e1;  ++(++++++++(++++e2));
144         --++----i1;  ++(++++++++(++++i2));
145         if (e1!=i1 || e2!=i2) {
146                 clog << "Semantics of prefix increment/decrement operators screwed." << endl;
147                 ++result;
148         }
149         
150         // This one has a good chance of detecting problems in self-assignment:
151         // (which incidentally was severely broken from version 0.7.3 to 0.8.2).
152         ex selfprobe = numeric("65536");
153         selfprobe = selfprobe;
154         if (!is_exactly_a<numeric>(selfprobe)) {
155                 clog << "ex (of numeric) after self-assignment became " << selfprobe << endl;
156                 ++result;
157         }
158         
159         return result;
160 }
161
162 /* This checks whether subs() works as intended in some special cases. */
163 static unsigned exam_subs()
164 {
165         unsigned result = 0;
166         symbol x("x");
167         ex e1, e2;
168
169         // This used to fail in GiNaC 1.0.5 because it first substituted
170         // x+1 -> (x-1)+1 -> x, and then substituted again x -> x-1, giving
171         // the wrong result
172         e1 = x+1;
173         e2 = e1.subs(x == x-1);
174         if (!e2.is_equal(x)) {
175                 clog << "(x+1).subs(x==x-1) erroneously returned " << e2 << " instead of x" << endl;
176                 ++result;
177         }
178
179         // And this used to fail in GiNaC 1.5.8 because it first substituted
180         // exp(x) -> exp(log(x)) -> x, and then substituted again x -> log(x)
181         e1 = exp(x);
182         e2 = e1.subs(x == log(x));
183         if (!e2.is_equal(x)) {
184                 clog << "exp(x).subs(x==log(x)) erroneously returned " << e2 << " instead of x" << endl;
185                 ++result;
186         }
187
188         e1 = sin(1+sin(x));
189         e2 = e1.subs(sin(wild()) == cos(wild()));
190         if (!e2.is_equal(cos(1+cos(x)))) {
191                 clog << "sin(1+sin(x)).subs(sin($1)==cos($1)) erroneously returned " << e2 << " instead of cos(1+cos(x))" << endl;
192                 ++result;
193         }
194
195         return result;
196 }
197
198 /* Joris van der Hoeven (he of TeXmacs fame) is a funny guy.  He has his own
199  * ideas what a symbolic system should do.  Let's make sure we won't disappoint
200  * him some day.  Incidentally, this seems to always have worked. */
201 static unsigned exam_joris()
202 {
203         unsigned result = 0;
204         symbol x("x");
205
206         ex e = expand(pow(x, x-1) * x);
207         if (e != pow(x, x)) {
208                 clog << "x^(x-1)*x did not expand to x^x.  Please call Joris!" << endl;
209                 ++result;
210         }
211
212         return result;
213 }
214
215 /* Test Chris Dams' algebraic substitutions. */
216 static unsigned exam_subs_algebraic()
217 {
218         unsigned result = 0;
219         symbol x("x"), y("y");
220
221         ex e = ex(x*x*x*y*y).subs(x*y==2, subs_options::algebraic);
222         if (e != 4*x) {
223                 clog << "(x^3*y^2).subs(x*y==2,subs_options::algebraic) erroneously returned " << e << endl;
224                 ++result;
225         }
226
227         e = ex(x*x*x*x*x).subs(x*x==y, subs_options::algebraic);
228         if (e != y*y*x) {
229                 clog << "x^5.subs(x^2==y,subs_options::algebraic) erroneously returned " << e << endl;
230                 ++result;
231         }
232
233         e=x*x*y;
234         if (!e.has(x*y, has_options::algebraic))
235         {       clog << "(x^2*y).has(x*y, has_options::algebraic) erroneously returned false." << endl;
236                 ++result;
237         }
238
239         if (e.has(x*y*y, has_options::algebraic))
240         {       clog << "(x^2*y).has(x*y*y, has_options::algebraic) erroneously returned true." << endl;
241                 ++result;
242         }
243
244         e=x*x*x*y;
245         if (!e.has(x*x, has_options::algebraic))
246         {       clog << "(x^3*y).has(x*x, has_options::algebraic) erroneously returned false." << endl;
247                 ++result;
248         }
249
250         if (e.has(y*y, has_options::algebraic))
251         {       clog << "(x^3*y).has(y*y, has_options::algebraic) erroneously returned true." << endl;
252                 ++result;
253         }
254
255         return result;
256 }
257
258 /* Test suitable cases of the exponent power law: (e^t)^s=e^(ts). */
259 static unsigned exam_exponent_power_law()
260 {
261         unsigned result = 0;
262         symbol x("x");
263         realsymbol s("s");
264         possymbol t("t");
265
266         exmap pwr_exp =
267                 { {pow(exp(x), 2), exp(2*x)},
268                   {pow(exp(s), t), exp(s*t)},
269                   {exp(x)*pow(exp(x),-1), 1} };
270
271         for (auto e : pwr_exp) {
272                 if (! (e.first.is_equal(e.second)) ) {
273                         clog << "power of exponent  " << e.first << " produces error.\n";
274                         ++result;
275                 }
276         }
277
278         return result;
279 }
280
281 unsigned exam_misc()
282 {
283         unsigned result = 0;
284         
285         cout << "examining miscellaneous other things" << flush;
286         
287         result += exam_expand_subs();  cout << '.' << flush;
288         result += exam_expand_subs2();  cout << '.' << flush;
289         result += exam_expand_power(); cout << '.' << flush;
290         result += exam_operator_semantics(); cout << '.' << flush;
291         result += exam_subs(); cout << '.' << flush;
292         result += exam_joris(); cout << '.' << flush;
293         result += exam_subs_algebraic(); cout << '.' << flush;
294         result += exam_exponent_power_law(); cout << '.' << flush;
295         
296         return result;
297 }
298
299 int main(int argc, char** argv)
300 {
301         return exam_misc();
302 }