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 singular_globals_proxy(object): |
---|
28 | def __mycbm(self,name,*args): |
---|
29 | l=list2arg_list(args) |
---|
30 | prepare_ring(args) |
---|
31 | res= _Singular.cbm(name,l) |
---|
32 | finish_ring() |
---|
33 | return res |
---|
34 | |
---|
35 | def __getattr__(self,name): |
---|
36 | proc=_Singular.get_idhdl(name) |
---|
37 | if proc.is_zero(): |
---|
38 | if _Singular.is_builtin(name): |
---|
39 | def fun_wrapper(*args): |
---|
40 | return self.__mycbm(name,*args) |
---|
41 | try: |
---|
42 | fun_wrapper.__name__=name |
---|
43 | except: |
---|
44 | pass |
---|
45 | return fun_wrapper |
---|
46 | else: |
---|
47 | raise AttributeError("Global variable " + name + " not present in the Singular interpreter") |
---|
48 | if proc.is_proc(): |
---|
49 | def fun_wrapper(*args): |
---|
50 | |
---|
51 | |
---|
52 | proc=_Singular.get_idhdl(name) |
---|
53 | if not proc.is_proc(): |
---|
54 | proc.print_type() |
---|
55 | raise Exception |
---|
56 | prepare_ring(args) |
---|
57 | l=list2arg_list(args) |
---|
58 | erg= _Singular.call_interpreter_method(proc, l) |
---|
59 | finish_ring() |
---|
60 | return erg |
---|
61 | try: |
---|
62 | fun_wrapper.__name__=name |
---|
63 | except: |
---|
64 | pass |
---|
65 | return fun_wrapper |
---|
66 | else: |
---|
67 | res=_Singular.transfer_to_python(proc) |
---|
68 | if res is None: |
---|
69 | raise AttributeError("Global variable "+name+" has unknown type") |
---|
70 | return res |
---|
71 | def __setattr__(self,name,value): |
---|
72 | id=_Singular.get_idhdl(name) |
---|
73 | if id.is_zero(): |
---|
74 | raise Expception |
---|
75 | else: |
---|
76 | if isinstance(value,list): |
---|
77 | value=list2arg_list(value) |
---|
78 | id.write(value) |
---|
79 | #for compatibility the old name |
---|
80 | global_functions=singular_globals_proxy |
---|
81 | |
---|
82 | |
---|
83 | def find_rings(arglist): |
---|
84 | """FIXME: doesn't handle everything and depth""" |
---|
85 | for item in arglist: |
---|
86 | if isinstance(item,polynomial) or isinstance(item,ideal): |
---|
87 | return [item.ring()] |
---|
88 | return [] |
---|
89 | |
---|
90 | |
---|
91 | oldrings=[] |
---|
92 | def prepare_ring(arglist): |
---|
93 | rl=find_rings(arglist) |
---|
94 | debug_out("rl is" +str(rl)) |
---|
95 | if len(rl)==1: |
---|
96 | r=rl[0] |
---|
97 | oldrings.append(ring()) |
---|
98 | r.set() |
---|
99 | else: |
---|
100 | if len(rl)==0: |
---|
101 | oldrings.append(None) |
---|
102 | else: |
---|
103 | debug_out("Warning to many rings in call") |
---|
104 | oldrings.append(None) |
---|
105 | |
---|
106 | def finish_ring(): |
---|
107 | r=oldrings.pop() |
---|
108 | if r: |
---|
109 | r.set() |
---|
110 | #optimize(prepare_ring) |
---|
111 | #optimize(mycbm) |
---|
112 | #optimize(finish_ring) |
---|