6 #include <gsl/gsl_errno.h>
7 #include <gsl/gsl_sf.h>
8 #include <gsl/gsl_siman.h>
9 #include <gsl/gsl_randist.h>
14 #define J_END_COL(i) \
15 for (j=0; j<N; j++) gsl_matrix_set(J,i,j,J_COL[j]);
17 static inline _Bool IS_SMALL(double v) {
21 static inline double sinc(double x) {
22 return gsl_sf_sinc(x / M_PI);
25 static double target[N];
27 static double cb_Efunc(void *xp) {
33 for (i=0, e=0; i<N; i++) {
34 double d = F[i] - target[i];
40 static void cb_step(const gsl_rng *rng, void *xp, double step_size) {
44 gsl_ran_dir_nd(rng, N,step);
46 x[i] += step_size * step[i];
47 //printf("\n cb_step %p %.18g [", xp, step_size);
48 //for (i=0; i<N; i++) printf(" %.18g,", step[i]);
52 static double cb_metric(void *xp, void *yp) {
53 const double *x=xp, *y=yp;
56 for (i=0, s=0; i<N; i++) {
57 double d = x[i] - y[i];
63 static void printcore(const double *x) {
69 for (i=0; i<6; i++) printf(" %.18g,", x[i]);
70 for (i=0; i<6; i++) printf(" %.18g,", F[i]);
74 static void __attribute__((unused)) cb_print(void *xp) {
80 static double scan1double(void) {
85 if (ferror(stdin)) { perror("stdin"); exit(-1); }
86 if (feof(stdin)) exit(0);
87 if (r!=1) { fputs("bad input\n",stderr); exit(-1); }
97 gsl_rng *rng = gsl_rng_alloc(gsl_rng_ranlxd2);
100 /* 2N+1 doubles: target, initial guess, epsilon for residual */
102 target[i] = scan1double();
104 startpoint[i] = scan1double();
105 epsilon = scan1double();
109 gsl_siman_params_t siman_params = {
113 .t_min = epsilon * 1E-3,
118 cb_Efunc, cb_step, cb_metric,
120 0,0,0, sizeof(startpoint), siman_params);
122 printcore(startpoint);