chiark / gitweb /
curveopt: symbolic: compiles
[moebius3.git] / findcurve.c
1 /*
2  */
3
4 #include <math.h>
5 #include <string.h>
6
7 #include <gsl/gsl_errno.h>
8 #include <gsl/gsl_sf.h>
9 #include <gsl/gsl_siman.h>
10 #include <gsl/gsl_randist.h>
11
12 #include "symbolic.c"
13
14 #define J_END_COL(i) \
15   for (j=0; j<PN; 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 int NP;
26 static double *INPUT; /* dyanmic array, on main's stack */
27 static double PREP[NPREP];
28
29 static void prepare(double X[] /* startpoint */) {
30   /* fills in PREP and startpoint */
31   PREPARE;
32 }
33
34 static double cb_Efunc(void *xp) {
35   const double *X = xp;
36   double F[3], G[3];
37
38   CALCULATE_F_G;
39
40   double e = 0;
41   int P;
42   for (P=0; P<NP-3; P++) {
43     double P_cost;
44     CALCULATE_COST;
45     e += P_cost;
46   }
47   return e;
48 }
49
50 static void cb_step(const gsl_rng *rng, void *xp, double step_size) {
51   double *x = xp;
52   int i;
53   double step[NX];
54   gsl_ran_dir_nd(rng, NX, step);
55   for (i=0; i<NX; i++)
56     x[i] += step_size * step[i];
57   //printf("\n cb_step %p %10.7f [", xp, step_size);
58   //for (i=0; i<N; i++) printf(" %10.7f,", step[i]);
59   //printf("]\n");
60 }
61
62 static double cb_metric(void *xp, void *yp) {
63   const double *x=xp, *y=yp;
64   int i;
65   double s;
66   for (i=0, s=0; i<NX; i++) {
67     double d = x[i] - y[i];
68     s += d*d;
69   }
70   return sqrt(s);
71 }
72
73 static void printcore(const double *X) {
74   int i;
75   printf("[");
76   for (i=0; i<NX; i++) printf(" %.18g,", X[i]);
77   printf(" ]\n");
78 }
79
80 static void __attribute__((unused)) cb_print(void *xp) {
81   const double *x = xp;
82   printf("\n");
83   printcore(x);
84 }
85
86 static double scan1double(void) {
87   double v;
88   int r;
89
90   r = scanf("%lf",&v);
91   if (ferror(stdin)) { perror("stdin"); exit(-1); }
92   if (feof(stdin)) exit(0);
93   if (r!=1) { fputs("bad input\n",stderr); exit(-1); }
94   return v;
95 }
96
97 int main(int argc, const char *const *argv) {
98   double epsilon;
99   int i;
100
101   NP = atoi(argv[1]);
102
103   gsl_rng *rng = gsl_rng_alloc(gsl_rng_ranlxd2);
104
105   double input[NINPUT]; INPUT = input;
106   double startpoint[NX];
107
108   for (;;) {
109     /* NINPUT + 1 doubles: startpoint, epsilon for residual */
110     for (i=0; i<NINPUT; i++)
111       INPUT[i] = scan1double();
112     epsilon = scan1double();
113
114     gsl_rng_set(rng,0);
115
116     gsl_siman_params_t siman_params = {
117       .k = 1.0,
118       .t_initial = 0.5,
119       .mu_t = 1.001,
120       .t_min = epsilon * 1E-3,
121       .iters_fixed_T = 100,
122       .n_tries = 10,
123       .step_size = 0.05,
124     };
125
126     prepare(startpoint);
127
128     gsl_siman_solve(rng,
129                     startpoint,
130                     cb_Efunc, cb_step, cb_metric,
131                     0, // cb_print,
132                     0,0,0, sizeof(startpoint), siman_params);
133
134     printcore(startpoint);
135
136     printf("[]\n");
137     fflush(stdout);
138   }
139 }