remove the const modifier from function arguments
[liba.git] / src / rbf.c
blob4f3c7e2769a36c51b07e9797413964874d40f251
1 #include "a/rbf.h"
2 #include "a/math.h"
4 a_float const *a_rbf_iter(a_rbf const *ctx)
6 a_size const input_hidden_n = ctx->input_n * ctx->hidden_n;
7 for (a_size hidden_i = 0; hidden_i != ctx->hidden_n; ++hidden_i)
9 a_float num = 0;
10 if (ctx->center_n == input_hidden_n)
12 a_float const *center_p = ctx->center_p + hidden_i;
13 for (a_size input_i = 0; input_i != ctx->input_n; ++input_i, center_p += ctx->input_n)
15 a_float const r = ctx->input_p[input_i] - *center_p;
16 num += r * r;
19 else if (ctx->center_n == ctx->hidden_n)
21 for (a_size input_i = 0; input_i != ctx->input_n; ++input_i)
23 a_float const r = ctx->input_p[input_i] - ctx->center_p[hidden_i];
24 num += r * r;
27 else if (ctx->center_n == 1)
29 for (a_size input_i = 0; input_i != ctx->input_n; ++input_i)
31 a_float const r = ctx->input_p[input_i] - *ctx->center_p;
32 num += r * r;
35 a_float den = 1;
36 if (ctx->height_n == ctx->hidden_n)
38 den = ctx->height_p[hidden_i] * ctx->height_p[hidden_i];
40 else if (ctx->height_n == 1)
42 den = *ctx->height_p * *ctx->height_p;
44 ctx->hidden_p[hidden_i] = a_float_exp(-num / den);
46 a_float const *weight_p = ctx->weight;
47 for (a_size output_i = 0; output_i != ctx->output_n; ++output_i)
49 ctx->output_p[output_i] = 0;
50 for (a_size hidden = 0; hidden != ctx->hidden_n; ++hidden)
52 ctx->output_p[output_i] += *weight_p++ * ctx->hidden_p[hidden];
55 return ctx->output_p;
58 void a_rbf_tune(a_rbf const *ctx, a_float const *out)
60 a_float *weight = ctx->weight;
61 a_float *deltaw = ctx->deltaw;
62 for (a_size output_i = 0; output_i != ctx->output_n; ++output_i)
64 a_float error = out[output_i] - ctx->output_p[output_i];
65 for (a_size hidden_i = 0; hidden_i != ctx->hidden_n; ++hidden_i)
67 *deltaw = ctx->learn * error * ctx->hidden_p[hidden_i] + ctx->alpha * *deltaw;
68 *weight++ += *deltaw++;
73 void a_rbf_zero(a_rbf const *ctx)
75 a_size const hidden_output_n = ctx->hidden_n * ctx->output_n;
76 a_zero(ctx->input_p, sizeof(a_float) * ctx->input_n);
77 a_zero(ctx->hidden_p, sizeof(a_float) * ctx->hidden_n);
78 a_zero(ctx->output_p, sizeof(a_float) * ctx->output_n);
79 a_zero(ctx->weight, sizeof(a_float) * hidden_output_n);
80 a_zero(ctx->deltaw, sizeof(a_float) * hidden_output_n);