1 /** @file exam_misc.cpp
6 * GiNaC Copyright (C) 1999-2023 Johannes Gutenberg University Mainz, Germany
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.
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.
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
24 using namespace GiNaC;
30 static unsigned exam_expand_subs()
36 for (unsigned i=0; i<VECSIZE; ++i)
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));
43 if (e != pow(a[1],2)) {
44 clog << "Denny Fliegner's quick consistency check erroneously returned "
52 /* A simple modification of Denny Fliegner's three step consistency test:
53 * 1) e = (a0 + a1)^200
55 * 3) substitute a0 by -a1 in e
56 * after which e should return 0 (without expanding). */
57 static unsigned exam_expand_subs2()
60 symbol a("a"), b("b");
63 e = pow(a+b,200).expand();
67 clog << "e = pow(a+b,200).expand(); f = e.subs(a == -b); erroneously returned "
68 << f << " instead of simplifying to 0." << endl;
75 static unsigned exam_expand_power()
78 symbol x("x"), a("a"), b("b");
81 e = pow(x,pow(a+b,2)-pow(a,2)-pow(b,2)-a*b*2).expand();
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;
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()
102 // Assignment should not return const ex though it may be obfuscated:
107 if (e1!=i1 || e2!=i2) {
108 clog << "Semantics of ex::operator=() screwed." << endl;
113 if (e1!=i1 || e2!=i2) {
114 clog << "Semantics of ex::operator=() screwed." << endl;
119 if (e1!=i1 || e2!=i2) {
120 clog << "Semantics of ex::operator=() screwed." << endl;
124 // Prefix/postfix increment/decrement behavior:
129 if (e1!=i1 || e2!=i2) {
130 clog << "Semantics of increment/decrement operators screwed." << endl;
135 if (e1!=i1 || e2!=i2) {
136 clog << "Semantics of increment/decrement operators screwed." << endl;
140 // prefix increment/decrement must return an lvalue (contrary to postfix):
143 --++----e1; ++(++++++++(++++e2));
144 --++----i1; ++(++++++++(++++i2));
145 if (e1!=i1 || e2!=i2) {
146 clog << "Semantics of prefix increment/decrement operators screwed." << endl;
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;
162 /* This checks whether subs() works as intended in some special cases. */
163 static unsigned exam_subs()
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
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;
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)
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;
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;
195 // This used to fail in GiNaC 1.8.2 with subs_options::no_pattern
197 e2 = e1.subs(x == 1/x);
198 if (!e2.is_equal(x)) {
199 clog << "(1/x).subs(x==1/x) erroneously returned " << e2 << " instead of x" << endl;
202 e2 = e1.subs(x == 1/x, subs_options::no_pattern);
203 if (!e2.is_equal(x)) {
204 clog << "(1/x).subs(x==1/x, subs_options::no_pattern) erroneously returned " << e2 << " instead of x" << endl;
207 e2 = e1.subs(x == 1/x, subs_options::algebraic);
208 if (!e2.is_equal(x)) {
209 clog << "(1/x).subs(x==1/x, subs_options::algebraic) erroneously returned " << e2 << " instead of x" << endl;
216 /* Joris van der Hoeven (he of TeXmacs fame) is a funny guy. He has his own
217 * ideas what a symbolic system should do. Let's make sure we won't disappoint
218 * him some day. Incidentally, this seems to always have worked. */
219 static unsigned exam_joris()
224 ex e = expand(pow(x, x-1) * x);
225 if (e != pow(x, x)) {
226 clog << "x^(x-1)*x did not expand to x^x. Please call Joris!" << endl;
233 /* Test Chris Dams' algebraic substitutions. */
234 static unsigned exam_subs_algebraic()
237 symbol x("x"), y("y");
239 ex e = ex(x*x*x*y*y).subs(x*y==2, subs_options::algebraic);
241 clog << "(x^3*y^2).subs(x*y==2,subs_options::algebraic) erroneously returned " << e << endl;
245 e = ex(x*x*x*x*x).subs(x*x==y, subs_options::algebraic);
247 clog << "x^5.subs(x^2==y,subs_options::algebraic) erroneously returned " << e << endl;
252 if (!e.has(x*y, has_options::algebraic))
253 { clog << "(x^2*y).has(x*y, has_options::algebraic) erroneously returned false." << endl;
257 if (e.has(x*y*y, has_options::algebraic))
258 { clog << "(x^2*y).has(x*y*y, has_options::algebraic) erroneously returned true." << endl;
263 if (!e.has(x*x, has_options::algebraic))
264 { clog << "(x^3*y).has(x*x, has_options::algebraic) erroneously returned false." << endl;
268 if (e.has(y*y, has_options::algebraic))
269 { clog << "(x^3*y).has(y*y, has_options::algebraic) erroneously returned true." << endl;
276 /* Test suitable cases of the exponent power law: (e^t)^s=e^(ts). */
277 static unsigned exam_exponent_power_law()
285 { {pow(exp(x), 2), exp(2*x)},
286 {pow(exp(s), t), exp(s*t)},
287 {exp(x)*pow(exp(x),-1), 1} };
289 for (auto e : pwr_exp) {
290 if (! (e.first.is_equal(e.second)) ) {
291 clog << "power of exponent " << e.first << " produces error.\n";
303 cout << "examining miscellaneous other things" << flush;
305 result += exam_expand_subs(); cout << '.' << flush;
306 result += exam_expand_subs2(); cout << '.' << flush;
307 result += exam_expand_power(); cout << '.' << flush;
308 result += exam_operator_semantics(); cout << '.' << flush;
309 result += exam_subs(); cout << '.' << flush;
310 result += exam_joris(); cout << '.' << flush;
311 result += exam_subs_algebraic(); cout << '.' << flush;
312 result += exam_exponent_power_law(); cout << '.' << flush;
317 int main(int argc, char** argv)