1 | /**************************************** |
---|
2 | * Computer Algebra System SINGULAR * |
---|
3 | ****************************************/ |
---|
4 | /* |
---|
5 | * ABSTRACT: automatic type conversions |
---|
6 | */ |
---|
7 | |
---|
8 | #include "config.h" |
---|
9 | #include <kernel/mod2.h> |
---|
10 | #include <Singular/tok.h> |
---|
11 | #include <Singular/ipid.h> |
---|
12 | #include <misc/intvec.h> |
---|
13 | #include <misc/options.h> |
---|
14 | #include <omalloc/omalloc.h> |
---|
15 | #include <kernel/febase.h> |
---|
16 | #include <kernel/polys.h> |
---|
17 | #include <kernel/ideals.h> |
---|
18 | #include <Singular/subexpr.h> |
---|
19 | #include <coeffs/numbers.h> |
---|
20 | #include <coeffs/coeffs.h> |
---|
21 | #include <coeffs/bigintmat.h> |
---|
22 | //#include <polys/ext_fields/longalg.h> |
---|
23 | #ifdef HAVE_RINGS |
---|
24 | #include <coeffs/rmodulon.h> |
---|
25 | #include <coeffs/rmodulo2m.h> |
---|
26 | #include <coeffs/rintegers.h> |
---|
27 | #endif |
---|
28 | #include <polys/matpol.h> |
---|
29 | #include <Singular/silink.h> |
---|
30 | #include <kernel/syz.h> |
---|
31 | #include <Singular/attrib.h> |
---|
32 | #include <polys/monomials/ring.h> |
---|
33 | #include <Singular/ipshell.h> |
---|
34 | #include <Singular/ipconv.h> |
---|
35 | |
---|
36 | typedef void * (*iiConvertProc)(void * data); |
---|
37 | typedef void (*iiConvertProcL)(leftv out,leftv in); |
---|
38 | struct sConvertTypes |
---|
39 | { |
---|
40 | int i_typ; |
---|
41 | int o_typ; |
---|
42 | iiConvertProc p; |
---|
43 | iiConvertProcL pl; |
---|
44 | }; |
---|
45 | |
---|
46 | // all of these static conversion routines work destructive on their input |
---|
47 | |
---|
48 | static void * iiI2P(void *data) |
---|
49 | { |
---|
50 | poly p=pISet((int)(long)data); |
---|
51 | return (void *)p; |
---|
52 | } |
---|
53 | |
---|
54 | static void * iiBI2P(void *data) |
---|
55 | { |
---|
56 | number n=n_Init_bigint((number)data, coeffs_BIGINT /*currRing->cf*/, currRing->cf); |
---|
57 | n_Delete((number *)&data, coeffs_BIGINT); |
---|
58 | poly p=p_NSet(n, currRing); |
---|
59 | return (void *)p; |
---|
60 | } |
---|
61 | |
---|
62 | static void * iiI2V(void *data) |
---|
63 | { |
---|
64 | poly p=pISet((int)(long)data); |
---|
65 | if (p!=NULL) pSetComp(p,1); |
---|
66 | return (void *)p; |
---|
67 | } |
---|
68 | |
---|
69 | static void * iiBI2V(void *data) |
---|
70 | { |
---|
71 | number n=n_Init_bigint((number)data, coeffs_BIGINT/*currRing->cf*/, currRing->cf); |
---|
72 | n_Delete((number *)&data, coeffs_BIGINT); |
---|
73 | poly p=p_NSet(n, currRing); |
---|
74 | if (p!=NULL) pSetComp(p,1); |
---|
75 | return (void *)p; |
---|
76 | } |
---|
77 | |
---|
78 | static void * iiI2Id(void *data) |
---|
79 | { |
---|
80 | ideal I=idInit(1,1); |
---|
81 | I->m[0]=pISet((int)(long)data); |
---|
82 | return (void *)I; |
---|
83 | } |
---|
84 | |
---|
85 | static void * iiBI2Id(void *data) |
---|
86 | { |
---|
87 | ideal I=idInit(1,1); |
---|
88 | number n=n_Init_bigint((number)data, coeffs_BIGINT, currRing->cf); |
---|
89 | n_Delete((number *)&data,coeffs_BIGINT); |
---|
90 | poly p=pNSet(n); |
---|
91 | I->m[0]=p; |
---|
92 | return (void *)I; |
---|
93 | } |
---|
94 | static void * iiP2V(void *data) |
---|
95 | { |
---|
96 | poly p=(poly)data; |
---|
97 | if (p!=NULL) pSetCompP(p,1); |
---|
98 | return (void *)p; |
---|
99 | } |
---|
100 | |
---|
101 | static void * iiP2Id(void *data) |
---|
102 | { |
---|
103 | ideal I=idInit(1,1); |
---|
104 | |
---|
105 | I->m[0]=(poly)data; |
---|
106 | if (data!=NULL) |
---|
107 | { |
---|
108 | poly p=(poly)data; |
---|
109 | if (pGetComp(p)!=0) I->rank=pMaxComp(p); |
---|
110 | } |
---|
111 | return (void *)I; |
---|
112 | } |
---|
113 | |
---|
114 | static void * iiV2Ma(void *data) |
---|
115 | { |
---|
116 | matrix m=(matrix)idVec2Ideal((poly)data); |
---|
117 | int h=MATCOLS(m); |
---|
118 | MATCOLS(m)=MATROWS(m); |
---|
119 | MATROWS(m)=h; |
---|
120 | m->rank=h; |
---|
121 | pDelete((poly *)&data); |
---|
122 | return (void *)m; |
---|
123 | } |
---|
124 | |
---|
125 | static void * iiN2P(void *data); |
---|
126 | |
---|
127 | static void * iiDummy(void *data) |
---|
128 | { |
---|
129 | return data; |
---|
130 | } |
---|
131 | |
---|
132 | static void * iiMo2Ma(void *data) |
---|
133 | { |
---|
134 | void *res=id_Module2Matrix((ideal)data,currRing); |
---|
135 | return res; |
---|
136 | } |
---|
137 | |
---|
138 | static void * iiMa2Mo(void *data) |
---|
139 | { |
---|
140 | void *res=id_Matrix2Module((matrix)data,currRing); |
---|
141 | return res; |
---|
142 | } |
---|
143 | |
---|
144 | static void * iiI2Iv(void *data) |
---|
145 | { |
---|
146 | int s=(int)(long)data; |
---|
147 | intvec *iv=new intvec(s,s); |
---|
148 | return (void *)iv; |
---|
149 | } |
---|
150 | |
---|
151 | static void * iiI2N(void *data) |
---|
152 | { |
---|
153 | number n=nInit((int)(long)data); |
---|
154 | return (void *)n; |
---|
155 | } |
---|
156 | |
---|
157 | static void * iiI2BI(void *data) |
---|
158 | { |
---|
159 | number n=n_Init((int)(long)data, coeffs_BIGINT); |
---|
160 | return (void *)n; |
---|
161 | } |
---|
162 | |
---|
163 | static void * iiBI2N(void *data) |
---|
164 | { |
---|
165 | if (currRing==NULL) return NULL; |
---|
166 | // a bigint is really a number from char 0, with diffrent |
---|
167 | // operations... |
---|
168 | number n = n_Init_bigint((number)data, coeffs_BIGINT, currRing->cf); |
---|
169 | n_Delete((number *)&data, coeffs_BIGINT); |
---|
170 | return (void*)n; |
---|
171 | } |
---|
172 | |
---|
173 | static void * iiIm2Ma(void *data) |
---|
174 | { |
---|
175 | int i, j; |
---|
176 | intvec *iv = (intvec *)data; |
---|
177 | matrix m = mpNew(iv->rows(), iv->cols()); |
---|
178 | |
---|
179 | for (i=iv->rows(); i>0; i--) |
---|
180 | { |
---|
181 | for (j=iv->cols(); j>0; j--) |
---|
182 | { |
---|
183 | MATELEM(m, i, j) = pISet(IMATELEM(*iv, i, j)); |
---|
184 | } |
---|
185 | } |
---|
186 | delete iv; |
---|
187 | return (void *)m; |
---|
188 | } |
---|
189 | |
---|
190 | static void * iiIm2Bim(void *data) |
---|
191 | { |
---|
192 | return (void *)iv2bim((intvec*)data,coeffs_BIGINT); |
---|
193 | } |
---|
194 | |
---|
195 | static void * iiBim2Im(void *data) |
---|
196 | { |
---|
197 | return (void *)bim2iv((bigintmat*)data); |
---|
198 | } |
---|
199 | |
---|
200 | static void * iiN2P(void *data) |
---|
201 | { |
---|
202 | poly p=NULL; |
---|
203 | if (!nIsZero((number)data)) |
---|
204 | { |
---|
205 | p=pNSet((number)data); |
---|
206 | } |
---|
207 | //else |
---|
208 | //{ |
---|
209 | // nDelete((number *)&data); |
---|
210 | //} |
---|
211 | return (void *)p; |
---|
212 | } |
---|
213 | |
---|
214 | static void * iiN2Ma(void *data) |
---|
215 | { |
---|
216 | ideal I=idInit(1,1); |
---|
217 | if (!nIsZero((number)data)) |
---|
218 | { |
---|
219 | poly p=pNSet((number)data); |
---|
220 | I->m[0]=p; |
---|
221 | } |
---|
222 | //else |
---|
223 | //{ |
---|
224 | // nDelete((number *)&data); |
---|
225 | //} |
---|
226 | return (void *)I; |
---|
227 | } |
---|
228 | |
---|
229 | static void * iiS2Link(void *data) |
---|
230 | { |
---|
231 | si_link l=(si_link)omAlloc0Bin(ip_link_bin); |
---|
232 | slInit(l, (char *) data); |
---|
233 | omFree((ADDRESS)data); |
---|
234 | return (void *)l; |
---|
235 | } |
---|
236 | |
---|
237 | /* |
---|
238 | static void * iiR2L(void * data) |
---|
239 | { |
---|
240 | syStrategy tmp=(syStrategy)data; |
---|
241 | return (void *)syConvRes(tmp,TRUE); |
---|
242 | } |
---|
243 | */ |
---|
244 | static void iiR2L_l(leftv out, leftv in) |
---|
245 | { |
---|
246 | int add_row_shift = 0; |
---|
247 | intvec *weights=(intvec*)atGet(in,"isHomog",INTVEC_CMD); |
---|
248 | if (weights!=NULL) add_row_shift=weights->min_in(); |
---|
249 | |
---|
250 | syStrategy tmp=(syStrategy)in->CopyD(); |
---|
251 | |
---|
252 | out->data=(void *)syConvRes(tmp,TRUE,add_row_shift); |
---|
253 | } |
---|
254 | |
---|
255 | static void * iiL2R(void * data) |
---|
256 | { |
---|
257 | return (void *)syConvList((lists)data,TRUE); |
---|
258 | } |
---|
259 | |
---|
260 | // |
---|
261 | // automatic conversions: |
---|
262 | // |
---|
263 | #define IPCONV |
---|
264 | #define D(A) A |
---|
265 | #include <Singular/table.h> |
---|
266 | /*2 |
---|
267 | * try to convert 'input' of type 'inputType' to 'output' of type 'outputType' |
---|
268 | * return FALSE on success |
---|
269 | */ |
---|
270 | BOOLEAN iiConvert (int inputType, int outputType, int index, leftv input, leftv output) |
---|
271 | { |
---|
272 | memset(output,0,sizeof(sleftv)); |
---|
273 | if ((inputType==outputType) |
---|
274 | || (outputType==DEF_CMD) |
---|
275 | || ((outputType==IDHDL)&&(input->rtyp==IDHDL))) |
---|
276 | { |
---|
277 | memcpy(output,input,sizeof(*output)); |
---|
278 | memset(input,0,sizeof(*input)); |
---|
279 | return FALSE; |
---|
280 | } |
---|
281 | else if (outputType==ANY_TYPE) |
---|
282 | { |
---|
283 | output->rtyp=ANY_TYPE; |
---|
284 | output->data=(char *)input->Typ(); |
---|
285 | /* the name of the object:*/ |
---|
286 | if (input->e==NULL) |
---|
287 | { |
---|
288 | if (input->rtyp==IDHDL) |
---|
289 | /* preserve name: copy it */ |
---|
290 | output->name=omStrDup(IDID((idhdl)(input->data))); |
---|
291 | else if (input->name!=NULL) |
---|
292 | { |
---|
293 | if (input->rtyp==ALIAS_CMD) |
---|
294 | output->name=omStrDup(input->name); |
---|
295 | else |
---|
296 | { |
---|
297 | output->name=input->name; |
---|
298 | input->name=NULL; |
---|
299 | } |
---|
300 | } |
---|
301 | else if ((input->rtyp==POLY_CMD) && (input->name==NULL)) |
---|
302 | { |
---|
303 | if (input->data!=NULL) |
---|
304 | { |
---|
305 | int nr=pIsPurePower((poly)input->data); |
---|
306 | if (nr!=0) |
---|
307 | { |
---|
308 | if (pGetExp((poly)input->data,nr)==1) |
---|
309 | { |
---|
310 | output->name=omStrDup(currRing->names[nr-1]); |
---|
311 | } |
---|
312 | else |
---|
313 | { |
---|
314 | char *tmp=(char *)omAlloc(4); |
---|
315 | sprintf(tmp,"%c%d",*(currRing->names[nr-1]), |
---|
316 | (int)pGetExp((poly)input->data,nr)); |
---|
317 | output->name=tmp; |
---|
318 | } |
---|
319 | } |
---|
320 | else if(pIsConstant((poly)input->data)) |
---|
321 | { |
---|
322 | output->name=ndName(pGetCoeff((poly)input->data), currRing->cf); |
---|
323 | } |
---|
324 | #ifdef TEST |
---|
325 | else |
---|
326 | { |
---|
327 | WerrorS("wrong name, should not happen"); |
---|
328 | output->name=omStrDup("?"); |
---|
329 | } |
---|
330 | #endif |
---|
331 | } |
---|
332 | } |
---|
333 | else if ((input->rtyp==NUMBER_CMD) && (input->name==NULL)) |
---|
334 | { |
---|
335 | output->name=ndName((number)input->data, currRing->cf); |
---|
336 | } |
---|
337 | else |
---|
338 | { |
---|
339 | /* no need to preserve name: use it */ |
---|
340 | output->name=input->name; |
---|
341 | memset(input,0,sizeof(*input)); |
---|
342 | } |
---|
343 | } |
---|
344 | output->next=input->next; |
---|
345 | input->next=NULL; |
---|
346 | return FALSE; |
---|
347 | } |
---|
348 | if (index!=0) /* iiTestConvert does not returned 'failure' */ |
---|
349 | { |
---|
350 | index--; |
---|
351 | |
---|
352 | if((dConvertTypes[index].i_typ==inputType) |
---|
353 | &&(dConvertTypes[index].o_typ==outputType)) |
---|
354 | { |
---|
355 | if(TEST_V_ALLWARN) |
---|
356 | { |
---|
357 | Print("automatic conversion %s -> %s\n", |
---|
358 | Tok2Cmdname(inputType),Tok2Cmdname(outputType)); |
---|
359 | } |
---|
360 | if ((currRing==NULL) && (outputType>BEGIN_RING) && (outputType<END_RING)) |
---|
361 | return TRUE; |
---|
362 | output->rtyp=outputType; |
---|
363 | if (dConvertTypes[index].p!=NULL) |
---|
364 | { |
---|
365 | output->data=dConvertTypes[index].p(input->CopyD()); |
---|
366 | } |
---|
367 | else |
---|
368 | { |
---|
369 | dConvertTypes[index].pl(output,input); |
---|
370 | } |
---|
371 | if ((output->data==NULL) |
---|
372 | && ((outputType!=INT_CMD) |
---|
373 | &&(outputType!=POLY_CMD) |
---|
374 | &&(outputType!=VECTOR_CMD) |
---|
375 | &&(outputType!=NUMBER_CMD))) |
---|
376 | { |
---|
377 | return TRUE; |
---|
378 | } |
---|
379 | output->next=input->next; |
---|
380 | input->next=NULL; |
---|
381 | //if (outputType==MATRIX_CMD) Print("convert %d -> matrix\n",inputType); |
---|
382 | return FALSE; |
---|
383 | } |
---|
384 | } |
---|
385 | return TRUE; |
---|
386 | } |
---|
387 | |
---|
388 | /*2 |
---|
389 | * try to convert 'inputType' in 'outputType' |
---|
390 | * return 0 on failure, an index (<>0) on success |
---|
391 | */ |
---|
392 | int iiTestConvert (int inputType, int outputType) |
---|
393 | { |
---|
394 | if ((inputType==outputType) |
---|
395 | || (outputType==DEF_CMD) |
---|
396 | || (outputType==IDHDL) |
---|
397 | || (outputType==ANY_TYPE)) |
---|
398 | { |
---|
399 | return -1; |
---|
400 | } |
---|
401 | |
---|
402 | if ((currRing==NULL) && (outputType>BEGIN_RING) && (outputType<END_RING)) |
---|
403 | return 0; |
---|
404 | |
---|
405 | // search the list |
---|
406 | int i=0; |
---|
407 | while (dConvertTypes[i].i_typ!=0) |
---|
408 | { |
---|
409 | if((dConvertTypes[i].i_typ==inputType) |
---|
410 | &&(dConvertTypes[i].o_typ==outputType)) |
---|
411 | { |
---|
412 | //Print("test convert %d to %d (%s -> %s):%d\n",inputType,outputType, |
---|
413 | //Tok2Cmdname(inputType), Tok2Cmdname(outputType),i+1); |
---|
414 | return i+1; |
---|
415 | } |
---|
416 | i++; |
---|
417 | } |
---|
418 | //Print("test convert %d to %d (%s -> %s):0\n",inputType,outputType, |
---|
419 | // Tok2Cmdname(inputType), Tok2Cmdname(outputType)); |
---|
420 | return 0; |
---|
421 | } |
---|