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