Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/klib/blob/master/test/kmin_test.c # klib [Ported from github.com/attractivechaos/klib] added block arena Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `klib / test / kmin_test.c` 48 lines ยท 983 bytes Raw Blame History 1 #include 2 #include 3 #include "kmath.h" 4 5 static int n_evals; 6 7 double f_Chebyquad(int n, double *x, void *data) 8 { 9 int i, j; 10 double y[20][20], f; 11 int np, iw; 12 double sum; 13 for (j = 0; j != n; ++j) { 14 y[0][j] = 1.; 15 y[1][j] = 2. * x[j] - 1.; 16 } 17 for (i = 1; i != n; ++i) 18 for (j = 0; j != n; ++j) 19 y[i+1][j] = 2. * y[1][j] * y[i][j] - y[i-1][j]; 20 f = 0.; 21 np = n + 1; 22 iw = 1; 23 for (i = 0; i != np; ++i) { 24 sum = 0.; 25 for (j = 0; j != n; ++j) sum += y[i][j]; 26 sum /= n; 27 if (iw > 0) sum += 1. / ((i - 1) * (i + 1)); 28 iw = -iw; 29 f += sum * sum; 30 } 31 ++n_evals; 32 return f; 33 } 34 35 int main() 36 { 37 double x[20], y; 38 int n, i; 39 printf("\nMinimizer: Hooke-Jeeves\n"); 40 for (n = 2; n <= 8; n += 2) { 41 for (i = 0; i != n; ++i) x[i] = (double)(i + 1) / n; 42 n_evals = 0; 43 y = kmin_hj(f_Chebyquad, n, x, 0, KMIN_RADIUS, KMIN_EPS, KMIN_MAXCALL); 44 printf("n=%d,min=%.8lg,n_evals=%d\n", n, y, n_evals); 45 } 46 printf("\n"); 47 return 0; 48 }