]> www.ginac.de Git - ginac.git/blob - check/normalization.cpp
- added the beta function to GiNaC
[ginac.git] / check / normalization.cpp
1 /** @file normalization.cpp
2  *
3  *  Rational function normalization test suite. */
4
5 /*
6  *  GiNaC Copyright (C) 1999 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 <ginac/ginac.h>
24
25 #ifndef NO_GINAC_NAMESPACE
26 using namespace GiNaC;
27 #endif // ndef NO_GINAC_NAMESPACE
28
29 static symbol 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.compare(d) != 0) {
35                 clog << "normal form of " << e << " is " << en << " (should be " << d << ")" << endl;
36                 return 1;
37         }
38         return 0;
39 }
40
41 static unsigned normal1(void)
42 {
43         unsigned result = 0;
44         ex e, d;
45
46         // Expansion
47         e = pow(x, 2) - (x+1)*(x-1) - 1;
48         d = exZERO();
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 = numeric(2)/x + y/3;
58         d = (x*y/3 + 2) / x;
59         result += check_normal(e, d);
60
61         // Fraction addition
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         // Fraction cancellation
67         e = (pow(x, 2) - pow(y, 2)) / pow(x-y, 3);
68         d = (x + y) / (pow(x, 2) + pow(y, 2) - x * y * 2);
69         result += check_normal(e, d);
70
71         // Fraction cancellation
72         e = (pow(x, -1) + x) / (pow(x , 2) * 2 + 2);
73         d = pow(x * 2, -1);
74         result += check_normal(e, d);
75
76         // Distribution of powers
77         e = pow(x/y, 2);
78         d = pow(x, 2) / pow(y, 2);
79         result += check_normal(e, d);
80
81         // Distribution of powers (integer, distribute) and fraction addition
82         e = pow(pow(x, -1) + x, 2);
83         d = pow(pow(x, 2) + 1, 2) / pow(x, 2);
84         result += check_normal(e, d);
85
86         // Distribution of powers (non-integer, don't distribute) and fraction addition
87         e = pow(pow(x, -1) + x, numeric(1)/2);
88         d = pow((pow(x, 2) + 1) / x, numeric(1)/2);
89         result += check_normal(e, d);
90
91         // Replacement of functions with temporary symbols and fraction cancellation
92         e = pow(sin(x), 2) - pow(cos(x), 2);
93         e /= sin(x) + cos(x);
94         d = sin(x) - cos(x);
95         result += check_normal(e, d);
96
97         // Replacement of non-integer powers with temporary symbols
98         e = (pow(numeric(2), numeric(1)/2) * x + x) / x;
99         d = pow(numeric(2), numeric(1)/2) + 1;
100         result += check_normal(e, d);
101
102         // Replacement of complex numbers with temporary symbols
103         e = (x + y + x*I + y*I) / (x + y);
104         d = 1 + I;
105         result += check_normal(e, d);
106
107         e = (pow(x, 2) + pow(y, 2)) / (x + y*I);
108         d = e;
109         result += check_normal(e, d);
110
111         // More complex rational function
112         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);
113         d = (y*2 + z*2) / (x + y*2);
114         result += check_normal(e, d);
115
116         return result;
117 }
118
119 unsigned normalization(void)
120 {
121         unsigned result = 0;
122
123         cout << "checking rational function normalization..." << flush;
124         clog << "---------rational function normalization:" << endl;
125
126         result += normal1();
127
128         if (!result) {
129                 cout << " passed ";
130                 clog << "(no output)" << endl;
131         } else {
132                 cout << " failed ";
133         }
134         return result;
135 }