]> www.ginac.de Git - ginac.git/blob - check/normalization.cpp
- fixed typo.
[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 using namespace GiNaC;
25
26 static symbol x("x"), y("y"), z("z");
27
28 static unsigned check_normal(const ex &e, const ex &d)
29 {
30         ex en = e.normal();
31         if (en.compare(d) != 0) {
32                 clog << "normal form of " << e << " is " << en << " (should be " << d << ")" << endl;
33                 return 1;
34         }
35         return 0;
36 }
37
38 static unsigned 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 = exZERO();
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 = numeric(2)/x + y/3;
55         d = (x*y/3 + 2) / x;
56         result += check_normal(e, d);
57
58         // Fraction addition
59         e = pow(x, -1) + x/(x+1);
60         d = (pow(x, 2)+x+1)/(x*(x+1));
61         result += check_normal(e, d);
62
63         // Fraction cancellation
64         e = (pow(x, 2) - pow(y, 2)) / pow(x-y, 3);
65         d = (x + y) / (pow(x, 2) + pow(y, 2) - x * y * 2);
66         result += check_normal(e, d);
67
68         // Fraction cancellation
69         e = (pow(x, -1) + x) / (pow(x , 2) * 2 + 2);
70         d = pow(x * 2, -1);
71         result += check_normal(e, d);
72
73         // Distribution of powers
74         e = pow(x/y, 2);
75         d = pow(x, 2) / pow(y, 2);
76         result += check_normal(e, d);
77
78         // Distribution of powers (integer, distribute) and fraction addition
79         e = pow(pow(x, -1) + x, 2);
80         d = pow(pow(x, 2) + 1, 2) / pow(x, 2);
81         result += check_normal(e, d);
82
83         // Distribution of powers (non-integer, don't distribute) and fraction addition
84         e = pow(pow(x, -1) + x, numeric(1)/2);
85         d = pow((pow(x, 2) + 1) / x, numeric(1)/2);
86         result += check_normal(e, d);
87
88         // Replacement of functions with temporary symbols and fraction cancellation
89         e = pow(sin(x), 2) - pow(cos(x), 2);
90         e /= sin(x) + cos(x);
91         d = sin(x) - cos(x);
92         result += check_normal(e, d);
93
94         // Replacement of non-integer powers with temporary symbols
95         e = (pow(numeric(2), numeric(1)/2) * x + x) / x;
96         d = pow(numeric(2), numeric(1)/2) + 1;
97         result += check_normal(e, d);
98
99         // Replacement of complex numbers with temporary symbols
100         e = (x + y + x*I + y*I) / (x + y);
101         d = 1 + I;
102         result += check_normal(e, d);
103
104         e = (pow(x, 2) + pow(y, 2)) / (x + y*I);
105         d = e;
106         result += check_normal(e, d);
107
108         // More complex rational function
109         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);
110         d = (y*2 + z*2) / (x + y*2);
111         result += check_normal(e, d);
112
113         return result;
114 }
115
116 unsigned normalization(void)
117 {
118         unsigned result = 0;
119
120         cout << "checking rational function normalization..." << flush;
121         clog << "---------rational function normalization:" << endl;
122
123         result += normal1();
124
125         if (!result) {
126                 cout << " passed ";
127                 clog << "(no output)" << endl;
128         } else {
129                 cout << " failed ";
130         }
131         return result;
132 }