]> www.ginac.de Git - ginac.git/blob - check/check_inifcns.cpp
- Fixed the bug that broke xloop's po_redux.
[ginac.git] / check / check_inifcns.cpp
1 /** @file check_inifcns.cpp
2  *
3  *  This test routine applies assorted tests on initially known higher level
4  *  functions. */
5
6 /*
7  *  GiNaC Copyright (C) 1999-2000 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 "checks.h"
25
26 /* Some tests on the sine trigonometric function. */
27 static unsigned inifcns_check_sin(void)
28 {
29     unsigned result = 0;
30     bool errorflag = false;
31     
32     // sin(n*Pi) == 0?
33     errorflag = false;
34     for (int n=-10; n<=10; ++n) {
35         if (sin(n*Pi).eval() != numeric(0) ||
36             !sin(n*Pi).eval().info(info_flags::integer))
37             errorflag = true;
38     }
39     if (errorflag) {
40         // we don't count each of those errors
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     // compare sin((q*Pi).evalf()) with sin(q*Pi).eval().evalf() at various
61     // points.  E.g. if sin(Pi/10) returns something symbolic this should be
62     // equal to sqrt(5)/4-1/4.  This routine will spot programming mistakes
63     // of this kind:
64     errorflag = false;
65     ex argument;
66     numeric epsilon(double(1e-8));
67     for (int n=-340; n<=340; ++n) {
68         argument = n*Pi/60;
69         if (abs(sin(evalf(argument))-evalf(sin(argument)))>epsilon) {
70             clog << "sin(" << argument << ") returns "
71                  << sin(argument) << endl;
72             errorflag = true;
73         }
74     }
75     if (errorflag)
76         ++result;
77     
78     return result;
79 }
80
81 /* Simple tests on the cosine trigonometric function. */
82 static unsigned inifcns_check_cos(void)
83 {
84     unsigned result = 0;
85     bool errorflag;
86     
87     // cos((n+1/2)*Pi) == 0?
88     errorflag = false;
89     for (int n=-10; n<=10; ++n) {
90         if (cos((n+numeric(1,2))*Pi).eval() != numeric(0) ||
91             !cos((n+numeric(1,2))*Pi).eval().info(info_flags::integer))
92             errorflag = true;
93     }
94     if (errorflag) {
95         clog << "cos((n+1/2)*Pi) with integer n does not always return exact 0"
96              << endl;
97         ++result;
98     }
99     
100     // cos(n*Pi) == 0?
101     errorflag = false;
102     for (int n=-10; n<=10; ++n) {
103         if (!cos(n*Pi).eval().info(info_flags::integer) ||
104             !(cos(n*Pi).eval() == numeric(1) ||
105               cos(n*Pi).eval() == numeric(-1)))
106             errorflag = true;
107     }
108     if (errorflag) {
109         clog << "cos(n*Pi) with integer n does not always return exact {+|-}1"
110              << endl;
111         ++result;
112     }
113     
114     // compare cos((q*Pi).evalf()) with cos(q*Pi).eval().evalf() at various
115     // points.  E.g. if cos(Pi/12) returns something symbolic this should be
116     // equal to 1/4*(1+1/3*sqrt(3))*sqrt(6).  This routine will spot
117     // programming mistakes of this kind:
118     errorflag = false;
119     ex argument;
120     numeric epsilon(double(1e-8));
121     for (int n=-340; n<=340; ++n) {
122         argument = n*Pi/60;
123         if (abs(cos(evalf(argument))-evalf(cos(argument)))>epsilon) {
124             clog << "cos(" << argument << ") returns "
125                  << cos(argument) << endl;
126             errorflag = true;
127         }
128     }
129     if (errorflag)
130         ++result;
131     
132     return result;
133 }
134
135 /* Simple tests on the tangent trigonometric function. */
136 static unsigned inifcns_check_tan(void)
137 {
138     unsigned result = 0;
139     bool errorflag;
140     
141     // compare tan((q*Pi).evalf()) with tan(q*Pi).eval().evalf() at various
142     // points.  E.g. if tan(Pi/12) returns something symbolic this should be
143     // equal to 2-sqrt(3).  This routine will spot programming mistakes of 
144     // this kind:
145     errorflag = false;
146     ex argument;
147     numeric epsilon(double(1e-8));
148     for (int n=-340; n<=340; ++n) {
149         if (!(n%30) && (n%60))  // skip poles
150             ++n;
151         argument = n*Pi/60;
152         if (abs(tan(evalf(argument))-evalf(tan(argument)))>epsilon) {
153             clog << "tan(" << argument << ") returns "
154                  << tan(argument) << endl;
155             errorflag = true;
156         }
157     }
158     if (errorflag)
159         ++result;
160     
161     return result;
162 }
163
164 /* Simple tests on the dilogarithm function. */
165 static unsigned inifcns_check_Li2(void)
166 {
167     // NOTE: this can safely be removed once CLN supports dilogarithms and
168     // checks them itself.
169     unsigned result = 0;
170     bool errorflag;
171     
172     // check the relation Li2(z^2) == 2 * (Li2(z) + Li2(-z)) numerically, which
173     // should hold in the entire complex plane:
174     errorflag = false;
175     ex argument;
176     numeric epsilon(double(1e-16));
177     for (int n=0; n<200; ++n) {
178         argument = numeric(20.0*rand()/(RAND_MAX+1.0)-10.0)
179                  + numeric(20.0*rand()/(RAND_MAX+1.0)-10.0)*I;
180         if (abs(Li2(pow(argument,2))-2*Li2(argument)-2*Li2(-argument)) > epsilon) {
181             cout << "Li2(z) at z==" << argument
182                  << " failed to satisfy Li2(z^2)==2*(Li2(z)+Li2(-z))" << endl;
183             errorflag = true;
184         }
185     }
186     
187     if (errorflag)
188         ++result;
189     
190     return result;
191 }
192
193 unsigned check_inifcns(void)
194 {
195     unsigned result = 0;
196
197     cout << "checking consistency of symbolic functions" << flush;
198     clog << "---------consistency of symbolic functions:" << endl;
199     
200     result += inifcns_check_sin();  cout << '.' << flush;
201     result += inifcns_check_cos();  cout << '.' << flush;
202     result += inifcns_check_tan();  cout << '.' << flush;
203     result += inifcns_check_Li2();  cout << '.' << flush;
204     
205     if (!result) {
206         cout << " passed " << endl;
207         clog << "(no output)" << endl;
208     } else {
209         cout << " failed " << endl;
210     }
211     
212     return result;
213 }