diff options
author | Determinant <[email protected]> | 2015-05-25 16:07:34 +0800 |
---|---|---|
committer | Determinant <[email protected]> | 2015-05-25 16:07:34 +0800 |
commit | be922fa355fe385b45a2720d48eaf0c809e1874a (patch) | |
tree | 6ec8747f0463ffb36445d7cadec567c00b8fdbdf /examples | |
parent | 3036cc0f7597d72a36b1923ab5e0c56a4490be31 (diff) |
move example files to a dedicated directory
Diffstat (limited to 'examples')
-rw-r--r-- | examples/cumatrix_example.lua | 23 | ||||
-rw-r--r-- | examples/mmatrix_example.lua | 16 | ||||
-rw-r--r-- | examples/oop_example.c | 91 | ||||
-rw-r--r-- | examples/oop_example.lua | 7 |
4 files changed, 137 insertions, 0 deletions
diff --git a/examples/cumatrix_example.lua b/examples/cumatrix_example.lua new file mode 100644 index 0000000..f8235eb --- /dev/null +++ b/examples/cumatrix_example.lua @@ -0,0 +1,23 @@ +m = 10 +n = 10 +fm = nerv.CuMatrixFloat(m, n) +dm = nerv.CuMatrixDouble(m, n) +for i = 0, m - 1 do + for j = 0, n - 1 do + -- local t = math.random(10) + t = i / (j + 1) + fm[i][j] = t + dm[i][j] = t + end +end +print(fm) +fs = fm:softmax() +-- print(fs) +print(dm) +ds = dm:softmax() +-- print(ds) +print(fs) +print(fs + fs) +print(ds + ds) +print(fs - fs) +print(ds - ds) diff --git a/examples/mmatrix_example.lua b/examples/mmatrix_example.lua new file mode 100644 index 0000000..679fde3 --- /dev/null +++ b/examples/mmatrix_example.lua @@ -0,0 +1,16 @@ +m = 10 +n = 10 +fm = nerv.MMatrixFloat(m, n) +dm = nerv.MMatrixDouble(m, n) +for i = 0, m - 1 do + for j = 0, n - 1 do + -- local t = math.random(10) + t = i / (j + 1) + fm[i][j] = t + dm[i][j] = t + end +end +print(fm) +-- print(fm:softmax()) +print(dm) +-- print(dm:softmax()) diff --git a/examples/oop_example.c b/examples/oop_example.c new file mode 100644 index 0000000..e9a4ffe --- /dev/null +++ b/examples/oop_example.c @@ -0,0 +1,91 @@ +#include <math.h> +#include "lua.h" +#include "lauxlib.h" +#include "lualib.h" +#include "luaT/luaT.h" +#include <stdio.h> +#include <stdlib.h> + +const char *point_tname = "nerv.Point"; + +typedef struct { + double x, y; + int arr[100]; +} Point; + +static int point_get_sinx (lua_State *L) { + Point *p = luaT_checkudata(L, 1, point_tname); + lua_pushnumber(L, sin(p->x)); + return 1; +} + +static int point_set_x (lua_State *L) { + Point *p = luaT_checkudata(L, 1, point_tname); + p->x = luaL_checknumber(L, 2); + return 0; +} + + +static int point_get_y(lua_State *L) { + Point *p = luaT_checkudata(L, 1, point_tname); + lua_pushnumber(L, sin(p->x)); + return 1; +} + +static int point_newindex(lua_State *L) { + Point *p = luaT_checkudata(L, 1, point_tname); + if (lua_isnumber(L, 2)) + { + int d = luaL_checkinteger(L, 2); + double v = luaL_checknumber(L, 3); + if (0 <= d && d < 100) + p->arr[d] = v; + lua_pushboolean(L, 1); + return 1; + } + else + { + lua_pushboolean(L, 0); + return 1; + } +} + +static int point_index(lua_State *L) { + Point *p = luaT_checkudata(L, 1, point_tname); + if (lua_isnumber(L, 2)) + { + int d = luaL_checkinteger(L, 2); + if (0 <= d && d < 100) + lua_pushnumber(L, p->arr[d]); + lua_pushboolean(L, 1); + return 2; + } + else + { + lua_pushboolean(L, 0); + return 1; + } +} + +int point_new(lua_State *L) { + Point *self = (Point *)malloc(sizeof(Point)); + self->x = 0; + self->y = 0; + luaT_pushudata(L, self, point_tname); + return 1; +} + +static const luaL_Reg point[] = { + {"get_sinx", point_get_sinx}, + {"set_x", point_set_x}, + {"get_y", point_get_y}, + {"__index__", point_index}, + {"__newindex__", point_newindex}, + {NULL, NULL} +}; + +void nerv_point_init(lua_State *L) { + luaT_newmetatable(L, "nerv.Point", NULL, point_new, NULL, NULL); + luaL_register(L, NULL, point); + lua_pop(L, 1); +} diff --git a/examples/oop_example.lua b/examples/oop_example.lua new file mode 100644 index 0000000..45da36e --- /dev/null +++ b/examples/oop_example.lua @@ -0,0 +1,7 @@ +a = nerv.Point() +print(a:get_sinx()) +a:set_x(3.14) +print(a:get_sinx()) +print(a[2]) +a[2] = 3 +print(a[2]) |