From be922fa355fe385b45a2720d48eaf0c809e1874a Mon Sep 17 00:00:00 2001
From: Determinant <ted.sybil@gmail.com>
Date: Mon, 25 May 2015 16:07:34 +0800
Subject: move example files to a dedicated directory

---
 examples/cumatrix_example.lua | 23 +++++++++++
 examples/mmatrix_example.lua  | 16 ++++++++
 examples/oop_example.c        | 91 +++++++++++++++++++++++++++++++++++++++++++
 examples/oop_example.lua      |  7 ++++
 4 files changed, 137 insertions(+)
 create mode 100644 examples/cumatrix_example.lua
 create mode 100644 examples/mmatrix_example.lua
 create mode 100644 examples/oop_example.c
 create mode 100644 examples/oop_example.lua

(limited to 'examples')

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])
-- 
cgit v1.2.3-70-g09d2