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