]> www.ginac.de Git - ginac.git/blob - check/inifcns_consist.cpp
Initial revision
[ginac.git] / check / inifcns_consist.cpp
1 // check/inifcns_consist.cpp
2
3 /* This test routine applies assorted tests on initially known higher level
4  * functions. */
5
6 #include "ginac.h"
7
8 /* Simple tests on the sine trigonometric function. */
9 static unsigned inifcns_consist_sin(void)
10 {
11     unsigned result = 0;
12     bool errorflag;
13     
14     // sin(n*Pi) == 0?
15     errorflag = false;
16     for (int n=-10; n<=10; ++n) {
17         if (  sin(n*Pi).eval() != numeric(0) ||
18              !sin(n*Pi).eval().info(info_flags::integer) )
19             errorflag = true;
20     }
21     if ( errorflag ) {
22         clog << "sin(n*Pi) with integer n does not always return exact 0"
23              << endl;
24         ++result;
25     }
26     
27     // sin((n+1/2)*Pi) == {+|-}1?
28     errorflag = false;
29     for (int n=-10; n<=10; ++n) {
30         if ( ! sin((n+numeric(1,2))*Pi).eval().info(info_flags::integer) ||
31              !(sin((n+numeric(1,2))*Pi).eval() == numeric(1) ||
32                sin((n+numeric(1,2))*Pi).eval() == numeric(-1)) )
33             errorflag = true;
34     }
35     if ( errorflag ) {
36         clog << "sin((n+1/2)*Pi) with integer n does not always return exact {+|-}1"
37              << endl;
38         ++result;
39     }
40     
41     return result;
42 }
43
44 /* Simple tests on the cosine trigonometric function. */
45 static unsigned inifcns_consist_cos(void)
46 {
47     unsigned result = 0;
48     bool errorflag;
49     
50     // cos((n+1/2)*Pi) == 0?
51     errorflag = false;
52     for (int n=-10; n<=10; ++n) {
53         if (  cos((n+numeric(1,2))*Pi).eval() != numeric(0) ||
54              !cos((n+numeric(1,2))*Pi).eval().info(info_flags::integer) )
55             errorflag = true;
56     }
57     if ( errorflag ) {
58         clog << "cos((n+1/2)*Pi) with integer n does not always return exact 0"
59              << endl;
60         ++result;
61     }
62     
63     // cos(n*Pi) == 0?
64     errorflag = false;
65     for (int n=-10; n<=10; ++n) {
66         if ( ! cos(n*Pi).eval().info(info_flags::integer) ||
67              !(cos(n*Pi).eval() == numeric(1) ||
68                cos(n*Pi).eval() == numeric(-1)) )
69             errorflag = true;
70     }
71     if ( errorflag ) {
72         clog << "cos(n*Pi) with integer n does not always return exact {+|-}1"
73              << endl;
74         ++result;
75     }
76     
77     return result;
78 }
79
80 /* Assorted tests on other transcendental functions. */
81 static unsigned inifcns_consist_trans(void)
82 {
83     unsigned result = 0;
84     symbol x("x");
85     ex chk;
86     
87     chk = asin(1)-acos(0);
88     if (!chk.is_zero()) {
89         clog << "asin(1)-acos(0) erroneously returned " << chk
90              << " instead of 0" << endl;
91         ++result;
92     }
93     
94     // arbitrary check of type sin(f(x)):
95     chk = pow(sin(acos(x)),2) + pow(sin(asin(x)),2)
96         - (1+pow(x,2))*pow(sin(atan(x)),2);
97     if (chk != 1-pow(x,2)) {
98         clog << "sin(acos(x))^2 + sin(asin(x))^2 - (1+x^2)*sin(atan(x))^2 "
99              << "erroneously returned " << chk << " instead of 1-x^2" << endl;
100         ++result;
101     }
102     
103     // arbitrary check of type cos(f(x)):
104     chk = pow(cos(acos(x)),2) + pow(cos(asin(x)),2)
105         - (1+pow(x,2))*pow(cos(atan(x)),2);
106     if (!chk.is_zero()) {
107         clog << "cos(acos(x))^2 + cos(asin(x))^2 - (1+x^2)*cos(atan(x))^2 "
108              << "erroneously returned " << chk << " instead of 0" << endl;
109         ++result;
110     }
111     
112     // arbitrary check of type tan(f(x)):
113     chk = tan(acos(x))*tan(asin(x)) - tan(atan(x));
114     if (chk != 1-x) {
115         clog << "tan(acos(x))*tan(asin(x)) - tan(atan(x)) "
116              << "erroneously returned " << chk << " instead of -x+1" << endl;
117         ++result;
118     }
119     
120     // arbitrary check of type sinh(f(x)):
121     chk = -pow(sinh(acosh(x)),2).expand()*pow(sinh(atanh(x)),2)
122         - pow(sinh(asinh(x)),2);
123     if (!chk.is_zero()) {
124         clog << "expand(-(sinh(acosh(x)))^2)*(sinh(atanh(x))^2) - sinh(asinh(x))^2 "
125              << "erroneously returned " << chk << " instead of 0" << endl;
126         ++result;
127     }
128     
129     // arbitrary check of type cosh(f(x)):
130     chk = (pow(cosh(asinh(x)),2) - 2*pow(cosh(acosh(x)),2))
131         * pow(cosh(atanh(x)),2);
132     if (chk != 1) {
133         clog << "(cosh(asinh(x))^2 - 2*cosh(acosh(x))^2) * cosh(atanh(x))^2 "
134              << "erroneously returned " << chk << " instead of 1" << endl;
135         ++result;
136     }
137     
138     // arbitrary check of type tanh(f(x)):
139     chk = (pow(tanh(asinh(x)),-2) - pow(tanh(acosh(x)),2)).expand()
140         * pow(tanh(atanh(x)),2);
141     if (chk != 2) {
142         clog << "expand(tanh(acosh(x))^2 - tanh(asinh(x))^(-2)) * tanh(atanh(x))^2 "
143              << "erroneously returned " << chk << " instead of 2" << endl;
144         ++result;
145     }
146     
147     return result;
148 }
149
150 /* Simple tests on the Gamma combinatorial function.  We stuff in arguments
151  * where the result exists in closed form and check if it's ok. */
152 static unsigned inifcns_consist_gamma(void)
153 {
154     unsigned result = 0;
155     ex e;
156     
157     e = gamma(ex(1));
158     for (int i=2; i<8; ++i) {
159         e += gamma(ex(i));
160     }
161     if ( e != numeric(874) ) {
162         clog << "gamma(1)+...+gamma(7) erroneously returned "
163              << e << " instead of 874" << endl;
164         ++result;
165     }
166     
167     e = gamma(ex(1));
168     for (int i=2; i<8; ++i) {
169         e *= gamma(ex(i));
170     }
171     if ( e != numeric(24883200) ) {
172         clog << "gamma(1)*...*gamma(7) erroneously returned "
173              << e << " instead of 24883200" << endl;
174         ++result;
175     }
176               
177     e = gamma(ex(numeric(5, 2)))*gamma(ex(numeric(9, 2)))*64;
178     if ( e != 315*Pi ) {
179         clog << "64*gamma(5/2)*gamma(9/2) erroneously returned "
180              << e << " instead of 315*Pi" << endl;
181         ++result;
182     }
183     
184     e = gamma(ex(numeric(-13, 2)));
185     for (int i=-13; i<7; i=i+2) {
186         e += gamma(ex(numeric(i, 2)));
187     }
188     e = (e*gamma(ex(numeric(15, 2)))*numeric(512));
189     if ( e != numeric(633935)*Pi ) {
190         clog << "512*(gamma(-13/2)+...+gamma(5/2))*gamma(15/2) erroneously returned "
191              << e << " instead of 633935*Pi" << endl;
192         ++result;
193     }
194     
195     return result;
196 }
197
198 unsigned inifcns_consist(void)
199 {
200     unsigned result = 0;
201
202     cout << "checking consistency of symbolic functions..." << flush;
203     clog << "---------consistency of symbolic functions:" << endl;
204     
205     result += inifcns_consist_sin();
206     result += inifcns_consist_cos();
207     result += inifcns_consist_trans();
208     result += inifcns_consist_gamma();
209
210     if ( !result ) {
211         cout << " passed ";
212         clog << "(no output)" << endl;
213     } else {
214         cout << " failed ";
215     }
216     
217     return result;
218 }