00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifdef HAVE_CONFIG_H
00027 # include <config.h>
00028 #endif
00029
00030
00036
00039
00040
00041
00042
00043 #include <tests.h>
00044
00045 #include <xsh_error.h>
00046 #include <xsh_msg.h>
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059 #include <cpl.h>
00060
00061
00062 #include <stdio.h>
00063 #include <stdlib.h>
00064 #include <math.h>
00065 #include <gsl/gsl_bspline.h>
00066 #include <gsl/gsl_multifit.h>
00067 #include <gsl/gsl_rng.h>
00068 #include <gsl/gsl_randist.h>
00069 #include <gsl/gsl_statistics.h>
00070
00071
00072
00073
00074
00075 #define MODULE_ID "XSH_BSPLINE"
00076
00077
00078 #define N 200
00079
00080
00081 #define NCOEFFS 12
00082
00083
00084 #define NBREAK (NCOEFFS -2)
00085
00093 int main( int argc, char **argv)
00094 {
00095
00096 int ret =0;
00097
00098
00099 const size_t n = N;
00100 const size_t ncoeffs = NCOEFFS;
00101 const size_t nbreak = NBREAK;
00102 size_t i, j;
00103 gsl_bspline_workspace *bw;
00104 gsl_vector *B;
00105 double dy;
00106 gsl_rng *r;
00107 gsl_vector *c, *w;
00108 gsl_vector *x, *y;
00109 gsl_matrix *X, *cov;
00110 gsl_multifit_linear_workspace *mw;
00111 double chisq, Rsq, dof, tss;
00112
00113
00114 TESTS_INIT(MODULE_ID);
00115
00116 gsl_rng_env_setup();
00117 r = gsl_rng_alloc(gsl_rng_default);
00118
00119
00120 bw = gsl_bspline_alloc(4, nbreak);
00121 B = gsl_vector_alloc(ncoeffs);
00122
00123 x = gsl_vector_alloc(n);
00124 y = gsl_vector_alloc(n);
00125 X = gsl_matrix_alloc(n, ncoeffs);
00126 c = gsl_vector_alloc(ncoeffs);
00127 w = gsl_vector_alloc(n);
00128 cov = gsl_matrix_alloc(ncoeffs, ncoeffs);
00129 mw = gsl_multifit_linear_alloc(n, ncoeffs);
00130
00131 printf("#m=0,S=0\n");
00132
00133 for (i = 0; i < n; ++i)
00134 {
00135 double sigma;
00136 double xi = (15.0 / (N - 1)) * i;
00137 double yi = cos(xi) * exp(-0.1 * xi);
00138
00139 sigma = 0.1 * yi;
00140 dy = gsl_ran_gaussian(r, sigma);
00141 yi += dy;
00142
00143 gsl_vector_set(x, i, xi);
00144 gsl_vector_set(y, i, yi);
00145 gsl_vector_set(w, i, 1.0 / (sigma * sigma));
00146
00147 printf("%f %f\n", xi, yi);
00148 }
00149
00150
00151 gsl_bspline_knots_uniform(0.0, 15.0, bw);
00152
00153
00154 for (i = 0; i < n; ++i)
00155 {
00156 double xi = gsl_vector_get(x, i);
00157
00158
00159 gsl_bspline_eval(xi, B, bw);
00160
00161
00162 for (j = 0; j < ncoeffs; ++j)
00163 {
00164 double Bj = gsl_vector_get(B, j);
00165 gsl_matrix_set(X, i, j, Bj);
00166 }
00167 }
00168
00169
00170 gsl_multifit_wlinear(X, w, y, c, cov, &chisq, mw);
00171
00172 dof = n - ncoeffs;
00173
00174
00175
00176
00177
00178
00179
00180 xsh_msg("ok1");
00181
00182 {
00183 double xi, yi, yerr;
00184
00185 printf("#m=1,S=0\n");
00186 for (xi = 0.0; xi < 15.0; xi += 0.1)
00187 {
00188 gsl_bspline_eval(xi, B, bw);
00189 gsl_multifit_linear_est(B, c, cov, &yi, &yerr);
00190 printf("%f %f\n", xi, yi);
00191 }
00192 }
00193 xsh_msg("ok3");
00194
00195
00196
00197 gsl_rng_free(r);
00198 gsl_bspline_free(bw);
00199 gsl_vector_free(B);
00200 gsl_vector_free(x);
00201 gsl_vector_free(y);
00202 gsl_matrix_free(X);
00203 gsl_vector_free(c);
00204 gsl_vector_free(w);
00205 gsl_matrix_free(cov);
00206 gsl_multifit_linear_free(mw);
00207
00208
00209 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00210 xsh_error_dump(CPL_MSG_ERROR);
00211 ret= 1;
00212 }
00213 TEST_END();
00214 return ret ;
00215 }
00216