]> www.ginac.de Git - ginac.git/blob - check/expand_subs.cpp
- ASSERT macro renamed to GINAC_ASSERT
[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 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 using namespace GiNaC;
36
37 #define VECSIZE 100
38
39 static unsigned expand_subs1(void)
40 {
41     symbol a1("a1");
42     symbol a[VECSIZE];
43     ex e, aux;
44
45     a[1] = a1;
46     for (unsigned i=0; i<VECSIZE; ++i) {
47         e = e + a[i];
48     }
49
50     // prepare aux so it will swallow anything but a1^2:
51     aux = -e + a[0] + a[1];
52     e = expand(subs(expand(pow(e, 2)), a[0] == aux));
53
54     if (e != pow(a1,2)) {
55         clog << "Denny Fliegner's quick consistency check erroneously returned "
56              << e << "." << endl;
57         return 1;
58     }
59     return 0;
60 }
61
62 static unsigned expand_subs2(void)
63 {
64     symbol a("a"), b("b");
65     ex e, f;
66
67     // Here the final expand() should be superflous. For no particular reason
68     // at all, we don't use the wrapper-functions but the methods instead:
69     e = pow(a+b,200).expand();
70     f = e.subs(a == -b);
71
72     if (f != 0) {
73         clog << "e = pow(a+b,200).expand(); f = e.subs(a == -b); erroneously returned "
74              << f << " instead of simplifying to 0." << endl;
75         return 1;
76     }
77     return 0;
78 }
79
80 unsigned expand_subs(void)
81 {
82     unsigned result = 0;
83
84     cout << "checking commutative expansion and substitution..." << flush;
85     clog << "---------commutative expansion and substitution:" << endl;
86
87     result += expand_subs1();
88     result += expand_subs2();
89
90     if (! result) {
91         cout << " passed ";
92         clog << "(no output)" << endl;
93     } else {
94         cout << " failed ";
95     }
96
97     return result;
98 }