chiark / gitweb /
curveopt: symbolic: wip, before go back to conditional in C
[moebius3.git] / findcurve.c
1 /*
2  */
3
4 #include <math.h>
5
6 #include <gsl/gsl_errno.h>
7 #include <gsl/gsl_sf.h>
8 #include <gsl/gsl_siman.h>
9 #include <gsl/gsl_randist.h>
10
11 #include "symbolic.c"
12
13 #define X(i) x[i]
14 #define J_END_COL(i) \
15   for (j=0; j<N; j++) gsl_matrix_set(J,i,j,J_COL[j]);
16
17 static inline _Bool IS_SMALL(double v) {
18   return v < 1E6;
19 }
20
21 static inline double sinc(double x) {
22   return gsl_sf_sinc(x / M_PI);
23 }
24
25 static double target[N];
26
27 static double cb_Efunc(void *xp) {
28   const double *x = xp;
29
30   e = 0;
31   for (i=0; i<N-3; i++) {
32     // A is point i, B i+1, C i+2, etc.
33     if (i == 0) {
34     } else if (i==N-2) {
35     } else {
36       E_CALCULATE_ED;
37
38   double F[N], e;
39   int i;
40   X_EXTRACT;
41   F_POPULATE;
42   for (i=0, e=0; i<N; i++) {
43     double d = F[i] - target[i];
44     e += d*d;
45   }
46   //printf("\n cb_Efunc %p %10.7f [", xp, e);
47   //for (i=0; i<N; i++) printf(" %10.7f,", x[i]);
48   //printf("]\n");
49   return e;
50 }
51
52 static int cb_fdf(const gsl_vector *x, void *params,
53                  gsl_vector *f, gsl_matrix *J) {
54   
55 }
56
57 static void cb_step(const gsl_rng *rng, void *xp, double step_size) {
58   double *x = xp;
59   int i;
60   double step[N];
61   gsl_ran_dir_nd(rng, N,step);
62   for (i=0; i<N; i++)
63     x[i] += step_size * step[i];
64   //printf("\n cb_step %p %10.7f [", xp, step_size);
65   //for (i=0; i<N; i++) printf(" %10.7f,", step[i]);
66   //printf("]\n");
67 }
68
69 static double cb_metric(void *xp, void *yp) {
70   const double *x=xp, *y=yp;
71   int i;
72   double s;
73   for (i=0, s=0; i<N; i++) {
74     double d = x[i] - y[i];
75     s += d*d;
76   }
77   return sqrt(s);
78 }
79
80 static void printcore(const double *x) {
81   int i;
82   double F[N];
83   X_EXTRACT;
84   F_POPULATE;
85   printf("[");
86   for (i=0; i<6; i++) printf(" %.18g,", x[i]);
87   for (i=0; i<6; i++) printf(" %.18g,", F[i]);
88   printf(" ]\n");
89 }
90
91 static void __attribute__((unused)) cb_print(void *xp) {
92   const double *x = xp;
93   printf("\n");
94   printcore(x);
95 }
96
97 static double scan1double(void) {
98   double v;
99   int r;
100
101   r = scanf("%lf",&v);
102   if (ferror(stdin)) { perror("stdin"); exit(-1); }
103   if (feof(stdin)) exit(0);
104   if (r!=1) { fputs("bad input\n",stderr); exit(-1); }
105   return v;
106 }
107
108 int main(void) {
109   double epsilon;
110   int i;
111
112   double startpoint[N];
113
114   gsl_rng *rng = gsl_rng_alloc(gsl_rng_ranlxd2);
115
116   for (;;) {
117     /* 2N+1 doubles: target, initial guess, epsilon for residual */
118     for (i=0; i<N; i++)
119       target[i] = scan1double();
120     for (i=0; i<N; i++)
121       startpoint[i] = scan1double();
122     epsilon = scan1double();
123
124     gsl_rng_set(rng,0);
125
126     gsl_siman_params_t siman_params = {
127       .k = 1.0,
128       .t_initial = 0.5,
129       .mu_t = 1.001,
130       .t_min = epsilon * 1E-3,
131       .iters_fixed_T = 100,
132       .n_tries = 10,
133       .step_size = 0.05,
134     };
135
136     gsl_siman_solve(rng,
137                     startpoint,
138                     cb_Efunc, cb_step, cb_metric,
139                     0, // cb_print,
140                     0,0,0, sizeof(startpoint), siman_params);
141
142     printcore(startpoint);
143
144     printf("[]\n");
145     fflush(stdout);
146   }
147 }