[0e1846] | 1 | /**************************************** |
---|
| 2 | * Computer Algebra System SINGULAR * |
---|
| 3 | ****************************************/ |
---|
[cf42ab1] | 4 | /* $Id: maps.cc,v 1.30 2000-12-06 11:03:19 Singular Exp $ */ |
---|
[0e1846] | 5 | /* |
---|
| 6 | * ABSTRACT - the mapping of polynomials to other rings |
---|
| 7 | */ |
---|
| 8 | |
---|
| 9 | #include "mod2.h" |
---|
| 10 | #include "tok.h" |
---|
| 11 | #include "febase.h" |
---|
| 12 | #include "polys.h" |
---|
| 13 | #include "numbers.h" |
---|
| 14 | #include "ring.h" |
---|
| 15 | #include "ideals.h" |
---|
| 16 | #include "matpol.h" |
---|
[512a2b] | 17 | #include "omalloc.h" |
---|
[0e1846] | 18 | #include "kstd1.h" |
---|
| 19 | #include "lists.h" |
---|
| 20 | #include "longalg.h" |
---|
| 21 | #include "maps.h" |
---|
[416465] | 22 | #include "prCopy.h" |
---|
[0e1846] | 23 | |
---|
[ec7aac] | 24 | // This is a very dirty way to "normalize" numbers w.r.t. a |
---|
| 25 | // MinPoly |
---|
| 26 | static poly pMinPolyNormalize(poly p); |
---|
| 27 | |
---|
[0b4b97] | 28 | /* debug output: Tok2Cmdname in maApplyFetch*/ |
---|
| 29 | //#include "ipshell.h" |
---|
| 30 | |
---|
[e20214] | 31 | #define MAX_MAP_DEG 128 |
---|
[0e1846] | 32 | |
---|
| 33 | /*2 |
---|
| 34 | * copy a map |
---|
| 35 | */ |
---|
| 36 | map maCopy(map theMap) |
---|
| 37 | { |
---|
| 38 | int i; |
---|
| 39 | map m=(map)idInit(IDELEMS(theMap),0); |
---|
| 40 | for (i=IDELEMS(theMap)-1; i>=0; i--) |
---|
| 41 | m->m[i] = pCopy(theMap->m[i]); |
---|
[c232af] | 42 | m->preimage=omStrDup(theMap->preimage); |
---|
[0e1846] | 43 | return m; |
---|
| 44 | } |
---|
| 45 | |
---|
[e20214] | 46 | |
---|
| 47 | /*2 |
---|
| 48 | * return the image of var(v)^pExp, where var(v) maps to p |
---|
| 49 | */ |
---|
| 50 | poly maEvalVariable(poly p, int v,int pExp,matrix s) |
---|
[0e1846] | 51 | { |
---|
[e20214] | 52 | if (pExp==1) |
---|
| 53 | return pCopy(p); |
---|
[5812c69] | 54 | |
---|
[e20214] | 55 | poly res; |
---|
| 56 | |
---|
| 57 | if((s!=NULL)&&(pExp<MAX_MAP_DEG)) |
---|
[0e1846] | 58 | { |
---|
[e20214] | 59 | int j=2; |
---|
| 60 | poly p0=p; |
---|
| 61 | // find starting point |
---|
| 62 | if(MATELEM(s,v,1)==NULL) |
---|
| 63 | { |
---|
| 64 | MATELEM(s,v,1)=pCopy(p/*theMap->m[v-1]*/); |
---|
| 65 | } |
---|
| 66 | else |
---|
| 67 | { |
---|
| 68 | while((j<=pExp)&&(MATELEM(s,v,j)!=NULL)) |
---|
| 69 | { |
---|
| 70 | j++; |
---|
| 71 | } |
---|
| 72 | p0=MATELEM(s,v,j-1); |
---|
| 73 | } |
---|
| 74 | // multiply |
---|
| 75 | for(;j<=pExp;j++) |
---|
| 76 | { |
---|
[a6a239] | 77 | p0=MATELEM(s,v,j)=ppMult_qq(p0, p); |
---|
[fdd95ca] | 78 | pNormalize(p0); |
---|
[e20214] | 79 | } |
---|
| 80 | res=pCopy(p0/*MATELEM(s,v,pExp)*/); |
---|
| 81 | } |
---|
| 82 | else //if ((p->next!=NULL)&&(p->next->next==NULL)) |
---|
| 83 | { |
---|
| 84 | res=pPower(pCopy(p),pExp); |
---|
| 85 | } |
---|
| 86 | return res; |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | poly maEvalMonom(map theMap, poly p,ring preimage_r,matrix s) |
---|
| 90 | { |
---|
| 91 | poly q=pOne(); |
---|
[0e1846] | 92 | pSetCoeff(q,nMap(pGetCoeff(p))); |
---|
| 93 | |
---|
[e20214] | 94 | int i; |
---|
| 95 | for(i=preimage_r->N; i>0; i--) |
---|
[0e1846] | 96 | { |
---|
[a6a239] | 97 | int pExp=p_GetExp( p,i,preimage_r); |
---|
[e78cce] | 98 | if (pExp != 0) |
---|
[0e1846] | 99 | { |
---|
| 100 | if (theMap->m[i-1]!=NULL) |
---|
| 101 | { |
---|
| 102 | poly p1=theMap->m[i-1]; |
---|
[e20214] | 103 | poly pp=maEvalVariable(p1,i,pExp,s); |
---|
[0e1846] | 104 | q = pMult(q,pp); |
---|
| 105 | } |
---|
| 106 | else |
---|
| 107 | { |
---|
| 108 | pDelete(&q); |
---|
| 109 | break; |
---|
| 110 | } |
---|
| 111 | } |
---|
| 112 | } |
---|
[a6a239] | 113 | int modulComp = p_GetComp( p,preimage_r); |
---|
[0e1846] | 114 | if (q!=NULL) pSetCompP(q,modulComp); |
---|
[e20214] | 115 | return q; |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | poly maEval(map theMap, poly p,ring preimage_r,matrix s) |
---|
| 119 | { |
---|
| 120 | poly result = NULL; |
---|
| 121 | int i; |
---|
| 122 | |
---|
| 123 | // for(i=1; i<=preimage_r->N; i++) |
---|
| 124 | // { |
---|
| 125 | // pTest(theMap->m[i-1]); |
---|
| 126 | // } |
---|
| 127 | // while (p!=NULL) |
---|
| 128 | // { |
---|
| 129 | // poly q=maEvalMonom(theMap,p,preimage_r,s); |
---|
| 130 | // result = pAdd(result,q); |
---|
| 131 | // pIter(p); |
---|
| 132 | // } |
---|
| 133 | if (p!=NULL) |
---|
| 134 | { |
---|
| 135 | int l = pLength(p)-1; |
---|
| 136 | poly* monoms; |
---|
| 137 | if (l>0) |
---|
| 138 | { |
---|
[c232af] | 139 | monoms = (poly*) omAlloc(l*sizeof(poly)); |
---|
[e20214] | 140 | |
---|
| 141 | for (i=0; i<l; i++) |
---|
| 142 | { |
---|
| 143 | monoms[i]=maEvalMonom(theMap,p,preimage_r,s); |
---|
| 144 | pIter(p); |
---|
| 145 | } |
---|
| 146 | } |
---|
| 147 | result=maEvalMonom(theMap,p,preimage_r,s); |
---|
| 148 | if (l>0) |
---|
| 149 | { |
---|
| 150 | for(i = l-1; i>=0; i--) |
---|
| 151 | { |
---|
| 152 | result=pAdd(result, monoms[i]); |
---|
| 153 | } |
---|
[c232af] | 154 | omFreeSize((ADDRESS)monoms,l*sizeof(poly)); |
---|
[e20214] | 155 | } |
---|
[ec7aac] | 156 | if (currRing->minpoly!=NULL) result=pMinPolyNormalize(result); |
---|
[0e1846] | 157 | } |
---|
| 158 | pTest(result); |
---|
| 159 | return result; |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | /*2 |
---|
[e78cce] | 163 | *shifts the variables between minvar and maxvar of p \in p_ring to the |
---|
[0e1846] | 164 | *first maxvar-minvar+1 variables in the actual ring |
---|
| 165 | *be carefull: there is no range check for the variables of p |
---|
| 166 | */ |
---|
[e78cce] | 167 | static poly pChangeSizeOfPoly(ring p_ring, poly p,int minvar,int maxvar) |
---|
[0e1846] | 168 | { |
---|
| 169 | int i; |
---|
| 170 | poly result = NULL,resultWorkP; |
---|
| 171 | number n; |
---|
| 172 | |
---|
| 173 | if (p==NULL) return result; |
---|
| 174 | else result = pInit(); |
---|
| 175 | resultWorkP = result; |
---|
| 176 | while (p!=NULL) |
---|
| 177 | { |
---|
| 178 | for (i=minvar;i<=maxvar;i++) |
---|
[a6a239] | 179 | pSetExp(resultWorkP,i-minvar+1,p_GetExp(p,i,p_ring)); |
---|
| 180 | pSetComp(resultWorkP,p_GetComp(p,p_ring)); |
---|
[0e1846] | 181 | n=nCopy(pGetCoeff(p)); |
---|
| 182 | pSetCoeff(resultWorkP,n); |
---|
| 183 | pSetm(resultWorkP); |
---|
| 184 | pIter(p); |
---|
| 185 | if (p!=NULL) |
---|
| 186 | { |
---|
| 187 | pNext(resultWorkP) = pInit(); |
---|
| 188 | pIter(resultWorkP); |
---|
| 189 | } |
---|
| 190 | } |
---|
| 191 | return result; |
---|
| 192 | } |
---|
| 193 | |
---|
| 194 | |
---|
| 195 | /*2 |
---|
| 196 | *returns the preimage of id under theMap, |
---|
| 197 | *if id is empty or zero the kernel is computed |
---|
| 198 | */ |
---|
| 199 | ideal maGetPreimage(ring theImageRing, map theMap, ideal id) |
---|
| 200 | { |
---|
[88ff58] | 201 | int i,j; |
---|
| 202 | int ordersize = rBlocks(currRing) + 1; |
---|
[0e1846] | 203 | poly p,pp,q; |
---|
| 204 | ideal temp1; |
---|
| 205 | ideal temp2; |
---|
[c232af] | 206 | int *orders = (int*) omAlloc0(sizeof(int)*(ordersize)); |
---|
| 207 | int *block0 = (int*) omAlloc0(sizeof(int)*(ordersize)); |
---|
| 208 | int *block1 = (int*) omAlloc0(sizeof(int)*(ordersize)); |
---|
| 209 | int **wv = (int **) omAlloc0(ordersize * sizeof(int *)); |
---|
[88ff58] | 210 | |
---|
[0e1846] | 211 | int imagepvariables = theImageRing->N; |
---|
| 212 | ring sourcering = currRing; |
---|
| 213 | int N = pVariables+imagepvariables; |
---|
[d242f68] | 214 | char** names = (char**) omAlloc0(N*sizeof(char*)); |
---|
| 215 | |
---|
| 216 | memcpy(names, currRing->names, currRing->N*sizeof(char*)); |
---|
[e78cce] | 217 | sip_sring tmpR; |
---|
[0e1846] | 218 | |
---|
| 219 | if (theImageRing->OrdSgn == 1) orders[0] = ringorder_dp; |
---|
| 220 | else orders[0] = ringorder_ls; |
---|
| 221 | block1[0] = imagepvariables; |
---|
| 222 | block0[0] = 1; |
---|
| 223 | /* |
---|
| 224 | *if (sourcering->order[blockmax]) |
---|
| 225 | *{ |
---|
| 226 | * if (sourcering->OrdSgn == 1) orders[1] = ringorder_dp; |
---|
| 227 | * else orders[1] = ringorder_ls; |
---|
| 228 | * block1[1] = N; |
---|
| 229 | *} |
---|
| 230 | *else |
---|
| 231 | */ |
---|
[88ff58] | 232 | for (i=0; i<ordersize - 1; i++) |
---|
[0e1846] | 233 | { |
---|
[88ff58] | 234 | orders[i+1] = sourcering->order[i]; |
---|
| 235 | block0[i+1] = sourcering->block0[i]+imagepvariables; |
---|
| 236 | block1[i+1] = sourcering->block1[i]+imagepvariables; |
---|
| 237 | wv[i+1] = sourcering->wvhdl[i]; |
---|
[0e1846] | 238 | } |
---|
[47faf56] | 239 | tmpR = *currRing; |
---|
[e78cce] | 240 | tmpR.N = N; |
---|
| 241 | tmpR.order = orders; |
---|
| 242 | tmpR.block0 = block0; |
---|
| 243 | tmpR.block1 = block1; |
---|
| 244 | tmpR.wvhdl = wv; |
---|
[d242f68] | 245 | tmpR.names = names; |
---|
[88ff58] | 246 | rComplete(&tmpR, 1); |
---|
| 247 | rTest(&tmpR); |
---|
[47faf56] | 248 | |
---|
| 249 | // change to new ring |
---|
[cf42ab1] | 250 | rChangeCurrRing(&tmpR); |
---|
[0e1846] | 251 | if (id==NULL) |
---|
| 252 | j = 0; |
---|
| 253 | else |
---|
| 254 | j = IDELEMS(id); |
---|
[a204ea] | 255 | int j0=j; |
---|
| 256 | if (theImageRing->qideal!=NULL) j+=IDELEMS(theImageRing->qideal); |
---|
[0e1846] | 257 | temp1 = idInit(sourcering->N+j,1); |
---|
| 258 | for (i=0;i<sourcering->N;i++) |
---|
| 259 | { |
---|
| 260 | if ((i<IDELEMS(theMap)) && (theMap->m[i]!=NULL)) |
---|
| 261 | { |
---|
[e78cce] | 262 | p = pChangeSizeOfPoly(theImageRing, theMap->m[i],1,imagepvariables); |
---|
[0e1846] | 263 | q = p; |
---|
| 264 | while (pNext(q)) pIter(q); |
---|
| 265 | pNext(q) = pOne(); |
---|
| 266 | pIter(q); |
---|
| 267 | } |
---|
| 268 | else |
---|
| 269 | q = p = pOne(); |
---|
| 270 | pGetCoeff(q)=nNeg(pGetCoeff(q)); |
---|
| 271 | pSetExp(q,i+1+imagepvariables,1); |
---|
| 272 | pSetm(q); |
---|
| 273 | temp1->m[i] = p; |
---|
| 274 | } |
---|
[a204ea] | 275 | for (i=sourcering->N;i<sourcering->N+j0;i++) |
---|
[0e1846] | 276 | { |
---|
[e78cce] | 277 | temp1->m[i] = pChangeSizeOfPoly(theImageRing, |
---|
| 278 | id->m[i-sourcering->N],1,imagepvariables); |
---|
[0e1846] | 279 | } |
---|
[a204ea] | 280 | for (i=sourcering->N+j0;i<sourcering->N+j;i++) |
---|
| 281 | { |
---|
| 282 | temp1->m[i] = pChangeSizeOfPoly(theImageRing, |
---|
| 283 | theImageRing->qideal->m[i-sourcering->N-j0], |
---|
[0b4b97] | 284 | 1,imagepvariables); |
---|
[a204ea] | 285 | } |
---|
[0e1846] | 286 | // we ignore here homogenity - may be changed later: |
---|
[a1c44e] | 287 | temp2 = kStd(temp1,NULL,isNotHomog,NULL); |
---|
[0e1846] | 288 | idDelete(&temp1); |
---|
| 289 | for (i=0;i<IDELEMS(temp2);i++) |
---|
| 290 | { |
---|
| 291 | if (pLowVar(temp2->m[i])<imagepvariables) pDelete(&(temp2->m[i])); |
---|
| 292 | } |
---|
[47faf56] | 293 | |
---|
| 294 | // let's get back to the original ring |
---|
[cf42ab1] | 295 | rChangeCurrRing(sourcering); |
---|
[0e1846] | 296 | temp1 = idInit(5,1); |
---|
| 297 | j = 0; |
---|
| 298 | for (i=0;i<IDELEMS(temp2);i++) |
---|
| 299 | { |
---|
| 300 | p = temp2->m[i]; |
---|
| 301 | if (p!=NULL) |
---|
| 302 | { |
---|
[e78cce] | 303 | q = pChangeSizeOfPoly(&tmpR, p,imagepvariables+1,N); |
---|
[0e1846] | 304 | if (j>=IDELEMS(temp1)) |
---|
| 305 | { |
---|
| 306 | pEnlargeSet(&(temp1->m),IDELEMS(temp1),5); |
---|
| 307 | IDELEMS(temp1)+=5; |
---|
| 308 | } |
---|
| 309 | temp1->m[j] = q; |
---|
| 310 | j++; |
---|
| 311 | } |
---|
| 312 | } |
---|
[d242f68] | 313 | id_Delete(&temp2, &tmpR); |
---|
[0e1846] | 314 | idSkipZeroes(temp1); |
---|
[d242f68] | 315 | rUnComplete(&tmpR); |
---|
[c232af] | 316 | omFreeSize(orders, sizeof(int)*(ordersize)); |
---|
| 317 | omFreeSize(block0, sizeof(int)*(ordersize)); |
---|
| 318 | omFreeSize(block1, sizeof(int)*(ordersize)); |
---|
| 319 | omFreeSize(wv, sizeof(int*)*(ordersize)); |
---|
[d242f68] | 320 | omFreeSize(names, (currRing->N)*sizeof(char*)); |
---|
[0e1846] | 321 | return temp1; |
---|
| 322 | } |
---|
| 323 | |
---|
[7375424] | 324 | void maFindPerm(char **preim_names, int preim_n, char **preim_par, int preim_p, |
---|
[e20214] | 325 | char **names, int n, char **par, int nop, |
---|
[811826] | 326 | int * perm, int *par_perm, int ch) |
---|
[0e1846] | 327 | { |
---|
| 328 | int i,j; |
---|
| 329 | /* find correspondig vars */ |
---|
| 330 | for (i=0; i<preim_n; i++) |
---|
| 331 | { |
---|
| 332 | for(j=0; j<n; j++) |
---|
| 333 | { |
---|
| 334 | if (strcmp(preim_names[i],names[j])==0) |
---|
| 335 | { |
---|
| 336 | if (BVERBOSE(V_IMAP)) |
---|
| 337 | Print("// var %s: nr %d -> nr %d\n",preim_names[i],i+1,j+1); |
---|
| 338 | /* var i+1 from preimage ring is var j+1 (index j+1) from image ring */ |
---|
| 339 | perm[i+1]=j+1; |
---|
| 340 | break; |
---|
| 341 | } |
---|
| 342 | } |
---|
[811826] | 343 | if ((perm[i+1]==0)&&(par!=NULL) |
---|
| 344 | // do not consider par of Fq |
---|
| 345 | && (ch < 2)) |
---|
[0e1846] | 346 | { |
---|
[cc0296] | 347 | for(j=0; j<nop; j++) |
---|
[0e1846] | 348 | { |
---|
| 349 | if (strcmp(preim_names[i],par[j])==0) |
---|
| 350 | { |
---|
| 351 | if (BVERBOSE(V_IMAP)) |
---|
| 352 | Print("// var %s: nr %d -> par %d\n",preim_names[i],i+1,j+1); |
---|
| 353 | /* var i+1 from preimage ring is par j+1 (index j) from image ring */ |
---|
| 354 | perm[i+1]=-(j+1); |
---|
| 355 | } |
---|
| 356 | } |
---|
| 357 | } |
---|
| 358 | } |
---|
| 359 | if (par_perm!=NULL) |
---|
| 360 | { |
---|
[7375424] | 361 | for (i=0; i<preim_p; i++) |
---|
[0e1846] | 362 | { |
---|
| 363 | for(j=0; j<n; j++) |
---|
| 364 | { |
---|
| 365 | if (strcmp(preim_par[i],names[j])==0) |
---|
| 366 | { |
---|
| 367 | if (BVERBOSE(V_IMAP)) |
---|
| 368 | Print("// par %s: par %d -> nr %d\n",preim_par[i],i+1,j+1); |
---|
| 369 | /* var i+1 from preimage ring is var j+1 (index j+1) from image ring */ |
---|
| 370 | par_perm[i]=j+1; |
---|
| 371 | break; |
---|
| 372 | } |
---|
| 373 | } |
---|
| 374 | if ((par!=NULL) && (par_perm[i]==0)) |
---|
| 375 | { |
---|
[cc0296] | 376 | for(j=0; j<nop; j++) |
---|
[0e1846] | 377 | { |
---|
| 378 | if (strcmp(preim_par[i],par[j])==0) |
---|
| 379 | { |
---|
| 380 | if (BVERBOSE(V_IMAP)) |
---|
| 381 | Print("// par %s: nr %d -> par %d\n",preim_par[i],i+1,j+1); |
---|
| 382 | /* var i+1 from preimage ring is par j+1 (index j) from image ring */ |
---|
| 383 | par_perm[i]=-(j+1); |
---|
| 384 | } |
---|
| 385 | } |
---|
| 386 | } |
---|
| 387 | } |
---|
| 388 | } |
---|
| 389 | } |
---|
| 390 | |
---|
| 391 | /*2 |
---|
| 392 | * embeds poly p from the subring r into the current ring |
---|
| 393 | */ |
---|
| 394 | poly maIMap(ring r, poly p) |
---|
| 395 | { |
---|
| 396 | /* the simplest case:*/ |
---|
| 397 | if(r==currRing) return pCopy(p); |
---|
[d4373f] | 398 | //nSetMap(rInternalChar(r),r->parameter,rPar(r),r->minpoly); |
---|
| 399 | nSetMap(r); |
---|
[c232af] | 400 | int *perm=(int *)omAlloc0((r->N+1)*sizeof(int)); |
---|
| 401 | //int *par_perm=(int *)omAlloc0(rPar(r)*sizeof(int)); |
---|
[811826] | 402 | maFindPerm(r->names,r->N, r->parameter, r->P, |
---|
| 403 | currRing->names,currRing->N,currRing->parameter, currRing->P, |
---|
| 404 | perm,NULL, currRing->ch); |
---|
[e78cce] | 405 | poly res=pPermPoly(p,perm,r/*,par_perm,rPar(r)*/); |
---|
[c232af] | 406 | omFreeSize((ADDRESS)perm,(r->N+1)*sizeof(int)); |
---|
| 407 | //omFreeSize((ADDRESS)par_perm,rPar(r)*sizeof(int)); |
---|
[0e1846] | 408 | return res; |
---|
| 409 | } |
---|
| 410 | |
---|
| 411 | /*3 |
---|
| 412 | * find the max. degree in one variable, but not larger than MAX_MAP_DEG |
---|
| 413 | */ |
---|
[e78cce] | 414 | static int maMaxDeg_Ma(ideal a,ring preimage_r) |
---|
[0e1846] | 415 | { |
---|
| 416 | int i,j; |
---|
[e78cce] | 417 | int N = preimage_r->N; |
---|
[0e1846] | 418 | poly p; |
---|
[c232af] | 419 | int *m=(int *)omAlloc0(N*sizeof(int)); |
---|
[0e1846] | 420 | |
---|
| 421 | for (i=MATROWS(a)*MATCOLS(a)-1;i>=0;i--) |
---|
| 422 | { |
---|
| 423 | p=a->m[i]; |
---|
| 424 | //pTest(p); // cannot test p because it is from another ring |
---|
| 425 | while(p!=NULL) |
---|
| 426 | { |
---|
| 427 | for(j=N-1;j>=0;j--) |
---|
| 428 | { |
---|
[a6a239] | 429 | m[j]=max(m[j],p_GetExp( p,j+1,preimage_r)); |
---|
[0e1846] | 430 | if (m[j]>=MAX_MAP_DEG) |
---|
| 431 | { |
---|
| 432 | i=MAX_MAP_DEG; |
---|
| 433 | goto max_deg_fertig_id; |
---|
| 434 | } |
---|
| 435 | } |
---|
| 436 | pIter(p); |
---|
| 437 | } |
---|
| 438 | } |
---|
| 439 | i=m[0]; |
---|
| 440 | for(j=N-1;j>0;j--) |
---|
| 441 | { |
---|
| 442 | i=max(i,m[j]); |
---|
| 443 | } |
---|
| 444 | max_deg_fertig_id: |
---|
[c232af] | 445 | omFreeSize((ADDRESS)m,N*sizeof(int)); |
---|
[0e1846] | 446 | return i; |
---|
| 447 | } |
---|
| 448 | |
---|
| 449 | /*3 |
---|
| 450 | * find the max. degree in one variable, but not larger than MAX_MAP_DEG |
---|
| 451 | */ |
---|
[e78cce] | 452 | static int maMaxDeg_P(poly p,ring preimage_r) |
---|
[0e1846] | 453 | { |
---|
| 454 | int i,j; |
---|
[e78cce] | 455 | int N = preimage_r->N; |
---|
[c232af] | 456 | int *m=(int *)omAlloc0(N*sizeof(int)); |
---|
[0e1846] | 457 | |
---|
[47faf56] | 458 | // pTest(p); |
---|
[0e1846] | 459 | while(p!=NULL) |
---|
| 460 | { |
---|
| 461 | for(j=N-1;j>=0;j--) |
---|
| 462 | { |
---|
[a6a239] | 463 | m[j]=max(m[j],p_GetExp(p,j+1,preimage_r)); |
---|
[0e1846] | 464 | if (m[j]>=MAX_MAP_DEG) |
---|
| 465 | { |
---|
| 466 | i=MAX_MAP_DEG; |
---|
| 467 | goto max_deg_fertig_p; |
---|
| 468 | } |
---|
| 469 | } |
---|
| 470 | pIter(p); |
---|
| 471 | } |
---|
| 472 | i=m[0]; |
---|
| 473 | for(j=N-1;j>0;j--) |
---|
| 474 | { |
---|
| 475 | i=max(i,m[j]); |
---|
| 476 | } |
---|
| 477 | max_deg_fertig_p: |
---|
[c232af] | 478 | omFreeSize((ADDRESS)m,N*sizeof(int)); |
---|
[0e1846] | 479 | return i; |
---|
| 480 | } |
---|
| 481 | |
---|
| 482 | /*2 |
---|
| 483 | * maps the expression w to res, |
---|
| 484 | * switch what: MAP_CMD: use theMap for mapping, N for preimage ring |
---|
[0b4b97] | 485 | * //FETCH_CMD: use pOrdPoly for mapping |
---|
[0e1846] | 486 | * IMAP_CMD: use perm for mapping, N for preimage ring |
---|
| 487 | * default: map only poly-structures, |
---|
| 488 | * use perm and par_perm, N and P, |
---|
| 489 | */ |
---|
[e78cce] | 490 | BOOLEAN maApplyFetch(int what,map theMap,leftv res, leftv w, ring preimage_r, |
---|
| 491 | int *perm, int *par_perm, int P) |
---|
[0e1846] | 492 | { |
---|
| 493 | int i; |
---|
[e78cce] | 494 | int N = preimage_r->N; |
---|
[0b4b97] | 495 | //Print("N=%d what=%s ",N,Tok2Cmdname(what)); |
---|
[0e1846] | 496 | //if (perm!=NULL) for(i=1;i<=N;i++) Print("%d -> %d ",i,perm[i]); |
---|
| 497 | //PrintS("\n"); |
---|
| 498 | //Print("P=%d ",P); |
---|
| 499 | //if (par_perm!=NULL) for(i=0;i<P;i++) Print("%d -> %d ",i,par_perm[i]); |
---|
| 500 | //PrintS("\n"); |
---|
| 501 | void *data=w->Data(); |
---|
| 502 | res->rtyp = w->rtyp; |
---|
| 503 | switch (w->rtyp) |
---|
| 504 | { |
---|
| 505 | case NUMBER_CMD: |
---|
| 506 | if (P!=0) |
---|
| 507 | { |
---|
| 508 | res->data=(void *)naPermNumber((number)data,par_perm,P); |
---|
| 509 | res->rtyp=POLY_CMD; |
---|
| 510 | if (currRing->minpoly!=NULL) |
---|
[ec7aac] | 511 | res->data=(void *)pMinPolyNormalize((poly)res->data); |
---|
[74fe824] | 512 | pTest((poly) res->data); |
---|
[0e1846] | 513 | } |
---|
| 514 | else |
---|
| 515 | { |
---|
| 516 | res->data=(void *)nMap((number)data); |
---|
| 517 | if (currRing->minpoly!=NULL) |
---|
| 518 | { |
---|
| 519 | number a=(number)res->data; |
---|
[74fe824] | 520 | nNormalize(a); |
---|
| 521 | res->data=(void *)a; |
---|
[0e1846] | 522 | } |
---|
[74fe824] | 523 | nTest((number) res->data); |
---|
[0e1846] | 524 | } |
---|
| 525 | break; |
---|
| 526 | case POLY_CMD: |
---|
| 527 | case VECTOR_CMD: |
---|
[74fe824] | 528 | if ((what==FETCH_CMD)&& (nMap==nCopy)) |
---|
[416465] | 529 | res->data=(void *)prCopyR( (poly)data, preimage_r); |
---|
[0b4b97] | 530 | else |
---|
[74fe824] | 531 | if ((what==IMAP_CMD) || ((what==FETCH_CMD) /* && (nMap!=nCopy)*/)) |
---|
[e78cce] | 532 | res->data=(void *)pPermPoly((poly)data,perm,preimage_r,par_perm,P); |
---|
[0e1846] | 533 | else /*if (what==MAP_CMD)*/ |
---|
| 534 | { |
---|
[e78cce] | 535 | matrix s=mpNew(N,maMaxDeg_P((poly)data, preimage_r)); |
---|
| 536 | res->data=(void *)maEval(theMap,(poly)data,preimage_r,s); |
---|
[0e1846] | 537 | idDelete((ideal *)&s); |
---|
| 538 | } |
---|
| 539 | if (currRing->minpoly!=NULL) |
---|
[ec7aac] | 540 | res->data=(void *)pMinPolyNormalize((poly)res->data); |
---|
[0e1846] | 541 | pTest((poly)res->data); |
---|
| 542 | break; |
---|
| 543 | case MODUL_CMD: |
---|
| 544 | case MATRIX_CMD: |
---|
| 545 | case IDEAL_CMD: |
---|
| 546 | case MAP_CMD: |
---|
| 547 | { |
---|
| 548 | int C=((matrix)data)->cols(); |
---|
| 549 | int R; |
---|
| 550 | if (w->rtyp==MAP_CMD) R=1; |
---|
| 551 | else R=((matrix)data)->rows(); |
---|
| 552 | matrix m=mpNew(R,C); |
---|
| 553 | char *tmpR=NULL; |
---|
| 554 | if(w->rtyp==MAP_CMD) |
---|
| 555 | { |
---|
| 556 | tmpR=((map)data)->preimage; |
---|
| 557 | ((matrix)data)->rank=((matrix)data)->rows(); |
---|
| 558 | } |
---|
| 559 | if (what==FETCH_CMD) |
---|
| 560 | { |
---|
| 561 | for (i=R*C-1;i>=0;i--) |
---|
| 562 | { |
---|
[416465] | 563 | m->m[i]=prCopyR(((ideal)data)->m[i], preimage_r); |
---|
[0e1846] | 564 | pTest(m->m[i]); |
---|
| 565 | } |
---|
| 566 | } |
---|
[0b4b97] | 567 | else |
---|
| 568 | if (what==IMAP_CMD) |
---|
[0e1846] | 569 | { |
---|
| 570 | for (i=R*C-1;i>=0;i--) |
---|
| 571 | { |
---|
[e78cce] | 572 | m->m[i]=pPermPoly(((ideal)data)->m[i],perm,preimage_r,par_perm,P); |
---|
[0e1846] | 573 | pTest(m->m[i]); |
---|
| 574 | } |
---|
| 575 | } |
---|
| 576 | else /* if(what==MAP_CMD) */ |
---|
| 577 | { |
---|
[e78cce] | 578 | matrix s=mpNew(N,maMaxDeg_Ma((ideal)data,preimage_r)); |
---|
[0e1846] | 579 | for (i=R*C-1;i>=0;i--) |
---|
| 580 | { |
---|
[e78cce] | 581 | m->m[i]=maEval(theMap,((ideal)data)->m[i],preimage_r,s); |
---|
[0e1846] | 582 | pTest(m->m[i]); |
---|
| 583 | } |
---|
| 584 | idDelete((ideal *)&s); |
---|
| 585 | } |
---|
| 586 | if (currRing->minpoly!=NULL) |
---|
| 587 | { |
---|
| 588 | for (i=R*C-1;i>=0;i--) |
---|
| 589 | { |
---|
[ec7aac] | 590 | m->m[i]=pMinPolyNormalize(m->m[i]); |
---|
[0e1846] | 591 | pTest(m->m[i]); |
---|
| 592 | } |
---|
| 593 | } |
---|
| 594 | if(w->rtyp==MAP_CMD) |
---|
| 595 | { |
---|
| 596 | ((map)data)->preimage=tmpR; |
---|
[c232af] | 597 | ((map)m)->preimage=omStrDup(tmpR); |
---|
[0e1846] | 598 | } |
---|
| 599 | else |
---|
| 600 | { |
---|
| 601 | m->rank=((matrix)data)->rank; |
---|
| 602 | } |
---|
| 603 | res->data=(char *)m; |
---|
[88ff58] | 604 | idTest((ideal) m); |
---|
[0e1846] | 605 | break; |
---|
| 606 | } |
---|
[88ff58] | 607 | |
---|
[0e1846] | 608 | case LIST_CMD: |
---|
| 609 | { |
---|
| 610 | lists l=(lists)data; |
---|
[c232af] | 611 | lists ml=(lists)omAllocBin(slists_bin); |
---|
[0e1846] | 612 | ml->Init(l->nr+1); |
---|
| 613 | for(i=0;i<=l->nr;i++) |
---|
| 614 | { |
---|
| 615 | if (((l->m[i].rtyp>BEGIN_RING)&&(l->m[i].rtyp<END_RING)) |
---|
| 616 | ||(l->m[i].rtyp==LIST_CMD)) |
---|
| 617 | { |
---|
[e78cce] | 618 | if (maApplyFetch(what,theMap,&ml->m[i],&l->m[i], |
---|
| 619 | preimage_r,perm,par_perm,P)) |
---|
[0e1846] | 620 | { |
---|
| 621 | ml->Clean(); |
---|
[c232af] | 622 | omFreeBin((ADDRESS)ml, slists_bin); |
---|
[0e1846] | 623 | res->rtyp=0; |
---|
| 624 | return TRUE; |
---|
| 625 | } |
---|
| 626 | } |
---|
| 627 | else |
---|
| 628 | { |
---|
| 629 | ml->m[i].Copy(&l->m[i]); |
---|
| 630 | } |
---|
| 631 | } |
---|
| 632 | res->data=(char *)ml; |
---|
| 633 | break; |
---|
| 634 | } |
---|
| 635 | default: |
---|
| 636 | { |
---|
| 637 | return TRUE; |
---|
| 638 | } |
---|
| 639 | } |
---|
| 640 | return FALSE; |
---|
| 641 | } |
---|
| 642 | |
---|
[ec7aac] | 643 | // This is a very dirty way to cancel monoms whose number equals the |
---|
| 644 | // MinPoly |
---|
| 645 | static poly pMinPolyNormalize(poly p) |
---|
| 646 | { |
---|
| 647 | number one = nInit(1); |
---|
| 648 | spolyrec rp; |
---|
| 649 | |
---|
| 650 | poly q = &rp; |
---|
| 651 | |
---|
| 652 | while (p != NULL) |
---|
| 653 | { |
---|
| 654 | // this returns 0, if p == MinPoly |
---|
| 655 | number product = nMult(pGetCoeff(p), one); |
---|
| 656 | if (product == NULL) |
---|
| 657 | { |
---|
[512a2b] | 658 | pDeleteLm(&p); |
---|
[ec7aac] | 659 | } |
---|
| 660 | else |
---|
| 661 | { |
---|
| 662 | pSetCoeff(p, product); |
---|
| 663 | pNext(q) = p; |
---|
| 664 | q = p; |
---|
| 665 | p = pNext(p); |
---|
| 666 | } |
---|
| 667 | } |
---|
| 668 | pNext(q) = NULL; |
---|
| 669 | return rp.next; |
---|
| 670 | } |
---|