1 | #include "kernel/mod2.h" // general settings/macros |
---|
2 | |
---|
3 | #ifdef SINGULAR_4_1 |
---|
4 | #include <reporter/reporter.h> // for Print, WerrorS |
---|
5 | #include <coeffs/numbers.h> // nRegister, coeffs.h |
---|
6 | #include <coeffs/rmodulon.h> // ZnmInfo |
---|
7 | #include <coeffs/bigintmat.h> // bigintmat |
---|
8 | |
---|
9 | #include <Singular/blackbox.h> // blackbox type |
---|
10 | #include <Singular/ipshell.h> // IsPrime |
---|
11 | |
---|
12 | #include <Singular/ipid.h> // for SModulFunctions, leftv |
---|
13 | |
---|
14 | #include <Singular/number2.h> |
---|
15 | |
---|
16 | char *crString(coeffs c) |
---|
17 | { |
---|
18 | if (c==NULL) |
---|
19 | { |
---|
20 | return omStrDup("oo"); |
---|
21 | } |
---|
22 | return omStrDup(nCoeffName(c)); |
---|
23 | } |
---|
24 | void crPrint(coeffs c) |
---|
25 | { |
---|
26 | char *s=crString(c); |
---|
27 | PrintS(s); |
---|
28 | omFree(s); |
---|
29 | } |
---|
30 | |
---|
31 | // ----------------------------------------------------------- |
---|
32 | // interpreter stuff for cring/coeffs |
---|
33 | // ----------------------------------------------------------- |
---|
34 | BOOLEAN jjCRING_Zp(leftv res, leftv a, leftv b) |
---|
35 | { |
---|
36 | coeffs c1=(coeffs)a->Data(); |
---|
37 | int i2=(int)(long)b->Data(); |
---|
38 | if (c1->type==n_Z) |
---|
39 | { |
---|
40 | if (i2==IsPrime(i2)) |
---|
41 | { |
---|
42 | res->data=(void *)nInitChar(n_Zp,(void*)(long)i2); |
---|
43 | } |
---|
44 | else |
---|
45 | { |
---|
46 | ZnmInfo info; |
---|
47 | mpz_ptr modBase= (mpz_ptr) omAlloc(sizeof(mpz_t)); |
---|
48 | mpz_init_set_ui(modBase,i2); |
---|
49 | info.base= modBase; |
---|
50 | info.exp= 1; |
---|
51 | res->data=(void *)nInitChar(n_Zn,&info); |
---|
52 | } |
---|
53 | return FALSE; |
---|
54 | } |
---|
55 | return TRUE; |
---|
56 | } |
---|
57 | BOOLEAN jjCRING_Zm(leftv res, leftv a, leftv b) |
---|
58 | { |
---|
59 | coeffs c1=(coeffs)a->Data(); |
---|
60 | number i2=(number)b->Data(); |
---|
61 | if (c1->type==n_Z) |
---|
62 | { |
---|
63 | ZnmInfo info; |
---|
64 | number modBase= (number) omAlloc(sizeof(mpz_t)); |
---|
65 | nlGMP(i2,modBase,coeffs_BIGINT); |
---|
66 | info.base= (mpz_ptr)modBase; |
---|
67 | info.exp= 1; |
---|
68 | res->data=(void *)nInitChar(n_Zn,&info); |
---|
69 | return FALSE; |
---|
70 | } |
---|
71 | return TRUE; |
---|
72 | } |
---|
73 | |
---|
74 | // ----------------------------------------------------------- |
---|
75 | // interpreter stuff for Number/number2 |
---|
76 | // ----------------------------------------------------------- |
---|
77 | BOOLEAN jjNUMBER2_OP2(leftv res, leftv a, leftv b) |
---|
78 | { |
---|
79 | int op=iiOp; |
---|
80 | // binary operations for number2 |
---|
81 | number2 a2=NULL; |
---|
82 | number aa=NULL; |
---|
83 | number2 b2=NULL; |
---|
84 | number bb=NULL; |
---|
85 | if (a->Typ()==CNUMBER_CMD) |
---|
86 | { |
---|
87 | a2=(number2)a->Data(); |
---|
88 | aa=a2->n; |
---|
89 | } |
---|
90 | if (b->Typ()==CNUMBER_CMD) |
---|
91 | { |
---|
92 | b2=(number2)b->Data(); |
---|
93 | if ((a2!=NULL) && (a2->cf!=b2->cf)) |
---|
94 | { |
---|
95 | WerrorS("Number not compatible"); |
---|
96 | return TRUE; |
---|
97 | } |
---|
98 | bb=b2->n; |
---|
99 | } |
---|
100 | number2 r=(number2)omAlloc(sizeof(*r)); |
---|
101 | if (a2!=NULL) r->cf=a2->cf; |
---|
102 | else r->cf=b2->cf; |
---|
103 | if (r->cf==NULL) op=0; // force error |
---|
104 | else |
---|
105 | if (a2==NULL) |
---|
106 | { |
---|
107 | if (a->Typ()==INT_CMD) aa=n_Init((long)a->Data(),r->cf); |
---|
108 | else if (a->Typ()==BIGINT_CMD) |
---|
109 | { |
---|
110 | //aa=n_Init_bigint((number)a->Data(),coeffs_BIGINT,r->cf); |
---|
111 | nMapFunc nMap=n_SetMap(coeffs_BIGINT,r->cf); |
---|
112 | aa=nMap((number)a->Data(),coeffs_BIGINT,r->cf); |
---|
113 | } |
---|
114 | else op=0; |
---|
115 | } |
---|
116 | if ((b2==NULL) &&(op!='^') &&(op!=0)) |
---|
117 | { |
---|
118 | if (b->Typ()==INT_CMD) bb=n_Init((long)b->Data(),r->cf); |
---|
119 | else if (b->Typ()==BIGINT_CMD) |
---|
120 | { |
---|
121 | //bb=n_Init_bigint((number)b->Data(),coeffs_BIGINT,r->cf); |
---|
122 | nMapFunc nMap=n_SetMap(coeffs_BIGINT,r->cf); |
---|
123 | bb=nMap((number)b->Data(),coeffs_BIGINT,r->cf); |
---|
124 | } |
---|
125 | else op=0; |
---|
126 | } |
---|
127 | switch(op) |
---|
128 | { |
---|
129 | case '+': r->n=n_Add(aa,bb,r->cf);break; |
---|
130 | case '-': r->n=n_Sub(aa,bb,r->cf);break; |
---|
131 | case '*': r->n=n_Mult(aa,bb,r->cf);break; |
---|
132 | case '/': r->n=n_Div(aa,bb,r->cf);break; |
---|
133 | case '%': r->n=n_IntMod(aa,bb,r->cf);break; |
---|
134 | |
---|
135 | case '^': n_Power(aa,(int)(long)b->Data(),&(r->n),r->cf); break; |
---|
136 | |
---|
137 | default: Werror("unknown binary operation %s(%d)",Tok2Cmdname(op),op); |
---|
138 | omFree(r); |
---|
139 | return TRUE; |
---|
140 | } |
---|
141 | res->data=(void*)r; |
---|
142 | r->cf->ref++; |
---|
143 | return FALSE; |
---|
144 | } |
---|
145 | BOOLEAN jjNUMBER2_OP1(leftv res, leftv a) |
---|
146 | { |
---|
147 | int op=iiOp; |
---|
148 | // unary operations for number2 |
---|
149 | number2 a2=(number2)a->Data(); |
---|
150 | number2 r=(number2)omAlloc(sizeof(*r)); |
---|
151 | r->cf=a2->cf; |
---|
152 | if (a2->cf==NULL) op=0; // force error |
---|
153 | switch(op) |
---|
154 | { |
---|
155 | case '-': r->n=n_Copy(a2->n,a2->cf);r->n=n_InpNeg(r->n,a2->cf);break; |
---|
156 | default: Werror("unknown unary operation %s(%d)",Tok2Cmdname(op),op); |
---|
157 | omFree(r); |
---|
158 | return TRUE; |
---|
159 | } |
---|
160 | res->data=(void*)r; |
---|
161 | r->cf->ref++; |
---|
162 | return FALSE; |
---|
163 | } |
---|
164 | |
---|
165 | BOOLEAN jjNUMBER2CR(leftv res, leftv a, leftv b) |
---|
166 | { |
---|
167 | number2 r=(number2)omAlloc(sizeof(*r)); |
---|
168 | r->cf=(coeffs)b->CopyD(); |
---|
169 | BOOLEAN bo=FALSE; |
---|
170 | switch(a->Typ()) |
---|
171 | { |
---|
172 | case INT_CMD: |
---|
173 | r->n=n_Init((long)a->Data(),r->cf); break; |
---|
174 | case BIGINT_CMD: |
---|
175 | { |
---|
176 | nMapFunc nMap=n_SetMap(coeffs_BIGINT,r->cf); |
---|
177 | r->n=nMap((number)a->Data(),coeffs_BIGINT,r->cf); break; |
---|
178 | } |
---|
179 | case NUMBER_CMD: |
---|
180 | { |
---|
181 | nMapFunc nMap=n_SetMap(currRing->cf,r->cf); |
---|
182 | if (nMap!=NULL) |
---|
183 | r->n=nMap((number)a->Data(),currRing->cf,r->cf); |
---|
184 | else |
---|
185 | bo=TRUE; |
---|
186 | break; |
---|
187 | } |
---|
188 | case CNUMBER_CMD: |
---|
189 | { |
---|
190 | number2 a2=(number2)a->Data(); |
---|
191 | if (a2->cf==NULL) bo=TRUE; |
---|
192 | else |
---|
193 | { |
---|
194 | nMapFunc nMap=n_SetMap(a2->cf,r->cf); |
---|
195 | if (nMap!=NULL) |
---|
196 | r->n=nMap(a2->n,a2->cf,r->cf); |
---|
197 | else |
---|
198 | bo=TRUE; |
---|
199 | } |
---|
200 | break; |
---|
201 | } |
---|
202 | default: bo=TRUE; break; |
---|
203 | } |
---|
204 | if (bo) |
---|
205 | { |
---|
206 | Werror("no conversion to Number from %s",Tok2Cmdname(a->Typ())); |
---|
207 | omFreeSize(r,sizeof(*r)); |
---|
208 | } |
---|
209 | else |
---|
210 | res->data=(void*)r; |
---|
211 | return bo; |
---|
212 | } |
---|
213 | |
---|
214 | BOOLEAN jjN2_CR(leftv res, leftv a) // number2 ->cring |
---|
215 | { |
---|
216 | number2 n=(number2)a->Data(); |
---|
217 | n->cf->ref++; |
---|
218 | res->data=(void*)n->cf; |
---|
219 | return FALSE; |
---|
220 | } |
---|
221 | |
---|
222 | BOOLEAN jjCM_CR(leftv res, leftv a) // cmatrix ->cring |
---|
223 | { |
---|
224 | bigintmat *b=(bigintmat*)a->Data(); |
---|
225 | coeffs cf=b->basecoeffs(); |
---|
226 | if (cf!=NULL) |
---|
227 | { |
---|
228 | cf->ref++; |
---|
229 | } |
---|
230 | res->data=(void*)cf; |
---|
231 | return FALSE; |
---|
232 | } |
---|
233 | |
---|
234 | BOOLEAN jjCMATRIX_3(leftv res, leftv r, leftv c,leftv cf) |
---|
235 | { |
---|
236 | bigintmat *b=new bigintmat((int)(long)r->Data(), |
---|
237 | (int)(long)c->Data(), |
---|
238 | (coeffs)cf->Data()); |
---|
239 | res->data=(char*)b; |
---|
240 | return FALSE; |
---|
241 | } |
---|
242 | |
---|
243 | BOOLEAN jjN2_N(leftv res, leftv a) // number2 ->number |
---|
244 | { |
---|
245 | number2 n2=(number2)a->Data(); |
---|
246 | BOOLEAN bo=TRUE; |
---|
247 | if (currRing!=NULL) |
---|
248 | { |
---|
249 | nMapFunc nMap=n_SetMap(n2->cf,currRing->cf); |
---|
250 | if (nMap!=NULL) |
---|
251 | { |
---|
252 | res->data=(void*)nMap(n2->n,n2->cf,currRing->cf); |
---|
253 | bo=FALSE; |
---|
254 | } |
---|
255 | } |
---|
256 | return bo; |
---|
257 | } |
---|
258 | |
---|
259 | BOOLEAN jjEQUAL_CR(leftv res, leftv a, leftv b) |
---|
260 | { |
---|
261 | coeffs a2=(coeffs)a->Data(); |
---|
262 | coeffs b2=(coeffs)b->Data(); |
---|
263 | res->data=(void*)(long)(a2==b2); |
---|
264 | return FALSE; |
---|
265 | } |
---|
266 | // ----------------------------------------------------------- |
---|
267 | // operations with Number/number2 |
---|
268 | // ----------------------------------------------------------- |
---|
269 | number2 n2Copy(const number2 d) |
---|
270 | { |
---|
271 | number2 r=NULL; |
---|
272 | if ((d!=NULL)&&(d->cf!=NULL)) |
---|
273 | { |
---|
274 | r=(number2)omAlloc(sizeof(*r)); |
---|
275 | d->cf->ref++; |
---|
276 | r->cf=d->cf; |
---|
277 | if (d->cf!=NULL) |
---|
278 | r->n=n_Copy(d->n,d->cf); |
---|
279 | else |
---|
280 | r->n=NULL; |
---|
281 | } |
---|
282 | return r; |
---|
283 | } |
---|
284 | void n2Delete(number2 &d) |
---|
285 | { |
---|
286 | if (d!=NULL) |
---|
287 | { |
---|
288 | if (d->cf!=NULL) |
---|
289 | { |
---|
290 | n_Delete(&d->n,d->cf); |
---|
291 | nKillChar(d->cf); |
---|
292 | } |
---|
293 | omFreeSize(d,sizeof(*d)); |
---|
294 | d=NULL; |
---|
295 | } |
---|
296 | } |
---|
297 | char *n2String(number2 d, BOOLEAN typed) |
---|
298 | { |
---|
299 | StringSetS(""); |
---|
300 | if ((d!=NULL) && (d->cf!=NULL)) |
---|
301 | { |
---|
302 | if (typed) StringAppendS("Number("); |
---|
303 | n_Write(d->n,d->cf); |
---|
304 | if (typed) StringAppendS(")"); |
---|
305 | } |
---|
306 | else StringAppendS("oo"); |
---|
307 | return StringEndS(); |
---|
308 | } |
---|
309 | |
---|
310 | void n2Print(number2 d) |
---|
311 | { |
---|
312 | char *s=n2String(d,FALSE); |
---|
313 | PrintS(s); |
---|
314 | omFree(s); |
---|
315 | } |
---|
316 | |
---|
317 | #include <coeffs/bigintmat.h> |
---|
318 | BOOLEAN jjBIM2_CR(leftv res, leftv a) // bigintmat ->cring |
---|
319 | { |
---|
320 | bigintmat *b=(bigintmat*)a->Data(); |
---|
321 | coeffs cf=b->basecoeffs(); |
---|
322 | cf->ref++; |
---|
323 | res->data=(void*)cf; |
---|
324 | return FALSE; |
---|
325 | } |
---|
326 | |
---|
327 | BOOLEAN jjR2_CR(leftv res, leftv a) // ring ->cring |
---|
328 | { |
---|
329 | ring r=(ring)a->Data(); |
---|
330 | coeffs cf=r->cf; |
---|
331 | cf->ref++; |
---|
332 | res->data=(void*)cf; |
---|
333 | return FALSE; |
---|
334 | } |
---|
335 | #endif |
---|