]> www.ginac.de Git - ginac.git/blob - check/exam_normalization.cpp
- removed inert Diff() function; only Derivative() remains
[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-2000 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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.compare(d) != 0) {
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(void)
39 {
40     unsigned result = 0;
41     ex e, d;
42     
43     // Expansion
44     e = pow(x, 2) - (x+1)*(x-1) - 1;
45     d = ex(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(void)
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     e = (pow(x, 2) - pow(y, 2)) / pow(x-y, 3);
84     d = (x + y) / (pow(x, 2) + pow(y, 2) - x * y * 2);
85     result += check_normal(e, d);
86     
87     e = (pow(x, -1) + x) / (pow(x , 2) * 2 + 2);
88     d = pow(x * 2, -1);
89     result += check_normal(e, d);
90     
91     // Fraction cancellation with rational coefficients
92     e = (pow(x, 2) - pow(y, 2)) / pow(x/2 - y/2, 3);
93     d = (8 * x + 8 * y) / (pow(x, 2) + pow(y, 2) - x * y * 2);
94     result += check_normal(e, d);
95     
96     // Fraction cancellation with rational coefficients
97     e = z/5 * (x/7 + y/10) / (x/14 + y/20);
98     d = 2*z/5;
99     result += check_normal(e, d);
100     
101     return result;
102 }
103
104 static unsigned exam_normal3(void)
105 {
106     unsigned result = 0;
107     ex e, d;
108     
109     // Distribution of powers
110     e = pow(x/y, 2);
111     d = pow(x, 2) / pow(y, 2);
112     result += check_normal(e, d);
113     
114     // Distribution of powers (integer, distribute) and fraction addition
115     e = pow(pow(x, -1) + x, 2);
116     d = pow(pow(x, 2) + 1, 2) / pow(x, 2);
117     result += check_normal(e, d);
118     
119     // Distribution of powers (non-integer, don't distribute) and fraction addition
120     e = pow(pow(x, -1) + x, numeric(1)/2);
121     d = pow((pow(x, 2) + 1) / x, numeric(1)/2);
122     result += check_normal(e, d);
123     
124     return result;
125 }
126
127 static unsigned exam_normal4(void)
128 {
129     unsigned result = 0;
130     ex e, d;
131     
132     // Replacement of functions with temporary symbols and fraction cancellation
133     e = pow(sin(x), 2) - pow(cos(x), 2);
134     e /= sin(x) + cos(x);
135     d = sin(x) - cos(x);
136     result += check_normal(e, d);
137     
138     // Replacement of non-integer powers with temporary symbols
139     e = (pow(numeric(2), numeric(1)/2) * x + x) / x;
140     d = pow(numeric(2), numeric(1)/2) + 1;
141     result += check_normal(e, d);
142     
143     // Replacement of complex numbers with temporary symbols
144     e = (x + y + x*I + y*I) / (x + y);
145     d = 1 + I;
146     result += check_normal(e, d);
147     
148     e = (pow(x, 2) + pow(y, 2)) / (x + y*I);
149     d = e;
150     result += check_normal(e, d);
151     
152     // More complex rational function
153     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);
154     d = (y*2 + z*2) / (x + y*2);
155     result += check_normal(e, d);
156     
157     return result;
158 }
159
160 unsigned exam_normalization(void)
161 {
162     unsigned result = 0;
163     
164     cout << "examining rational function normalization" << flush;
165     clog << "----------rational function normalization:" << endl;
166     
167     result += exam_normal1();  cout << '.' << flush;
168     result += exam_normal2();  cout << '.' << flush;
169     result += exam_normal3();  cout << '.' << flush;
170     result += exam_normal4();  cout << '.' << flush;
171     
172     if (!result) {
173         cout << " passed " << endl;
174         clog << "(no output)" << endl;
175     } else {
176         cout << " failed " << endl;
177     }
178     
179     return result;
180 }