]> www.ginac.de Git - ginac.git/blob - check/expand_subs.cpp
- Renamed flag NO_GINAC_NAMESPACE to NO_NAMESPACE_GINAC because of m4.
[ginac.git] / check / expand_subs.cpp
1 /** @file 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 /*
16  *  GiNaC Copyright (C) 1999-2000 Johannes Gutenberg University Mainz, Germany
17  *
18  *  This program is free software; you can redistribute it and/or modify
19  *  it under the terms of the GNU General Public License as published by
20  *  the Free Software Foundation; either version 2 of the License, or
21  *  (at your option) any later version.
22  *
23  *  This program is distributed in the hope that it will be useful,
24  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
25  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26  *  GNU General Public License for more details.
27  *
28  *  You should have received a copy of the GNU General Public License
29  *  along with this program; if not, write to the Free Software
30  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
31  */
32
33
34 #include <ginac/ginac.h>
35
36 #ifndef NO_NAMESPACE_GINAC
37 using namespace GiNaC;
38 #endif // ndef NO_NAMESPACE_GINAC
39
40 #define VECSIZE 100
41
42 static unsigned expand_subs1(void)
43 {
44     symbol a1("a1");
45     symbol a[VECSIZE];
46     ex e, aux;
47
48     a[1] = a1;
49     for (unsigned i=0; i<VECSIZE; ++i) {
50         e = e + a[i];
51     }
52
53     // prepare aux so it will swallow anything but a1^2:
54     aux = -e + a[0] + a[1];
55     e = expand(subs(expand(pow(e, 2)), a[0] == aux));
56
57     if (e != pow(a1,2)) {
58         clog << "Denny Fliegner's quick consistency check erroneously returned "
59              << e << "." << endl;
60         return 1;
61     }
62     return 0;
63 }
64
65 static unsigned expand_subs2(void)
66 {
67     symbol a("a"), b("b");
68     ex e, f;
69
70     // Here the final expand() should be superflous. For no particular reason
71     // at all, we don't use the wrapper-functions but the methods instead:
72     e = pow(a+b,200).expand();
73     f = e.subs(a == -b);
74
75     if (f != 0) {
76         clog << "e = pow(a+b,200).expand(); f = e.subs(a == -b); erroneously returned "
77              << f << " instead of simplifying to 0." << endl;
78         return 1;
79     }
80     return 0;
81 }
82
83 unsigned expand_subs(void)
84 {
85     unsigned result = 0;
86
87     cout << "checking commutative expansion and substitution..." << flush;
88     clog << "---------commutative expansion and substitution:" << endl;
89     
90     result += expand_subs1();
91     result += expand_subs2();
92     
93     if (!result) {
94         cout << " passed ";
95         clog << "(no output)" << endl;
96     } else {
97         cout << " failed ";
98     }
99
100     return result;
101 }