release 0.1.13
[liba.git] / lua / src / mf.c
blob129d738c0ead2e92f0a9d849483e17a4d441d06c
1 /***
2 membership function
3 @module liba.mf
4 */
6 #include "a.h"
7 #include "a/mf.h"
9 /***
10 gaussian membership function
11 @tparam number x input value for which to compute membership value.
12 @tparam number sigma is the standard deviation.
13 @tparam number c is the mean.
14 @treturn number membership value.
15 @function gauss
17 static int liba_mf_gauss_(lua_State *L, int arg)
19 a_float const x = (a_float)luaL_checknumber(L, arg + 1);
20 a_float const sigma = (a_float)luaL_checknumber(L, arg + 2);
21 a_float const c = (a_float)luaL_checknumber(L, arg + 3);
22 lua_pushnumber(L, (lua_Number)a_mf_gauss(x, sigma, c));
23 return 1;
25 static int liba_mf_gauss(lua_State *L)
27 return liba_mf_gauss_(L, 0);
30 /***
31 product of two sigmoidal membership functions
32 @tparam number x input value for which to compute membership value.
33 @tparam number sigma1 is the standard deviation of the left gaussian function.
34 @tparam number c1 is the mean of the left gaussian function.
35 @tparam number sigma2 is the standard deviation of the right gaussian function.
36 @tparam number c2 is the mean of the right gaussian function.
37 @treturn number membership value.
38 @function gauss2
40 static int liba_mf_gauss2_(lua_State *L, int arg)
42 a_float const x = (a_float)luaL_checknumber(L, arg + 1);
43 a_float const sigma1 = (a_float)luaL_checknumber(L, arg + 2);
44 a_float const c1 = (a_float)luaL_checknumber(L, arg + 3);
45 a_float const sigma2 = (a_float)luaL_checknumber(L, arg + 4);
46 a_float const c2 = (a_float)luaL_checknumber(L, arg + 4);
47 lua_pushnumber(L, (lua_Number)a_mf_gauss2(x, sigma1, c1, sigma2, c2));
48 return 1;
50 static int liba_mf_gauss2(lua_State *L)
52 return liba_mf_gauss2_(L, 0);
55 /***
56 generalized bell-shaped membership function
57 @tparam number x input value for which to compute membership value.
58 @tparam number a defines the width of the membership function, where a larger value creates a wider membership function.
59 @tparam number b defines the shape of the curve on either side of the central plateau, where a larger value creates a more steep transition.
60 @tparam number c defines the center of the membership function.
61 @treturn number membership value.
62 @function gbell
64 static int liba_mf_gbell_(lua_State *L, int arg)
66 a_float const x = (a_float)luaL_checknumber(L, arg + 1);
67 a_float const a = (a_float)luaL_checknumber(L, arg + 2);
68 a_float const b = (a_float)luaL_checknumber(L, arg + 3);
69 a_float const c = (a_float)luaL_checknumber(L, arg + 4);
70 lua_pushnumber(L, (lua_Number)a_mf_gbell(x, a, b, c));
71 return 1;
73 static int liba_mf_gbell(lua_State *L)
75 return liba_mf_gbell_(L, 0);
78 /***
79 sigmoidal membership function
80 @tparam number x input value for which to compute membership value.
81 @tparam number a defines the width of the transition area.
82 @tparam number c defines the center of the transition area.
83 @treturn number membership value.
84 @function sig
86 static int liba_mf_sig_(lua_State *L, int arg)
88 a_float const x = (a_float)luaL_checknumber(L, arg + 1);
89 a_float const a = (a_float)luaL_checknumber(L, arg + 2);
90 a_float const c = (a_float)luaL_checknumber(L, arg + 3);
91 lua_pushnumber(L, (lua_Number)a_mf_sig(x, a, c));
92 return 1;
94 static int liba_mf_sig(lua_State *L)
96 return liba_mf_sig_(L, 0);
99 /***
100 difference between two sigmoidal membership functions
101 @tparam number x input value for which to compute membership value.
102 @tparam number a1 defines the width of the first transition area.
103 @tparam number c1 defines the center of the first transition area.
104 @tparam number a2 defines the width of the second transition area.
105 @tparam number c2 defines the center of the second transition area.
106 @treturn number membership value.
107 @function dsig
109 static int liba_mf_dsig_(lua_State *L, int arg)
111 a_float const x = (a_float)luaL_checknumber(L, arg + 1);
112 a_float const a1 = (a_float)luaL_checknumber(L, arg + 2);
113 a_float const c1 = (a_float)luaL_checknumber(L, arg + 3);
114 a_float const a2 = (a_float)luaL_checknumber(L, arg + 4);
115 a_float const c2 = (a_float)luaL_checknumber(L, arg + 4);
116 lua_pushnumber(L, (lua_Number)a_mf_dsig(x, a1, c1, a2, c2));
117 return 1;
119 static int liba_mf_dsig(lua_State *L)
121 return liba_mf_dsig_(L, 0);
124 /***
125 product of two sigmoidal membership functions
126 @tparam number x input value for which to compute membership value.
127 @tparam number a1 defines the width of the first transition area.
128 @tparam number c1 defines the center of the first transition area.
129 @tparam number a2 defines the width of the second transition area.
130 @tparam number c2 defines the center of the second transition area.
131 @treturn number membership value.
132 @function psig
134 static int liba_mf_psig_(lua_State *L, int arg)
136 a_float const x = (a_float)luaL_checknumber(L, arg + 1);
137 a_float const a1 = (a_float)luaL_checknumber(L, arg + 2);
138 a_float const c1 = (a_float)luaL_checknumber(L, arg + 3);
139 a_float const a2 = (a_float)luaL_checknumber(L, arg + 4);
140 a_float const c2 = (a_float)luaL_checknumber(L, arg + 4);
141 lua_pushnumber(L, (lua_Number)a_mf_psig(x, a1, c1, a2, c2));
142 return 1;
144 static int liba_mf_psig(lua_State *L)
146 return liba_mf_psig_(L, 0);
149 /***
150 trapezoidal membership function
151 @tparam number x input value for which to compute membership value.
152 @tparam number a defines its left foot.
153 @tparam number b defines its left shoulder.
154 @tparam number c defines its right shoulder.
155 @tparam number d defines its right foot.
156 @treturn number membership value.
157 @function trap
159 static int liba_mf_trap_(lua_State *L, int arg)
161 a_float const x = (a_float)luaL_checknumber(L, arg + 1);
162 a_float const a = (a_float)luaL_checknumber(L, arg + 2);
163 a_float const b = (a_float)luaL_checknumber(L, arg + 3);
164 a_float const c = (a_float)luaL_checknumber(L, arg + 4);
165 a_float const d = (a_float)luaL_checknumber(L, arg + 4);
166 lua_pushnumber(L, (lua_Number)a_mf_trap(x, a, b, c, d));
167 return 1;
169 static int liba_mf_trap(lua_State *L)
171 return liba_mf_trap_(L, 0);
174 /***
175 triangular membership function
176 @tparam number x input value for which to compute membership value.
177 @tparam number a defines its left foot.
178 @tparam number b defines its peak.
179 @tparam number c defines its right foot.
180 @treturn number membership value.
181 @function tri
183 static int liba_mf_tri_(lua_State *L, int arg)
185 a_float const x = (a_float)luaL_checknumber(L, arg + 1);
186 a_float const a = (a_float)luaL_checknumber(L, arg + 2);
187 a_float const b = (a_float)luaL_checknumber(L, arg + 3);
188 a_float const c = (a_float)luaL_checknumber(L, arg + 4);
189 lua_pushnumber(L, (lua_Number)a_mf_tri(x, a, b, c));
190 return 1;
192 static int liba_mf_tri(lua_State *L)
194 return liba_mf_tri_(L, 0);
197 /***
198 linear s-shaped saturation membership function
199 @tparam number x input value for which to compute membership value.
200 @tparam number a defines its foot.
201 @tparam number b defines its shoulder.
202 @treturn number membership value.
203 @function lins
205 static int liba_mf_lins_(lua_State *L, int arg)
207 a_float const x = (a_float)luaL_checknumber(L, arg + 1);
208 a_float const a = (a_float)luaL_checknumber(L, arg + 2);
209 a_float const b = (a_float)luaL_checknumber(L, arg + 3);
210 lua_pushnumber(L, (lua_Number)a_mf_lins(x, a, b));
211 return 1;
213 static int liba_mf_lins(lua_State *L)
215 return liba_mf_lins_(L, 0);
218 /***
219 linear z-shaped saturation membership function
220 @tparam number x input value for which to compute membership value.
221 @tparam number a defines its shoulder.
222 @tparam number b defines its foot.
223 @treturn number membership value.
224 @function linz
226 static int liba_mf_linz_(lua_State *L, int arg)
228 a_float const x = (a_float)luaL_checknumber(L, arg + 1);
229 a_float const a = (a_float)luaL_checknumber(L, arg + 2);
230 a_float const b = (a_float)luaL_checknumber(L, arg + 3);
231 lua_pushnumber(L, (lua_Number)a_mf_linz(x, a, b));
232 return 1;
234 static int liba_mf_linz(lua_State *L)
236 return liba_mf_linz_(L, 0);
239 /***
240 s-shaped membership function
241 @tparam number x input value for which to compute membership value.
242 @tparam number a defines its foot.
243 @tparam number b defines its shoulder.
244 @treturn number membership value.
245 @function s
247 static int liba_mf_s_(lua_State *L, int arg)
249 a_float const x = (a_float)luaL_checknumber(L, arg + 1);
250 a_float const a = (a_float)luaL_checknumber(L, arg + 2);
251 a_float const b = (a_float)luaL_checknumber(L, arg + 3);
252 lua_pushnumber(L, (lua_Number)a_mf_s(x, a, b));
253 return 1;
255 static int liba_mf_s(lua_State *L)
257 return liba_mf_s_(L, 0);
260 /***
261 z-shaped membership function
262 @tparam number x input value for which to compute membership value.
263 @tparam number a defines its shoulder.
264 @tparam number b defines its foot.
265 @treturn number membership value.
266 @function z
268 static int liba_mf_z_(lua_State *L, int arg)
270 a_float const x = (a_float)luaL_checknumber(L, arg + 1);
271 a_float const a = (a_float)luaL_checknumber(L, arg + 2);
272 a_float const b = (a_float)luaL_checknumber(L, arg + 3);
273 lua_pushnumber(L, (lua_Number)a_mf_z(x, a, b));
274 return 1;
276 static int liba_mf_z(lua_State *L)
278 return liba_mf_z_(L, 0);
281 /***
282 z-shaped membership function
283 @tparam number x input value for which to compute membership value.
284 @tparam number a defines its left foot.
285 @tparam number b defines its left shoulder.
286 @tparam number c defines its right shoulder.
287 @tparam number d defines its right foot.
288 @treturn number membership value.
289 @function pi
291 static int liba_mf_pi_(lua_State *L, int arg)
293 a_float const x = (a_float)luaL_checknumber(L, arg + 1);
294 a_float const a = (a_float)luaL_checknumber(L, arg + 2);
295 a_float const b = (a_float)luaL_checknumber(L, arg + 3);
296 a_float const c = (a_float)luaL_checknumber(L, arg + 4);
297 a_float const d = (a_float)luaL_checknumber(L, arg + 4);
298 lua_pushnumber(L, (lua_Number)a_mf_pi(x, a, b, c, d));
299 return 1;
301 static int liba_mf_pi(lua_State *L)
303 return liba_mf_pi_(L, 0);
306 /***
307 membership function
308 @tparam int e type for membership function
309 @tparam number x input value for which to compute membership value.
310 @tparam number ... is an array that stores parameters.
311 @treturn number membership value.
312 @function mf
314 static int liba_mf_mf(lua_State *L)
316 lua_Integer const e = luaL_checkinteger(L, 1);
317 switch (e)
319 case A_MF_PI:
320 return liba_mf_pi_(L, 1);
321 case A_MF_Z:
322 return liba_mf_z_(L, 1);
323 case A_MF_S:
324 return liba_mf_s_(L, 1);
325 case A_MF_LINZ:
326 return liba_mf_linz_(L, 1);
327 case A_MF_LINS:
328 return liba_mf_lins_(L, 1);
329 case A_MF_TRI:
330 return liba_mf_tri_(L, 1);
331 case A_MF_TRAP:
332 return liba_mf_trap_(L, 1);
333 case A_MF_PSIG:
334 return liba_mf_psig_(L, 1);
335 case A_MF_DSIG:
336 return liba_mf_dsig_(L, 1);
337 case A_MF_SIG:
338 return liba_mf_sig_(L, 1);
339 case A_MF_GBELL:
340 return liba_mf_gbell_(L, 1);
341 case A_MF_GAUSS2:
342 return liba_mf_gauss2_(L, 1);
343 case A_MF_GAUSS:
344 return liba_mf_gauss_(L, 1);
345 case A_MF_NUL:
346 default:
347 return 0;
351 static int liba_mf_(lua_State *L)
353 lua_pushcfunction(L, liba_mf_mf);
354 lua_replace(L, 1);
355 lua_call(L, lua_gettop(L) - 1, 1);
356 return 1;
359 int luaopen_liba_mf(lua_State *L)
361 /***
362 enumeration for membership function
363 @field NUL none
364 @field GAUSS gaussian membership function
365 @field GAUSS2 gaussian combination membership function
366 @field GBELL generalized bell-shaped membership function
367 @field SIG sigmoidal membership function
368 @field DSIG difference between two sigmoidal membership functions
369 @field PSIG product of two sigmoidal membership functions
370 @field TRAP trapezoidal membership function
371 @field TRI triangular membership function
372 @field LINS linear s-shaped saturation membership function
373 @field LINZ linear z-shaped saturation membership function
374 @field S s-shaped membership function
375 @field Z z-shaped membership function
376 @field PI pi-shaped membership function
377 @table mf
379 static lua_int const enums[] = {
380 {"NUL", A_MF_NUL},
381 {"GAUSS", A_MF_GAUSS},
382 {"GAUSS2", A_MF_GAUSS2},
383 {"GBELL", A_MF_GBELL},
384 {"SIG", A_MF_SIG},
385 {"DSIG", A_MF_DSIG},
386 {"PSIG", A_MF_PSIG},
387 {"TRAP", A_MF_TRAP},
388 {"TRI", A_MF_TRI},
389 {"LINS", A_MF_LINS},
390 {"LINZ", A_MF_LINZ},
391 {"S", A_MF_S},
392 {"Z", A_MF_Z},
393 {"PI", A_MF_PI},
395 static lua_fun const funcs[] = {
396 {"gauss", liba_mf_gauss},
397 {"gauss2", liba_mf_gauss2},
398 {"gbell", liba_mf_gbell},
399 {"sig", liba_mf_sig},
400 {"dsig", liba_mf_dsig},
401 {"psig", liba_mf_psig},
402 {"trap", liba_mf_trap},
403 {"tri", liba_mf_tri},
404 {"lins", liba_mf_lins},
405 {"linz", liba_mf_linz},
406 {"s", liba_mf_s},
407 {"z", liba_mf_z},
408 {"pi", liba_mf_pi},
409 {"mf", liba_mf_mf},
411 lua_createtable(L, 0, A_LEN(enums) + A_LEN(funcs));
412 lua_int_reg(L, -1, enums, A_LEN(enums));
413 lua_fun_reg(L, -1, funcs, A_LEN(funcs));
414 lua_createtable(L, 0, 1);
415 lua_fun_set(L, -1, "__call", liba_mf_);
416 lua_setmetatable(L, -2);
417 return 1;