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