1 | /**************************************** |
---|
2 | * Computer Algebra System SINGULAR * |
---|
3 | ****************************************/ |
---|
4 | /* |
---|
5 | * ABSTRACT: automatic type conversions |
---|
6 | */ |
---|
7 | |
---|
8 | |
---|
9 | |
---|
10 | |
---|
11 | #include <kernel/mod2.h> |
---|
12 | #include <Singular/tok.h> |
---|
13 | #include <Singular/ipid.h> |
---|
14 | #include <misc/intvec.h> |
---|
15 | #include <misc/options.h> |
---|
16 | #include <omalloc/omalloc.h> |
---|
17 | #include <kernel/polys.h> |
---|
18 | #include <kernel/ideals.h> |
---|
19 | #include <Singular/subexpr.h> |
---|
20 | #include <coeffs/numbers.h> |
---|
21 | #include <coeffs/coeffs.h> |
---|
22 | #include <coeffs/bigintmat.h> |
---|
23 | //#include <polys/ext_fields/longalg.h> |
---|
24 | #include <polys/matpol.h> |
---|
25 | #include <Singular/links/silink.h> |
---|
26 | #include <kernel/GBEngine/syz.h> |
---|
27 | #include <Singular/attrib.h> |
---|
28 | #include <polys/monomials/ring.h> |
---|
29 | #include <Singular/ipshell.h> |
---|
30 | #include <Singular/number2.h> |
---|
31 | #include <Singular/ipconv.h> |
---|
32 | |
---|
33 | typedef void * (*iiConvertProc)(void * data); |
---|
34 | typedef void (*iiConvertProcL)(leftv out,leftv in); |
---|
35 | struct sConvertTypes |
---|
36 | { |
---|
37 | int i_typ; |
---|
38 | int o_typ; |
---|
39 | iiConvertProc p; |
---|
40 | iiConvertProcL pl; |
---|
41 | }; |
---|
42 | |
---|
43 | // all of these static conversion routines work destructive on their input |
---|
44 | |
---|
45 | static void * iiI2P(void *data) |
---|
46 | { |
---|
47 | poly p=pISet((int)(long)data); |
---|
48 | return (void *)p; |
---|
49 | } |
---|
50 | |
---|
51 | static void * iiBI2P(void *data) |
---|
52 | { |
---|
53 | nMapFunc nMap=n_SetMap(coeffs_BIGINT,currRing->cf); |
---|
54 | if (nMap==NULL) |
---|
55 | { |
---|
56 | Werror("no conversion from bigint to %s", nCoeffString(currRing->cf)); |
---|
57 | return NULL; |
---|
58 | } |
---|
59 | number n=nMap((number)data,coeffs_BIGINT,currRing->cf); |
---|
60 | n_Delete((number *)&data, coeffs_BIGINT); |
---|
61 | poly p=p_NSet(n, currRing); |
---|
62 | return (void *)p; |
---|
63 | } |
---|
64 | |
---|
65 | static void * iiI2V(void *data) |
---|
66 | { |
---|
67 | poly p=pISet((int)(long)data); |
---|
68 | if (p!=NULL) pSetComp(p,1); |
---|
69 | return (void *)p; |
---|
70 | } |
---|
71 | |
---|
72 | static void * iiBI2V(void *data) |
---|
73 | { |
---|
74 | nMapFunc nMap=n_SetMap(coeffs_BIGINT,currRing->cf); |
---|
75 | if (nMap==NULL) |
---|
76 | { |
---|
77 | Werror("no conversion from bigint to %s", nCoeffString(currRing->cf)); |
---|
78 | return NULL; |
---|
79 | } |
---|
80 | number n=nMap((number)data,coeffs_BIGINT,currRing->cf); |
---|
81 | n_Delete((number *)&data, coeffs_BIGINT); |
---|
82 | poly p=p_NSet(n, currRing); |
---|
83 | if (p!=NULL) pSetComp(p,1); |
---|
84 | return (void *)p; |
---|
85 | } |
---|
86 | |
---|
87 | static void * iiI2Id(void *data) |
---|
88 | { |
---|
89 | ideal I=idInit(1,1); |
---|
90 | I->m[0]=pISet((int)(long)data); |
---|
91 | return (void *)I; |
---|
92 | } |
---|
93 | |
---|
94 | static void * iiBI2Id(void *data) |
---|
95 | { |
---|
96 | ideal I=idInit(1,1); |
---|
97 | nMapFunc nMap=n_SetMap(coeffs_BIGINT,currRing->cf); |
---|
98 | if (nMap==NULL) |
---|
99 | { |
---|
100 | Werror("no conversion from bigint to %s", nCoeffString(currRing->cf)); |
---|
101 | return NULL; |
---|
102 | } |
---|
103 | number n=nMap((number)data,coeffs_BIGINT,currRing->cf); |
---|
104 | n_Delete((number *)&data,coeffs_BIGINT); |
---|
105 | poly p=pNSet(n); |
---|
106 | I->m[0]=p; |
---|
107 | return (void *)I; |
---|
108 | } |
---|
109 | static void * iiP2V(void *data) |
---|
110 | { |
---|
111 | poly p=(poly)data; |
---|
112 | if (p!=NULL) pSetCompP(p,1); |
---|
113 | return (void *)p; |
---|
114 | } |
---|
115 | |
---|
116 | static void * iiP2Id(void *data) |
---|
117 | { |
---|
118 | ideal I=idInit(1,1); |
---|
119 | |
---|
120 | if (data!=NULL) |
---|
121 | { |
---|
122 | poly p=(poly)data; |
---|
123 | I->m[0]=p; |
---|
124 | if (pGetComp(p)!=0) I->rank=pMaxComp(p); |
---|
125 | } |
---|
126 | return (void *)I; |
---|
127 | } |
---|
128 | |
---|
129 | static void * iiV2Ma(void *data) |
---|
130 | { |
---|
131 | matrix m=(matrix)idVec2Ideal((poly)data); |
---|
132 | int h=MATCOLS(m); |
---|
133 | MATCOLS(m)=MATROWS(m); |
---|
134 | MATROWS(m)=h; |
---|
135 | m->rank=h; |
---|
136 | pDelete((poly *)&data); |
---|
137 | return (void *)m; |
---|
138 | } |
---|
139 | |
---|
140 | static void * iiN2P(void *data); |
---|
141 | |
---|
142 | static void * iiDummy(void *data) |
---|
143 | { |
---|
144 | return data; |
---|
145 | } |
---|
146 | |
---|
147 | static void * iiMo2Ma(void *data) |
---|
148 | { |
---|
149 | void *res=id_Module2Matrix((ideal)data,currRing); |
---|
150 | return res; |
---|
151 | } |
---|
152 | |
---|
153 | static void * iiMa2Mo(void *data) |
---|
154 | { |
---|
155 | void *res=id_Matrix2Module((matrix)data,currRing); |
---|
156 | return res; |
---|
157 | } |
---|
158 | |
---|
159 | static void * iiI2Iv(void *data) |
---|
160 | { |
---|
161 | int s=(int)(long)data; |
---|
162 | intvec *iv=new intvec(s,s); |
---|
163 | return (void *)iv; |
---|
164 | } |
---|
165 | |
---|
166 | static void * iiI2N(void *data) |
---|
167 | { |
---|
168 | number n=nInit((int)(long)data); |
---|
169 | return (void *)n; |
---|
170 | } |
---|
171 | |
---|
172 | static void * iiI2BI(void *data) |
---|
173 | { |
---|
174 | number n=n_Init((int)(long)data, coeffs_BIGINT); |
---|
175 | return (void *)n; |
---|
176 | } |
---|
177 | |
---|
178 | #ifdef SINGULAR_4_1 |
---|
179 | static void * iiI2NN(void *data) |
---|
180 | { |
---|
181 | if (currRing==NULL) |
---|
182 | { |
---|
183 | WerrorS("missing basering while converting int to Number"); |
---|
184 | return NULL; |
---|
185 | } |
---|
186 | number n=nInit((int)(long)data); |
---|
187 | number2 nn=(number2)omAlloc(sizeof(*nn)); |
---|
188 | nn->cf=currRing->cf; nn->cf->ref++; |
---|
189 | nn->n=n; |
---|
190 | return (void *)nn; |
---|
191 | } |
---|
192 | static void * iiI2CP(void *data) |
---|
193 | { |
---|
194 | if (currRing==NULL) |
---|
195 | { |
---|
196 | WerrorS("missing basering while converting int to Poly"); |
---|
197 | return NULL; |
---|
198 | } |
---|
199 | poly n=pISet((int)(long)data); |
---|
200 | poly2 nn=(poly2)omAlloc(sizeof(*nn)); |
---|
201 | nn->cf=currRing; nn->cf->ref++; |
---|
202 | nn->n=n; |
---|
203 | return (void *)nn; |
---|
204 | } |
---|
205 | #endif |
---|
206 | |
---|
207 | static void * iiBI2N(void *data) |
---|
208 | { |
---|
209 | if (currRing==NULL) return NULL; |
---|
210 | nMapFunc nMap=n_SetMap(coeffs_BIGINT,currRing->cf); |
---|
211 | if (nMap==NULL) |
---|
212 | { |
---|
213 | Werror("no conversion from bigint to %s", nCoeffString(currRing->cf)); |
---|
214 | return NULL; |
---|
215 | } |
---|
216 | number n=nMap((number)data,coeffs_BIGINT,currRing->cf); |
---|
217 | n_Delete((number *)&data, coeffs_BIGINT); |
---|
218 | return (void*)n; |
---|
219 | } |
---|
220 | |
---|
221 | #ifdef SINGULAR_4_1 |
---|
222 | static void * iiBI2NN(void *data) |
---|
223 | { |
---|
224 | if (currRing==NULL) |
---|
225 | { |
---|
226 | WerrorS("missing basering while converting bigint to Number"); |
---|
227 | return NULL; |
---|
228 | } |
---|
229 | nMapFunc nMap=n_SetMap(coeffs_BIGINT,currRing->cf); |
---|
230 | if (nMap==NULL) |
---|
231 | { |
---|
232 | Werror("no conversion from bigint to %s",currRing->cf->cfCoeffString(currRing->cf)); |
---|
233 | return NULL; |
---|
234 | } |
---|
235 | number n=nMap((number)data,coeffs_BIGINT,currRing->cf); |
---|
236 | n_Delete((number *)&data, coeffs_BIGINT); |
---|
237 | number2 nn=(number2)omAlloc(sizeof(*nn)); |
---|
238 | nn->cf=currRing->cf; nn->cf->ref++; |
---|
239 | nn->n=n; |
---|
240 | return (void*)nn; |
---|
241 | } |
---|
242 | static void * iiBI2CP(void *data) |
---|
243 | { |
---|
244 | if (currRing==NULL) |
---|
245 | { |
---|
246 | WerrorS("missing basering while converting bigint to Poly"); |
---|
247 | return NULL; |
---|
248 | } |
---|
249 | nMapFunc nMap=n_SetMap(coeffs_BIGINT,currRing->cf); |
---|
250 | if (nMap==NULL) |
---|
251 | { |
---|
252 | Werror("no conversion from bigint to %s",currRing->cf->cfCoeffString(currRing->cf)); |
---|
253 | return NULL; |
---|
254 | } |
---|
255 | number n=nMap((number)data,coeffs_BIGINT,currRing->cf); |
---|
256 | n_Delete((number *)&data, coeffs_BIGINT); |
---|
257 | poly2 nn=(poly2)omAlloc(sizeof(*nn)); |
---|
258 | nn->cf=currRing; nn->cf->ref++; |
---|
259 | nn->n=pNSet(n); |
---|
260 | return (void*)nn; |
---|
261 | } |
---|
262 | static void * iiP2CP(void *data) |
---|
263 | { |
---|
264 | poly2 nn=(poly2)omAlloc(sizeof(*nn)); |
---|
265 | nn->cf=currRing; nn->cf->ref++; |
---|
266 | nn->n=(poly)data; |
---|
267 | return (void*)nn; |
---|
268 | } |
---|
269 | #endif |
---|
270 | |
---|
271 | #ifdef SINGULAR_4_1 |
---|
272 | static void * iiNN2N(void *data) |
---|
273 | { |
---|
274 | number2 d=(number2)data; |
---|
275 | if ((currRing==NULL) |
---|
276 | || (currRing->cf!=d->cf)) |
---|
277 | { |
---|
278 | WerrorS("cannot convert: incompatible"); |
---|
279 | return NULL; |
---|
280 | } |
---|
281 | number n = n_Copy(d->n, d->cf); |
---|
282 | n2Delete(d); |
---|
283 | return (void*)n; |
---|
284 | } |
---|
285 | #endif |
---|
286 | |
---|
287 | #ifdef SINGULAR_4_1 |
---|
288 | static void * iiNN2P(void *data) |
---|
289 | { |
---|
290 | number2 d=(number2)data; |
---|
291 | if ((currRing==NULL) |
---|
292 | || (currRing->cf!=d->cf)) |
---|
293 | { |
---|
294 | WerrorS("cannot convert: incompatible"); |
---|
295 | return NULL; |
---|
296 | } |
---|
297 | number n = n_Copy(d->n, d->cf); |
---|
298 | n2Delete(d); |
---|
299 | return (void*)p_NSet(n,currRing); |
---|
300 | } |
---|
301 | #endif |
---|
302 | |
---|
303 | static void * iiIm2Ma(void *data) |
---|
304 | { |
---|
305 | int i, j; |
---|
306 | intvec *iv = (intvec *)data; |
---|
307 | matrix m = mpNew(iv->rows(), iv->cols()); |
---|
308 | |
---|
309 | for (i=iv->rows(); i>0; i--) |
---|
310 | { |
---|
311 | for (j=iv->cols(); j>0; j--) |
---|
312 | { |
---|
313 | MATELEM(m, i, j) = pISet(IMATELEM(*iv, i, j)); |
---|
314 | } |
---|
315 | } |
---|
316 | delete iv; |
---|
317 | return (void *)m; |
---|
318 | } |
---|
319 | |
---|
320 | static void * iiIm2Bim(void *data) |
---|
321 | { |
---|
322 | intvec *iv=(intvec*)data; |
---|
323 | void *r=(void *)iv2bim(iv,coeffs_BIGINT); |
---|
324 | delete iv; |
---|
325 | return r; |
---|
326 | } |
---|
327 | |
---|
328 | static void * iiN2P(void *data) |
---|
329 | { |
---|
330 | poly p=NULL; |
---|
331 | if (!nIsZero((number)data)) |
---|
332 | { |
---|
333 | p=pNSet((number)data); |
---|
334 | } |
---|
335 | //else |
---|
336 | //{ |
---|
337 | // nDelete((number *)&data); |
---|
338 | //} |
---|
339 | return (void *)p; |
---|
340 | } |
---|
341 | |
---|
342 | static void * iiN2Ma(void *data) |
---|
343 | { |
---|
344 | ideal I=idInit(1,1); |
---|
345 | if (!nIsZero((number)data)) |
---|
346 | { |
---|
347 | poly p=pNSet((number)data); |
---|
348 | I->m[0]=p; |
---|
349 | } |
---|
350 | //else |
---|
351 | //{ |
---|
352 | // nDelete((number *)&data); |
---|
353 | //} |
---|
354 | return (void *)I; |
---|
355 | } |
---|
356 | |
---|
357 | static void * iiS2Link(void *data) |
---|
358 | { |
---|
359 | si_link l=(si_link)omAlloc0Bin(ip_link_bin); |
---|
360 | slInit(l, (char *) data); |
---|
361 | omFree((ADDRESS)data); |
---|
362 | return (void *)l; |
---|
363 | } |
---|
364 | |
---|
365 | static void iiR2L_l(leftv out, leftv in) |
---|
366 | { |
---|
367 | int add_row_shift = 0; |
---|
368 | intvec *weights=(intvec*)atGet(in,"isHomog",INTVEC_CMD); |
---|
369 | if (weights!=NULL) add_row_shift=weights->min_in(); |
---|
370 | |
---|
371 | syStrategy tmp=(syStrategy)in->CopyD(); |
---|
372 | |
---|
373 | out->data=(void *)syConvRes(tmp,TRUE,add_row_shift); |
---|
374 | } |
---|
375 | |
---|
376 | static void iiL2R(leftv out, leftv in) |
---|
377 | { |
---|
378 | int add_row_shift = 0; |
---|
379 | lists l=(lists)in->Data(); |
---|
380 | intvec *ww=NULL; |
---|
381 | if (l->nr>=0) ww=(intvec *)atGet(&(l->m[0]),"isHomog",INTVEC_CMD); |
---|
382 | out->data=(void *)syConvList(l); |
---|
383 | if (ww!=NULL) |
---|
384 | { |
---|
385 | intvec *weights=ivCopy(ww); |
---|
386 | atSet(out,"isHomog",weights,INTVEC_CMD); |
---|
387 | } |
---|
388 | } |
---|
389 | |
---|
390 | // |
---|
391 | // automatic conversions: |
---|
392 | // |
---|
393 | #define IPCONV |
---|
394 | #define D(A) A |
---|
395 | #define NULL_VAL NULL |
---|
396 | #include <Singular/table.h> |
---|
397 | /*2 |
---|
398 | * try to convert 'input' of type 'inputType' to 'output' of type 'outputType' |
---|
399 | * return FALSE on success |
---|
400 | */ |
---|
401 | BOOLEAN iiConvert (int inputType, int outputType, int index, leftv input, leftv output,const struct sConvertTypes *dConvertTypes) |
---|
402 | { |
---|
403 | memset(output,0,sizeof(sleftv)); |
---|
404 | if ((inputType==outputType) |
---|
405 | || (outputType==DEF_CMD) |
---|
406 | || ((outputType==IDHDL)&&(input->rtyp==IDHDL))) |
---|
407 | { |
---|
408 | memcpy(output,input,sizeof(*output)); |
---|
409 | memset(input,0,sizeof(*input)); |
---|
410 | return FALSE; |
---|
411 | } |
---|
412 | else if (outputType==ANY_TYPE) |
---|
413 | { |
---|
414 | output->rtyp=ANY_TYPE; |
---|
415 | output->data=(char *)(long)input->Typ(); |
---|
416 | /* the name of the object:*/ |
---|
417 | if (input->e==NULL) |
---|
418 | { |
---|
419 | if (input->rtyp==IDHDL) |
---|
420 | /* preserve name: copy it */ |
---|
421 | output->name=omStrDup(IDID((idhdl)(input->data))); |
---|
422 | else if (input->name!=NULL) |
---|
423 | { |
---|
424 | if (input->rtyp==ALIAS_CMD) |
---|
425 | output->name=omStrDup(input->name); |
---|
426 | else |
---|
427 | { |
---|
428 | output->name=input->name; |
---|
429 | input->name=NULL; |
---|
430 | } |
---|
431 | } |
---|
432 | else if ((input->rtyp==POLY_CMD) && (input->name==NULL)) |
---|
433 | { |
---|
434 | if (input->data!=NULL) |
---|
435 | { |
---|
436 | int nr=pIsPurePower((poly)input->data); |
---|
437 | if (nr!=0) |
---|
438 | { |
---|
439 | if (pGetExp((poly)input->data,nr)==1) |
---|
440 | { |
---|
441 | output->name=omStrDup(currRing->names[nr-1]); |
---|
442 | } |
---|
443 | else |
---|
444 | { |
---|
445 | char *tmp=(char *)omAlloc(4); |
---|
446 | sprintf(tmp,"%c%d",*(currRing->names[nr-1]), |
---|
447 | (int)pGetExp((poly)input->data,nr)); |
---|
448 | output->name=tmp; |
---|
449 | } |
---|
450 | } |
---|
451 | else if(pIsConstant((poly)input->data)) |
---|
452 | { |
---|
453 | StringSetS(""); |
---|
454 | number n=(pGetCoeff((poly)input->data)); |
---|
455 | n_Write(n, currRing->cf); |
---|
456 | (pGetCoeff((poly)input->data))=n; |
---|
457 | output->name=StringEndS(); |
---|
458 | } |
---|
459 | } |
---|
460 | } |
---|
461 | else if ((input->rtyp==NUMBER_CMD) && (input->name==NULL)) |
---|
462 | { |
---|
463 | StringSetS(""); |
---|
464 | number n=(number)input->data; |
---|
465 | n_Write(n, currRing->cf); |
---|
466 | input->data=(void*)n; |
---|
467 | output->name=StringEndS(); |
---|
468 | } |
---|
469 | else |
---|
470 | { |
---|
471 | /* no need to preserve name: use it */ |
---|
472 | output->name=input->name; |
---|
473 | memset(input,0,sizeof(*input)); |
---|
474 | } |
---|
475 | } |
---|
476 | output->next=input->next; |
---|
477 | input->next=NULL; |
---|
478 | return errorreported; |
---|
479 | } |
---|
480 | if (index!=0) /* iiTestConvert does not returned 'failure' */ |
---|
481 | { |
---|
482 | index--; |
---|
483 | |
---|
484 | if((dConvertTypes[index].i_typ==inputType) |
---|
485 | &&(dConvertTypes[index].o_typ==outputType)) |
---|
486 | { |
---|
487 | if(traceit&TRACE_CONV) |
---|
488 | { |
---|
489 | Print("automatic conversion %s -> %s\n", |
---|
490 | Tok2Cmdname(inputType),Tok2Cmdname(outputType)); |
---|
491 | } |
---|
492 | if ((currRing==NULL) && (outputType>BEGIN_RING) && (outputType<END_RING)) |
---|
493 | return TRUE; |
---|
494 | output->rtyp=outputType; |
---|
495 | if (dConvertTypes[index].p!=NULL) |
---|
496 | { |
---|
497 | output->data=dConvertTypes[index].p(input->CopyD()); |
---|
498 | } |
---|
499 | else |
---|
500 | { |
---|
501 | dConvertTypes[index].pl(output,input); |
---|
502 | } |
---|
503 | if ((output->data==NULL) |
---|
504 | && ((outputType!=INT_CMD) |
---|
505 | &&(outputType!=POLY_CMD) |
---|
506 | &&(outputType!=VECTOR_CMD) |
---|
507 | &&(outputType!=NUMBER_CMD))) |
---|
508 | { |
---|
509 | return TRUE; |
---|
510 | } |
---|
511 | if (errorreported) return TRUE; |
---|
512 | output->next=input->next; |
---|
513 | input->next=NULL; |
---|
514 | //if (outputType==MATRIX_CMD) Print("convert %d -> matrix\n",inputType); |
---|
515 | return FALSE; |
---|
516 | } |
---|
517 | } |
---|
518 | return TRUE; |
---|
519 | } |
---|
520 | |
---|
521 | /*2 |
---|
522 | * try to convert 'inputType' in 'outputType' |
---|
523 | * return 0 on failure, an index (<>0) on success |
---|
524 | */ |
---|
525 | int iiTestConvert (int inputType, int outputType,const struct sConvertTypes *dConvertTypes) |
---|
526 | { |
---|
527 | if ((inputType==outputType) |
---|
528 | || (outputType==DEF_CMD) |
---|
529 | || (outputType==IDHDL) |
---|
530 | || (outputType==ANY_TYPE)) |
---|
531 | { |
---|
532 | return -1; |
---|
533 | } |
---|
534 | if (inputType==UNKNOWN) return 0; |
---|
535 | |
---|
536 | if ((currRing==NULL) && (outputType>BEGIN_RING) && (outputType<END_RING)) |
---|
537 | return 0; |
---|
538 | //if ((currRing==NULL) && (outputType==CNUMBER_CMD)) |
---|
539 | // return 0; |
---|
540 | |
---|
541 | // search the list |
---|
542 | int i=0; |
---|
543 | while (dConvertTypes[i].i_typ!=0) |
---|
544 | { |
---|
545 | if((dConvertTypes[i].i_typ==inputType) |
---|
546 | &&(dConvertTypes[i].o_typ==outputType)) |
---|
547 | { |
---|
548 | //Print("test convert %d to %d (%s -> %s):%d\n",inputType,outputType, |
---|
549 | //Tok2Cmdname(inputType), Tok2Cmdname(outputType),i+1); |
---|
550 | return i+1; |
---|
551 | } |
---|
552 | i++; |
---|
553 | } |
---|
554 | //Print("test convert %d to %d (%s -> %s):0, tested:%d\n",inputType,outputType, |
---|
555 | // Tok2Cmdname(inputType), Tok2Cmdname(outputType),i); |
---|
556 | return 0; |
---|
557 | } |
---|