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