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