]> www.ginac.de Git - ginac.git/blob - check/exam_normalization.cpp
Fixed make distcheck issues.
[ginac.git] / check / exam_normalization.cpp
1 /** @file exam_normalization.cpp
2  *
3  *  Rational function normalization test suite. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2007 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21  */
22
23 #include <iostream>
24 #include "ginac.h"
25 using namespace std;
26 using namespace GiNaC;
27
28 static symbol w("w"), x("x"), y("y"), z("z");
29
30 static unsigned check_normal(const ex &e, const ex &d)
31 {
32         ex en = e.normal();
33         if (!en.is_equal(d)) {
34                 clog << "normal form of " << e << " erroneously returned "
35                      << en << " (should be " << d << ")" << endl;
36                 return 1;
37         }
38         return 0;
39 }
40
41 static unsigned exam_normal1()
42 {
43         unsigned result = 0;
44         ex e, d;
45         
46         // Expansion
47         e = pow(x, 2) - (x+1)*(x-1) - 1;
48         d = 0;
49         result += check_normal(e, d);
50         
51         // Expansion inside functions
52         e = sin(x*(x+1)-x) + 1;
53         d = sin(pow(x, 2)) + 1;
54         result += check_normal(e, d);
55         
56         // Fraction addition
57         e = 2/x + y/3;
58         d = (x*y + 6) / (x*3);
59         result += check_normal(e, d);
60         
61         e = pow(x, -1) + x/(x+1);
62         d = (pow(x, 2)+x+1)/(x*(x+1));
63         result += check_normal(e, d);
64
65         return result;
66 }
67
68 static unsigned exam_normal2()
69 {
70         unsigned result = 0;
71         ex e, d;
72         
73         // Fraction cancellation
74         e = numeric(1)/2 * z * (2*x + 2*y);
75         d = z * (x + y);
76         result += check_normal(e, d);
77         
78         e = numeric(1)/6 * z * (3*x + 3*y) * (2*x + 2*w);
79         d = z * (x + y) * (x + w);
80         result += check_normal(e, d);
81         
82         e = (3*x + 3*y) * (w/3 + z/3);
83         d = (x + y) * (w + z);
84         result += check_normal(e, d);
85         
86         // Fails stochastically with the new tinfo mechanism, because
87         // sometimes the equivalent answer ... / pow(y - x, 2) is calculated.
88         // TODO: make check for both cases.
89 //      e = (pow(x, 2) - pow(y, 2)) / pow(x-y, 3);
90 //      d = (x + y) / pow(x - y, 2);
91 //      result += check_normal(e, d);
92         
93         e = (pow(x, -1) + x) / (pow(x , 2) * 2 + 2);
94         d = pow(x * 2, -1);
95         result += check_normal(e, d);
96         
97         // Fails stochastically with the new tinfo mechanism, because
98         // sometimes the equivalent answer ... / pow(y - x, 2) is calculated.
99         // TODO: make check for both cases.
100         // Fraction cancellation with rational coefficients
101 //      e = (pow(x, 2) - pow(y, 2)) / pow(x/2 - y/2, 3);
102 //      d = (8 * x + 8 * y) / pow(x - y, 2);
103 //      result += check_normal(e, d);
104         
105         // Fraction cancellation with rational coefficients
106         e = z/5 * (x/7 + y/10) / (x/14 + y/20);
107         d = 2*z/5;
108         result += check_normal(e, d);
109         
110         return result;
111 }
112
113 static unsigned exam_normal3()
114 {
115         unsigned result = 0;
116         ex e, d;
117         
118         // Distribution of powers
119         e = pow(x/y, 2);
120         d = pow(x, 2) / pow(y, 2);
121         result += check_normal(e, d);
122         
123         // Distribution of powers (integer, distribute) and fraction addition
124         e = pow(pow(x, -1) + x, 2);
125         d = pow(pow(x, 2) + 1, 2) / pow(x, 2);
126         result += check_normal(e, d);
127         
128         // Distribution of powers (non-integer, don't distribute) and fraction addition
129         e = pow(pow(x, -1) + x, numeric(1)/2);
130         d = pow((pow(x, 2) + 1) / x, numeric(1)/2);
131         result += check_normal(e, d);
132         
133         return result;
134 }
135
136 static unsigned exam_normal4()
137 {
138         unsigned result = 0;
139         ex e, d;
140         
141         // Replacement of functions with temporary symbols and fraction cancellation
142         e = pow(sin(x), 2) - pow(cos(x), 2);
143         e /= sin(x) + cos(x);
144         d = sin(x) - cos(x);
145         result += check_normal(e, d);
146         
147         // Replacement of non-integer powers with temporary symbols
148         e = (pow(numeric(2), numeric(1)/2) * x + x) / x;
149         d = pow(numeric(2), numeric(1)/2) + 1;
150         result += check_normal(e, d);
151         
152         // Replacement of complex numbers with temporary symbols
153         e = (x + y + x*I + y*I) / (x + y);
154         d = 1 + I;
155         result += check_normal(e, d);
156         
157         e = (pow(x, 2) + pow(y, 2)) / (x + y*I);
158         d = e;
159         result += check_normal(e, d);
160         
161         // More complex rational function
162         e = (pow(x-y*2,4)/pow(pow(x,2)-pow(y,2)*4,2)+1)*(x+y*2)*(y+z)/(pow(x,2)+pow(y,2)*4);
163         d = (y*2 + z*2) / (x + y*2);
164         result += check_normal(e, d);
165         
166         return result;
167 }
168
169 /* Test content(), integer_content(), primpart(). */
170 static unsigned check_content(const ex & e, const ex & x, const ex & ic, const ex & c, const ex & pp)
171 {
172         unsigned result = 0;
173
174         ex r_ic = e.integer_content();
175         if (!r_ic.is_equal(ic)) {
176                 clog << "integer_content(" << e << ") erroneously returned "
177                      << r_ic << " instead of " << ic << endl;
178                 ++result;
179         }
180
181         ex r_c = e.content(x);
182         if (!r_c.is_equal(c)) {
183                 clog << "content(" << e << ", " << x << ") erroneously returned "
184                      << r_c << " instead of " << c << endl;
185                 ++result;
186         }
187
188         ex r_pp = e.primpart(x);
189         if (!r_pp.is_equal(pp)) {
190                 clog << "primpart(" << e << ", " << x << ") erroneously returned "
191                      << r_pp << " instead of " << pp << endl;
192                 ++result;
193         }
194
195         ex r = r_c*r_pp*e.unit(x);
196         if (!(r - e).expand().is_zero()) {
197                 clog << "product of unit, content, and primitive part of " << e << " yielded "
198                      << r << " instead of " << e << endl;
199                 ++result;
200         }
201
202         return result;
203 }
204
205 static unsigned exam_content()
206 {
207         unsigned result = 0;
208         symbol x("x"), y("y");
209
210         result += check_content(ex(-3)/4, x, ex(3)/4, ex(3)/4, 1);
211         result += check_content(-x/4, x, ex(1)/4, ex(1)/4, x);
212         result += check_content(5*x-15, x, 5, 5, x-3);
213         result += check_content(5*x*y-15*y*y, x, 5, 5*y, x-3*y);
214         result += check_content(-15*x/2+ex(25)/3, x, ex(5)/6, ex(5)/6, 9*x-10);
215         result += check_content(-x*y, x, 1, y, x);
216
217         return result;
218 }
219
220 unsigned exam_normalization()
221 {
222         unsigned result = 0;
223         
224         cout << "examining rational function normalization" << flush;
225         
226         result += exam_normal1(); cout << '.' << flush;
227         result += exam_normal2(); cout << '.' << flush;
228         result += exam_normal3(); cout << '.' << flush;
229         result += exam_normal4(); cout << '.' << flush;
230         result += exam_content(); cout << '.' << flush;
231         
232         return result;
233 }
234
235 int main(int argc, char** argv)
236 {
237         return exam_normalization();
238 }