Revision 14, 1.4 KB
(checked in by jgaeddert, 16 years ago)
|
adding utility functions, basic concept; not yet functional
|
Line | |
---|
1 | // |
---|
2 | // Utility |
---|
3 | // |
---|
4 | |
---|
5 | #include <math.h> |
---|
6 | #include <stdlib.h> |
---|
7 | #include "utility.h" |
---|
8 | |
---|
9 | typedef float(*utility_function_prototype)(float _x, float _eta, float _sig); |
---|
10 | float util_func_atan(float _x, float _eta, float _sig); |
---|
11 | float util_func_gauss(float _x, float _eta, float _sig); |
---|
12 | |
---|
13 | struct utility_s { |
---|
14 | float target; |
---|
15 | int shape; |
---|
16 | int scale; |
---|
17 | int minmax; |
---|
18 | utility_function_prototype f; |
---|
19 | float eta; |
---|
20 | float sig; |
---|
21 | }; |
---|
22 | |
---|
23 | utility utility_create(float _target, int _shape, int _scale, int _minmax) |
---|
24 | { |
---|
25 | utility u = (utility) malloc(sizeof(struct utility_s)); |
---|
26 | u->target = _target; |
---|
27 | u->shape = _shape; |
---|
28 | u->scale = _scale; |
---|
29 | u->minmax = _minmax; |
---|
30 | |
---|
31 | switch (u->shape) { |
---|
32 | case UTIL_SHAPE_BELL: |
---|
33 | u->f = &util_func_gauss; |
---|
34 | u->eta = u->target; |
---|
35 | u->sig = 1.0f; |
---|
36 | break; |
---|
37 | case UTIL_SHAPE_RAMP: |
---|
38 | u->f = &util_func_atan; |
---|
39 | u->eta = 0.0f; |
---|
40 | u->sig = 1.0f; |
---|
41 | break; |
---|
42 | default:; |
---|
43 | } |
---|
44 | |
---|
45 | return u; |
---|
46 | } |
---|
47 | |
---|
48 | void utility_free(utility _u) |
---|
49 | { |
---|
50 | free(_u); |
---|
51 | } |
---|
52 | |
---|
53 | float utility_evaluate(utility _u, float _x) |
---|
54 | { |
---|
55 | float y = _x / _u->target; |
---|
56 | if (_u->scale==UTIL_REL_LOG) |
---|
57 | y = logf(y); |
---|
58 | return _u->f( y, _u->eta, _u->sig); |
---|
59 | |
---|
60 | } |
---|
61 | |
---|
62 | float util_func_atan(float _x, float _eta, float _sig) { |
---|
63 | return tanhf((_x-_eta)/_sig); |
---|
64 | } |
---|
65 | |
---|
66 | float util_func_gauss(float _x, float _eta, float _sig) { |
---|
67 | return expf( -powf((_x-_eta),2)/(2*_sig*_sig) ); |
---|
68 | } |
---|
69 | |
---|