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