]> www.ginac.de Git - ginac.git/blob - check/normalization.cpp
- modified the comment blocks so the copyright message no longer appears in
[ginac.git] / check / normalization.cpp
1 /** @file normalization.cpp
2  *
3  *  Rational function normalization test suite.
4  *
5  *  GiNaC Copyright (C) 1999 Johannes Gutenberg University Mainz, Germany
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include <ginac/ginac.h>
23
24 static symbol x("x"), y("y"), z("z");
25
26 static unsigned check_normal(const ex &e, const ex &d)
27 {
28         ex en = e.normal();
29         if (en.compare(d) != 0) {
30                 clog << "normal form of " << e << " is " << en << " (should be " << d << ")" << endl;
31                 return 1;
32         }
33         return 0;
34 }
35
36 static unsigned normal1(void)
37 {
38         unsigned result = 0;
39         ex e, d;
40
41         // Expansion
42         e = pow(x, 2) - (x+1)*(x-1) - 1;
43         d = exZERO();
44         result += check_normal(e, d);
45
46         // Expansion inside functions
47         e = sin(x*(x+1)-x) + 1;
48         d = sin(pow(x, 2)) + 1;
49         result += check_normal(e, d);
50
51         // Fraction addition
52         e = numeric(2)/x + y/3;
53         d = (x*y/3 + 2) / x;
54         result += check_normal(e, d);
55
56         // Fraction addition
57         e = pow(x, -1) + x/(x+1);
58         d = (pow(x, 2)+x+1)/(x*(x+1));
59         result += check_normal(e, d);
60
61         // Fraction cancellation
62         e = (pow(x, 2) - pow(y, 2)) / pow(x-y, 3);
63         d = (x + y) / (pow(x, 2) + pow(y, 2) - x * y * 2);
64         result += check_normal(e, d);
65
66         // Fraction cancellation
67         e = (pow(x, -1) + x) / (pow(x , 2) * 2 + 2);
68         d = pow(x * 2, -1);
69         result += check_normal(e, d);
70
71         // Distribution of powers
72         e = pow(x/y, 2);
73         d = pow(x, 2) / pow(y, 2);
74         result += check_normal(e, d);
75
76         // Distribution of powers (integer, distribute) and fraction addition
77         e = pow(pow(x, -1) + x, 2);
78         d = pow(pow(x, 2) + 1, 2) / pow(x, 2);
79         result += check_normal(e, d);
80
81         // Distribution of powers (non-integer, don't distribute) and fraction addition
82         e = pow(pow(x, -1) + x, numeric(1)/2);
83         d = pow((pow(x, 2) + 1) / x, numeric(1)/2);
84         result += check_normal(e, d);
85
86         // Replacement of functions with temporary symbols and fraction cancellation
87         e = pow(sin(x), 2) - pow(cos(x), 2);
88         e /= sin(x) + cos(x);
89         d = sin(x) - cos(x);
90         result += check_normal(e, d);
91
92         // Replacement of non-integer powers with temporary symbols
93         e = (pow(numeric(2), numeric(1)/2) * x + x) / x;
94         d = pow(numeric(2), numeric(1)/2) + 1;
95         result += check_normal(e, d);
96
97         // Replacement of complex numbers with temporary symbols
98         e = (x + y + x*I + y*I) / (x + y);
99         d = 1 + I;
100         result += check_normal(e, d);
101
102         e = (pow(x, 2) + pow(y, 2)) / (x + y*I);
103         d = e;
104         result += check_normal(e, d);
105
106         // More complex rational function
107         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);
108         d = (y*2 + z*2) / (x + y*2);
109         result += check_normal(e, d);
110
111         return result;
112 }
113
114 unsigned normalization(void)
115 {
116         unsigned result = 0;
117
118         cout << "checking rational function normalization..." << flush;
119         clog << "---------rational function normalization:" << endl;
120
121         result += normal1();
122
123         if (!result) {
124                 cout << " passed ";
125                 clog << "(no output)" << endl;
126         } else {
127                 cout << " failed ";
128         }
129         return result;
130 }