1 /** @file check_inifcns.cpp
3 * This test routine applies assorted tests on initially known higher level
7 * GiNaC Copyright (C) 1999-2007 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 #include <cstdlib> // rand()
28 using namespace GiNaC;
30 /* Some tests on the sine trigonometric function. */
31 static unsigned inifcns_check_sin()
34 bool errorflag = false;
38 for (int n=-10; n<=10; ++n) {
39 if (sin(n*Pi).eval() != numeric(0) ||
40 !sin(n*Pi).eval().info(info_flags::integer))
44 // we don't count each of those errors
45 clog << "sin(n*Pi) with integer n does not always return exact 0"
50 // sin((n+1/2)*Pi) == {+|-}1?
52 for (int n=-10; n<=10; ++n) {
53 if (!sin((n+numeric(1,2))*Pi).eval().info(info_flags::integer) ||
54 !(sin((n+numeric(1,2))*Pi).eval() == numeric(1) ||
55 sin((n+numeric(1,2))*Pi).eval() == numeric(-1)))
59 clog << "sin((n+1/2)*Pi) with integer n does not always return exact {+|-}1"
64 // compare sin((q*Pi).evalf()) with sin(q*Pi).eval().evalf() at various
65 // points. E.g. if sin(Pi/10) returns something symbolic this should be
66 // equal to sqrt(5)/4-1/4. This routine will spot programming mistakes
70 numeric epsilon(double(1e-8));
71 for (int n=-340; n<=340; ++n) {
73 if (abs(sin(evalf(argument))-evalf(sin(argument)))>epsilon) {
74 clog << "sin(" << argument << ") returns "
75 << sin(argument) << endl;
85 /* Simple tests on the cosine trigonometric function. */
86 static unsigned inifcns_check_cos()
91 // cos((n+1/2)*Pi) == 0?
93 for (int n=-10; n<=10; ++n) {
94 if (cos((n+numeric(1,2))*Pi).eval() != numeric(0) ||
95 !cos((n+numeric(1,2))*Pi).eval().info(info_flags::integer))
99 clog << "cos((n+1/2)*Pi) with integer n does not always return exact 0"
106 for (int n=-10; n<=10; ++n) {
107 if (!cos(n*Pi).eval().info(info_flags::integer) ||
108 !(cos(n*Pi).eval() == numeric(1) ||
109 cos(n*Pi).eval() == numeric(-1)))
113 clog << "cos(n*Pi) with integer n does not always return exact {+|-}1"
118 // compare cos((q*Pi).evalf()) with cos(q*Pi).eval().evalf() at various
119 // points. E.g. if cos(Pi/12) returns something symbolic this should be
120 // equal to 1/4*(1+1/3*sqrt(3))*sqrt(6). This routine will spot
121 // programming mistakes of this kind:
124 numeric epsilon(double(1e-8));
125 for (int n=-340; n<=340; ++n) {
127 if (abs(cos(evalf(argument))-evalf(cos(argument)))>epsilon) {
128 clog << "cos(" << argument << ") returns "
129 << cos(argument) << endl;
139 /* Simple tests on the tangent trigonometric function. */
140 static unsigned inifcns_check_tan()
145 // compare tan((q*Pi).evalf()) with tan(q*Pi).eval().evalf() at various
146 // points. E.g. if tan(Pi/12) returns something symbolic this should be
147 // equal to 2-sqrt(3). This routine will spot programming mistakes of
151 numeric epsilon(double(1e-8));
152 for (int n=-340; n<=340; ++n) {
153 if (!(n%30) && (n%60)) // skip poles
156 if (abs(tan(evalf(argument))-evalf(tan(argument)))>epsilon) {
157 clog << "tan(" << argument << ") returns "
158 << tan(argument) << endl;
168 /* Simple tests on the dilogarithm function. */
169 static unsigned inifcns_check_Li2()
171 // NOTE: this can safely be removed once CLN supports dilogarithms and
172 // checks them itself.
176 // check the relation Li2(z^2) == 2 * (Li2(z) + Li2(-z)) numerically, which
177 // should hold in the entire complex plane:
180 numeric epsilon(double(1e-16));
181 for (int n=0; n<200; ++n) {
182 argument = numeric(20.0*rand()/(RAND_MAX+1.0)-10.0)
183 + numeric(20.0*rand()/(RAND_MAX+1.0)-10.0)*I;
184 if (abs(Li2(pow(argument,2))-2*Li2(argument)-2*Li2(-argument)) > epsilon) {
185 clog << "Li2(z) at z==" << argument
186 << " failed to satisfy Li2(z^2)==2*(Li2(z)+Li2(-z))" << endl;
197 unsigned check_inifcns()
201 cout << "checking consistency of symbolic functions" << flush;
203 result += inifcns_check_sin(); cout << '.' << flush;
204 result += inifcns_check_cos(); cout << '.' << flush;
205 result += inifcns_check_tan(); cout << '.' << flush;
206 result += inifcns_check_Li2(); cout << '.' << flush;
211 int main(int argc, char** argv)
213 return check_inifcns();