1 /** @file check_inifcns.cpp
3 * This test routine applies assorted tests on initially known higher level
7 * GiNaC Copyright (C) 1999-2009 Johannes Gutenberg University Mainz, Germany
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 using namespace GiNaC;
27 #include <cstdlib> // for rand()
31 /* Some tests on the sine trigonometric function. */
32 static unsigned inifcns_check_sin()
35 bool errorflag = false;
39 for (int n=-10; n<=10; ++n) {
40 if (sin(n*Pi).eval() != numeric(0) ||
41 !sin(n*Pi).eval().info(info_flags::integer))
45 // we don't count each of those errors
46 clog << "sin(n*Pi) with integer n does not always return exact 0"
51 // sin((n+1/2)*Pi) == {+|-}1?
53 for (int n=-10; n<=10; ++n) {
54 if (!sin((n+numeric(1,2))*Pi).eval().info(info_flags::integer) ||
55 !(sin((n+numeric(1,2))*Pi).eval() == numeric(1) ||
56 sin((n+numeric(1,2))*Pi).eval() == numeric(-1)))
60 clog << "sin((n+1/2)*Pi) with integer n does not always return exact {+|-}1"
65 // compare sin((q*Pi).evalf()) with sin(q*Pi).eval().evalf() at various
66 // points. E.g. if sin(Pi/10) returns something symbolic this should be
67 // equal to sqrt(5)/4-1/4. This routine will spot programming mistakes
71 numeric epsilon(double(1e-8));
72 for (int n=-340; n<=340; ++n) {
74 if (abs(sin(evalf(argument))-evalf(sin(argument)))>epsilon) {
75 clog << "sin(" << argument << ") returns "
76 << sin(argument) << endl;
86 /* Simple tests on the cosine trigonometric function. */
87 static unsigned inifcns_check_cos()
92 // cos((n+1/2)*Pi) == 0?
94 for (int n=-10; n<=10; ++n) {
95 if (cos((n+numeric(1,2))*Pi).eval() != numeric(0) ||
96 !cos((n+numeric(1,2))*Pi).eval().info(info_flags::integer))
100 clog << "cos((n+1/2)*Pi) with integer n does not always return exact 0"
107 for (int n=-10; n<=10; ++n) {
108 if (!cos(n*Pi).eval().info(info_flags::integer) ||
109 !(cos(n*Pi).eval() == numeric(1) ||
110 cos(n*Pi).eval() == numeric(-1)))
114 clog << "cos(n*Pi) with integer n does not always return exact {+|-}1"
119 // compare cos((q*Pi).evalf()) with cos(q*Pi).eval().evalf() at various
120 // points. E.g. if cos(Pi/12) returns something symbolic this should be
121 // equal to 1/4*(1+1/3*sqrt(3))*sqrt(6). This routine will spot
122 // programming mistakes of this kind:
125 numeric epsilon(double(1e-8));
126 for (int n=-340; n<=340; ++n) {
128 if (abs(cos(evalf(argument))-evalf(cos(argument)))>epsilon) {
129 clog << "cos(" << argument << ") returns "
130 << cos(argument) << endl;
140 /* Simple tests on the tangent trigonometric function. */
141 static unsigned inifcns_check_tan()
146 // compare tan((q*Pi).evalf()) with tan(q*Pi).eval().evalf() at various
147 // points. E.g. if tan(Pi/12) returns something symbolic this should be
148 // equal to 2-sqrt(3). This routine will spot programming mistakes of
152 numeric epsilon(double(1e-8));
153 for (int n=-340; n<=340; ++n) {
154 if (!(n%30) && (n%60)) // skip poles
157 if (abs(tan(evalf(argument))-evalf(tan(argument)))>epsilon) {
158 clog << "tan(" << argument << ") returns "
159 << tan(argument) << endl;
169 /* Simple tests on the dilogarithm function. */
170 static unsigned inifcns_check_Li2()
172 // NOTE: this can safely be removed once CLN supports dilogarithms and
173 // checks them itself.
177 // check the relation Li2(z^2) == 2 * (Li2(z) + Li2(-z)) numerically, which
178 // should hold in the entire complex plane:
181 numeric epsilon(double(1e-16));
182 for (int n=0; n<200; ++n) {
183 argument = numeric(20.0*rand()/(RAND_MAX+1.0)-10.0)
184 + numeric(20.0*rand()/(RAND_MAX+1.0)-10.0)*I;
185 if (abs(Li2(pow(argument,2))-2*Li2(argument)-2*Li2(-argument)) > epsilon) {
186 clog << "Li2(z) at z==" << argument
187 << " failed to satisfy Li2(z^2)==2*(Li2(z)+Li2(-z))" << endl;
198 unsigned check_inifcns()
202 cout << "checking consistency of symbolic functions" << flush;
204 result += inifcns_check_sin(); cout << '.' << flush;
205 result += inifcns_check_cos(); cout << '.' << flush;
206 result += inifcns_check_tan(); cout << '.' << flush;
207 result += inifcns_check_Li2(); cout << '.' << flush;
212 int main(int argc, char** argv)
214 return check_inifcns();