-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfastsqr.c
74 lines (56 loc) · 1.53 KB
/
fastsqr.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
#include <Python.h>
#include <math.h>
int fpow(int a, int b) {
int res = 1;
while (b > 0) {
if (b & 1)
res = res * a;
a = a * a;
b >>= 1;
}
return res;
}
static PyObject *method_sqr(PyObject *self, PyObject *args) {
int n = 0, exp = 0;
int sqr = 0;
if(!PyArg_ParseTuple(args, "ii", &n, &exp)) {
return NULL;
}
sqr = fpow(n, exp);
return PyLong_FromLong(sqr);
}
static PyObject *method_arrsqr(PyObject *self, PyObject *args) {
PyObject *float_list;
int len;
double *arr;
if (!PyArg_ParseTuple(args, "O", &float_list))
return NULL;
len = PyObject_Length(float_list);
if (len < 0) return NULL;
arr = (double *)malloc(sizeof(double *) * len);
if (arr == NULL) return NULL;
PyObject *sqrs = PyList_New(len);
for(int i = 0;i < len; i++){
PyObject *e;
e = PyList_GetItem(float_list, i);
if (!PyFloat_Check(e)) arr[i] = 0.0;
arr[i] = pow(PyFloat_AsDouble(e),2);
PyList_SetItem(sqrs, i, Py_BuildValue("d", arr[i]));
}
return sqrs;
}
static PyMethodDef Methods[] = {
{"sqr", method_sqr, METH_VARARGS, "fast square computation"},
{"asqr", method_arrsqr, METH_VARARGS, "array element-wise square computation"},
{NULL, NULL, 0, NULL}
};
static struct PyModuleDef tachnet = {
PyModuleDef_HEAD_INIT,
"tachnet",
"Astronomically fast deep learning library",
-1,
Methods
};
PyMODINIT_FUNC PyInit_tachnet(void) {
return PyModule_Create(&tachnet);
}