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