1 /** @file exam_paranoia.cpp
3 * This set of tests checks for some of GiNaC's oopses which showed up during
4 * development. Things were evaluated wrongly and so. Such a sick behaviour
5 * shouldn't occur any more. But we are paranoic and we want to exclude these
6 * these oopses for good, so we run those stupid tests... */
9 * GiNaC Copyright (C) 1999-2016 Johannes Gutenberg University Mainz, Germany
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 using namespace GiNaC;
32 // The very first pair of historic problems had its roots in power.cpp and was
33 // finally resolved on April 27th 1999. (Fixing the first on April 23rd
34 // actually introduced the second.)
35 static unsigned exam_paranoia1()
38 symbol x("x"), y("y"), z("z");
45 // In the first one expand did not do any job at all:
46 if (!g.expand().is_equal(x)) {
47 clog << "e = x*y*z; f = y*z; expand(e/f) erroneously returned "
48 << g.expand() << endl;
52 // This one somehow used to return 0:
54 if (!e.expand().is_equal(e)) {
55 clog << "expand(pow(x + 1, -1)) erroneously returned "
56 << e.expand() << endl;
63 // And here the second oops which showed up until May 17th 1999. It had to do
64 // with lexicographic canonicalization and thus showed up only if the variables
65 // had the names as given here:
66 static unsigned exam_paranoia2()
69 symbol x("x"), y("y"), z("z");
76 // After .eval(), g should be zero:
78 clog << "e = (x + z*x); f = e*y; g = (f - e*y) erroneously returned g == "
86 // The third bug was introduced on May 18th 1999, discovered on May 19 and
87 // fixed that same day. It worked when x was substituted by 1 but not with
89 static unsigned exam_paranoia3()
92 symbol x("x"), y("y");
99 clog << "e = x*y - y; f = e.subs(x == 2) erroneously returned "
107 // The fourth bug was also discovered on May 19th 1999 and fixed immediately:
108 static unsigned exam_paranoia4()
114 e = pow(x, 2) + x + 1;
115 f = pow(x, 2) + x + 1;
119 clog << "e = pow(x,2) + x + 1; f = pow(x,2) + x + 1; g = e-f; g erroneously returned "
127 // The fifth oops was discovered on May 20th 1999 and fixed a day later:
128 static unsigned exam_paranoia5()
131 symbol x("x"), y("y");
135 f = pow(x, 2) * pow(y, 2) + 2*x*y + 1;
137 if (!(e-f).expand().is_zero()) {
138 clog << "e = pow(x*y+1,2); f = pow(x,2)*pow(y,2) + 2*x*y + 1; (e-f).expand() erroneously returned "
139 << (e-f).expand() << endl;
146 // This one was discovered on Jun 1st 1999 and fixed the same day:
147 static unsigned exam_paranoia6()
156 if (!f.is_equal(pow(x, 5))) {
157 clog << "e = pow(x, -5); f = e.denom(); f was " << f << " (should be x^5)" << endl;
163 // This one was introduced on June 1st 1999 by some aggressive manual
164 // optimization. Discovered and fixed on June 2nd.
165 static unsigned exam_paranoia7()
168 symbol x("x"), y("y");
171 ex f = expand(pow(e, 2) - (e*y*(x + 1)));
174 clog << "e=y+y*x+2; f=expand(pow(e,2)-(e*y*(x+1))) has "
175 << f.nops() << " arguments instead of 3 ( f=="
176 << f << " )" << endl;
182 // This one was a result of the rewrite of mul::max_coefficient when we
183 // introduced the overall_coefficient field in expairseq objects on Oct 1st
184 // 1999. Fixed on Oct 4th.
185 static unsigned exam_paranoia8()
195 if (!f.is_equal(e)) {
196 clog << "normal(-x/(x+1)) returns " << f << " instead of -x/(x+1)\n";
199 } catch (const exception &err) {
200 clog << "normal(-x/(x+1) throws " << err.what() << endl;
206 // This one was a result of a modification to frac_cancel() & Co. to avoid
207 // expanding the numerator and denominator when bringing them from Q[X] to
208 // Z[X]. multiply_lcm() forgot to multiply the x-linear term with the LCM of
209 // the coefficient's denominators (2 in this case). Introduced on Jan 25th
210 // 2000 and fixed on Jan 31th.
211 static unsigned exam_paranoia9()
216 ex e = (exp(-x)-2*x*exp(-x)+pow(x,2)/2*exp(-x))/exp(-x);
219 if (!f.is_equal(1-2*x+pow(x,2)/2)) {
220 clog << "normal(" << e << ") returns " << f << " instead of 1-2*x+1/2*x^2\n";
226 // I have no idea when this broke. It has been working long ago, before 0.4.0
227 // and on Feb 13th 2000 I found out that things like 2^(3/2) throw an exception
228 // "power::eval(): pow(0,0) is undefined" instead of simplifying to 2*2^(1/2).
229 // It was fixed that same day.
230 static unsigned exam_paranoia10()
240 if (!(r-2*sqrt(ex(2))).is_zero()) {
241 clog << "2^(3/2) erroneously returned " << r << " instead of 2*sqrt(2)" << endl;
244 } catch (const exception &err) {
245 clog << "2^(3/2) throws " << err.what() << endl;
251 // After the rewriting of basic::normal() & Co. to return {num, den} lists,
252 // add::normal() forgot to multiply the denominator of the overall_coeff of
253 // its expanded and normalized children with the denominator of the expanded
254 // child (did you get this? Well, never mind...). Fixed on Feb 21th 2000.
255 static unsigned exam_paranoia11()
260 ex e = ((-5-2*x)-((2-5*x)/(-2+x))*(3+2*x))/(5-4*x);
262 ex d = normal((4+10*x+8*pow(x,2))/(x-2)/(5-4*x));
264 if (!(f - d).expand().is_zero()) {
265 clog << "normal(" << e << ") returns " << f << " instead of " << d << endl;
271 // This one returned 0 because add::normal() incorrectly assumed that if the
272 // common denominator is 1, all the denominators would be 1 (they can in fact
273 // be +/-1). Fixed on Aug 2nd 2000.
274 static unsigned exam_paranoia12()
279 ex e = 2-2*(1+x)/(-1-x);
283 if (!(f - d).expand().is_zero()) {
284 clog << "normal(" << e << ") returns " << f
285 << " instead of " << d << endl;
291 // This one caused a division by 0 because heur_gcd() didn't check its
292 // input polynomials against 0. Fixed on Aug 4th 2000.
293 static unsigned exam_paranoia13()
296 symbol a("a"), b("b"), c("c");
298 ex e = (b*a-c*a)/(4-a);
299 ex d = (c*a-b*a)/(a-4);
303 if (!(f - d).expand().is_zero()) {
304 clog << "normal(" << e << ") returns " << f
305 << " instead of " << d << endl;
308 } catch (const exception &err) {
309 clog << "normal(" << e << ") throws " << err.what() << endl;
315 // A bug introduced on July 19, 2001. quo() and rem() would sometimes call
316 // vector::reserve() with a negative argument. Fixed on Dec 20, 2001.
317 static unsigned exam_paranoia14()
322 ex q = quo(1, pow(x, 3), x);
324 clog << "quo(1,x^3,x) erroneously returned " << q << " instead of 0\n";
331 // Under certain conditions, power::expand_add_2() could produce non-canonical
332 // numeric expairs. Fixed on Oct 24, 2002.
333 static unsigned exam_paranoia15()
337 ex q = (pow(pow(2, numeric(1, 2))*2+1, 2)).expand();
338 // this used to produce "1+4*sqrt(2)+4*2" which would never evaluate
341 if (!(q-9-4*pow(2, numeric(1, 2))).is_zero()) {
342 clog << "expand((sqrt(2)*2+1)^2) erroneously returned " << q << " instead of 9-4*sqrt(2)\n";
349 // Expanding products containing powers of sums could return results that
350 // were not fully expanded. Fixed on Dec 10, 2003.
351 static unsigned exam_paranoia16()
354 symbol a("a"), b("b"), c("c"), d("d"), e("e");
357 e1 = pow(1+a*sqrt(b+c), 2);
360 if (e2.has(pow(a, 2)*(b+c))) {
361 clog << "expand(" << e1 << ") didn't fully expand\n";
365 e1 = (d*sqrt(a+b)+a*sqrt(c+d))*(b*sqrt(a+b)+a*sqrt(c+d));
368 if (e2.has(pow(a, 2)*(c+d))) {
369 clog << "expand(" << e1 << ") didn't fully expand\n";
373 e1 = (a+sqrt(b+c))*sqrt(b+c)*(d+sqrt(b+c));
376 if (e2.has(a*(b+c))) {
377 clog << "expand(" << e1 << ") didn't fully expand\n";
381 e1 = pow(sqrt(a+b)+sqrt(c+d), 3);
384 if (e2.has(3*(a+b)*sqrt(c+d)) || e2.has(3*(c+d)*sqrt(a+b))) {
385 clog << "expand(" << e1 << ") didn't fully expand\n";
392 if (e2.has(c*(d+e))) {
393 clog << "expand(" << e1 << ") didn't fully expand\n";
397 e1 = 2*pow(1+a, 2)/a;
400 if (e2.has(pow(a, 2))) {
401 clog << "expand(" << e1 << ") didn't fully expand\n";
406 e2 = pow(pow(e1, -1), -1);
409 clog << "double reciprocal expanded where it should not\n";
416 // Bug in reposition_dummy_indices() could result in correct expression
417 // turned into one with inconsistent indices. Fixed on Aug 29, 2006
418 static unsigned exam_paranoia17()
420 varidx mu1(symbol("mu1"), 4);
421 varidx mu2(symbol("mu2"), 4);
422 varidx mu3(symbol("mu3"), 4);
423 varidx mu4(symbol("mu4"), 4);
424 varidx mu5(symbol("mu5"), 4);
425 varidx mu6(symbol("mu6"), 4);
428 ev2.push_back(mu3.toggle_variance());
430 ev2.push_back(mu5.toggle_variance());
431 ev2.push_back(mu6.toggle_variance());
434 // notice: all indices are contracted ...
436 ex test_cycl = indexed(symbol("A"), sy_cycl(), ev2);
437 test_cycl = test_cycl.simplify_indexed();
438 // ... so there should be zero free indices in the end.
439 return test_cycl.get_free_indices().size();
442 // Bug in add::eval() could result in numeric terms not being collected into
443 // the overall coefficient. Fixed first on Sep 22, 2010 and again on Dec 17 2015
444 static unsigned exam_paranoia18()
448 ex sqrt2 = sqrt(ex(2));
449 ex e1 = 1 + 2*(sqrt2+1)*(sqrt2-1);
450 if (e1.real_part() != 3) {
451 clog << "real_part(1+2*(sqrt(2)+1)*(sqrt(2)-1)) failed to evaluate to 3\n";
455 ex sqrt3 = sqrt(ex(3));
456 ex e2 = 2 + 2*(sqrt2+1)*(sqrt2-1) - 2*(sqrt3+1)*(sqrt3-1);
457 if (e2.real_part() != 0) {
458 clog << "real_part(2+2*(sqrt(2)+1)*(sqrt(2)-1)-3*(sqrt(3)+1)*(sqrt(3)-1)) failed to evaluate to 0\n";
465 // Bug in mul::conjugate when factors are evaluated at branch cuts, reported as
467 static unsigned exam_paranoia19()
470 ex e = conjugate(a*sqrt(ex(-2))*sqrt(ex(-3)));
471 ex c = a*conjugate(sqrt(ex(-2)))*conjugate(sqrt(ex(-3)));
472 if (!subs(e-c, a==42).is_zero()) {
473 clog << "subs(a*conjugate(sqrt(-2))*conjugate(sqrt(-3))-conjugate(a*sqrt(-2)*sqrt(-3)),a==42) failed to evaluate to 0\n";
479 // Bugs in is_polynomial (fixed 2011-05-20 and 2014-07-26).
480 static unsigned exam_paranoia20()
483 symbol x("x"), y("y");
484 ex e1 = sqrt(x*x+1)*sqrt(x+1);
485 if (e1.is_polynomial(x)) {
486 clog << "sqrt(x*x+1)*sqrt(x+1) is wrongly reported to be a polynomial in x\n";
490 if (!e2.is_polynomial(x)) {
491 clog << "sqrt(Pi)*x is wrongly reported to be no polynomial in x\n";
495 if (!e3.is_polynomial(y)) {
496 clog << "sqrt(x) is wrongly reported to be no polynomial in y\n";
500 if (e4.is_polynomial(x)) {
501 clog << "(1+y)/(2+x) is wrongly reported to be a polynomial in x\n";
507 static unsigned is_polynomial_false_positive()
510 symbol x("x"), n("n");
511 exvector nonpoly_exprs;
512 nonpoly_exprs.push_back(1/(1-x));
513 nonpoly_exprs.push_back(1/(x+1));
514 nonpoly_exprs.push_back(-1/(x-1));
515 nonpoly_exprs.push_back(1/(1-x*x));
516 nonpoly_exprs.push_back(1/(1-pow(x,n)));
517 nonpoly_exprs.push_back(x-1/(x-1));
518 for (exvector::const_iterator ep = nonpoly_exprs.begin();
519 ep != nonpoly_exprs.end(); ++ep) {
520 if (ep->is_polynomial(x)) {
521 clog << "(" << *ep << ").is_polynomial(" << x << ") "
522 "erroneously returned true" << endl;
529 // Bug in power::expand reported by Isuru Fernando (fixed 2015-05-07).
530 static unsigned exam_paranoia21()
533 ex e = pow(x + sqrt(ex(2))*x, 2).expand();
535 clog << "(x+sqrt(2)*x)^2 was wrongly expanded to " << e << "\n";
541 // Bug in power::expand (fixed 2015-07-18).
542 static unsigned exam_paranoia22()
544 symbol x("x"), y("y");
545 ex e = pow(sqrt(1+x)+y*sqrt(1+x), 2).expand();
547 clog << "(sqrt(1+x)+y*sqrt(1+x))^2 was wrongly expanded to " << e << "\n";
553 // Bug in expairseq::evalchildren().
554 static unsigned exam_paranoia23()
560 v1.push_back(expair(1, 1));
561 v1.push_back(expair(2*x, -1));
562 ex e1 = add(v1); // Should be e==1-2*x,
563 if (!e1.is_equal(1-2*x)) {
564 clog << "Failure constructing " << e1 << " from add.\n";
569 v2.push_back(expair(x, 1));
570 v2.push_back(expair(1,-1));
571 ex e2 = mul(v2); // Should be e==x;
572 if (!e2.is_equal(x)) {
573 clog << "Failure constructing " << e2 << " from mul.\n";
580 unsigned exam_paranoia()
584 cout << "examining several historic failures just out of paranoia" << flush;
586 result += exam_paranoia1(); cout << '.' << flush;
587 result += exam_paranoia2(); cout << '.' << flush;
588 result += exam_paranoia3(); cout << '.' << flush;
589 result += exam_paranoia4(); cout << '.' << flush;
590 result += exam_paranoia5(); cout << '.' << flush;
591 result += exam_paranoia6(); cout << '.' << flush;
592 result += exam_paranoia7(); cout << '.' << flush;
593 result += exam_paranoia8(); cout << '.' << flush;
594 result += exam_paranoia9(); cout << '.' << flush;
595 result += exam_paranoia10(); cout << '.' << flush;
596 result += exam_paranoia11(); cout << '.' << flush;
597 result += exam_paranoia12(); cout << '.' << flush;
598 result += exam_paranoia13(); cout << '.' << flush;
599 result += exam_paranoia14(); cout << '.' << flush;
600 result += exam_paranoia15(); cout << '.' << flush;
601 result += exam_paranoia16(); cout << '.' << flush;
602 result += exam_paranoia17(); cout << '.' << flush;
603 result += exam_paranoia18(); cout << '.' << flush;
604 result += exam_paranoia19(); cout << '.' << flush;
605 result += exam_paranoia20(); cout << '.' << flush;
606 result += is_polynomial_false_positive(); cout << '.' << flush;
607 result += exam_paranoia21(); cout << '.' << flush;
608 result += exam_paranoia22(); cout << '.' << flush;
609 result += exam_paranoia23(); cout << '.' << flush;
614 int main(int argc, char** argv)
616 return exam_paranoia();