1 | from Singular import * |
---|
2 | #from _Singular import * |
---|
3 | import _Singular |
---|
4 | try: |
---|
5 | import psyco |
---|
6 | def optimize(f): |
---|
7 | psyco.bind(f) |
---|
8 | except: |
---|
9 | def optimize(f): |
---|
10 | pass |
---|
11 | def debug_out(s): |
---|
12 | pass |
---|
13 | #print s |
---|
14 | #def build_arg_list(*args) |
---|
15 | def list2arg_list(args): |
---|
16 | l=_Singular.i_arg_list() |
---|
17 | for a in args: |
---|
18 | if isinstance(a,list): |
---|
19 | l.append(list2arg_list(a)) |
---|
20 | # at=i_arg_list() |
---|
21 | # for a2 in a: |
---|
22 | # at.append(a2) |
---|
23 | # l.append(at) |
---|
24 | else: |
---|
25 | l.append(a) |
---|
26 | return l |
---|
27 | class SingularGlobalsProxy(object): |
---|
28 | """The Most important class |
---|
29 | singular=singular_globals_proxy() |
---|
30 | for some ideal |
---|
31 | singular.groebner(i) |
---|
32 | singular.std(i) |
---|
33 | in general all interpreter and kernel functions |
---|
34 | with supported data type work |
---|
35 | for some global poly variable p in the interpreter and p in python |
---|
36 | singular.p=p |
---|
37 | p=singular.p |
---|
38 | this is always a copy as this object is only some magic proxy""" |
---|
39 | def __mycbm(self,name,*args): |
---|
40 | l=list2arg_list(args) |
---|
41 | prepare_ring(args) |
---|
42 | res= _Singular.cbm(name,l) |
---|
43 | finish_ring() |
---|
44 | return res |
---|
45 | |
---|
46 | def __getattr__(self,name): |
---|
47 | proc=_Singular.get_idhdl(name) |
---|
48 | if proc.is_zero(): |
---|
49 | if _Singular.is_builtin(name): |
---|
50 | def fun_wrapper(*args): |
---|
51 | return self.__mycbm(name,*args) |
---|
52 | try: |
---|
53 | fun_wrapper.__name__=name |
---|
54 | except: |
---|
55 | pass |
---|
56 | return fun_wrapper |
---|
57 | else: |
---|
58 | raise AttributeError("Global variable " + name + " not present in the Singular interpreter") |
---|
59 | if proc.is_proc(): |
---|
60 | def fun_wrapper(*args): |
---|
61 | |
---|
62 | |
---|
63 | proc=_Singular.get_idhdl(name) |
---|
64 | if not proc.is_proc(): |
---|
65 | proc.print_type() |
---|
66 | raise Exception |
---|
67 | prepare_ring(args) |
---|
68 | l=list2arg_list(args) |
---|
69 | erg= _Singular.call_interpreter_method(proc, l) |
---|
70 | finish_ring() |
---|
71 | return erg |
---|
72 | try: |
---|
73 | fun_wrapper.__name__=name |
---|
74 | except: |
---|
75 | pass |
---|
76 | return fun_wrapper |
---|
77 | else: |
---|
78 | res=_Singular.transfer_to_python(proc) |
---|
79 | if res is None: |
---|
80 | raise AttributeError("Global variable "+name+" has unknown type") |
---|
81 | return res |
---|
82 | def __setattr__(self,name,value): |
---|
83 | id=_Singular.get_idhdl(name) |
---|
84 | if id.is_zero(): |
---|
85 | raise Expception |
---|
86 | else: |
---|
87 | if isinstance(value,list): |
---|
88 | value=list2arg_list(value) |
---|
89 | id.write(value) |
---|
90 | #for compatibility the old name |
---|
91 | #global_functions=singular_globals_proxy |
---|
92 | |
---|
93 | |
---|
94 | def find_rings(arglist): |
---|
95 | """FIXME: doesn't handle everything and depth""" |
---|
96 | for item in arglist: |
---|
97 | if isinstance(item,Polynomial) or isinstance(item,Ideal) or isinstance(item, Number) or isinstance(item, Vector): |
---|
98 | return [item.ring()] |
---|
99 | return [] |
---|
100 | |
---|
101 | |
---|
102 | oldrings=[] |
---|
103 | def prepare_ring(arglist): |
---|
104 | rl=find_rings(arglist) |
---|
105 | debug_out("rl is" +str(rl)) |
---|
106 | if len(rl)==1: |
---|
107 | r=rl[0] |
---|
108 | oldrings.append(Ring()) |
---|
109 | r.set() |
---|
110 | else: |
---|
111 | if len(rl)==0: |
---|
112 | oldrings.append(None) |
---|
113 | else: |
---|
114 | debug_out("Warning to many rings in call") |
---|
115 | oldrings.append(None) |
---|
116 | |
---|
117 | def finish_ring(): |
---|
118 | r=oldrings.pop() |
---|
119 | if r: |
---|
120 | r.set() |
---|
121 | #optimize(prepare_ring) |
---|
122 | #optimize(mycbm) |
---|
123 | #optimize(finish_ring) |
---|