-
Notifications
You must be signed in to change notification settings - Fork 298
Expand file tree
/
Copy pathtinyexpy.pyx
More file actions
82 lines (61 loc) · 2.45 KB
/
tinyexpy.pyx
File metadata and controls
82 lines (61 loc) · 2.45 KB
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
cimport tinyexpy
from libc cimport stdlib
from typing import List
DEF SIZE_VARS = 100
cdef class Tinyexpy:
@staticmethod
cdef double add(double a, double b):
return a + b
def py_te_interp(self, expression: str, error: int) -> float:
py_expression = expression.encode("utf8")
cdef int c_error = error
cdef char *c_expression = py_expression
cdef double res = te_interp(c_expression, &c_error)
print(f"Evaluating:\n\t{c_expression.decode('utf8')}\nResult:\n\t{res}\n")
return res
def py_te_interp_customize(self, expression: str, py_vars: List[dict]):
cdef char *te_variable_name
cdef char *c_expression
cdef int var_count = len(py_vars)
cdef int c_err
cdef te_expr *n
cdef double[SIZE_VARS] x
cdef double r
cdef te_variable c_te_variable
cdef te_variable[SIZE_VARS] vars
py_expression = expression.encode("utf8")
c_expression = py_expression
for i, each_py_vars in enumerate(py_vars):
each_value = each_py_vars.get("value")
if isinstance(each_value, float) or isinstance(each_value, int):
each_name = each_py_vars.get("name").encode("utf8")
te_variable_name = each_name
x[i] = float(each_value)
c_te_variable = te_variable(te_variable_name, &x[i])
vars[i] = c_te_variable
n = te_compile(c_expression, vars, var_count, &c_err)
if n != NULL:
# te_print(n)
r = te_eval(n)
te_free(n)
print(f"Evaluating:\n\t{c_expression.decode('utf8')}\nResult:\n\t{r}\n")
return r
def py_te_interp_my_func(self, func_name: str, expression: str):
cdef te_variable[1] vars
cdef char *c_expression
cdef char *myfunc
cdef int c_err
cdef te_expr *n
cdef double r
py_expression = expression.encode("utf8")
py_func_name = func_name.encode("utf8")
c_expression = py_expression
myfunc = py_func_name
vars[0] = te_variable(name=myfunc, address=<double (*)(double, double)>self.add, type=TE_FUNCTION2)
n = te_compile(c_expression, vars, 1, &c_err)
if n != NULL:
# te_print(n)
r = te_eval(n)
te_free(n)
print(f"Evaluating:\n\t{c_expression.decode('utf8')}\nResult:\n\t{r}\n")
return r