modified: src1/input.c
[GalaxyCodeBases.git] / c_cpp / lib / klib / test / kmin_test.c
blob33ccd1cbce7166e3409d48611f13070274cdf27b
1 #include <stdio.h>
2 #include <math.h>
3 #include "kmath.h"
5 static int n_evals;
7 double f_Chebyquad(int n, double *x, void *data)
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.;
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;
31 ++n_evals;
32 return f;
35 int main()
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);
46 printf("\n");
47 return 0;