]> www.ginac.de Git - ginac.git/blob - check/expand_subs.cpp
9f4105a1f5ea44d9e5ea3132b4b8157c9ea91e7d
[ginac.git] / check / expand_subs.cpp
1 // check/expand_subs.cpp
2
3 /* The first test routine implements Denny Fliegner's quick consistency check:
4  *     e = (a0 + a1 + a2 + a3 + ...)^2
5  *     expand e
6  *     substitute a0 by (-a2 - a3 - ...) in e
7  *     expand e
8  * after which e should be just a1^2.
9  * In addition, a simpler modification is tested in the second test:
10  *     e = (a0 + a1)^200
11  *     expand e
12  *     substitute a0 by -a1 in e
13  * after which e should return 0 (without expanding). */
14
15 #include <ginac/ginac.h>
16
17 #define VECSIZE 100
18
19 static unsigned expand_subs1(void)
20 {
21     symbol a1("a1");
22     symbol a[VECSIZE];
23     ex e, aux;
24
25     a[1] = a1;
26     for (unsigned i=0; i<VECSIZE; ++i) {
27         e = e + a[i];
28     }
29
30     // prepare aux so it will swallow anything but a1^2:
31     aux = -e + a[0] + a[1];
32     e = expand(subs(expand(pow(e, 2)), a[0] == aux));
33
34     if (e != pow(a1,2)) {
35         clog << "Denny Fliegner's quick consistency check erroneously returned "
36              << e << "." << endl;
37         return 1;
38     }
39     return 0;
40 }
41
42 static unsigned expand_subs2(void)
43 {
44     symbol a("a"), b("b");
45     ex e, f;
46
47     // Here the final expand() should be superflous. For no particular reason
48     // at all, we don't use the wrapper-functions but the methods instead:
49     e = pow(a+b,200).expand();
50     f = e.subs(a == -b);
51
52     if (f != 0) {
53         clog << "e = pow(a+b,200).expand(); f = e.subs(a == -b); erroneously returned "
54              << f << " instead of simplifying to 0." << endl;
55         return 1;
56     }
57     return 0;
58 }
59
60 unsigned expand_subs(void)
61 {
62     unsigned result = 0;
63
64     cout << "checking commutative expansion and substitution..." << flush;
65     clog << "---------commutative expansion and substitution:" << endl;
66
67     result += expand_subs1();
68     result += expand_subs2();
69
70     if (! result) {
71         cout << " passed ";
72         clog << "(no output)" << endl;
73     } else {
74         cout << " failed ";
75     }
76
77     return result;
78 }