Added script front-end for primer-design code
[htsworkflow.git] / htswanalysis / MACS / lib / gsl / gsl-1.11 / bspline / test.c
1 /* bspline/test.c
2  * 
3  * Copyright (C) 2006, 2007 Brian Gough
4  * 
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.
9  * 
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.
14  * 
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.
18  */
19
20 #include <config.h>
21 #include <gsl/gsl_test.h>
22 #include <gsl/gsl_errno.h>
23 #include <gsl/gsl_bspline.h>
24 #include <gsl/gsl_ieee_utils.h>
25
26 void
27 test_bspline (gsl_bspline_workspace * bw)
28 {
29   gsl_vector *B;
30   size_t i, j;
31   size_t n = 100;
32   size_t ncoeffs = gsl_bspline_ncoeffs (bw);
33   size_t order = gsl_bspline_order (bw);
34   size_t nbreak = gsl_bspline_nbreak (bw);
35   double a = gsl_bspline_breakpoint (0, bw);
36   double b = gsl_bspline_breakpoint (nbreak - 1, bw);
37
38   B = gsl_vector_alloc (ncoeffs);
39
40   for (i = 0; i < n; i++)
41     {
42       double xi = a + (b - a) * (i / (n - 1.0));
43       double sum = 0;
44       gsl_bspline_eval (xi, B, bw);
45
46       for (j = 0; j < ncoeffs; j++)
47         {
48           double Bj = gsl_vector_get (B, j);
49           int s = (Bj < 0 || Bj > 1);
50           gsl_test (s,
51                     "basis-spline coefficient %u is in range [0,1] for x=%g",
52                     j, xi);
53           sum += Bj;
54         }
55
56       gsl_test_rel (sum, 1.0, order * GSL_DBL_EPSILON,
57                     "basis-spline order %u is normalized for x=%g", order,
58                     xi);
59     }
60
61   gsl_vector_free (B);
62 }
63
64
65
66 int
67 main (int argc, char **argv)
68 {
69   size_t order, breakpoints, i;
70
71   gsl_ieee_env_setup ();
72
73   argc = 0;                     /* prevent warnings about unused parameters */
74   argv = 0;
75
76   for (order = 1; order < 10; order++)
77     {
78       for (breakpoints = 2; breakpoints < 100; breakpoints++)
79         {
80           double a = -1.23 * order, b = 45.6 * order;
81           gsl_bspline_workspace *bw = gsl_bspline_alloc (order, breakpoints);
82           gsl_bspline_knots_uniform (a, b, bw);
83           test_bspline (bw);
84           gsl_bspline_free (bw);
85         }
86     }
87
88
89   for (order = 1; order < 10; order++)
90     {
91       for (breakpoints = 2; breakpoints < 100; breakpoints++)
92         {
93           double a = -1.23 * order, b = 45.6 * order;
94           gsl_bspline_workspace *bw = gsl_bspline_alloc (order, breakpoints);
95           gsl_vector *k = gsl_vector_alloc (breakpoints);
96           for (i = 0; i < breakpoints; i++)
97             {
98               double f, x;
99               f = sqrt (i / (breakpoints - 1.0));
100               x = (1 - f) * a + f * b;
101               gsl_vector_set (k, i, x);
102             };
103           gsl_bspline_knots (k, bw);
104           test_bspline (bw);
105           gsl_vector_free (k);
106           gsl_bspline_free (bw);
107         }
108     }
109
110   exit (gsl_test_summary ());
111 }