[0e1846] | 1 | /**************************************** |
---|
| 2 | * Computer Algebra System SINGULAR * |
---|
| 3 | ****************************************/ |
---|
[dd2714] | 4 | /* $Id: maps.cc,v 1.38 2002-01-22 09:57:53 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 |
---|
[367e88] | 25 | // MinPoly |
---|
[ec7aac] | 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 | |
---|
[4508ce5] | 89 | static poly maEvalMonom(map theMap, poly p,ring preimage_r,matrix s, nMapFunc nMap) |
---|
[e20214] | 90 | { |
---|
[23d5e6e] | 91 | poly q=pNSet(nMap(pGetCoeff(p))); |
---|
[0e1846] | 92 | |
---|
[e20214] | 93 | int i; |
---|
| 94 | for(i=preimage_r->N; i>0; i--) |
---|
[0e1846] | 95 | { |
---|
[a6a239] | 96 | int pExp=p_GetExp( p,i,preimage_r); |
---|
[e78cce] | 97 | if (pExp != 0) |
---|
[0e1846] | 98 | { |
---|
| 99 | if (theMap->m[i-1]!=NULL) |
---|
| 100 | { |
---|
| 101 | poly p1=theMap->m[i-1]; |
---|
[e20214] | 102 | poly pp=maEvalVariable(p1,i,pExp,s); |
---|
[0e1846] | 103 | q = pMult(q,pp); |
---|
| 104 | } |
---|
| 105 | else |
---|
| 106 | { |
---|
| 107 | pDelete(&q); |
---|
| 108 | break; |
---|
| 109 | } |
---|
| 110 | } |
---|
| 111 | } |
---|
[a6a239] | 112 | int modulComp = p_GetComp( p,preimage_r); |
---|
[0e1846] | 113 | if (q!=NULL) pSetCompP(q,modulComp); |
---|
[e20214] | 114 | return q; |
---|
| 115 | } |
---|
| 116 | |
---|
[4508ce5] | 117 | poly maEval(map theMap, poly p,ring preimage_r,nMapFunc nMap,matrix s) |
---|
[e20214] | 118 | { |
---|
| 119 | poly result = NULL; |
---|
| 120 | int i; |
---|
| 121 | |
---|
| 122 | // for(i=1; i<=preimage_r->N; i++) |
---|
| 123 | // { |
---|
| 124 | // pTest(theMap->m[i-1]); |
---|
| 125 | // } |
---|
| 126 | // while (p!=NULL) |
---|
| 127 | // { |
---|
| 128 | // poly q=maEvalMonom(theMap,p,preimage_r,s); |
---|
| 129 | // result = pAdd(result,q); |
---|
| 130 | // pIter(p); |
---|
| 131 | // } |
---|
| 132 | if (p!=NULL) |
---|
| 133 | { |
---|
| 134 | int l = pLength(p)-1; |
---|
| 135 | poly* monoms; |
---|
| 136 | if (l>0) |
---|
| 137 | { |
---|
[c232af] | 138 | monoms = (poly*) omAlloc(l*sizeof(poly)); |
---|
[e20214] | 139 | |
---|
| 140 | for (i=0; i<l; i++) |
---|
| 141 | { |
---|
[4508ce5] | 142 | monoms[i]=maEvalMonom(theMap,p,preimage_r,s, nMap); |
---|
[e20214] | 143 | pIter(p); |
---|
| 144 | } |
---|
| 145 | } |
---|
[4508ce5] | 146 | result=maEvalMonom(theMap,p,preimage_r,s, nMap); |
---|
[e20214] | 147 | if (l>0) |
---|
| 148 | { |
---|
| 149 | for(i = l-1; i>=0; i--) |
---|
| 150 | { |
---|
| 151 | result=pAdd(result, monoms[i]); |
---|
| 152 | } |
---|
[c232af] | 153 | omFreeSize((ADDRESS)monoms,l*sizeof(poly)); |
---|
[e20214] | 154 | } |
---|
[ec7aac] | 155 | if (currRing->minpoly!=NULL) result=pMinPolyNormalize(result); |
---|
[0e1846] | 156 | } |
---|
| 157 | pTest(result); |
---|
| 158 | return result; |
---|
| 159 | } |
---|
| 160 | |
---|
| 161 | /*2 |
---|
[e78cce] | 162 | *shifts the variables between minvar and maxvar of p \in p_ring to the |
---|
[0e1846] | 163 | *first maxvar-minvar+1 variables in the actual ring |
---|
| 164 | *be carefull: there is no range check for the variables of p |
---|
| 165 | */ |
---|
[e78cce] | 166 | static poly pChangeSizeOfPoly(ring p_ring, poly p,int minvar,int maxvar) |
---|
[0e1846] | 167 | { |
---|
| 168 | int i; |
---|
| 169 | poly result = NULL,resultWorkP; |
---|
| 170 | number n; |
---|
| 171 | |
---|
| 172 | if (p==NULL) return result; |
---|
| 173 | else result = pInit(); |
---|
| 174 | resultWorkP = result; |
---|
| 175 | while (p!=NULL) |
---|
| 176 | { |
---|
| 177 | for (i=minvar;i<=maxvar;i++) |
---|
[a6a239] | 178 | pSetExp(resultWorkP,i-minvar+1,p_GetExp(p,i,p_ring)); |
---|
| 179 | pSetComp(resultWorkP,p_GetComp(p,p_ring)); |
---|
[0e1846] | 180 | n=nCopy(pGetCoeff(p)); |
---|
| 181 | pSetCoeff(resultWorkP,n); |
---|
| 182 | pSetm(resultWorkP); |
---|
| 183 | pIter(p); |
---|
| 184 | if (p!=NULL) |
---|
| 185 | { |
---|
| 186 | pNext(resultWorkP) = pInit(); |
---|
| 187 | pIter(resultWorkP); |
---|
| 188 | } |
---|
| 189 | } |
---|
| 190 | return result; |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | |
---|
| 194 | /*2 |
---|
| 195 | *returns the preimage of id under theMap, |
---|
| 196 | *if id is empty or zero the kernel is computed |
---|
| 197 | */ |
---|
| 198 | ideal maGetPreimage(ring theImageRing, map theMap, ideal id) |
---|
| 199 | { |
---|
[88ff58] | 200 | int i,j; |
---|
| 201 | int ordersize = rBlocks(currRing) + 1; |
---|
[0e1846] | 202 | poly p,pp,q; |
---|
| 203 | ideal temp1; |
---|
| 204 | ideal temp2; |
---|
[c232af] | 205 | int *orders = (int*) omAlloc0(sizeof(int)*(ordersize)); |
---|
| 206 | int *block0 = (int*) omAlloc0(sizeof(int)*(ordersize)); |
---|
| 207 | int *block1 = (int*) omAlloc0(sizeof(int)*(ordersize)); |
---|
| 208 | int **wv = (int **) omAlloc0(ordersize * sizeof(int *)); |
---|
[88ff58] | 209 | |
---|
[0e1846] | 210 | int imagepvariables = theImageRing->N; |
---|
| 211 | ring sourcering = currRing; |
---|
| 212 | int N = pVariables+imagepvariables; |
---|
[d242f68] | 213 | char** names = (char**) omAlloc0(N*sizeof(char*)); |
---|
| 214 | |
---|
| 215 | memcpy(names, currRing->names, currRing->N*sizeof(char*)); |
---|
[c22d84] | 216 | memcpy(&(names[currRing->N]), theImageRing->names, |
---|
| 217 | (theImageRing->N*sizeof(char*))); |
---|
[e78cce] | 218 | sip_sring tmpR; |
---|
[0e1846] | 219 | |
---|
[23d5e6e] | 220 | //if (theImageRing->OrdSgn == 1)s |
---|
| 221 | orders[0] = ringorder_dp; |
---|
| 222 | //else orders[0] = ringorder_ls; |
---|
[0e1846] | 223 | block1[0] = imagepvariables; |
---|
| 224 | block0[0] = 1; |
---|
| 225 | /* |
---|
| 226 | *if (sourcering->order[blockmax]) |
---|
| 227 | *{ |
---|
| 228 | * if (sourcering->OrdSgn == 1) orders[1] = ringorder_dp; |
---|
| 229 | * else orders[1] = ringorder_ls; |
---|
| 230 | * block1[1] = N; |
---|
| 231 | *} |
---|
| 232 | *else |
---|
| 233 | */ |
---|
[88ff58] | 234 | for (i=0; i<ordersize - 1; i++) |
---|
[0e1846] | 235 | { |
---|
[88ff58] | 236 | orders[i+1] = sourcering->order[i]; |
---|
| 237 | block0[i+1] = sourcering->block0[i]+imagepvariables; |
---|
| 238 | block1[i+1] = sourcering->block1[i]+imagepvariables; |
---|
| 239 | wv[i+1] = sourcering->wvhdl[i]; |
---|
[0e1846] | 240 | } |
---|
[47faf56] | 241 | tmpR = *currRing; |
---|
[e78cce] | 242 | tmpR.N = N; |
---|
| 243 | tmpR.order = orders; |
---|
| 244 | tmpR.block0 = block0; |
---|
| 245 | tmpR.block1 = block1; |
---|
| 246 | tmpR.wvhdl = wv; |
---|
[d242f68] | 247 | tmpR.names = names; |
---|
[88ff58] | 248 | rComplete(&tmpR, 1); |
---|
| 249 | rTest(&tmpR); |
---|
[47faf56] | 250 | |
---|
| 251 | // change to new ring |
---|
[cf42ab1] | 252 | rChangeCurrRing(&tmpR); |
---|
[0e1846] | 253 | if (id==NULL) |
---|
| 254 | j = 0; |
---|
| 255 | else |
---|
| 256 | j = IDELEMS(id); |
---|
[a204ea] | 257 | int j0=j; |
---|
| 258 | if (theImageRing->qideal!=NULL) j+=IDELEMS(theImageRing->qideal); |
---|
[0e1846] | 259 | temp1 = idInit(sourcering->N+j,1); |
---|
| 260 | for (i=0;i<sourcering->N;i++) |
---|
| 261 | { |
---|
| 262 | if ((i<IDELEMS(theMap)) && (theMap->m[i]!=NULL)) |
---|
| 263 | { |
---|
[23d5e6e] | 264 | p = pSort(pChangeSizeOfPoly(theImageRing,theMap->m[i],1,imagepvariables)); |
---|
[0e1846] | 265 | q = p; |
---|
[23d5e6e] | 266 | while (pNext(q)!=NULL) pIter(q); |
---|
| 267 | pNext(q) = pISet(-1); |
---|
[0e1846] | 268 | pIter(q); |
---|
| 269 | } |
---|
| 270 | else |
---|
[23d5e6e] | 271 | q = p = pISet(-1); |
---|
[0e1846] | 272 | pSetExp(q,i+1+imagepvariables,1); |
---|
| 273 | pSetm(q); |
---|
| 274 | temp1->m[i] = p; |
---|
| 275 | } |
---|
[a204ea] | 276 | for (i=sourcering->N;i<sourcering->N+j0;i++) |
---|
[0e1846] | 277 | { |
---|
[23d5e6e] | 278 | temp1->m[i] = pSort(pChangeSizeOfPoly(theImageRing, |
---|
| 279 | id->m[i-sourcering->N],1,imagepvariables)); |
---|
[0e1846] | 280 | } |
---|
[a204ea] | 281 | for (i=sourcering->N+j0;i<sourcering->N+j;i++) |
---|
| 282 | { |
---|
[23d5e6e] | 283 | temp1->m[i] = pSort(pChangeSizeOfPoly(theImageRing, |
---|
[a204ea] | 284 | theImageRing->qideal->m[i-sourcering->N-j0], |
---|
[23d5e6e] | 285 | 1,imagepvariables)); |
---|
[a204ea] | 286 | } |
---|
[0e1846] | 287 | // we ignore here homogenity - may be changed later: |
---|
[a1c44e] | 288 | temp2 = kStd(temp1,NULL,isNotHomog,NULL); |
---|
[0e1846] | 289 | idDelete(&temp1); |
---|
| 290 | for (i=0;i<IDELEMS(temp2);i++) |
---|
| 291 | { |
---|
| 292 | if (pLowVar(temp2->m[i])<imagepvariables) pDelete(&(temp2->m[i])); |
---|
| 293 | } |
---|
[47faf56] | 294 | |
---|
| 295 | // let's get back to the original ring |
---|
[cf42ab1] | 296 | rChangeCurrRing(sourcering); |
---|
[0e1846] | 297 | temp1 = idInit(5,1); |
---|
| 298 | j = 0; |
---|
| 299 | for (i=0;i<IDELEMS(temp2);i++) |
---|
| 300 | { |
---|
| 301 | p = temp2->m[i]; |
---|
| 302 | if (p!=NULL) |
---|
| 303 | { |
---|
[e78cce] | 304 | q = pChangeSizeOfPoly(&tmpR, p,imagepvariables+1,N); |
---|
[0e1846] | 305 | if (j>=IDELEMS(temp1)) |
---|
| 306 | { |
---|
| 307 | pEnlargeSet(&(temp1->m),IDELEMS(temp1),5); |
---|
| 308 | IDELEMS(temp1)+=5; |
---|
| 309 | } |
---|
| 310 | temp1->m[j] = q; |
---|
| 311 | j++; |
---|
| 312 | } |
---|
| 313 | } |
---|
[d242f68] | 314 | id_Delete(&temp2, &tmpR); |
---|
[0e1846] | 315 | idSkipZeroes(temp1); |
---|
[d242f68] | 316 | rUnComplete(&tmpR); |
---|
[c232af] | 317 | omFreeSize(orders, sizeof(int)*(ordersize)); |
---|
| 318 | omFreeSize(block0, sizeof(int)*(ordersize)); |
---|
| 319 | omFreeSize(block1, sizeof(int)*(ordersize)); |
---|
| 320 | omFreeSize(wv, sizeof(int*)*(ordersize)); |
---|
[d242f68] | 321 | omFreeSize(names, (currRing->N)*sizeof(char*)); |
---|
[0e1846] | 322 | return temp1; |
---|
| 323 | } |
---|
| 324 | |
---|
[7375424] | 325 | void maFindPerm(char **preim_names, int preim_n, char **preim_par, int preim_p, |
---|
[e20214] | 326 | char **names, int n, char **par, int nop, |
---|
[811826] | 327 | int * perm, int *par_perm, int ch) |
---|
[0e1846] | 328 | { |
---|
| 329 | int i,j; |
---|
| 330 | /* find correspondig vars */ |
---|
| 331 | for (i=0; i<preim_n; i++) |
---|
| 332 | { |
---|
| 333 | for(j=0; j<n; j++) |
---|
| 334 | { |
---|
| 335 | if (strcmp(preim_names[i],names[j])==0) |
---|
| 336 | { |
---|
| 337 | if (BVERBOSE(V_IMAP)) |
---|
| 338 | Print("// var %s: nr %d -> nr %d\n",preim_names[i],i+1,j+1); |
---|
| 339 | /* var i+1 from preimage ring is var j+1 (index j+1) from image ring */ |
---|
| 340 | perm[i+1]=j+1; |
---|
| 341 | break; |
---|
| 342 | } |
---|
| 343 | } |
---|
[811826] | 344 | if ((perm[i+1]==0)&&(par!=NULL) |
---|
| 345 | // do not consider par of Fq |
---|
| 346 | && (ch < 2)) |
---|
[0e1846] | 347 | { |
---|
[cc0296] | 348 | for(j=0; j<nop; j++) |
---|
[0e1846] | 349 | { |
---|
| 350 | if (strcmp(preim_names[i],par[j])==0) |
---|
| 351 | { |
---|
| 352 | if (BVERBOSE(V_IMAP)) |
---|
| 353 | Print("// var %s: nr %d -> par %d\n",preim_names[i],i+1,j+1); |
---|
| 354 | /* var i+1 from preimage ring is par j+1 (index j) from image ring */ |
---|
| 355 | perm[i+1]=-(j+1); |
---|
| 356 | } |
---|
| 357 | } |
---|
| 358 | } |
---|
| 359 | } |
---|
| 360 | if (par_perm!=NULL) |
---|
| 361 | { |
---|
[7375424] | 362 | for (i=0; i<preim_p; i++) |
---|
[0e1846] | 363 | { |
---|
| 364 | for(j=0; j<n; j++) |
---|
| 365 | { |
---|
| 366 | if (strcmp(preim_par[i],names[j])==0) |
---|
| 367 | { |
---|
| 368 | if (BVERBOSE(V_IMAP)) |
---|
| 369 | Print("// par %s: par %d -> nr %d\n",preim_par[i],i+1,j+1); |
---|
[c22d84] | 370 | /*par i+1 from preimage ring is var j+1 (index j+1) from image ring*/ |
---|
[0e1846] | 371 | par_perm[i]=j+1; |
---|
| 372 | break; |
---|
| 373 | } |
---|
| 374 | } |
---|
| 375 | if ((par!=NULL) && (par_perm[i]==0)) |
---|
| 376 | { |
---|
[cc0296] | 377 | for(j=0; j<nop; j++) |
---|
[0e1846] | 378 | { |
---|
| 379 | if (strcmp(preim_par[i],par[j])==0) |
---|
| 380 | { |
---|
| 381 | if (BVERBOSE(V_IMAP)) |
---|
| 382 | Print("// par %s: nr %d -> par %d\n",preim_par[i],i+1,j+1); |
---|
[c22d84] | 383 | /*par i+1 from preimage ring is par j+1 (index j) from image ring */ |
---|
[0e1846] | 384 | par_perm[i]=-(j+1); |
---|
| 385 | } |
---|
| 386 | } |
---|
| 387 | } |
---|
| 388 | } |
---|
| 389 | } |
---|
| 390 | } |
---|
| 391 | |
---|
| 392 | /*2 |
---|
| 393 | * embeds poly p from the subring r into the current ring |
---|
| 394 | */ |
---|
| 395 | poly maIMap(ring r, poly p) |
---|
| 396 | { |
---|
| 397 | /* the simplest case:*/ |
---|
| 398 | if(r==currRing) return pCopy(p); |
---|
[4508ce5] | 399 | nMapFunc nMap=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); |
---|
[4508ce5] | 405 | poly res=pPermPoly(p,perm,r, nMap /*,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, |
---|
[4508ce5] | 491 | int *perm, int *par_perm, int P, nMapFunc nMap) |
---|
[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 | { |
---|
[b98976] | 508 | res->data=(void *)naPermNumber((number)data,par_perm,P, preimage_r); |
---|
[0e1846] | 509 | res->rtyp=POLY_CMD; |
---|
| 510 | if (currRing->minpoly!=NULL) |
---|
[ec7aac] | 511 | res->data=(void *)pMinPolyNormalize((poly)res->data); |
---|
[367e88] | 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; |
---|
[367e88] | 520 | nNormalize(a); |
---|
[74fe824] | 521 | res->data=(void *)a; |
---|
[0e1846] | 522 | } |
---|
[50cbdc] | 523 | #ifdef LDEBUG |
---|
[74fe824] | 524 | nTest((number) res->data); |
---|
[50cbdc] | 525 | #endif |
---|
[0e1846] | 526 | } |
---|
| 527 | break; |
---|
| 528 | case POLY_CMD: |
---|
| 529 | case VECTOR_CMD: |
---|
[74fe824] | 530 | if ((what==FETCH_CMD)&& (nMap==nCopy)) |
---|
[416465] | 531 | res->data=(void *)prCopyR( (poly)data, preimage_r); |
---|
[0b4b97] | 532 | else |
---|
[74fe824] | 533 | if ((what==IMAP_CMD) || ((what==FETCH_CMD) /* && (nMap!=nCopy)*/)) |
---|
[4508ce5] | 534 | res->data=(void *)pPermPoly((poly)data,perm,preimage_r,nMap,par_perm,P); |
---|
[0e1846] | 535 | else /*if (what==MAP_CMD)*/ |
---|
| 536 | { |
---|
[e78cce] | 537 | matrix s=mpNew(N,maMaxDeg_P((poly)data, preimage_r)); |
---|
[4508ce5] | 538 | res->data=(void *)maEval(theMap,(poly)data,preimage_r,nMap,s); |
---|
[0e1846] | 539 | idDelete((ideal *)&s); |
---|
| 540 | } |
---|
| 541 | if (currRing->minpoly!=NULL) |
---|
[ec7aac] | 542 | res->data=(void *)pMinPolyNormalize((poly)res->data); |
---|
[0e1846] | 543 | pTest((poly)res->data); |
---|
| 544 | break; |
---|
| 545 | case MODUL_CMD: |
---|
| 546 | case MATRIX_CMD: |
---|
| 547 | case IDEAL_CMD: |
---|
| 548 | case MAP_CMD: |
---|
| 549 | { |
---|
| 550 | int C=((matrix)data)->cols(); |
---|
| 551 | int R; |
---|
| 552 | if (w->rtyp==MAP_CMD) R=1; |
---|
| 553 | else R=((matrix)data)->rows(); |
---|
| 554 | matrix m=mpNew(R,C); |
---|
| 555 | char *tmpR=NULL; |
---|
| 556 | if(w->rtyp==MAP_CMD) |
---|
| 557 | { |
---|
| 558 | tmpR=((map)data)->preimage; |
---|
| 559 | ((matrix)data)->rank=((matrix)data)->rows(); |
---|
| 560 | } |
---|
[dd2714] | 561 | if ((what==FETCH_CMD)&& (nMap==nCopy)) |
---|
[0e1846] | 562 | { |
---|
| 563 | for (i=R*C-1;i>=0;i--) |
---|
| 564 | { |
---|
[416465] | 565 | m->m[i]=prCopyR(((ideal)data)->m[i], preimage_r); |
---|
[0e1846] | 566 | pTest(m->m[i]); |
---|
| 567 | } |
---|
| 568 | } |
---|
[0b4b97] | 569 | else |
---|
[dd2714] | 570 | if ((what==IMAP_CMD) || ((what==FETCH_CMD) /* && (nMap!=nCopy)*/)) |
---|
[0e1846] | 571 | { |
---|
| 572 | for (i=R*C-1;i>=0;i--) |
---|
| 573 | { |
---|
[4508ce5] | 574 | m->m[i]=pPermPoly(((ideal)data)->m[i],perm,preimage_r,nMap,par_perm,P); |
---|
[0e1846] | 575 | pTest(m->m[i]); |
---|
| 576 | } |
---|
| 577 | } |
---|
| 578 | else /* if(what==MAP_CMD) */ |
---|
| 579 | { |
---|
[e78cce] | 580 | matrix s=mpNew(N,maMaxDeg_Ma((ideal)data,preimage_r)); |
---|
[0e1846] | 581 | for (i=R*C-1;i>=0;i--) |
---|
| 582 | { |
---|
[4508ce5] | 583 | m->m[i]=maEval(theMap,((ideal)data)->m[i],preimage_r,nMap,s); |
---|
[0e1846] | 584 | pTest(m->m[i]); |
---|
| 585 | } |
---|
| 586 | idDelete((ideal *)&s); |
---|
| 587 | } |
---|
| 588 | if (currRing->minpoly!=NULL) |
---|
| 589 | { |
---|
| 590 | for (i=R*C-1;i>=0;i--) |
---|
| 591 | { |
---|
[ec7aac] | 592 | m->m[i]=pMinPolyNormalize(m->m[i]); |
---|
[0e1846] | 593 | pTest(m->m[i]); |
---|
| 594 | } |
---|
| 595 | } |
---|
| 596 | if(w->rtyp==MAP_CMD) |
---|
| 597 | { |
---|
| 598 | ((map)data)->preimage=tmpR; |
---|
[c232af] | 599 | ((map)m)->preimage=omStrDup(tmpR); |
---|
[0e1846] | 600 | } |
---|
| 601 | else |
---|
| 602 | { |
---|
| 603 | m->rank=((matrix)data)->rank; |
---|
| 604 | } |
---|
| 605 | res->data=(char *)m; |
---|
[88ff58] | 606 | idTest((ideal) m); |
---|
[0e1846] | 607 | break; |
---|
| 608 | } |
---|
[88ff58] | 609 | |
---|
[0e1846] | 610 | case LIST_CMD: |
---|
| 611 | { |
---|
| 612 | lists l=(lists)data; |
---|
[c232af] | 613 | lists ml=(lists)omAllocBin(slists_bin); |
---|
[0e1846] | 614 | ml->Init(l->nr+1); |
---|
| 615 | for(i=0;i<=l->nr;i++) |
---|
| 616 | { |
---|
| 617 | if (((l->m[i].rtyp>BEGIN_RING)&&(l->m[i].rtyp<END_RING)) |
---|
| 618 | ||(l->m[i].rtyp==LIST_CMD)) |
---|
| 619 | { |
---|
[e78cce] | 620 | if (maApplyFetch(what,theMap,&ml->m[i],&l->m[i], |
---|
[4508ce5] | 621 | preimage_r,perm,par_perm,P,nMap)) |
---|
[0e1846] | 622 | { |
---|
| 623 | ml->Clean(); |
---|
[c232af] | 624 | omFreeBin((ADDRESS)ml, slists_bin); |
---|
[0e1846] | 625 | res->rtyp=0; |
---|
| 626 | return TRUE; |
---|
| 627 | } |
---|
| 628 | } |
---|
| 629 | else |
---|
| 630 | { |
---|
| 631 | ml->m[i].Copy(&l->m[i]); |
---|
| 632 | } |
---|
| 633 | } |
---|
| 634 | res->data=(char *)ml; |
---|
| 635 | break; |
---|
| 636 | } |
---|
| 637 | default: |
---|
| 638 | { |
---|
| 639 | return TRUE; |
---|
| 640 | } |
---|
| 641 | } |
---|
| 642 | return FALSE; |
---|
| 643 | } |
---|
| 644 | |
---|
[367e88] | 645 | // This is a very dirty way to cancel monoms whose number equals the |
---|
| 646 | // MinPoly |
---|
[ec7aac] | 647 | static poly pMinPolyNormalize(poly p) |
---|
| 648 | { |
---|
| 649 | number one = nInit(1); |
---|
| 650 | spolyrec rp; |
---|
[367e88] | 651 | |
---|
[ec7aac] | 652 | poly q = &rp; |
---|
[367e88] | 653 | |
---|
[ec7aac] | 654 | while (p != NULL) |
---|
| 655 | { |
---|
| 656 | // this returns 0, if p == MinPoly |
---|
| 657 | number product = nMult(pGetCoeff(p), one); |
---|
| 658 | if (product == NULL) |
---|
| 659 | { |
---|
[512a2b] | 660 | pDeleteLm(&p); |
---|
[ec7aac] | 661 | } |
---|
| 662 | else |
---|
| 663 | { |
---|
| 664 | pSetCoeff(p, product); |
---|
| 665 | pNext(q) = p; |
---|
| 666 | q = p; |
---|
| 667 | p = pNext(p); |
---|
| 668 | } |
---|
| 669 | } |
---|
| 670 | pNext(q) = NULL; |
---|
| 671 | return rp.next; |
---|
| 672 | } |
---|