Loading [MathJax]/extensions/TeX/AMSsymbols.js
LAL 7.7.0.1-5e288d3
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
InterpolateTest.c
Go to the documentation of this file.
1/*
2* Copyright (C) 2007 Jolien Creighton
3*
4* This program is free software; you can redistribute it and/or modify
5* it under the terms of the GNU General Public License as published by
6* the Free Software Foundation; either version 2 of the License, or
7* (at your option) any later version.
8*
9* This program is distributed in the hope that it will be useful,
10* but WITHOUT ANY WARRANTY; without even the implied warranty of
11* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12* GNU General Public License for more details.
13*
14* You should have received a copy of the GNU General Public License
15* along with with program; see the file COPYING. If not, write to the
16* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17* MA 02110-1301 USA
18*/
19
20/**
21 * \file
22 * \ingroup Interpolate_h
23 *
24 * \brief Tests the routines in \ref Interpolate.h.
25 *
26 * ### Usage ###
27 *
28 * \code
29 * InterpolateTest [options]
30 * Options:
31 * -h print this message
32 * -q quiet: run silently
33 * -v verbose: print extra information
34 * -d level set lalDebugLevel to level
35 * \endcode
36 *
37 * ### Exit codes ###
38 *
39 * <table><tr><th>Code</th><th>Explanation</th></tr>
40 * <tr><td>0</td><td>Success, normal exit.</td></tr>
41 * <tr><td>1</td><td>Subroutine failed.</td></tr>
42 * </table>
43 *
44 */
45
46/** \cond DONT_DOXYGEN */
47#include <config.h>
48
49#include <stdio.h>
50#include <string.h>
51#include <stdlib.h>
52#include <math.h>
53
54#include <lal/LALStdlib.h>
55#include <lal/LALgetopt.h>
56#include <lal/AVFactories.h>
57#include <lal/Interpolate.h>
58#include <lal/LALString.h>
59
60#define CODES_(x) #x
61#define CODES(x) CODES_(x)
62
63int verbose = 0;
64
65static void Usage (const char *program, int exitflag);
66
67static void ParseOptions (int argc, char *argv[]);
68
69static void TestStatus (LALStatus *status, const char *expectCodes, int exitCode);
70
71int main (int argc, char *argv[])
72{
73 enum {ArraySize = 10};
74
75 static LALStatus status;
76 static REAL4Vector *x;
77 static REAL4Vector *y;
78 static REAL8Vector *xx;
79 static REAL8Vector *yy;
80
81 SInterpolateOut sintout;
82 SInterpolatePar sintpar;
83 DInterpolateOut dintout;
84 DInterpolatePar dintpar;
85
86 int i;
87
88 ParseOptions (argc, argv);
89
90 LALSCreateVector (&status, &x, ArraySize);
91 TestStatus (&status, CODES(0), 1);
92 LALSCreateVector (&status, &y, ArraySize);
93 TestStatus (&status, CODES(0), 1);
94 LALDCreateVector (&status, &xx, ArraySize);
95 TestStatus (&status, CODES(0), 1);
96 LALDCreateVector (&status, &yy, ArraySize);
97 TestStatus (&status, CODES(0), 1);
98
99 if ( verbose )
100 printf ("Initial data:\n");
101 if ( verbose )
102 printf ("y = P(x) = 7 - 8x + 2x^2 + 2x^3 - x^4\n");
103 if ( verbose )
104 printf ("-----------------\n");
105 if ( verbose )
106 printf ("x\ty\n");
107 if ( verbose )
108 printf ("=================\n");
109 for (i = 0; i < ArraySize; ++i)
110 {
111 REAL4 xi = x->data[i] = xx->data[i] = i*i;
112 y->data[i] = yy->data[i] = 7 + xi*(-8 + xi*(2 + xi*(2 - xi)));
113 if ( verbose )
114 printf ("%.0f\t%.0f\n", x->data[i], y->data[i]);
115 }
116 if ( verbose )
117 printf ("-----------------\n");
118
119 if ( verbose )
120 printf ("\nInterpolate to x = 0.3:\n");
121 if ( verbose )
122 printf ("---------------------------------\n");
123 if ( verbose )
124 printf ("order\ty\t\tdy\n");
125 if ( verbose )
126 printf ("=================================\n");
127 sintpar.x = x->data;
128 sintpar.y = y->data;
129 dintpar.x = xx->data;
130 dintpar.y = yy->data;
131 for (i = 2; i < ArraySize; ++i)
132 {
133 sintpar.n = i;
134 dintpar.n = i;
135 LALSPolynomialInterpolation (&status, &sintout, 0.3, &sintpar);
136 TestStatus (&status, CODES(0), 1);
137 LALDPolynomialInterpolation (&status, &dintout, 0.3, &dintpar);
138 TestStatus (&status, CODES(0), 1);
139 if ( verbose )
140 printf ("%d\t%f\t%f\t%f\t%f\n", i - 1,
141 sintout.y, sintout.dy, dintout.y, dintout.dy);
142 }
143 if ( verbose )
144 printf ("---------------------------------\n");
145
146 if ( verbose )
147 printf ("\nExtrapolate to x = -0.3:\n");
148 if ( verbose )
149 printf ("---------------------------------\n");
150 if ( verbose )
151 printf ("order\ty\t\tdy\n");
152 if ( verbose )
153 printf ("=================================\n");
154 sintpar.x = x->data;
155 sintpar.y = y->data;
156 dintpar.x = xx->data;
157 dintpar.y = yy->data;
158 for (i = 2; i < ArraySize; ++i)
159 {
160 sintpar.n = i;
161 dintpar.n = i;
162 LALSPolynomialInterpolation (&status, &sintout, -0.3, &sintpar);
163 TestStatus (&status, CODES(0), 1);
164 LALDPolynomialInterpolation (&status, &dintout, -0.3, &dintpar);
165 TestStatus (&status, CODES(0), 1);
166 if ( verbose )
167 printf ("%d\t%f\t%f\t%f\t%f\n", i - 1,
168 sintout.y, sintout.dy, dintout.y, dintout.dy);
169 }
170 if ( verbose )
171 printf ("---------------------------------\n");
172
173
175 TestStatus (&status, CODES(0), 1);
177 TestStatus (&status, CODES(0), 1);
179 TestStatus (&status, CODES(0), 1);
181 TestStatus (&status, CODES(0), 1);
182
184
185 LALSCreateVector (&status, &x, ArraySize);
186 TestStatus (&status, CODES(0), 1);
187 LALSCreateVector (&status, &y, ArraySize);
188 TestStatus (&status, CODES(0), 1);
189 LALDCreateVector (&status, &xx, ArraySize);
190 TestStatus (&status, CODES(0), 1);
191 LALDCreateVector (&status, &yy, ArraySize);
192 TestStatus (&status, CODES(0), 1);
193
194
195 if ( verbose )
196 printf ("\nCheck error conditions:\n");
197
198 if ( verbose )
199 printf ("\nNull pointer:\r");
200 LALSPolynomialInterpolation (&status, NULL, -0.3, &sintpar);
201 TestStatus (&status, CODES(INTERPOLATEH_ENULL), 1);
202 LALDPolynomialInterpolation (&status, NULL, -0.3, &dintpar);
203 TestStatus (&status, CODES(INTERPOLATEH_ENULL), 1);
204 if ( verbose )
205 printf ("Null pointer check passed.\n");
206
207 if ( verbose )
208 printf ("\nNull pointer:\r");
209 LALSPolynomialInterpolation (&status, &sintout, -0.3, NULL);
210 TestStatus (&status, CODES(INTERPOLATEH_ENULL), 1);
211 LALDPolynomialInterpolation (&status, &dintout, -0.3, NULL);
212 TestStatus (&status, CODES(INTERPOLATEH_ENULL), 1);
213 if ( verbose )
214 printf ("Null pointer check passed.\n");
215
216 sintpar.x = NULL;
217 dintpar.x = NULL;
218 if ( verbose )
219 printf ("\nNull pointer:\r");
220 LALSPolynomialInterpolation (&status, &sintout, -0.3, &sintpar);
221 TestStatus (&status, CODES(INTERPOLATEH_ENULL), 1);
222 LALDPolynomialInterpolation (&status, &dintout, -0.3, &dintpar);
223 TestStatus (&status, CODES(INTERPOLATEH_ENULL), 1);
224 if ( verbose )
225 printf ("Null pointer check passed.\n");
226
227 sintpar.x = x->data;
228 sintpar.y = NULL;
229 dintpar.x = xx->data;
230 dintpar.y = NULL;
231 if ( verbose )
232 printf ("\nNull pointer:\r");
233 LALSPolynomialInterpolation (&status, &sintout, -0.3, &sintpar);
234 TestStatus (&status, CODES(INTERPOLATEH_ENULL), 1);
235 LALDPolynomialInterpolation (&status, &dintout, -0.3, &dintpar);
236 TestStatus (&status, CODES(INTERPOLATEH_ENULL), 1);
237 if ( verbose )
238 printf ("Null pointer check passed.\n");
239
240 sintpar.y = y->data;
241 sintpar.n = 1;
242 dintpar.y = yy->data;
243 dintpar.n = 1;
244 if ( verbose )
245 printf ("\nInvalid size:\r");
246 LALSPolynomialInterpolation (&status, &sintout, -0.3, &sintpar);
247 TestStatus (&status, CODES(INTERPOLATEH_ESIZE), 1);
248 dintout.dy = XLALREAL8PolynomialInterpolation (&(dintout.y), -0.3, dintpar.y, dintpar.x, dintpar.n);
249 if (xlalErrno == XLAL_ESIZE)
250 xlalErrno = 0;
251 else
252 XLAL_ERROR_MAIN(xlalErrno, "xlalErrno=%i (%s) at line %i", xlalErrno, XLALErrorString(xlalErrno), __LINE__);
253 if ( verbose )
254 printf ("Invalid size check passed.\n");
255
256 x->data[1] = x->data[0] = 2;
257 xx->data[1] = xx->data[0] = 2;
258 sintpar.n = 3;
259 dintpar.n = 3;
260 if ( verbose )
261 printf ("\nZero divide:\r");
262 LALSPolynomialInterpolation (&status, &sintout, -0.3, &sintpar);
263 TestStatus (&status, CODES(INTERPOLATEH_EZERO), 1);
264 dintout.dy = XLALREAL8PolynomialInterpolation (&(dintout.y), -0.3, dintpar.y, dintpar.x, dintpar.n);
265 if (xlalErrno == XLAL_EFPDIV0)
266 xlalErrno = 0;
267 else
268 XLAL_ERROR_MAIN(xlalErrno, "xlalErrno=%i (%s) at line %i", xlalErrno, XLALErrorString(xlalErrno), __LINE__);
269 if ( verbose )
270 printf ("Zero divide check passed.\n");
271
272
274 TestStatus (&status, CODES(0), 1);
276 TestStatus (&status, CODES(0), 1);
278 TestStatus (&status, CODES(0), 1);
280 TestStatus (&status, CODES(0), 1);
281
282 return 0;
283}
284
285
286/*
287 * TestStatus ()
288 *
289 * Routine to check that the status code status->statusCode agrees with one of
290 * the codes specified in the space-delimited string ignored; if not,
291 * exit to the system with code exitcode.
292 *
293 */
294static void
295TestStatus (LALStatus *status, const char *ignored, int exitcode)
296{
297 char str[64];
298 char *tok;
299
300 if (verbose)
301 {
303 }
304
305 if (XLALStringCopy(str, ignored, sizeof(str)))
306 {
307 if ((tok = strtok (str, " ")))
308 {
309 do
310 {
311 if (status->statusCode == atoi (tok))
312 {
313 return;
314 }
315 }
316 while ((tok = strtok (NULL, " ")));
317 }
318 else
319 {
320 if (status->statusCode == atoi (str))
321 {
322 return;
323 }
324 }
325 }
326
327 fprintf (stderr, "\nExiting to system with code %d\n", exitcode);
328 exit (exitcode);
329}
330
331/*
332 * Usage ()
333 *
334 * Prints a usage message for program program and exits with code exitcode.
335 *
336 */
337 static void
338Usage (const char *program, int exitcode)
339{
340 fprintf (stderr, "Usage: %s [options]\n", program);
341 fprintf (stderr, "Options:\n");
342 fprintf (stderr, " -h print this message\n");
343 fprintf (stderr, " -q quiet: run silently\n");
344 fprintf (stderr, " -v verbose: print extra information\n");
345 fprintf (stderr, " -d level set lalDebugLevel to level\n");
346 exit (exitcode);
347}
348
349
350/*
351 * ParseOptions ()
352 *
353 * Parses the argc - 1 option strings in argv[].
354 *
355 */
356static void
357ParseOptions (int argc, char *argv[])
358{
359 FILE *fp;
360
361 while (1)
362 {
363 int c = -1;
364
365 c = LALgetopt (argc, argv, "hqvd:");
366 if (c == -1)
367 {
368 break;
369 }
370
371 switch (c)
372 {
373 case 'd': /* set debug level */
374 break;
375
376 case 'v': /* verbose */
377 ++verbose;
378 break;
379
380 case 'q': /* quiet: run silently */
381 fp = freopen ("/dev/null", "w", stderr);
382 if (fp == NULL)
383 {
384 fprintf(stderr, "Error: Unable to open /dev/null\n");
385 exit(1);
386 }
387 fp = freopen ("/dev/null", "w", stdout);
388 if (fp == NULL)
389 {
390 fprintf(stderr, "Error: Unable to open /dev/null\n");
391 exit(1);
392 }
393 break;
394
395 case 'h':
396 Usage (argv[0], 0);
397 break;
398
399 default:
400 Usage (argv[0], 1);
401 }
402
403 }
404
405 if (LALoptind < argc)
406 {
407 Usage (argv[0], 1);
408 }
409
410 return;
411}
412/** \endcond */
const char * program
void REPORTSTATUS(LALStatus *status)
Definition: LALError.c:322
void LALCheckMemoryLeaks(void)
Definition: LALMalloc.c:784
int LALgetopt(int argc, char *const *argv, const char *optstring)
Definition: LALgetopt.c:172
int LALoptind
Definition: LALgetopt.c:79
#define fprintf
static void Usage(const char *program, int exitcode)
Definition: XLALChisqTest.c:70
static void ParseOptions(int argc, char *argv[])
Definition: XLALChisqTest.c:88
int main(int argc, char *argv[])
Definition: cache.c:25
#define INTERPOLATEH_ENULL
Null pointer.
Definition: Interpolate.h:91
#define INTERPOLATEH_ESIZE
Invalid size.
Definition: Interpolate.h:92
void LALDPolynomialInterpolation(LALStatus *status, DInterpolateOut *output, REAL8 target, DInterpolatePar *params)
Definition: Interpolate.c:115
void LALSPolynomialInterpolation(LALStatus *status, SInterpolateOut *output, REAL4 target, SInterpolatePar *params)
Definition: Interpolate.c:29
#define INTERPOLATEH_EZERO
Zero divide.
Definition: Interpolate.h:93
REAL8 XLALREAL8PolynomialInterpolation(REAL8 *yout, REAL8 xtarget, REAL8 *y, REAL8 *x, UINT4 n)
Definition: Interpolate.c:140
float REAL4
Single precision real floating-point number (4 bytes).
size_t XLALStringCopy(char *dst, const char *src, size_t size)
Copy sources string src to destination string dst.
Definition: LALString.c:104
void LALDCreateVector(LALStatus *, REAL8Vector **, UINT4)
void LALDDestroyVector(LALStatus *, REAL8Vector **)
void LALSDestroyVector(LALStatus *, REAL4Vector **)
void LALSCreateVector(LALStatus *, REAL4Vector **, UINT4)
const char * XLALErrorString(int code)
Returns the error message associated with an error number.
Definition: XLALError.c:409
#define xlalErrno
Modifiable lvalue containing the XLAL error number.
Definition: XLALError.h:571
#define XLAL_ERROR_MAIN(...)
Macro to invoke a failure from a C main() routine.
Definition: XLALError.h:765
@ XLAL_ESIZE
Wrong size.
Definition: XLALError.h:420
@ XLAL_EFPDIV0
IEEE Division by zero floating point error.
Definition: XLALError.h:449
These structures contain the output of the interpolation.
Definition: Interpolate.h:114
REAL8 y
The interpolated value.
Definition: Interpolate.h:115
REAL8 dy
The estimated error in the interpolated value.
Definition: Interpolate.h:116
These structures contain the interpolation parameters; These are the arrays of n domain values and t...
Definition: Interpolate.h:141
REAL8 * y
The array of values to interpolate.
Definition: Interpolate.h:144
REAL8 * x
The array of domain values.
Definition: Interpolate.h:143
UINT4 n
The number of points in the arrays to use in the interpolation.
Definition: Interpolate.h:142
LAL status structure, see The LALStatus structure for more details.
Definition: LALDatatypes.h:947
Vector of type REAL4, see DATATYPE-Vector types for more details.
Definition: LALDatatypes.h:145
Vector of type REAL8, see DATATYPE-Vector types for more details.
Definition: LALDatatypes.h:154
REAL8 * data
Pointer to the data array.
Definition: LALDatatypes.h:159
These structures contain the output of the interpolation.
Definition: Interpolate.h:105
REAL4 dy
The estimated error in the interpolated value.
Definition: Interpolate.h:107
REAL4 y
The interpolated value.
Definition: Interpolate.h:106
These structures contain the interpolation parameters; These are the arrays of n domain values and t...
Definition: Interpolate.h:127
REAL4 * x
The array of domain values.
Definition: Interpolate.h:129
UINT4 n
The number of points in the arrays to use in the interpolation.
Definition: Interpolate.h:128
REAL4 * y
The array of values to interpolate.
Definition: Interpolate.h:130
int verbose
Definition: tconvert.c:103
FILE * fp
Definition: tconvert.c:105