]> www.ginac.de Git - ginac.git/blob - check/normalization.cpp
- diff() is now only defined on classes ex and basic, where it handles
[ginac.git] / check / normalization.cpp
1 /** @file 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 "ginac.h"
24
25 #ifndef NO_NAMESPACE_GINAC
26 using namespace GiNaC;
27 #endif // ndef NO_NAMESPACE_GINAC
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 << " erroneously returned "
36              << en << " (should be " << d << ")" << endl;
37         return 1;
38     }
39     return 0;
40 }
41
42 static unsigned normal1(void)
43 {
44     unsigned result = 0;
45     ex e, d;
46     
47     // Expansion
48     e = pow(x, 2) - (x+1)*(x-1) - 1;
49     d = ex(0);
50     result += check_normal(e, d);
51     
52     // Expansion inside functions
53     e = sin(x*(x+1)-x) + 1;
54     d = sin(pow(x, 2)) + 1;
55     result += check_normal(e, d);
56     
57     // Fraction addition
58     e = numeric(2)/x + y/3;
59     d = (x*y/3 + 2) / x;
60     result += check_normal(e, d);
61     
62     // Fraction addition
63     e = pow(x, -1) + x/(x+1);
64     d = (pow(x, 2)+x+1)/(x*(x+1));
65     result += check_normal(e, d);
66     
67     // Fraction cancellation
68     e = (pow(x, 2) - pow(y, 2)) / pow(x-y, 3);
69     d = (x + y) / (pow(x, 2) + pow(y, 2) - x * y * 2);
70     result += check_normal(e, d);
71     
72     // Fraction cancellation
73     e = (pow(x, -1) + x) / (pow(x , 2) * 2 + 2);
74     d = pow(x * 2, -1);
75     result += check_normal(e, d);
76
77         // Fraction cancellation with rational coefficients
78         e = (pow(x, 2) - pow(y, 2)) / pow(x/2 - y/2, 3);
79         d = (8 * x + 8 * y) / (pow(x, 2) + pow(y, 2) - x * y * 2);
80         result += check_normal(e, d);
81
82         // Fraction cancellation with rational coefficients
83         e = z/5 * (x/7 + y/10) / (x/14 + y/20);
84         d = 2*z/5;
85         result += check_normal(e, d);
86     
87     // Distribution of powers
88     e = pow(x/y, 2);
89     d = pow(x, 2) / pow(y, 2);
90     result += check_normal(e, d);
91     
92     // Distribution of powers (integer, distribute) and fraction addition
93     e = pow(pow(x, -1) + x, 2);
94     d = pow(pow(x, 2) + 1, 2) / pow(x, 2);
95     result += check_normal(e, d);
96     
97     // Distribution of powers (non-integer, don't distribute) and fraction addition
98     e = pow(pow(x, -1) + x, numeric(1)/2);
99     d = pow((pow(x, 2) + 1) / x, numeric(1)/2);
100     result += check_normal(e, d);
101     
102     // Replacement of functions with temporary symbols and fraction cancellation
103     e = pow(sin(x), 2) - pow(cos(x), 2);
104     e /= sin(x) + cos(x);
105     d = sin(x) - cos(x);
106     result += check_normal(e, d);
107     
108     // Replacement of non-integer powers with temporary symbols
109     e = (pow(numeric(2), numeric(1)/2) * x + x) / x;
110     d = pow(numeric(2), numeric(1)/2) + 1;
111     result += check_normal(e, d);
112     
113     // Replacement of complex numbers with temporary symbols
114     e = (x + y + x*I + y*I) / (x + y);
115     d = 1 + I;
116     result += check_normal(e, d);
117     
118     e = (pow(x, 2) + pow(y, 2)) / (x + y*I);
119     d = e;
120     result += check_normal(e, d);
121     
122     // More complex rational function
123     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);
124     d = (y*2 + z*2) / (x + y*2);
125     result += check_normal(e, d);
126     
127     return result;
128 }
129
130 unsigned normalization(void)
131 {
132     unsigned result = 0;
133     
134     cout << "checking rational function normalization..." << flush;
135     clog << "---------rational function normalization:" << endl;
136     
137     result += normal1();
138     
139     if (!result) {
140         cout << " passed ";
141         clog << "(no output)" << endl;
142     } else {
143         cout << " failed ";
144     }
145     return result;
146 }