chiark / gitweb /
curveopt: symbolic: wip, before abandon provision for fdf
[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   double e = 0;
31   for (P=0; P<N-3; P++) {
32     double d;
33     // A is point #p, B #p+1, C #p+2, etc.
34     if (P == 0) {
35     } else if (P==N-2) {
36     } else {
37       E_CALCULATE_MID;
38
39   double F[N], e;
40   int i;
41   X_EXTRACT;
42   F_POPULATE;
43   for (i=0, e=0; i<N; i++) {
44     double d = F[i] - target[i];
45     e += d*d;
46   }
47   //printf("\n cb_Efunc %p %10.7f [", xp, e);
48   //for (i=0; i<N; i++) printf(" %10.7f,", x[i]);
49   //printf("]\n");
50   return e;
51 }
52
53 static int cb_fdf(const gsl_vector *x, void *params,
54                  gsl_vector *f, gsl_matrix *J) {
55   
56 }
57
58 static void cb_step(const gsl_rng *rng, void *xp, double step_size) {
59   double *x = xp;
60   int i;
61   double step[N];
62   gsl_ran_dir_nd(rng, N,step);
63   for (i=0; i<N; i++)
64     x[i] += step_size * step[i];
65   //printf("\n cb_step %p %10.7f [", xp, step_size);
66   //for (i=0; i<N; i++) printf(" %10.7f,", step[i]);
67   //printf("]\n");
68 }
69
70 static double cb_metric(void *xp, void *yp) {
71   const double *x=xp, *y=yp;
72   int i;
73   double s;
74   for (i=0, s=0; i<N; i++) {
75     double d = x[i] - y[i];
76     s += d*d;
77   }
78   return sqrt(s);
79 }
80
81 static void printcore(const double *x) {
82   int i;
83   double F[N];
84   X_EXTRACT;
85   F_POPULATE;
86   printf("[");
87   for (i=0; i<6; i++) printf(" %.18g,", x[i]);
88   for (i=0; i<6; i++) printf(" %.18g,", F[i]);
89   printf(" ]\n");
90 }
91
92 static void __attribute__((unused)) cb_print(void *xp) {
93   const double *x = xp;
94   printf("\n");
95   printcore(x);
96 }
97
98 static double scan1double(void) {
99   double v;
100   int r;
101
102   r = scanf("%lf",&v);
103   if (ferror(stdin)) { perror("stdin"); exit(-1); }
104   if (feof(stdin)) exit(0);
105   if (r!=1) { fputs("bad input\n",stderr); exit(-1); }
106   return v;
107 }
108
109 int main(void) {
110   double epsilon;
111   int i;
112
113   double startpoint[N];
114
115   gsl_rng *rng = gsl_rng_alloc(gsl_rng_ranlxd2);
116
117   for (;;) {
118     /* 2N+1 doubles: target, initial guess, epsilon for residual */
119     for (i=0; i<N; i++)
120       target[i] = scan1double();
121     for (i=0; i<N; i++)
122       startpoint[i] = scan1double();
123     epsilon = scan1double();
124
125     gsl_rng_set(rng,0);
126
127     gsl_siman_params_t siman_params = {
128       .k = 1.0,
129       .t_initial = 0.5,
130       .mu_t = 1.001,
131       .t_min = epsilon * 1E-3,
132       .iters_fixed_T = 100,
133       .n_tries = 10,
134       .step_size = 0.05,
135     };
136
137     gsl_siman_solve(rng,
138                     startpoint,
139                     cb_Efunc, cb_step, cb_metric,
140                     0, // cb_print,
141                     0,0,0, sizeof(startpoint), siman_params);
142
143     printcore(startpoint);
144
145     printf("[]\n");
146     fflush(stdout);
147   }
148 }