]> www.ginac.de Git - ginac.git/blob - check/inifcns_consist.cpp
- some files were missing in the distribution
[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 /*
7  *  GiNaC Copyright (C) 1999 Johannes Gutenberg University Mainz, Germany
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 2 of the License, or
12  *  (at your option) any later version.
13  *
14  *  This program is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *  GNU General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License
20  *  along with this program; if not, write to the Free Software
21  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
23
24 #include <ginac/ginac.h>
25 using namespace GiNaC;
26
27 /* Simple tests on the sine trigonometric function. */
28 static unsigned inifcns_consist_sin(void)
29 {
30     unsigned result = 0;
31     bool errorflag;
32     
33     // sin(n*Pi) == 0?
34     errorflag = false;
35     for (int n=-10; n<=10; ++n) {
36         if (  sin(n*Pi).eval() != numeric(0) ||
37              !sin(n*Pi).eval().info(info_flags::integer) )
38             errorflag = true;
39     }
40     if ( errorflag ) {
41         clog << "sin(n*Pi) with integer n does not always return exact 0"
42              << endl;
43         ++result;
44     }
45     
46     // sin((n+1/2)*Pi) == {+|-}1?
47     errorflag = false;
48     for (int n=-10; n<=10; ++n) {
49         if ( ! sin((n+numeric(1,2))*Pi).eval().info(info_flags::integer) ||
50              !(sin((n+numeric(1,2))*Pi).eval() == numeric(1) ||
51                sin((n+numeric(1,2))*Pi).eval() == numeric(-1)) )
52             errorflag = true;
53     }
54     if ( errorflag ) {
55         clog << "sin((n+1/2)*Pi) with integer n does not always return exact {+|-}1"
56              << endl;
57         ++result;
58     }
59     
60     return result;
61 }
62
63 /* Simple tests on the cosine trigonometric function. */
64 static unsigned inifcns_consist_cos(void)
65 {
66     unsigned result = 0;
67     bool errorflag;
68     
69     // cos((n+1/2)*Pi) == 0?
70     errorflag = false;
71     for (int n=-10; n<=10; ++n) {
72         if (  cos((n+numeric(1,2))*Pi).eval() != numeric(0) ||
73              !cos((n+numeric(1,2))*Pi).eval().info(info_flags::integer) )
74             errorflag = true;
75     }
76     if ( errorflag ) {
77         clog << "cos((n+1/2)*Pi) with integer n does not always return exact 0"
78              << endl;
79         ++result;
80     }
81     
82     // cos(n*Pi) == 0?
83     errorflag = false;
84     for (int n=-10; n<=10; ++n) {
85         if ( ! cos(n*Pi).eval().info(info_flags::integer) ||
86              !(cos(n*Pi).eval() == numeric(1) ||
87                cos(n*Pi).eval() == numeric(-1)) )
88             errorflag = true;
89     }
90     if ( errorflag ) {
91         clog << "cos(n*Pi) with integer n does not always return exact {+|-}1"
92              << endl;
93         ++result;
94     }
95     
96     return result;
97 }
98
99 /* Assorted tests on other transcendental functions. */
100 static unsigned inifcns_consist_trans(void)
101 {
102     unsigned result = 0;
103     symbol x("x");
104     ex chk;
105     
106     chk = asin(1)-acos(0);
107     if (!chk.is_zero()) {
108         clog << "asin(1)-acos(0) erroneously returned " << chk
109              << " instead of 0" << endl;
110         ++result;
111     }
112     
113     // arbitrary check of type sin(f(x)):
114     chk = pow(sin(acos(x)),2) + pow(sin(asin(x)),2)
115         - (1+pow(x,2))*pow(sin(atan(x)),2);
116     if (chk != 1-pow(x,2)) {
117         clog << "sin(acos(x))^2 + sin(asin(x))^2 - (1+x^2)*sin(atan(x))^2 "
118              << "erroneously returned " << chk << " instead of 1-x^2" << endl;
119         ++result;
120     }
121     
122     // arbitrary check of type cos(f(x)):
123     chk = pow(cos(acos(x)),2) + pow(cos(asin(x)),2)
124         - (1+pow(x,2))*pow(cos(atan(x)),2);
125     if (!chk.is_zero()) {
126         clog << "cos(acos(x))^2 + cos(asin(x))^2 - (1+x^2)*cos(atan(x))^2 "
127              << "erroneously returned " << chk << " instead of 0" << endl;
128         ++result;
129     }
130     
131     // arbitrary check of type tan(f(x)):
132     chk = tan(acos(x))*tan(asin(x)) - tan(atan(x));
133     if (chk != 1-x) {
134         clog << "tan(acos(x))*tan(asin(x)) - tan(atan(x)) "
135              << "erroneously returned " << chk << " instead of -x+1" << endl;
136         ++result;
137     }
138     
139     // arbitrary check of type sinh(f(x)):
140     chk = -pow(sinh(acosh(x)),2).expand()*pow(sinh(atanh(x)),2)
141         - pow(sinh(asinh(x)),2);
142     if (!chk.is_zero()) {
143         clog << "expand(-(sinh(acosh(x)))^2)*(sinh(atanh(x))^2) - sinh(asinh(x))^2 "
144              << "erroneously returned " << chk << " instead of 0" << endl;
145         ++result;
146     }
147     
148     // arbitrary check of type cosh(f(x)):
149     chk = (pow(cosh(asinh(x)),2) - 2*pow(cosh(acosh(x)),2))
150         * pow(cosh(atanh(x)),2);
151     if (chk != 1) {
152         clog << "(cosh(asinh(x))^2 - 2*cosh(acosh(x))^2) * cosh(atanh(x))^2 "
153              << "erroneously returned " << chk << " instead of 1" << endl;
154         ++result;
155     }
156     
157     // arbitrary check of type tanh(f(x)):
158     chk = (pow(tanh(asinh(x)),-2) - pow(tanh(acosh(x)),2)).expand()
159         * pow(tanh(atanh(x)),2);
160     if (chk != 2) {
161         clog << "expand(tanh(acosh(x))^2 - tanh(asinh(x))^(-2)) * tanh(atanh(x))^2 "
162              << "erroneously returned " << chk << " instead of 2" << endl;
163         ++result;
164     }
165     
166     return result;
167 }
168
169 /* Simple tests on the Gamma combinatorial function.  We stuff in arguments
170  * where the result exists in closed form and check if it's ok. */
171 static unsigned inifcns_consist_gamma(void)
172 {
173     unsigned result = 0;
174     ex e;
175     
176     e = gamma(ex(1));
177     for (int i=2; i<8; ++i) {
178         e += gamma(ex(i));
179     }
180     if ( e != numeric(874) ) {
181         clog << "gamma(1)+...+gamma(7) erroneously returned "
182              << e << " instead of 874" << endl;
183         ++result;
184     }
185     
186     e = gamma(ex(1));
187     for (int i=2; i<8; ++i) {
188         e *= gamma(ex(i));
189     }
190     if ( e != numeric(24883200) ) {
191         clog << "gamma(1)*...*gamma(7) erroneously returned "
192              << e << " instead of 24883200" << endl;
193         ++result;
194     }
195               
196     e = gamma(ex(numeric(5, 2)))*gamma(ex(numeric(9, 2)))*64;
197     if ( e != 315*Pi ) {
198         clog << "64*gamma(5/2)*gamma(9/2) erroneously returned "
199              << e << " instead of 315*Pi" << endl;
200         ++result;
201     }
202     
203     e = gamma(ex(numeric(-13, 2)));
204     for (int i=-13; i<7; i=i+2) {
205         e += gamma(ex(numeric(i, 2)));
206     }
207     e = (e*gamma(ex(numeric(15, 2)))*numeric(512));
208     if ( e != numeric(633935)*Pi ) {
209         clog << "512*(gamma(-13/2)+...+gamma(5/2))*gamma(15/2) erroneously returned "
210              << e << " instead of 633935*Pi" << endl;
211         ++result;
212     }
213     
214     return result;
215 }
216
217 unsigned inifcns_consist(void)
218 {
219     unsigned result = 0;
220
221     cout << "checking consistency of symbolic functions..." << flush;
222     clog << "---------consistency of symbolic functions:" << endl;
223     
224     result += inifcns_consist_sin();
225     result += inifcns_consist_cos();
226     result += inifcns_consist_trans();
227     result += inifcns_consist_gamma();
228
229     if ( !result ) {
230         cout << " passed ";
231         clog << "(no output)" << endl;
232     } else {
233         cout << " failed ";
234     }
235     
236     return result;
237 }