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