From 83006367aeec856bf8e59231c78df5b1802e3138 Mon Sep 17 00:00:00 2001 From: Determinant Date: Sun, 17 May 2015 23:40:25 +0800 Subject: add addition and multiplication for cumatrix --- matrix/generic/matrix.c | 26 +++++++++++++++++--------- matrix/generic/matrix.h | 5 +++++ 2 files changed, 22 insertions(+), 9 deletions(-) (limited to 'matrix/generic') diff --git a/matrix/generic/matrix.c b/matrix/generic/matrix.c index 29919d8..9ced397 100644 --- a/matrix/generic/matrix.c +++ b/matrix/generic/matrix.c @@ -1,4 +1,4 @@ -#ifdef MATRIX_GENERIC +#ifdef NERV_GENERIC_MATRIX #include "../../common.h" #include "matrix.h" @@ -14,17 +14,22 @@ void nerv_float_matrix_(data_retain)(Matrix *self) { (*self->data_ref)++; } -int nerv_float_matrix_(new)(lua_State *L) { +Matrix *nerv_float_matrix_(new_)(long nrow, long ncol) { Matrix *self = (Matrix *)malloc(sizeof(Matrix)); - self->nrow = luaL_checkinteger(L, 1); - self->ncol = luaL_checkinteger(L, 2); + self->nrow = nrow; + self->ncol = ncol; self->nmax = self->nrow * self->ncol; - self->stride = MATRIX_DATA_STRIDE(self->ncol); - self->data.f = MATRIX_DATA_ALLOC(self->stride * self->nrow); + MATRIX_DATA_ALLOC(&self->data.f, &self->stride, sizeof(float) * self->ncol, self->nrow); self->data_ref = (long *)malloc(sizeof(long)); *self->data_ref = 0; nerv_float_matrix_(data_retain)(self); - luaT_pushudata(L, self, nerv_float_matrix_(tname)); + return self; +} + +int nerv_float_matrix_(new)(lua_State *L) { + luaT_pushudata(L, nerv_float_matrix_(new_)(luaL_checkinteger(L, 1), + luaL_checkinteger(L, 2)), + nerv_float_matrix_(tname)); return 1; } @@ -58,7 +63,7 @@ static int nerv_float_matrix_(newindex)(lua_State *L) { { if (idx < 0 || idx >= self->ncol) nerv_error(L, "index must be within range [0, %d)", self->ncol); - self->data.f[idx] = luaL_checknumber(L, 3); + MATRIX_DATA_WRITE(self->data.f, idx, luaL_checknumber(L, 3)); } else nerv_error(L, "cannot assign a scalar to row vector"); @@ -82,7 +87,7 @@ static int nerv_float_matrix_(index)(lua_State *L) { { if (idx < 0 || idx >= self->ncol) nerv_error(L, "index must be within range [0, %d)", self->ncol); - lua_pushnumber(L, self->data.f[idx]); + lua_pushnumber(L, MATRIX_DATA_READ(self->data.f, idx)); } else { @@ -127,6 +132,9 @@ void nerv_float_matrix_(init)(lua_State *L) { luaT_newmetatable(L, nerv_float_matrix_(tname), nerv_matrix_tname, nerv_float_matrix_(new), nerv_float_matrix_(destroy), NULL); luaL_register(L, NULL, nerv_float_matrix_(methods)); +#ifdef MATRIX_INIT + MATRIX_INIT(L); +#endif lua_pop(L, 1); } #endif diff --git a/matrix/generic/matrix.h b/matrix/generic/matrix.h index d02b56e..655ff3d 100644 --- a/matrix/generic/matrix.h +++ b/matrix/generic/matrix.h @@ -1,3 +1,6 @@ +#ifndef NERV_GENERIC_MATRIX_H +#define NERV_GENERIC_MATRIX_H + typedef struct Matrix { long stride; /* size of a row */ long ncol, nrow, nmax; /* dimension of the matrix */ @@ -7,3 +10,5 @@ typedef struct Matrix { } data; /* pointer to actual storage */ long *data_ref; } Matrix; + +#endif -- cgit v1.2.3-70-g09d2