]> www.ginac.de Git - ginac.git/blob - check/exam_misc.cpp
added a check that expand(x^((a+b)^2-a^2-b^2-2*a*b)) -> 1
[ginac.git] / check / exam_misc.cpp
1 /** @file exam_misc.cpp
2  *
3  */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2001 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23
24 #include "exams.h"
25
26 #define VECSIZE 30
27 static unsigned exam_expand_subs(void)
28 {
29         unsigned result = 0;
30         symbol a1("a1");
31         symbol a[VECSIZE];
32         ex e, aux;
33         
34         a[1] = a1;
35         for (unsigned i=0; i<VECSIZE; ++i) {
36                 e = e + a[i];
37         }
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(a1,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(void)
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(void)
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 unsigned exam_misc(void)
93 {
94         unsigned result = 0;
95         
96         cout << "examining miscellaneous other things" << flush;
97         clog << "----------miscellaneous other things:" << endl;
98         
99         result += exam_expand_subs();  cout << '.' << flush;
100         result += exam_expand_subs2();  cout << '.' << flush;
101         result += exam_expand_power(); cout << '.' << flush;
102         
103         if (!result) {
104                 cout << " passed " << endl;
105                 clog << "(no output)" << endl;
106         } else {
107                 cout << " failed " << endl;
108         }
109         
110         return result;
111 }