]> www.ginac.de Git - ginac.git/blob - check/normalization.cpp
89809c1a88715912132e2c83493fe89f77122be3
[ginac.git] / check / normalization.cpp
1 // check/normalization.cpp
2
3 /* Rational function normalization test-suite. */
4
5 #include <GiNaC/ginac.h>
6
7 static symbol x("x"), y("y"), z("z");
8
9 static unsigned check_normal(const ex &e, const ex &d)
10 {
11         ex en = e.normal();
12         if (en.compare(d) != 0) {
13                 clog << "normal form of " << e << " is " << en << " (should be " << d << ")" << endl;
14                 return 1;
15         }
16         return 0;
17 }
18
19 static unsigned normal1(void)
20 {
21         unsigned result = 0;
22         ex e, d;
23
24         // Expansion
25         e = pow(x, 2) - (x+1)*(x-1) - 1;
26         d = exZERO();
27         result += check_normal(e, d);
28
29         // Expansion inside functions
30         e = sin(x*(x+1)-x) + 1;
31         d = sin(pow(x, 2)) + 1;
32         result += check_normal(e, d);
33
34         // Fraction addition
35         e = numeric(2)/x + y/3;
36         d = (x*y/3 + 2) / x;
37         result += check_normal(e, d);
38
39         // Fraction addition
40         e = pow(x, -1) + x/(x+1);
41         d = (pow(x, 2)+x+1)/(x*(x+1));
42         result += check_normal(e, d);
43
44         // Fraction cancellation
45         e = (pow(x, 2) - pow(y, 2)) / pow(x-y, 3);
46         d = (x + y) / (pow(x, 2) + pow(y, 2) - x * y * 2);
47         result += check_normal(e, d);
48
49         // Fraction cancellation
50         e = (pow(x, -1) + x) / (pow(x , 2) * 2 + 2);
51         d = pow(x * 2, -1);
52         result += check_normal(e, d);
53
54         // Distribution of powers
55         e = pow(x/y, 2);
56         d = pow(x, 2) / pow(y, 2);
57         result += check_normal(e, d);
58
59         // Distribution of powers (integer, distribute) and fraction addition
60         e = pow(pow(x, -1) + x, 2);
61         d = pow(pow(x, 2) + 1, 2) / pow(x, 2);
62         result += check_normal(e, d);
63
64         // Distribution of powers (non-integer, don't distribute) and fraction addition
65         e = pow(pow(x, -1) + x, numeric(1)/2);
66         d = pow((pow(x, 2) + 1) / x, numeric(1)/2);
67         result += check_normal(e, d);
68
69         // Replacement of functions with temporary symbols and fraction cancellation
70         e = pow(sin(x), 2) - pow(cos(x), 2);
71         e /= sin(x) + cos(x);
72         d = sin(x) - cos(x);
73         result += check_normal(e, d);
74
75         // Replacement of non-integer powers with temporary symbols
76         e = (pow(numeric(2), numeric(1)/2) * x + x) / x;
77         d = pow(numeric(2), numeric(1)/2) + 1;
78         result += check_normal(e, d);
79
80         // Replacement of complex numbers with temporary symbols
81         e = (x + y + x*I + y*I) / (x + y);
82         d = 1 + I;
83         result += check_normal(e, d);
84
85         e = (pow(x, 2) + pow(y, 2)) / (x + y*I);
86         d = e;
87         result += check_normal(e, d);
88
89         // More complex rational function
90         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);
91         d = (y*2 + z*2) / (x + y*2);
92         result += check_normal(e, d);
93
94         return result;
95 }
96
97 unsigned normalization(void)
98 {
99         unsigned result = 0;
100
101         cout << "checking rational function normalization..." << flush;
102         clog << "---------rational function normalization:" << endl;
103
104         result += normal1();
105
106         if (!result) {
107                 cout << " passed ";
108                 clog << "(no output)" << endl;
109         } else {
110                 cout << " failed ";
111         }
112         return result;
113 }