-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathutil.c
93 lines (77 loc) · 2.12 KB
/
util.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include "TH.h"
#include "luaT.h"
static int svm_spdot(lua_State *L)
{
THFloatTensor *tdense = luaT_checkudata(L,1,"torch.FloatTensor");
THIntTensor *indices;
if lua_isnil(L,2)
{
indices = NULL;
}
else
{
indices = luaT_checkudata(L,2,"torch.IntTensor");
}
THFloatTensor *tsparse = luaT_checkudata(L,3,"torch.FloatTensor");
luaL_argcheck(L,tdense->nDimension == 1, 1, "Dense Matrix is expected to 1D");
luaL_argcheck(L,!indices || indices->nDimension == 1, 2, "Index tensor is expected to 1D");
luaL_argcheck(L,tsparse->nDimension == 1, 3, "Sparse value tensor is expected to 1D");
if (!indices)
{
lua_pushnumber(L,(double)THFloatTensor_dot(tdense,tsparse));
return 1;
}
float *dense_data = THFloatTensor_data(tdense);
float *sparse_data = THFloatTensor_data(tsparse);
int *indices_data = THIntTensor_data(indices);
long i;
float res = 0;
for (i=0; i< indices->size[0]; i++)
{
res += sparse_data[i]*dense_data[indices_data[i]-1];
}
lua_pushnumber(L,(double)res);
return 1;
}
static int svm_spadd(lua_State *L)
{
THFloatTensor *tdense = luaT_checkudata(L,1,"torch.FloatTensor");
float c = (float)lua_tonumber(L,2);
THIntTensor *indices;
if (lua_isnil(L,3))
{
indices = NULL;
}
else
{
indices = luaT_checkudata(L,3,"torch.IntTensor");
}
THFloatTensor *tsparse = luaT_checkudata(L,4,"torch.FloatTensor");
luaL_argcheck(L,tdense->nDimension == 1, 1, "Dense Matrix is expected to 1D");
luaL_argcheck(L,!indices||indices->nDimension == 1, 3, "Index tensor is expected to 1D");
luaL_argcheck(L,tsparse->nDimension == 1, 4, "Sparse value tensor is expected to 1D");
if(!indices)
{
THFloatTensor_cadd(tdense,tdense,c,tsparse);
return 0;
}
float *dense_data = THFloatTensor_data(tdense);
float *sparse_data = THFloatTensor_data(tsparse);
int *indices_data = THIntTensor_data(indices);
long i;
for (i=0; i< indices->size[0]; i++)
{
dense_data[indices_data[i]-1] += c*sparse_data[i];
}
return 0;
}
static const struct luaL_Reg svm_util__ [] = {
{"spdot", svm_spdot},
{"spadd", svm_spadd},
{NULL, NULL}
};
int libsvm_util_init(lua_State *L)
{
luaL_register(L, "svm", svm_util__);
return 1;
}