summaryrefslogtreecommitdiff
path: root/bindings/python/pygmathlib/gmathlib.pyx
blob: a16807a1ba6450edae5df6619b459aaaf97a8d1d (plain)
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
'''
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 int guint
cdef extern from "glib.h":
    ctypedef struct GHashTable
    ctypedef struct GSList:
        void    *data
        GSList  *next
    cdef extern void g_slist_free (GSList *list)

cdef extern from "Python.h":
    object PyString_FromStringAndSize (char *string, int size)

cdef struct GMathContext
cdef struct GMathInputModule
cdef struct GMathOutputModule
cdef struct GMathFormula
cdef struct GMathTemplate
cdef struct GMathSymbol

cdef struct GMathContext:
    GHashTable      *templates
    GHashTable      *input_modules
    GHashTable      *output_modules

cdef struct GMathFormula:
    GMathContext    *context
    GMathSymbol     *root

cdef struct GMathTemplate:
    bool            has_value
    unsigned int    children_count

cdef struct GMathSymbol:
    char            *type
    char            *value

    void            **children

    void            *parent

    GMathTemplate   *template
    GMathContext    *context

cdef struct GMathArray:
    guint           rows
    guint           columns

    char            *alignment

    GMathSymbol     **children

    GMathSymbol     *parent

cdef enum GMathSupportStatus:
    GMathSupportError    = -2
    GMathNoIntrospection = -1
    GMathUnsupported     = 0
    GMathSupported       = 1

cdef extern GMathContext* gmathcontext_new ()
cdef extern void gmathcontext_free (GMathContext *context)
cdef extern GSList* gmathcontext_get_template_names (GMathContext *context)
cdef extern GMathSupportStatus gmathcontext_get_template_supported (
                   GMathContext *context, char *module, char *name)
cdef extern GMathInputModule* gmathcontext_get_input_module ( \
                GMathContext *context, char *name)
cdef extern GMathOutputModule* gmathcontext_get_output_module ( \
                GMathContext *context, char *name)
cdef extern bool gmathcontext_load_input_module (GMathContext *context,
                                                 char *name)
cdef extern bool gmathcontext_load_output_module (GMathContext *context,
                                                  char *name)

cdef extern GMathFormula* gmathcontext_import (GMathContext *context,
                                               char *module,
                                               char *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, char *module,
                                        char **data)

cdef extern GMathSymbol* gmathsymbol_new (GMathContext *context, char *type)
cdef extern GMathSymbol* gmathsymbol_new_array (GMathContext *context,
                                                guint rows, guint columns)
cdef extern bool gmathsymbol_set_array_child (GMathSymbol* array_sym,
                                              GMathSymbol* new_child,
                                              guint row, guint column)
cdef extern bool gmathsymbol_set_array_alignment (GMathSymbol* array_sym,
                                                  char alignment,
                                                  guint column)
cdef extern void gmathsymbol_free (GMathSymbol *symbol)
cdef extern void gmathsymbol_set_value (GMathSymbol *symbol, char *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, c_symbol = False,
                 copy = None):
        cdef GMathSymbol  *child
        cdef GMathContext *ccontext
        self.context = context
        ccontext = <GMathContext *> context.c_context
        if c_symbol:
            self.csymbol = <GMathSymbol *> symbol
        elif type:
            self.csymbol = gmathsymbol_new (ccontext, type)

        if copy:
            self.children = [None] * len (copy.children_)
            for i in range (0, len (copy.children_)):
                if copy.children_[i]:
                    self.set_child (copy.children_[i].copy (deep = True), i)
            return

        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,
                                          c_symbol = True))

    def set_child (self, child, index):
        cdef GMathSymbol *cchild
        if index >= self.csymbol.template.children_count:
            return
        self.children[index] = child
        if child:
            cchild = <GMathSymbol *> child.c_symbol
        else:
            cchild = NULL
        gmathsymbol_set_child (self.csymbol, cchild, index)

    def copy (self, deep = False):
        if deep:
            copy = self
        else:
            copy = None
        symbol = Symbol (self.context, type = self.type, copy = copy)
        symbol.value = self.value
        return symbol

    def free (self):
        for i in range (0, len (self.children)):
            child = self.children[i]
            if child:
                child.free ()
                self.set_child (None, i)
        gmathsymbol_free (self.csymbol)
        self.csymbol = NULL

    def __dealloc__ (self):
        self.free ()

    property type:
        def __get__ (self):
            if self.csymbol == NULL or self.csymbol.type == NULL:
                return ""
            return self.csymbol.type

    property value:
        def __get__ (self):
            if self.csymbol == NULL or self.csymbol.value == NULL:
                return ""
            return self.csymbol.value

        def __set__ (self, value):
            gmathsymbol_set_value (self.csymbol, value)

    property children_:
        def __get__ (self):
            return self.children

    property c_symbol:
        def __get__ (self):
            if self.csymbol == NULL:
                return None
            return <object> self.csymbol

cdef class Array:
    cdef GMathSymbol    *csymbol
    cdef object         context
    cdef object         children
    cdef object         _rows
    cdef object         _columns

    def __new__ (self, context, rows, columns):
        cdef GMathSymbol *child
        cdef GMathContext *ccontext
        self.context = context
        ccontext = <GMathContext *> context.c_context
        self.csymbol = gmathsymbol_new_array (ccontext, rows, columns)

        self._rows = rows
        self._columns = columns

        self.children = [None] * rows * columns

    def set_child (self, child, row, column):
        cdef GMathSymbol *cchild
        if row >= self.rows or column >= self.columns:
            return
        self.children[row * self.columns + column] = child
        if child:
            cchild = <GMathSymbol *> child.c_symbol
        else:
            cchild = NULL
        gmathsymbol_set_array_child (self.csymbol, cchild, row, column)

    def set_alignment (self, alignment, column):
        if column >= self.columns:
            return
        if len (alignment) != 1:
            return
        gmathsymbol_set_array_alignment (self.csymbol, ord (alignment), column)

    def free (self):
        for i in range (0, len (self.children)):
            child = self.children[i]
            if child:
                child.free ()
                column = i % self.columns
                row = int (float (i - column) / self.columns)
                self.set_child (None, row, column)
        gmathsymbol_free (self.csymbol)
        self.csymbol = NULL

    def __dealloc__ (self):
        self.free ()

    property rows:
        def __get__ (self):
            return self._rows

    property columns:
        def __get__ (self):
            return self._columns

    property c_symbol:
        def __get__ (self):
            if self.csymbol == NULL:
                return None
            return <object> self.csymbol

cdef class Formula:
    cdef GMathFormula   *cformula
    cdef object         context
    cdef object         root

    def __new__ (self, context, formula = None, c_formula = False):
        cdef GMathContext *ccontext
        self.context = context
        self.context.register_formula (self)
        ccontext = <GMathContext *> context.c_context
        if c_formula:
            self.cformula = <GMathFormula *> formula
        else:
            self.cformula = gmathformula_new (ccontext)
        if self.cformula.root != NULL:
            self.root = Symbol (self.context,
                                symbol = <object> self.cformula.root,
                                c_symbol = True)
        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):
        cdef GMathSymbol    *croot
        if root:
            croot = <GMathSymbol *> root.c_symbol
        else:
            croot = NULL
        gmathformula_set_root (self.cformula, croot)
        self.root = root

    def export (self):
        cdef char   *cdata
        cdef size_t size
        if not self.context.output_module:
            raise RuntimeError, "no output module selected"
        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):
            if self.cformula == NULL:
                return None
            return <object> self.cformula

cdef class Context:
    cdef GMathContext   *ccontext
    cdef object         _input_module
    cdef object         _output_module
    cdef object         formulas

    def __new__ (self, context = None, c_context = False):
        if c_context:
            self.ccontext = <GMathContext *> context
        else:
            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 get_template_names (self):
        cdef GSList *list, *list0
        list0 = gmathcontext_get_template_names (self.ccontext)
        list = list0
        ret = []
        while list != NULL:
            ret.append (strdup (<char *> list.data))
            free (list.data)
            list = list.next
        g_slist_free (<GSList *> list0)
        return ret

    def get_template_supported (self, name, module = None):
        if not module:
            if not self.output_module:
                raise RuntimeError, "no output module selected"
            module = self.output_module
        s = gmathcontext_get_template_supported (self.ccontext, module, name)
        if s == GMathSupported:
            return True
        elif s == GMathUnsupported:
            return False
        raise RuntimeError, "%s module doesn't support introspection" % module

    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:
            raise RuntimeError, "no input module selected"
        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 Atom (self, value):
        symbol = self.Symbol (type = "atom")
        symbol.value = value
        return symbol

    def Formula (self):
        return Formula (self)

    def Symbol (self, type = "", symbol = None):
        return Symbol (self, type, symbol)

    def Array (self, rows, columns):
        return Array (self, rows, columns)

    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):
                    raise RuntimeError, "%s input module not found." % name
            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):
                    raise RuntimeError, "%s output module not found." % name
            self._output_module = name

    property c_context:
        def __get__ (self):
            if self.ccontext == NULL:
                return None
            return <object> self.ccontext