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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
|
'''
gMathLib, mathematic expressions handling library
# Python bindings
Author : Guillaume Seguin
Email : guillaume@segu.in
Copyright (c) 2007 Guillaume Seguin <guillaume@segu.in>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
'''
ctypedef unsigned int bool
cdef extern from 'string.h':
ctypedef int size_t
cdef extern char *strdup (char *s)
cdef extern void free (void *f)
ctypedef char gchar
cdef extern from "glib.h":
ctypedef struct GData
cdef extern from "Python.h":
object PyString_FromStringAndSize (char *string, int size)
cdef struct GMathContext
cdef struct GMathModule
cdef struct GMathFormula
cdef struct GMathTemplate
cdef struct GMathSymbol
cdef struct GMathContext:
GData *templates
GData *input_modules
GData *output_modules
cdef struct GMathFormula:
GMathContext *context
GMathSymbol *root
cdef struct GMathTemplate:
bool has_value
unsigned int children_count
cdef struct GMathSymbol:
gchar *type
gchar *value
GMathSymbol **children
GMathSymbol *parent
GMathTemplate *template
GMathContext *context
cdef extern GMathContext* gmathcontext_new ()
cdef extern void gmathcontext_free (GMathContext *context)
cdef extern GMathModule* gmathcontext_get_input_module (GMathContext *context,
gchar *name)
cdef extern GMathModule* gmathcontext_get_output_module (GMathContext *context,
gchar *name)
cdef extern bool gmathcontext_load_input_module (GMathContext *context,
gchar *name)
cdef extern bool gmathcontext_load_output_module (GMathContext *context,
gchar *name)
cdef extern GMathFormula* gmathcontext_import (GMathContext *context,
gchar *module,
gchar *data)
cdef extern GMathFormula* gmathformula_new (GMathContext *context)
cdef extern void gmathformula_free (GMathFormula *formula)
cdef extern void gmathformula_set_root (GMathFormula *formula,
GMathSymbol *symbol)
cdef extern size_t gmathformula_export (GMathFormula *formula, gchar *module,
gchar **data)
cdef extern GMathSymbol* gmathsymbol_new (GMathContext *context, gchar *type)
cdef extern void gmathsymbol_free (GMathSymbol *symbol)
cdef extern void gmathsymbol_set_value (GMathSymbol *symbol, gchar *value)
cdef extern void gmathsymbol_set_child (GMathSymbol *symbol,
GMathSymbol *child, int index)
cdef extern GMathSymbol* gmathsymbol_get_child (GMathSymbol *symbol,
int index)
cdef class Symbol:
cdef GMathSymbol *csymbol
cdef object context
cdef object children
def __new__ (self, context, type = "", symbol = None):
cdef GMathSymbol *child
self.context = context
if symbol:
self.csymbol = <GMathSymbol *> symbol
elif type:
self.csymbol = \
gmathsymbol_new (<GMathContext *> self.context.c_context, type)
self.children = []
for i in range (0, self.csymbol.template.children_count):
child = gmathsymbol_get_child (self.csymbol, i)
if not child:
self.children.append (None)
continue
self.children.append (Symbol (context, symbol = <object> child))
def set_child (self, child, index):
if index >= self.csymbol.template.children_count:
return
self.children[index] = child
gmathsymbol_set_child (self.csymbol, <GMathSymbol *> child.c_symbol,
index)
def free (self):
gmathsymbol_free (self.csymbol)
self.csymbol = NULL
def __dealloc__ (self):
self.free ()
property value:
def __get__ (self):
return str (<object> self.csymbol.value)
def __set__ (self, value):
gmathsymbol_set_value (self.csymbol, value)
property c_symbol:
def __get__ (self):
return <object> self.csymbol
cdef class Formula:
cdef GMathFormula *cformula
cdef object context
cdef object root
def __new__ (self, context, formula = None):
self.context = context
self.context.register_formula (self)
if formula:
self.cformula = <GMathFormula *> formula
else:
self.cformula = \
gmathformula_new (<GMathContext *> self.context.c_context)
if self.cformula.root != NULL:
self.root = Symbol (<object> self.cformula.root)
else:
self.root = None
def free (self):
if self.root:
self.root.free ()
self.set_root (None)
gmathformula_free (self.cformula)
def __dealloc__ (self):
self.free ()
def set_root (self, root):
if root:
gmathformula_set_root (self.cformula, <GMathSymbol *> root.c_symbol)
else:
gmathformula_set_root (self.cformula, <GMathSymbol *> NULL)
self.root = root
def export (self):
cdef gchar *cdata
cdef size_t size
if not self.context.output_module:
print "Error: please load or select an output module."
return None
size = gmathformula_export (self.cformula, self.context.output_module,
&cdata)
if not size:
return None
data = PyString_FromStringAndSize (cdata, size)
free (cdata)
return data
property c_formula:
def __get__ (self):
return <object> self.cformula
cdef class Context:
cdef GMathContext *ccontext
cdef object _input_module
cdef object _output_module
cdef object formulas
def __new__ (self):
self.ccontext = gmathcontext_new ()
self.input_module = None
self.output_module = None
self.formulas = []
def __dealloc__ (self):
for formula in self.formulas:
formula.free ()
gmathcontext_free (self.ccontext)
def load_input_module (self, name):
gmathcontext_load_input_module (self.ccontext, name)
def load_output_module (self, name):
gmathcontext_load_output_module (self.ccontext, name)
def import_from_data (self, data):
if not self.input_module:
print "Error: please load or select an input module."
return None
cdef GMathFormula *formula
formula = gmathcontext_import (self.ccontext, self.input_module, data)
if formula == NULL:
return None
return Formula (self, <object> formula)
def register_formula (self, formula):
self.formulas.append (formula)
def Formula (self):
return Formula (self)
def Symbol (self, type = "", symbol = None):
return Symbol (self, type, symbol)
property input_module:
def __get__ (self):
return self._input_module
def __set__ (self, name):
if name and not gmathcontext_get_input_module (self.ccontext,
name):
self.load_input_module (name)
if not gmathcontext_get_input_module (self.ccontext, name):
print "Error: %s input module not found." % name
return
self._input_module = name
property output_module:
def __get__ (self):
return self._output_module
def __set__ (self, name):
if name and not gmathcontext_get_output_module (self.ccontext,
name):
self.load_output_module (name)
if not gmathcontext_get_output_module (self.ccontext, name):
print "Error: %s output module not found." % name
return
self._output_module = name
property c_context:
def __get__ (self):
return <object> self.ccontext
|