File: check_inifcns.cpp

package info (click to toggle)
ginac 1.0.8-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 3,544 kB
  • ctags: 3,232
  • sloc: cpp: 27,732; sh: 7,126; perl: 1,819; yacc: 763; lex: 345; makefile: 221; sed: 32
file content (213 lines) | stat: -rw-r--r-- 6,020 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/** @file check_inifcns.cpp
 *
 *  This test routine applies assorted tests on initially known higher level
 *  functions. */

/*
 *  GiNaC Copyright (C) 1999-2002 Johannes Gutenberg University Mainz, Germany
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "checks.h"

/* Some tests on the sine trigonometric function. */
static unsigned inifcns_check_sin(void)
{
	unsigned result = 0;
	bool errorflag = false;
	
	// sin(n*Pi) == 0?
	errorflag = false;
	for (int n=-10; n<=10; ++n) {
		if (sin(n*Pi).eval() != numeric(0) ||
			!sin(n*Pi).eval().info(info_flags::integer))
			errorflag = true;
	}
	if (errorflag) {
		// we don't count each of those errors
		clog << "sin(n*Pi) with integer n does not always return exact 0"
		     << endl;
		++result;
	}
	
	// sin((n+1/2)*Pi) == {+|-}1?
	errorflag = false;
	for (int n=-10; n<=10; ++n) {
		if (!sin((n+numeric(1,2))*Pi).eval().info(info_flags::integer) ||
		    !(sin((n+numeric(1,2))*Pi).eval() == numeric(1) ||
		      sin((n+numeric(1,2))*Pi).eval() == numeric(-1)))
			errorflag = true;
	}
	if (errorflag) {
		clog << "sin((n+1/2)*Pi) with integer n does not always return exact {+|-}1"
		     << endl;
		++result;
	}
	
	// compare sin((q*Pi).evalf()) with sin(q*Pi).eval().evalf() at various
	// points.  E.g. if sin(Pi/10) returns something symbolic this should be
	// equal to sqrt(5)/4-1/4.  This routine will spot programming mistakes
	// of this kind:
	errorflag = false;
	ex argument;
	numeric epsilon(double(1e-8));
	for (int n=-340; n<=340; ++n) {
		argument = n*Pi/60;
		if (abs(sin(evalf(argument))-evalf(sin(argument)))>epsilon) {
			clog << "sin(" << argument << ") returns "
			     << sin(argument) << endl;
			errorflag = true;
		}
	}
	if (errorflag)
		++result;
	
	return result;
}

/* Simple tests on the cosine trigonometric function. */
static unsigned inifcns_check_cos(void)
{
	unsigned result = 0;
	bool errorflag;
	
	// cos((n+1/2)*Pi) == 0?
	errorflag = false;
	for (int n=-10; n<=10; ++n) {
		if (cos((n+numeric(1,2))*Pi).eval() != numeric(0) ||
		    !cos((n+numeric(1,2))*Pi).eval().info(info_flags::integer))
			errorflag = true;
	}
	if (errorflag) {
		clog << "cos((n+1/2)*Pi) with integer n does not always return exact 0"
		     << endl;
		++result;
	}
	
	// cos(n*Pi) == 0?
	errorflag = false;
	for (int n=-10; n<=10; ++n) {
		if (!cos(n*Pi).eval().info(info_flags::integer) ||
		    !(cos(n*Pi).eval() == numeric(1) ||
		      cos(n*Pi).eval() == numeric(-1)))
			errorflag = true;
	}
	if (errorflag) {
		clog << "cos(n*Pi) with integer n does not always return exact {+|-}1"
		     << endl;
		++result;
	}
	
	// compare cos((q*Pi).evalf()) with cos(q*Pi).eval().evalf() at various
	// points.  E.g. if cos(Pi/12) returns something symbolic this should be
	// equal to 1/4*(1+1/3*sqrt(3))*sqrt(6).  This routine will spot
	// programming mistakes of this kind:
	errorflag = false;
	ex argument;
	numeric epsilon(double(1e-8));
	for (int n=-340; n<=340; ++n) {
		argument = n*Pi/60;
		if (abs(cos(evalf(argument))-evalf(cos(argument)))>epsilon) {
			clog << "cos(" << argument << ") returns "
			     << cos(argument) << endl;
			errorflag = true;
		}
	}
	if (errorflag)
		++result;
	
	return result;
}

/* Simple tests on the tangent trigonometric function. */
static unsigned inifcns_check_tan(void)
{
	unsigned result = 0;
	bool errorflag;
	
	// compare tan((q*Pi).evalf()) with tan(q*Pi).eval().evalf() at various
	// points.  E.g. if tan(Pi/12) returns something symbolic this should be
	// equal to 2-sqrt(3).  This routine will spot programming mistakes of 
	// this kind:
	errorflag = false;
	ex argument;
	numeric epsilon(double(1e-8));
	for (int n=-340; n<=340; ++n) {
		if (!(n%30) && (n%60))  // skip poles
			++n;
		argument = n*Pi/60;
		if (abs(tan(evalf(argument))-evalf(tan(argument)))>epsilon) {
			clog << "tan(" << argument << ") returns "
			     << tan(argument) << endl;
			errorflag = true;
		}
	}
	if (errorflag)
		++result;
	
	return result;
}

/* Simple tests on the dilogarithm function. */
static unsigned inifcns_check_Li2(void)
{
	// NOTE: this can safely be removed once CLN supports dilogarithms and
	// checks them itself.
	unsigned result = 0;
	bool errorflag;
	
	// check the relation Li2(z^2) == 2 * (Li2(z) + Li2(-z)) numerically, which
	// should hold in the entire complex plane:
	errorflag = false;
	ex argument;
	numeric epsilon(double(1e-16));
	for (int n=0; n<200; ++n) {
		argument = numeric(20.0*rand()/(RAND_MAX+1.0)-10.0)
		         + numeric(20.0*rand()/(RAND_MAX+1.0)-10.0)*I;
		if (abs(Li2(pow(argument,2))-2*Li2(argument)-2*Li2(-argument)) > epsilon) {
			clog << "Li2(z) at z==" << argument
			     << " failed to satisfy Li2(z^2)==2*(Li2(z)+Li2(-z))" << endl;
			errorflag = true;
		}
	}
	
	if (errorflag)
		++result;
	
	return result;
}

unsigned check_inifcns(void)
{
	unsigned result = 0;

	cout << "checking consistency of symbolic functions" << flush;
	clog << "---------consistency of symbolic functions:" << endl;
	
	result += inifcns_check_sin();  cout << '.' << flush;
	result += inifcns_check_cos();  cout << '.' << flush;
	result += inifcns_check_tan();  cout << '.' << flush;
	result += inifcns_check_Li2();  cout << '.' << flush;
	
	if (!result) {
		cout << " passed " << endl;
		clog << "(no output)" << endl;
	} else {
		cout << " failed " << endl;
	}
	
	return result;
}