LAL  7.5.0.1-fe68b98
VectorOpsTest.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 VectorOps_h
23  *
24  * \brief Tests the routines in \ref VectorOps.h. Exercises some of the error
25  * conditions and makes sure that they work.
26  *
27  * ### Usage ###
28  *
29  * \code
30  * VectorOpsTest [options]
31  * Options:
32  * -h print help
33  * -q quiet: run silently
34  * -v verbose: print extra information
35  * -d level set lalDebugLevel to level
36  * \endcode
37  *
38  * ### Exit codes ###
39  *
40  * <table><tr><th>Code</th><th>Explanation</th></tr>
41  * <tr><td>0</td><td>Success, normal exit.</td></tr>
42  * <tr><td>1</td><td>Subroutine failed.</td></tr>
43  * </table>
44  *
45  */
46 
47 /** \cond DONT_DOXYGEN */
48 
49 #include <complex.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <math.h>
54 #include <config.h>
55 #include <lal/LALStdlib.h>
56 #include <lal/LALConstants.h>
57 #include <lal/AVFactories.h>
58 #include <lal/VectorOps.h>
59 #include <lal/LALgetopt.h>
60 #include <lal/LALString.h>
61 
62 #define CODES_(x) #x
63 #define CODES(x) CODES_(x)
64 
65 int verbose = 0;
66 
67 static void
68 Usage( const char *program, int exitflag );
69 
70 static void
71 ParseOptions( int argc, char *argv[] );
72 
73 static void
74 TestStatus( LALStatus *status, const char *expectedCodes, int exitCode );
75 
76 int
77 main ( int argc, char *argv[] )
78 {
79  const int size = 8;
80  COMPLEX8Vector *z1 = NULL;
81  COMPLEX8Vector *z2 = NULL;
82  COMPLEX8Vector *z3 = NULL;
83  REAL4Vector *x1 = NULL;
84  REAL4Vector *x2 = NULL;
85  REAL4Vector *x3 = NULL;
86  REAL4Vector *y_1 = NULL;
87  REAL4Vector *y2 = NULL;
88  REAL4Vector *y3 = NULL;
89  static LALStatus status;
90  INT4 i;
91 
92 
93  ParseOptions( argc, argv );
94 
95  LALCCreateVector(&status, &z1, size);
96  TestStatus( &status, CODES(0), 1 );
97  LALCCreateVector(&status, &z2, size);
98  TestStatus( &status, CODES(0), 1 );
99  LALCCreateVector(&status, &z3, size);
100  TestStatus( &status, CODES(0), 1 );
101  LALSCreateVector(&status, &x1, size);
102  TestStatus( &status, CODES(0), 1 );
103  LALSCreateVector(&status, &x2, size);
104  TestStatus( &status, CODES(0), 1 );
105  LALSCreateVector(&status, &x3, size);
106  TestStatus( &status, CODES(0), 1 );
107  LALSCreateVector(&status, &y_1, size/2);
108  TestStatus( &status, CODES(0), 1 );
109  y2 = (REAL4Vector *)LALMalloc(sizeof(REAL4Vector));
110  y2->data = NULL;
111  y2->length = size;
112  y3 = (REAL4Vector *)LALMalloc(sizeof(REAL4Vector));
113  y3->data = (REAL4 *)LALMalloc(size*sizeof(REAL4));
114  y3->length = 0;
115 
116  for (i = 0; i < size; ++i)
117  {
118  z1->data[i] = 1 + i;
119  z1->data[i] += I * (2 + i*i);
120  z2->data[i] = 3 + i + i*i*i;
121  z2->data[i] += I * (4 + i*i + i*i*i);
122  x1->data[i] = 5 + i + i*i;
123  x2->data[i] = 6 + i + i*i + i*i*i;
124  }
125 
126  if (verbose) printf("\n");
127  LALCCVectorMultiply(&status, z3, z1, z2);
128  TestStatus( &status, CODES(0), 1 );
129  for (i = 0; i < size; ++i)
130  if (verbose) printf("(% 6.0f,% 6.0f) x (% 6.0f,% 6.0f) = (% 6.0f,% 6.0f)\n",
131  crealf(z1->data[i]), cimagf(z1->data[i]),
132  crealf(z2->data[i]), cimagf(z2->data[i]),
133  crealf(z3->data[i]), cimagf(z3->data[i]));
134 
135  if (verbose) printf("\n");
136  LALCCVectorMultiplyConjugate(&status, z3, z1, z2);
137  TestStatus( &status, CODES(0), 1 );
138  for (i = 0; i < size; ++i)
139  if (verbose) printf("(% 6.0f,% 6.0f) x (% 6.0f,% 6.0f)* = (% 6.0f,% 6.0f)\n",
140  crealf(z1->data[i]), cimagf(z1->data[i]),
141  crealf(z2->data[i]), cimagf(z2->data[i]),
142  crealf(z3->data[i]), cimagf(z3->data[i]));
143 
144  if (verbose) printf("\n");
145  LALCCVectorDivide(&status, z3, z1, z2);
146  TestStatus( &status, CODES(0), 1 );
147  for (i = 0; i < size; ++i)
148  if (verbose) printf("(% 6.0f,% 6.0f) / (% 6.0f,% 6.0f) = (% 9.6f,% 9.6f)\n",
149  crealf(z1->data[i]), cimagf(z1->data[i]),
150  crealf(z2->data[i]), cimagf(z2->data[i]),
151  crealf(z3->data[i]), cimagf(z3->data[i]));
152 
153  if (verbose) printf("\n");
154  LALSCVectorMultiply(&status, z3, x1, z1);
155  TestStatus( &status, CODES(0), 1 );
156  for (i = 0; i < size; ++i)
157  if (verbose) printf("% 6.0f x (% 6.0f,% 6.0f) = (% 6.0f,% 6.0f)\n",
158  crealf(x1->data[i]),
159  crealf(z1->data[i]), cimagf(z1->data[i]),
160  crealf(z3->data[i]), cimagf(z3->data[i]));
161 
162  if (verbose) printf("\n");
163  LALSSVectorMultiply(&status, x3, x1, x2);
164  TestStatus( &status, CODES(0), 1 );
165  for (i = 0; i < size; ++i)
166  if (verbose) printf("% 6.0f x % 6.0f = % 6.0f\n",
167  x1->data[i], x2->data[i], x3->data[i]);
168 
169  if (verbose) printf("\n");
170  LALSSVectorMultiply(&status, x3, x1, NULL);
171  TestStatus( &status, CODES(VECTOROPSH_ENULL), 1 );
172  LALSSVectorMultiply(&status, x3, y2, x2);
173  TestStatus( &status, CODES(VECTOROPSH_ENULL), 1 );
174  LALSSVectorMultiply(&status, y3, x1, x2);
175  TestStatus( &status, CODES(VECTOROPSH_ESIZE), 1 );
176  LALSSVectorMultiply(&status, x3, x1, y_1);
177  TestStatus( &status, CODES(VECTOROPSH_ESZMM), 1 );
178 
179  LALCDestroyVector(&status, &z1);
180  TestStatus( &status, CODES(0), 1 );
181  LALCDestroyVector(&status, &z2);
182  TestStatus( &status, CODES(0), 1 );
183  LALCDestroyVector(&status, &z3);
184  TestStatus( &status, CODES(0), 1 );
185  LALSDestroyVector(&status, &x1);
186  TestStatus( &status, CODES(0), 1 );
187  LALSDestroyVector(&status, &x2);
188  TestStatus( &status, CODES(0), 1 );
189  LALSDestroyVector(&status, &x3);
190  TestStatus( &status, CODES(0), 1 );
191  LALSDestroyVector(&status, &y_1);
192  TestStatus( &status, CODES(0), 1 );
193  LALFree(y2);
194  LALFree(y3->data);
195  LALFree(y3);
196 
197  x1 = x2 = x3 = y_1 = y2 = y3 = NULL;
198  z1 = z2 = z3 = NULL;
199 
200 
201  LALCCreateVector(&status, &z1, size);
202  TestStatus( &status, CODES(0), 1 );
203 
204  LALSCreateVector(&status, &x1, size);
205  TestStatus( &status, CODES(0), 1 );
206  LALSCreateVector(&status, &x2, size);
207  TestStatus( &status, CODES(0), 1 );
208  LALSCreateVector(&status, &x3, size);
209  TestStatus( &status, CODES(0), 1 );
210 
211 
212  for (i = 0; i < size; ++i)
213  {
214  z1->data[i] = (12.0 + i) *cos(LAL_PI/3.0*i);
215  z1->data[i] += I * (12.0 + i )*sin(LAL_PI/3.0*i);
216  }
217 
218  if (verbose) printf("\n");
219  LALCVectorAbs(&status, x1, z1);
220  TestStatus( &status, CODES(0), 1 );
221  for (i = 0; i < size; ++i)
222  if (verbose) printf(" Abs(% f,%f) = %f \n",
223  crealf(z1->data[i]), cimagf(z1->data[i]),
224  x1->data[i]);
225 
226  LALCVectorAngle(&status, x2, z1);
227  TestStatus( &status, CODES(0), 1 );
228  for (i = 0; i < size; ++i)
229  if (verbose) printf(" Angle(%f,%f) = %f \n",
230  crealf(z1->data[i]), cimagf(z1->data[i]),
231  x2->data[i]);
232 
233  LALUnwrapREAL4Angle(&status, x3, x2);
234  TestStatus( &status, CODES(0), 1 );
235  for (i = 0; i < size; ++i)
236  if (verbose) printf(" Unwrap Phase Angle ( %f ) = %f \n",
237  x2->data[i],
238  x3->data[i]);
239 
240 
241  LALSCreateVector(&status, &y_1, size/2);
242  TestStatus( &status, CODES(0), 1 );
243 
244  y2 = (REAL4Vector *)LALMalloc(sizeof(REAL4Vector));
245  y2->data = NULL;
246  y2->length = size;
247 
248  y3 = (REAL4Vector *)LALMalloc(sizeof(REAL4Vector));
249  y3->data = (REAL4 *)LALMalloc(size*sizeof(REAL4));
250  y3->length = 0;
251 
252  if (verbose) printf("\n");
253 
254  LALCVectorAbs(&status, x1, NULL);
255  TestStatus( &status, CODES(VECTOROPSH_ENULL), 1 );
256  LALCVectorAbs(&status, NULL, z1);
257  TestStatus( &status, CODES(VECTOROPSH_ENULL), 1 );
258  LALCVectorAbs(&status, y_1, z1);
259  TestStatus( &status, CODES(VECTOROPSH_ESZMM), 1 );
260  LALCVectorAbs(&status, y2, z1);
261  TestStatus( &status, CODES(VECTOROPSH_ENULL), 1 );
262  LALCVectorAbs(&status, y3, z1);
263  TestStatus( &status, CODES(VECTOROPSH_ESIZE), 1 );
264 
265 
266  LALCVectorAngle(&status, x2, NULL);
267  TestStatus( &status, CODES(VECTOROPSH_ENULL), 1 );
268  LALCVectorAngle(&status, NULL, z1);
269  TestStatus( &status, CODES(VECTOROPSH_ENULL), 1 );
270  LALCVectorAngle(&status, y_1, z1);
271  TestStatus( &status, CODES(VECTOROPSH_ESZMM), 1 );
272  LALCVectorAngle(&status, y2, z1);
273  TestStatus( &status, CODES(VECTOROPSH_ENULL), 1 );
274  LALCVectorAngle(&status, y3, z1);
275  TestStatus( &status, CODES(VECTOROPSH_ESIZE), 1 );
276 
277  LALUnwrapREAL4Angle(&status, x3, NULL);
278  TestStatus( &status, CODES(VECTOROPSH_ENULL), 1 );
279  LALUnwrapREAL4Angle(&status, NULL, x2);
280  TestStatus( &status, CODES(VECTOROPSH_ENULL), 1 );
281  LALUnwrapREAL4Angle(&status, y_1, x2);
282  TestStatus( &status, CODES(VECTOROPSH_ESZMM), 1 );
283  LALUnwrapREAL4Angle(&status, y2, x2);
284  TestStatus( &status, CODES(VECTOROPSH_ENULL), 1 );
285  LALUnwrapREAL4Angle(&status, y3, x2);
286  TestStatus( &status, CODES(VECTOROPSH_ESIZE), 1 );
287  LALUnwrapREAL4Angle(&status, x2, x2);
288  TestStatus( &status, CODES(VECTOROPSH_ESAME), 1 );
289 
290 
291  LALCDestroyVector(&status, &z1);
292  TestStatus( &status, CODES(0), 1 );
293 
294  LALSDestroyVector(&status, &x1);
295  TestStatus( &status, CODES(0), 1 );
296  LALSDestroyVector(&status, &x2);
297  TestStatus( &status, CODES(0), 1 );
298  LALSDestroyVector(&status, &x3);
299  TestStatus( &status, CODES(0), 1 );
300 
301  LALSDestroyVector(&status, &y_1);
302  TestStatus( &status, CODES(0), 1 );
303  LALFree(y2);
304  LALFree(y3->data);
305  LALFree(y3);
306 
308  return 0;
309 }
310 
311 
312 /*
313  * TestStatus()
314  *
315  * Routine to check that the status code status->statusCode agrees with one of
316  * the codes specified in the space-delimited string ignored; if not,
317  * exit to the system with code exitcode.
318  *
319  */
320 static void
321 TestStatus( LALStatus *status, const char *ignored, int exitcode )
322 {
323  char str[64];
324  char *tok;
325 
326  if ( verbose )
327  {
328  REPORTSTATUS( status );
329  }
330 
331  if (XLALStringCopy(str, ignored, sizeof(str)))
332  {
333  if ( ( tok = strtok( str, " " ) ) )
334  {
335  do
336  {
337  if ( status->statusCode == atoi( tok ) )
338  {
339  return;
340  }
341  }
342  while ( ( tok = strtok( NULL, " " ) ) );
343  }
344  else
345  {
346  if ( status->statusCode == atoi( str ) )
347  {
348  return;
349  }
350  }
351  }
352 
353  fprintf( stderr, "\nExiting to system with code %d\n", exitcode );
354  exit( exitcode );
355 }
356 
357 /*
358  * Usage()
359  *
360  * Prints a usage message for program program and exits with code exitcode.
361  *
362  */
363 static void
364 Usage( const char *program, int exitcode )
365 {
366  fprintf( stderr, "Usage: %s [options]\n", program );
367  fprintf( stderr, "Options:\n" );
368  fprintf( stderr, " -h print this message\n" );
369  fprintf( stderr, " -q quiet: run silently\n" );
370  fprintf( stderr, " -v verbose: print extra information\n" );
371  fprintf( stderr, " -d level set lalDebugLevel to level\n" );
372  exit( exitcode );
373 }
374 
375 
376 /*
377  * ParseOptions()
378  *
379  * Parses the argc - 1 option strings in argv[].
380  *
381  */
382 static void
383 ParseOptions( int argc, char *argv[] )
384 {
385  FILE *fp;
386  while ( 1 )
387  {
388  int c = -1;
389 
390  c = LALgetopt( argc, argv, "hqvd:" );
391  if ( c == -1 )
392  {
393  break;
394  }
395 
396  switch ( c )
397  {
398  case 'd': /* set debug level */
399  break;
400 
401  case 'v': /* verbose */
402  ++verbose;
403  break;
404 
405  case 'q': /* quiet: run silently (ignore error messages) */
406  fp = freopen( "/dev/null", "w", stderr );
407  if (fp == NULL)
408  {
409  fprintf(stderr, "Error: Unable to open /dev/null\n");
410  exit(1);
411  }
412  fp = freopen( "/dev/null", "w", stdout );
413  if (fp == NULL)
414  {
415  fprintf(stderr, "Error: Unable to open /dev/null\n");
416  exit(1);
417  }
418  break;
419 
420  case 'h':
421  Usage( argv[0], 0 );
422  break;
423 
424  default:
425  Usage( argv[0], 1 );
426  }
427 
428  }
429 
430  if ( LALoptind < argc )
431  {
432  Usage( argv[0], 1 );
433  }
434 
435  return;
436 }
437 /** \endcond */
const char * program
void REPORTSTATUS(LALStatus *status)
Definition: LALError.c:322
void LALCheckMemoryLeaks(void)
Definition: LALMalloc.c:784
#define LALMalloc(n)
Definition: LALMalloc.h:93
#define LALFree(p)
Definition: LALMalloc.h:96
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 LAL_PI
Archimedes's constant, pi.
Definition: LALConstants.h:179
int32_t INT4
Four-byte signed integer.
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 LALCCreateVector(LALStatus *, COMPLEX8Vector **, UINT4)
void LALCDestroyVector(LALStatus *, COMPLEX8Vector **)
void LALSDestroyVector(LALStatus *, REAL4Vector **)
void LALSCreateVector(LALStatus *, REAL4Vector **, UINT4)
void LALCCVectorDivide(LALStatus *status, COMPLEX8Vector *out, const COMPLEX8Vector *in1, const COMPLEX8Vector *in2)
UNDOCUMENTED.
void LALSSVectorMultiply(LALStatus *status, REAL4Vector *out, const REAL4Vector *in1, const REAL4Vector *in2)
UNDOCUMENTED.
void LALSCVectorMultiply(LALStatus *status, COMPLEX8Vector *out, const REAL4Vector *in1, const COMPLEX8Vector *in2)
UNDOCUMENTED.
void LALCCVectorMultiplyConjugate(LALStatus *status, COMPLEX8Vector *out, const COMPLEX8Vector *in1, const COMPLEX8Vector *in2)
UNDOCUMENTED.
void LALCCVectorMultiply(LALStatus *status, COMPLEX8Vector *out, const COMPLEX8Vector *in1, const COMPLEX8Vector *in2)
UNDOCUMENTED.
#define VECTOROPSH_ESIZE
Invalid input size.
Definition: VectorOps.h:52
#define VECTOROPSH_ENULL
Null pointer.
Definition: VectorOps.h:51
#define VECTOROPSH_ESZMM
Size mismatch.
Definition: VectorOps.h:53
#define VECTOROPSH_ESAME
Input/Output data vectors are the same.
Definition: VectorOps.h:54
void LALCVectorAngle(LALStatus *, REAL4Vector *, const COMPLEX8Vector *)
UNDOCUMENTED.
Definition: VectorPolar.c:274
void LALCVectorAbs(LALStatus *, REAL4Vector *, const COMPLEX8Vector *)
UNDOCUMENTED.
Definition: VectorPolar.c:212
void LALUnwrapREAL4Angle(LALStatus *, REAL4Vector *, const REAL4Vector *)
UNDOCUMENTED.
Definition: VectorPolar.c:335
Vector of type COMPLEX8, see DATATYPE-Vector types for more details.
Definition: LALDatatypes.h:163
COMPLEX8 * data
Pointer to the data array.
Definition: LALDatatypes.h:168
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
REAL4 * data
Pointer to the data array.
Definition: LALDatatypes.h:150
UINT4 length
Number of elements in array.
Definition: LALDatatypes.h:149
int verbose
Definition: tconvert.c:103
FILE * fp
Definition: tconvert.c:105