1 /* matrix/test_complex_source.c
3 * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 Gerard Jungman, Brian Gough
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or (at
8 * your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 void FUNCTION (test, func) (void);
21 void FUNCTION (test, trap) (void);
22 void FUNCTION (test, text) (void);
23 void FUNCTION (test, binary) (void);
24 void FUNCTION (test, arith) (void);
26 #define TEST(expr,desc) gsl_test((expr), NAME(gsl_matrix) desc " M=%d, N=%d", M, N)
29 FUNCTION (test, func) (void)
35 TYPE (gsl_matrix) * m = FUNCTION (gsl_matrix, alloc) (M, N);
37 gsl_test (m->data == 0, NAME (gsl_matrix) "_alloc returns valid pointer");
38 gsl_test (m->size1 != M, NAME (gsl_matrix) "_alloc returns valid size1");
39 gsl_test (m->size2 != N, NAME (gsl_matrix) "_alloc returns valid size2");
40 gsl_test (m->tda != N, NAME (gsl_matrix) "_alloc returns valid tda");
42 for (i = 0; i < M; i++)
44 for (j = 0; j < N; j++)
48 GSL_REAL (z) = (ATOMIC) k;
49 GSL_IMAG (z) = (ATOMIC) (k + 1000);
50 FUNCTION (gsl_matrix, set) (m, i, j, z);
56 for (i = 0; i < M; i++)
58 for (j = 0; j < N; j++)
61 if (m->data[2 * (i * N + j)] != k ||
62 m->data[2 * (i * N + j) + 1] != k + 1000)
67 gsl_test (status, NAME (gsl_matrix) "_set writes into array");
71 for (i = 0; i < M; i++)
73 for (j = 0; j < N; j++)
75 BASE z = FUNCTION (gsl_matrix, get) (m, i, j);
77 if (GSL_REAL (z) != k || GSL_IMAG (z) != k + 1000)
81 gsl_test (status, NAME (gsl_matrix) "_get reads from array");
83 FUNCTION (gsl_matrix, free) (m); /* free whatever is in m */
85 m = FUNCTION (gsl_matrix, calloc) (M, N);
88 int status = (FUNCTION(gsl_matrix,isnull)(m) != 1);
89 TEST (status, "_isnull" DESC " on calloc matrix");
91 status = (FUNCTION(gsl_matrix,ispos)(m) != 0);
92 TEST (status, "_ispos" DESC " on calloc matrix");
94 status = (FUNCTION(gsl_matrix,isneg)(m) != 0);
95 TEST (status, "_isneg" DESC " on calloc matrix");
98 for (i = 0; i < M; i++)
100 for (j = 0; j < N; j++)
103 FUNCTION (gsl_matrix, set) (m, i, j, z);
108 status = (FUNCTION(gsl_matrix,isnull)(m) != 1);
109 TEST (status, "_isnull" DESC " on null matrix") ;
111 status = (FUNCTION(gsl_matrix,ispos)(m) != 0);
112 TEST (status, "_ispos" DESC " on null matrix") ;
114 status = (FUNCTION(gsl_matrix,isneg)(m) != 0);
115 TEST (status, "_isneg" DESC " on null matrix") ;
120 for (i = 0; i < M; i++)
122 for (j = 0; j < N; j++)
126 GSL_REAL (z) = (ATOMIC) (k % 10);
127 GSL_IMAG (z) = (ATOMIC) ((k + 5) % 10);
128 FUNCTION (gsl_matrix, set) (m, i, j, z);
133 status = (FUNCTION(gsl_matrix,isnull)(m) != 0);
134 TEST (status, "_isnull" DESC " on non-negative matrix") ;
136 status = (FUNCTION(gsl_matrix,ispos)(m) != 0);
137 TEST (status, "_ispos" DESC " on non-negative matrix") ;
139 status = (FUNCTION(gsl_matrix,isneg)(m) != 0);
140 TEST (status, "_isneg" DESC " on non-negative matrix") ;
144 for (i = 0; i < M; i++)
146 for (j = 0; j < N; j++)
150 GSL_REAL (z) = (ATOMIC) ((k % 10) - 5);
151 GSL_IMAG (z) = (ATOMIC) (((k + 5) % 10) - 5);
152 FUNCTION (gsl_matrix, set) (m, i, j, z);
157 status = (FUNCTION(gsl_matrix,isnull)(m) != 0);
158 TEST (status, "_isnull" DESC " on mixed matrix") ;
160 status = (FUNCTION(gsl_matrix,ispos)(m) != 0);
161 TEST (status, "_ispos" DESC " on mixed matrix") ;
163 status = (FUNCTION(gsl_matrix,isneg)(m) != 0);
164 TEST (status, "_isneg" DESC " on mixed matrix") ;
168 for (i = 0; i < M; i++)
170 for (j = 0; j < N; j++)
174 GSL_REAL (z) = -(ATOMIC) (k % 10);
175 GSL_IMAG (z) = -(ATOMIC) ((k + 5) % 10);
176 FUNCTION (gsl_matrix, set) (m, i, j, z);
181 status = (FUNCTION(gsl_matrix,isnull)(m) != 0);
182 TEST (status, "_isnull" DESC " on non-positive matrix") ;
184 status = (FUNCTION(gsl_matrix,ispos)(m) != 0);
185 TEST (status, "_ispos" DESC " on non-positive matrix") ;
187 status = (FUNCTION(gsl_matrix,isneg)(m) != 0);
188 TEST (status, "_isneg" DESC " on non-positive matrix") ;
192 for (i = 0; i < M; i++)
194 for (j = 0; j < N; j++)
198 GSL_REAL (z) = (ATOMIC) (k % 10 + 1);
199 GSL_IMAG (z) = (ATOMIC) ((k + 5) % 10 + 1);
200 FUNCTION (gsl_matrix, set) (m, i, j, z);
205 status = (FUNCTION(gsl_matrix,isnull)(m) != 0);
206 TEST (status, "_isnull" DESC " on positive matrix") ;
208 status = (FUNCTION(gsl_matrix,ispos)(m) != 1);
209 TEST (status, "_ispos" DESC " on positive matrix") ;
211 status = (FUNCTION(gsl_matrix,isneg)(m) != 0);
212 TEST (status, "_isneg" DESC " on positive matrix") ;
216 for (i = 0; i < M; i++)
218 for (j = 0; j < N; j++)
222 GSL_REAL (z) = -(ATOMIC) (k % 10 + 1);
223 GSL_IMAG (z) = -(ATOMIC) ((k + 5) % 10 + 1);
224 FUNCTION (gsl_matrix, set) (m, i, j, z);
229 status = (FUNCTION(gsl_matrix,isnull)(m) != 0);
230 TEST (status, "_isnull" DESC " on negative matrix") ;
232 status = (FUNCTION(gsl_matrix,ispos)(m) != 0);
233 TEST (status, "_ispos" DESC " on negative matrix") ;
235 status = (FUNCTION(gsl_matrix,isneg)(m) != 1);
236 TEST (status, "_isneg" DESC " on negative matrix") ;
239 FUNCTION (gsl_matrix, free) (m); /* free whatever is in m */
242 #if !(USES_LONGDOUBLE && !HAVE_PRINTF_LONGDOUBLE)
244 FUNCTION (test, text) (void)
246 TYPE (gsl_matrix) * m = FUNCTION (gsl_matrix, alloc) (M, N);
252 FILE *f = fopen ("test.txt", "w");
254 for (i = 0; i < M; i++)
256 for (j = 0; j < N; j++)
260 GSL_REAL (z) = (ATOMIC) k;
261 GSL_IMAG (z) = (ATOMIC) (k + 1000);
262 FUNCTION (gsl_matrix, set) (m, i, j, z);
266 FUNCTION (gsl_matrix, fprintf) (f, m, OUT_FORMAT);
272 FILE *f = fopen ("test.txt", "r");
273 TYPE (gsl_matrix) * mm = FUNCTION (gsl_matrix, alloc) (M, N);
276 FUNCTION (gsl_matrix, fscanf) (f, mm);
278 for (i = 0; i < M; i++)
280 for (j = 0; j < N; j++)
283 if (mm->data[2 * (i * N + j)] != k
284 || mm->data[2 * (i * N + j) + 1] != k + 1000)
289 gsl_test (status, NAME (gsl_matrix) "_fprintf and fscanf");
292 FUNCTION (gsl_matrix, free) (mm);
295 FUNCTION (gsl_matrix, free) (m);
300 FUNCTION (test, binary) (void)
302 TYPE (gsl_matrix) * m = FUNCTION (gsl_matrix, alloc) (M, N);
308 FILE *f = fopen ("test.dat", "wb");
310 for (i = 0; i < M; i++)
312 for (j = 0; j < N; j++)
316 GSL_REAL (z) = (ATOMIC) k;
317 GSL_IMAG (z) = (ATOMIC) (k + 1000);
318 FUNCTION (gsl_matrix, set) (m, i, j, z);
322 FUNCTION (gsl_matrix, fwrite) (f, m);
328 FILE *f = fopen ("test.dat", "rb");
329 TYPE (gsl_matrix) * mm = FUNCTION (gsl_matrix, alloc) (M, N);
332 FUNCTION (gsl_matrix, fread) (f, mm);
334 for (i = 0; i < M; i++)
336 for (j = 0; j < N; j++)
339 if (mm->data[2 * (i * N + j)] != k
340 || mm->data[2 * (i * N + j) + 1] != k + 1000)
345 gsl_test (status, NAME (gsl_matrix) "_write and read");
348 FUNCTION (gsl_matrix, free) (mm);
351 FUNCTION (gsl_matrix, free) (m);
355 FUNCTION (test, trap) (void)
357 TYPE (gsl_matrix) * mc = FUNCTION (gsl_matrix, alloc) (M, N);
360 BASE z = { {(ATOMIC) 1.2, (ATOMIC) 3.4} };
364 FUNCTION (gsl_matrix, set) (mc, i - 1, j, z);
366 NAME (gsl_matrix) "_set traps 1st index below lower bound");
369 FUNCTION (gsl_matrix, set) (mc, i, j - 1, z);
371 NAME (gsl_matrix) "_set traps 2nd index below lower bound");
374 FUNCTION (gsl_matrix, set) (mc, M + 1, 0, z);
376 NAME (gsl_matrix) "_set traps 1st index above upper bound");
379 FUNCTION (gsl_matrix, set) (mc, 0, N + 1, z);
381 NAME (gsl_matrix) "_set traps 2nd index above upper bound");
384 FUNCTION (gsl_matrix, set) (mc, M, 0, z);
385 gsl_test (!status, NAME (gsl_matrix) "_set traps 1st index at upper bound");
388 FUNCTION (gsl_matrix, set) (mc, 0, N, z);
389 gsl_test (!status, NAME (gsl_matrix) "_set traps 2nd index at upper bound");
392 z1 = FUNCTION (gsl_matrix, get) (mc, i - 1, 0);
394 NAME (gsl_matrix) "_get traps 1st index below lower bound");
395 gsl_test (GSL_REAL (z1) != 0,
396 NAME (gsl_matrix) "_get, zero real for 1st index below l.b.");
397 gsl_test (GSL_IMAG (z1) != 0,
398 NAME (gsl_matrix) "_get, zero imag for 1st index below l.b.");
401 z1 = FUNCTION (gsl_matrix, get) (mc, 0, j - 1);
403 NAME (gsl_matrix) "_get traps 2nd index below lower bound");
404 gsl_test (GSL_REAL (z1) != 0,
405 NAME (gsl_matrix) "_get, zero real for 2nd index below l.b.");
406 gsl_test (GSL_IMAG (z1) != 0,
407 NAME (gsl_matrix) "_get, zero imag for 2nd index below l.b.");
410 z1 = FUNCTION (gsl_matrix, get) (mc, M + 1, 0);
412 NAME (gsl_matrix) "_get traps 1st index above upper bound");
413 gsl_test (GSL_REAL (z1) != 0,
414 NAME (gsl_matrix) "_get, zero real for 1st index above u.b.");
415 gsl_test (GSL_IMAG (z1) != 0,
416 NAME (gsl_matrix) "_get, zero imag for 1st index above u.b.");
419 z1 = FUNCTION (gsl_matrix, get) (mc, 0, N + 1);
421 NAME (gsl_matrix) "_get traps 2nd index above upper bound");
422 gsl_test (GSL_REAL (z1) != 0,
423 NAME (gsl_matrix) "_get, zero real for 2nd index above u.b.");
424 gsl_test (GSL_IMAG (z1) != 0,
425 NAME (gsl_matrix) "_get, zero imag for 2nd index above u.b.");
428 z1 = FUNCTION (gsl_matrix, get) (mc, M, 0);
429 gsl_test (!status, NAME (gsl_matrix) "_get traps 1st index at upper bound");
430 gsl_test (GSL_REAL (z1) != 0,
431 NAME (gsl_matrix) "_get, zero real for 1st index at u.b.");
432 gsl_test (GSL_IMAG (z1) != 0,
433 NAME (gsl_matrix) "_get, zero imag for 1st index at u.b.");
436 z1 = FUNCTION (gsl_matrix, get) (mc, 0, N);
437 gsl_test (!status, NAME (gsl_matrix) "_get traps 2nd index at upper bound");
438 gsl_test (GSL_REAL (z1) != 0,
439 NAME (gsl_matrix) "_get, zero real for 2nd index at u.b.");
440 gsl_test (GSL_IMAG (z1) != 0,
441 NAME (gsl_matrix) "_get, zero imag for 2nd index at u.b.");
443 FUNCTION (gsl_matrix, free) (mc);
448 FUNCTION (test, arith) (void)
453 /* Must use smaller dimensions to prevent approximation of floats in float_mul_elements test*/
455 TYPE (gsl_matrix) * a = FUNCTION (gsl_matrix, alloc) (P, Q);
456 TYPE (gsl_matrix) * b = FUNCTION (gsl_matrix, alloc) (P, Q);
457 TYPE (gsl_matrix) * m = FUNCTION (gsl_matrix, alloc) (P, Q);
463 for (i = 0; i < P; i++)
465 for (j = 0; j < Q; j++)
468 GSL_REAL (z) = (ATOMIC) k;
469 GSL_IMAG (z) = (ATOMIC) (k + 10);
470 GSL_REAL (z1) = (ATOMIC) (k + 5);
471 GSL_IMAG (z1) = (ATOMIC) (k + 20);
473 FUNCTION (gsl_matrix, set) (a, i, j, z);
474 FUNCTION (gsl_matrix, set) (b, i, j, z1);
480 FUNCTION (gsl_matrix, memcpy) (m, a);
482 FUNCTION (gsl_matrix, add) (m, b);
487 for (i = 0; i < P; i++)
489 for (j = 0; j < Q; j++)
491 BASE z = FUNCTION (gsl_matrix, get) (m, i, j);
492 if (GSL_REAL (z) != (ATOMIC) (2 * k + 5) ||
493 GSL_IMAG (z) != (ATOMIC) (2 * k + 30))
498 gsl_test (status, NAME (gsl_matrix) "_add matrix addition");
502 FUNCTION (gsl_matrix, memcpy) (m, a);
504 FUNCTION (gsl_matrix, sub) (m, b);
509 for (i = 0; i < P; i++)
511 for (j = 0; j < Q; j++)
513 BASE z = FUNCTION (gsl_matrix, get) (m, i, j);
514 if (GSL_REAL (z) != (ATOMIC) (-5)
515 || GSL_IMAG (z) != (ATOMIC) (-10))
520 gsl_test (status, NAME (gsl_matrix) "_sub matrix subtraction");
524 FUNCTION (gsl_matrix, memcpy) (m, a);
526 FUNCTION (gsl_matrix, mul_elements) (m, b);
531 for (i = 0; i < P; i++)
533 for (j = 0; j < Q; j++)
535 ATOMIC real = -(ATOMIC) (25 * k + 200);
536 ATOMIC imag = (ATOMIC) (2 * k * k + 35 * k + 50);
537 BASE z = FUNCTION (gsl_matrix, get) (m, i, j);
538 if (fabs (GSL_REAL (z) - real) > 100 * BASE_EPSILON ||
539 fabs (GSL_IMAG (z) - imag) > 100 * BASE_EPSILON)
543 printf ("%d\t%d\n", i, j);
544 printf (OUT_FORMAT "\n",
545 GSL_REAL (z) + (ATOMIC) (25 * (ATOMIC) k + 200));
546 printf (OUT_FORMAT "\n",
547 GSL_IMAG (z) - (ATOMIC) (2 * k * k + 35 * k + 50));
554 gsl_test (status, NAME (gsl_matrix) "_mul_elements multiplication");
559 FUNCTION (gsl_matrix, memcpy) (m, a);
561 FUNCTION (gsl_matrix, div_elements) (m, b);
566 for (i = 0; i < P; i++)
568 for (j = 0; j < Q; j++)
570 ATOMIC denom = (2 * k * k + 50 * k + 425);
571 ATOMIC real = (ATOMIC) (2 * k * k + 35 * k + 200) / denom;
572 ATOMIC imag = ((ATOMIC) (50) - (ATOMIC) (5 * k)) / denom;
573 BASE z = FUNCTION (gsl_matrix, get) (m, i, j);
574 if (fabs (GSL_REAL (z) - real) > 100 * BASE_EPSILON ||
575 fabs (GSL_IMAG (z) - imag) > 100 * BASE_EPSILON)
578 printf (OUT_FORMAT "\t",
579 GSL_REAL (z) - (ATOMIC) (2 * k * k + 35 * k +
581 printf (OUT_FORMAT "\n",
582 GSL_IMAG (z) - ((ATOMIC) (50) -
583 (ATOMIC) (5 * k)) / denom);
590 gsl_test (status, NAME (gsl_matrix) "_div_elements division");
593 FUNCTION (gsl_matrix, free) (a);
594 FUNCTION (gsl_matrix, free) (b);
595 FUNCTION (gsl_matrix, free) (m);