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