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