[0e1846] | 1 | /**************************************** |
---|
| 2 | * Computer Algebra System SINGULAR * |
---|
| 3 | ****************************************/ |
---|
[341696] | 4 | /* $Id$ */ |
---|
[0e1846] | 5 | /* |
---|
| 6 | * ABSTRACT: automatic type conversions |
---|
| 7 | */ |
---|
| 8 | |
---|
[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> |
---|
[0fb34ba] | 16 | #include <polys/polys.h> |
---|
[599326] | 17 | #include <kernel/ideals.h> |
---|
| 18 | #include <Singular/subexpr.h> |
---|
[0fb34ba] | 19 | #include <coeffs/numbers.h> |
---|
[599326] | 20 | #include <kernel/modulop.h> |
---|
| 21 | #include <kernel/longrat.h> |
---|
| 22 | #include <kernel/longalg.h> |
---|
[c81a40] | 23 | #ifdef HAVE_RINGS |
---|
[599326] | 24 | #include <kernel/rmodulon.h> |
---|
| 25 | #include <kernel/rmodulo2m.h> |
---|
| 26 | #include <kernel/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 | { |
---|
[8bd247] | 56 | number n=nInit_bigint((number)data); |
---|
[db5523] | 57 | nlDelete((number *)&data,NULL); |
---|
| 58 | poly p=pNSet(n); |
---|
| 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 | { |
---|
[8bd247] | 71 | number n=nInit_bigint((number)data); |
---|
[db5523] | 72 | nlDelete((number *)&data,NULL); |
---|
| 73 | poly p=pNSet(n); |
---|
| 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); |
---|
[8bd247] | 88 | number n=nInit_bigint((number)data); |
---|
[db5523] | 89 | nlDelete((number *)&data,NULL); |
---|
| 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 | I->m[0]=(poly)data; |
---|
| 106 | if (data!=NULL) |
---|
| 107 | { |
---|
| 108 | poly p=(poly)data; |
---|
| 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 | { |
---|
| 134 | void *res=idModule2Matrix((ideal)data); |
---|
| 135 | return res; |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | static void * iiMa2Mo(void *data) |
---|
| 139 | { |
---|
| 140 | void *res=idMatrix2Module((matrix)data); |
---|
| 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 | { |
---|
[8391d8] | 159 | number n=nlInit((int)(long)data, NULL /*dummy for nlInit*/); |
---|
[6fc941] | 160 | return (void *)n; |
---|
| 161 | } |
---|
| 162 | |
---|
[cfce1fc] | 163 | static void * iiBI2N(void *data) |
---|
| 164 | { |
---|
| 165 | if (currRing==NULL) return NULL; |
---|
| 166 | // a bigint is really a number from char 0, with diffrent operations... |
---|
[41ea815] | 167 | return (void*)nInit_bigint((number)data); |
---|
[cfce1fc] | 168 | } |
---|
| 169 | |
---|
[0e1846] | 170 | static void * iiIm2Ma(void *data) |
---|
| 171 | { |
---|
| 172 | int i, j; |
---|
| 173 | intvec *iv = (intvec *)data; |
---|
| 174 | matrix m = mpNew(iv->rows(), iv->cols()); |
---|
| 175 | |
---|
| 176 | for (i=iv->rows(); i>0; i--) |
---|
| 177 | { |
---|
| 178 | for (j=iv->cols(); j>0; j--) |
---|
| 179 | { |
---|
| 180 | MATELEM(m, i, j) = pISet(IMATELEM(*iv, i, j)); |
---|
| 181 | } |
---|
| 182 | } |
---|
[befd6b5] | 183 | delete iv; |
---|
[0e1846] | 184 | return (void *)m; |
---|
| 185 | } |
---|
| 186 | |
---|
| 187 | static void * iiN2P(void *data) |
---|
| 188 | { |
---|
| 189 | poly p=NULL; |
---|
| 190 | if (!nIsZero((number)data)) |
---|
| 191 | { |
---|
[3eabdb] | 192 | p=pNSet((number)data); |
---|
[0e1846] | 193 | } |
---|
[805b06c] | 194 | //else |
---|
| 195 | //{ |
---|
| 196 | // nDelete((number *)&data); |
---|
[0d84792] | 197 | //} |
---|
[0e1846] | 198 | return (void *)p; |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | static void * iiN2Ma(void *data) |
---|
| 202 | { |
---|
| 203 | ideal I=idInit(1,1); |
---|
| 204 | if (!nIsZero((number)data)) |
---|
| 205 | { |
---|
[3eabdb] | 206 | poly p=pNSet((number)data); |
---|
[0e1846] | 207 | I->m[0]=p; |
---|
| 208 | } |
---|
[805b06c] | 209 | //else |
---|
| 210 | //{ |
---|
| 211 | // nDelete((number *)&data); |
---|
[0d84792] | 212 | //} |
---|
[0e1846] | 213 | return (void *)I; |
---|
| 214 | } |
---|
| 215 | |
---|
| 216 | static void * iiS2Link(void *data) |
---|
| 217 | { |
---|
[c232af] | 218 | si_link l=(si_link)omAlloc0Bin(ip_link_bin); |
---|
[0e1846] | 219 | slInit(l, (char *) data); |
---|
[c232af] | 220 | omFree((ADDRESS)data); |
---|
[0e1846] | 221 | return (void *)l; |
---|
| 222 | } |
---|
| 223 | |
---|
[f43a74] | 224 | /* |
---|
[dfc6b54] | 225 | static void * iiR2L(void * data) |
---|
| 226 | { |
---|
[3b2d29] | 227 | syStrategy tmp=(syStrategy)data; |
---|
[c89df6] | 228 | return (void *)syConvRes(tmp,TRUE); |
---|
[dfc6b54] | 229 | } |
---|
[f43a74] | 230 | */ |
---|
| 231 | static void iiR2L_l(leftv out, leftv in) |
---|
| 232 | { |
---|
| 233 | int add_row_shift = 0; |
---|
| 234 | intvec *weights=(intvec*)atGet(in,"isHomog",INTVEC_CMD); |
---|
| 235 | if (weights!=NULL) add_row_shift=weights->min_in(); |
---|
| 236 | |
---|
| 237 | syStrategy tmp=(syStrategy)in->CopyD(); |
---|
[22579cf] | 238 | |
---|
[f43a74] | 239 | out->data=(void *)syConvRes(tmp,TRUE,add_row_shift); |
---|
| 240 | } |
---|
[dfc6b54] | 241 | |
---|
[3ebd198] | 242 | static void * iiL2R(void * data) |
---|
| 243 | { |
---|
[b982ef] | 244 | return (void *)syConvList((lists)data,TRUE); |
---|
[3ebd198] | 245 | } |
---|
| 246 | |
---|
[0e1846] | 247 | // |
---|
[78c048b] | 248 | // automatic conversions: |
---|
[0e1846] | 249 | // |
---|
[56c789] | 250 | #define IPCONV |
---|
| 251 | #define D(A) A |
---|
| 252 | #include <Singular/table.h> |
---|
[0e1846] | 253 | /*2 |
---|
| 254 | * try to convert 'input' of type 'inputType' to 'output' of type 'outputType' |
---|
| 255 | * return FALSE on success |
---|
| 256 | */ |
---|
| 257 | BOOLEAN iiConvert (int inputType, int outputType, int index, leftv input, leftv output) |
---|
| 258 | { |
---|
| 259 | memset(output,0,sizeof(sleftv)); |
---|
| 260 | if ((inputType==outputType) |
---|
| 261 | || (outputType==DEF_CMD) |
---|
| 262 | || ((outputType==IDHDL)&&(input->rtyp==IDHDL))) |
---|
| 263 | { |
---|
| 264 | memcpy(output,input,sizeof(*output)); |
---|
| 265 | memset(input,0,sizeof(*input)); |
---|
| 266 | return FALSE; |
---|
| 267 | } |
---|
| 268 | else if (outputType==ANY_TYPE) |
---|
| 269 | { |
---|
| 270 | output->rtyp=ANY_TYPE; |
---|
| 271 | output->data=(char *)input->Typ(); |
---|
| 272 | /* the name of the object:*/ |
---|
| 273 | if (input->e==NULL) |
---|
| 274 | { |
---|
| 275 | if (input->rtyp==IDHDL) |
---|
| 276 | /* preserve name: copy it */ |
---|
[c232af] | 277 | output->name=omStrDup(IDID((idhdl)(input->data))); |
---|
[0e1846] | 278 | else if (input->name!=NULL) |
---|
| 279 | { |
---|
| 280 | output->name=input->name; |
---|
| 281 | input->name=NULL; |
---|
| 282 | } |
---|
| 283 | else if ((input->rtyp==POLY_CMD) && (input->name==NULL)) |
---|
| 284 | { |
---|
[0d84792] | 285 | if (input->data!=NULL) |
---|
[0e1846] | 286 | { |
---|
[0d84792] | 287 | int nr=pIsPurePower((poly)input->data); |
---|
| 288 | if (nr!=0) |
---|
[0e1846] | 289 | { |
---|
[0d84792] | 290 | if (pGetExp((poly)input->data,nr)==1) |
---|
| 291 | { |
---|
[c232af] | 292 | output->name=omStrDup(currRing->names[nr-1]); |
---|
[0d84792] | 293 | } |
---|
| 294 | else |
---|
| 295 | { |
---|
[85e68dd] | 296 | char *tmp=(char *)omAlloc(4); |
---|
| 297 | sprintf(tmp,"%c%d",*(currRing->names[nr-1]), |
---|
| 298 | (int)pGetExp((poly)input->data,nr)); |
---|
[3f4454b] | 299 | output->name=tmp; |
---|
[0d84792] | 300 | } |
---|
[0e1846] | 301 | } |
---|
[0d84792] | 302 | else if(pIsConstant((poly)input->data)) |
---|
[0e1846] | 303 | { |
---|
[0d84792] | 304 | output->name=nName(pGetCoeff((poly)input->data)); |
---|
[0e1846] | 305 | } |
---|
| 306 | #ifdef TEST |
---|
[0d84792] | 307 | else |
---|
| 308 | { |
---|
| 309 | WerrorS("wrong name, should not happen"); |
---|
[c232af] | 310 | output->name=omStrDup("?"); |
---|
[0d84792] | 311 | } |
---|
[0e1846] | 312 | #endif |
---|
[0d84792] | 313 | } |
---|
[0e1846] | 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 | { |
---|
[d693da2] | 337 | if(TEST_V_ALLWARN) |
---|
| 338 | { |
---|
| 339 | Print("automatic conversion %s -> %s\n", |
---|
[3f4454b] | 340 | Tok2Cmdname(inputType),Tok2Cmdname(outputType)); |
---|
[d693da2] | 341 | } |
---|
[0e1846] | 342 | if ((currRing==NULL) && (outputType>BEGIN_RING) && (outputType<END_RING)) |
---|
| 343 | return TRUE; |
---|
| 344 | output->rtyp=outputType; |
---|
[f43a74] | 345 | if (dConvertTypes[index].p!=NULL) |
---|
| 346 | { |
---|
| 347 | output->data=dConvertTypes[index].p(input->CopyD()); |
---|
| 348 | } |
---|
| 349 | else |
---|
| 350 | { |
---|
| 351 | dConvertTypes[index].pl(output,input); |
---|
| 352 | } |
---|
[0e1846] | 353 | if ((output->data==NULL) |
---|
[3f4454b] | 354 | && ((outputType!=INT_CMD) |
---|
| 355 | &&(outputType!=POLY_CMD) |
---|
| 356 | &&(outputType!=VECTOR_CMD) |
---|
| 357 | &&(outputType!=NUMBER_CMD))) |
---|
[0e1846] | 358 | { |
---|
| 359 | return TRUE; |
---|
| 360 | } |
---|
| 361 | output->next=input->next; |
---|
| 362 | input->next=NULL; |
---|
[bc669c] | 363 | //if (outputType==MATRIX_CMD) Print("convert %d -> matrix\n",inputType); |
---|
[0e1846] | 364 | return FALSE; |
---|
| 365 | } |
---|
| 366 | } |
---|
| 367 | return TRUE; |
---|
| 368 | } |
---|
| 369 | |
---|
| 370 | /*2 |
---|
| 371 | * try to convert 'inputType' in 'outputType' |
---|
| 372 | * return 0 on failure, an index (<>0) on success |
---|
| 373 | */ |
---|
| 374 | int iiTestConvert (int inputType, int outputType) |
---|
| 375 | { |
---|
| 376 | if ((inputType==outputType) |
---|
| 377 | || (outputType==DEF_CMD) |
---|
| 378 | || (outputType==IDHDL) |
---|
| 379 | || (outputType==ANY_TYPE)) |
---|
| 380 | { |
---|
| 381 | return -1; |
---|
| 382 | } |
---|
| 383 | |
---|
| 384 | if ((currRing==NULL) && (outputType>BEGIN_RING) && (outputType<END_RING)) |
---|
| 385 | return 0; |
---|
| 386 | |
---|
| 387 | // search the list |
---|
| 388 | int i=0; |
---|
| 389 | while (dConvertTypes[i].i_typ!=0) |
---|
| 390 | { |
---|
| 391 | if((dConvertTypes[i].i_typ==inputType) |
---|
| 392 | &&(dConvertTypes[i].o_typ==outputType)) |
---|
| 393 | { |
---|
| 394 | //Print("test convert %d to %d (%s -> %s):%d\n",inputType,outputType, |
---|
| 395 | //Tok2Cmdname(inputType), Tok2Cmdname(outputType),i+1); |
---|
| 396 | return i+1; |
---|
| 397 | } |
---|
| 398 | i++; |
---|
| 399 | } |
---|
| 400 | //Print("test convert %d to %d (%s -> %s):0\n",inputType,outputType, |
---|
| 401 | // Tok2Cmdname(inputType), Tok2Cmdname(outputType)); |
---|
| 402 | return 0; |
---|
| 403 | } |
---|