chiark / gitweb /
Revert "simplex wip: compiles"
[moebius3.git] / findcurve.c
index 9d86ff485375f03f670d9415876ab1cb8462d48b..055018f3bfb6abf87e5dba0aabbcf8dcb59dc906 100644 (file)
@@ -2,6 +2,7 @@
  */
 
 #include <math.h>
+#include <string.h>
 
 #include <gsl/gsl_errno.h>
 #include <gsl/gsl_sf.h>
@@ -10,9 +11,8 @@
 
 #include "symbolic.c"
 
-#define X(i) x[i]
 #define J_END_COL(i) \
-  for (j=0; j<N; j++) gsl_matrix_set(J,i,j,J_COL[j]);
+  for (j=0; j<PN; j++) gsl_matrix_set(J,i,j,J_COL[j]);
 
 static inline _Bool IS_SMALL(double v) {
   return v < 1E6;
@@ -22,52 +22,72 @@ static inline double sinc(double x) {
   return gsl_sf_sinc(x / M_PI);
 }
 
-static double target[N];
+static int NP;
+static double *INPUT; /* dyanmic array, on main's stack */
+static double PREP[NPREP];
+
+static void printcore(const double *X) {
+  int i, j;
+  DECLARE_F_G;
+  CALCULATE_F_G;
+  printf("[");
+  for (i=0; i<NP; i++)
+    for (j=0; j<3; j++)
+      printf(" %25.18g,", POINT(i)[j]);
+  printf(" ]\n");
+}
+
+static void prepare(double X[] /* startpoint */) {
+  /* fills in PREP and startpoint */
+  PREPARE;
+}
+
+//#define DEBUG
 
 static double cb_Efunc(void *xp) {
-  const double *x = xp;
-  double F[N], e;
-  int i;
-  X_EXTRACT;
-  F_POPULATE;
-  for (i=0, e=0; i<N; i++) {
-    double d = F[i] - target[i];
-    e += d*d;
+  const double *X = xp;
+  int P;
+  DECLARE_F_G;
+  CALCULATE_F_G;
+
+#ifdef DEBUG
+  int i,j;
+  printf(" Efunc\n");
+  for (j=0; j<3; j++) {
+    for (P=0; P<NP; P++)
+      printf(" %7.4f", POINT(P)[j]);
+    printf("\n");
   }
-  return e;
+  printf("             ");
+#endif
+
+  CALCULATE_COST;
+  return cost;
 }
 
 static void cb_step(const gsl_rng *rng, void *xp, double step_size) {
   double *x = xp;
   int i;
-  double step[N];
-  gsl_ran_dir_nd(rng, N,step);
-  for (i=0; i<N; i++)
+  double step[NX];
+  gsl_ran_dir_nd(rng, NX, step);
+  for (i=0; i<NX; i++)
     x[i] += step_size * step[i];
+  //printf("\n cb_step %p %10.7f [", xp, step_size);
+  //for (i=0; i<N; i++) printf(" %10.7f,", step[i]);
+  //printf("]\n");
 }
 
 static double cb_metric(void *xp, void *yp) {
   const double *x=xp, *y=yp;
   int i;
   double s;
-  for (i=0, s=0; i<N; i++) {
+  for (i=0, s=0; i<NX; i++) {
     double d = x[i] - y[i];
     s += d*d;
   }
   return sqrt(s);
 }
 
-static void printcore(const double *x) {
-  int i;
-  double F[N];
-  X_EXTRACT;
-  F_POPULATE;
-  printf("[");
-  for (i=0; i<6; i++) printf(" %.18g,", x[i]);
-  for (i=0; i<6; i++) printf(" %.18g,", F[i]);
-  printf(" ]\n");
-}
-
 static void __attribute__((unused)) cb_print(void *xp) {
   const double *x = xp;
   printf("\n");
@@ -85,21 +105,22 @@ static double scan1double(void) {
   return v;
 }
 
-int main(void) {
+int main(int argc, const char *const *argv) {
   double epsilon;
   int i;
 
-  double startpoint[N];
+  NP = atoi(argv[1]);
+  epsilon = atof(argv[2]);
 
   gsl_rng *rng = gsl_rng_alloc(gsl_rng_ranlxd2);
 
+  double input[NINPUT]; INPUT = input;
+  double startpoint[NX];
+
   for (;;) {
-    /* 2N+1 doubles: target, initial guess, epsilon for residual */
-    for (i=0; i<N; i++)
-      target[i] = scan1double();
-    for (i=0; i<N; i++)
-      startpoint[i] = scan1double();
-    epsilon = scan1double();
+    /* NINPUT + 1 doubles: startpoint, epsilon for residual */
+    for (i=0; i<NINPUT; i++)
+      INPUT[i] = scan1double();
 
     gsl_rng_set(rng,0);
 
@@ -108,8 +129,13 @@ int main(void) {
       .t_initial = 0.5,
       .mu_t = 1.001,
       .t_min = epsilon * 1E-3,
+      .iters_fixed_T = 100,
+      .n_tries = 10,
+      .step_size = 0.05,
     };
 
+    prepare(startpoint);
+
     gsl_siman_solve(rng,
                    startpoint,
                    cb_Efunc, cb_step, cb_metric,