source: git/kernel/maps.cc @ e90187

spielwiese
Last change on this file since e90187 was e90187, checked in by Viktor Levandovskyy <levandov@…>, 20 years ago
*levandov: new functions in Plural with bugfixes git-svn-id: file:///usr/local/Singular/svn/trunk@7178 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 12.1 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id: maps.cc,v 1.2 2004-05-12 11:24:37 levandov Exp $ */
5/*
6* ABSTRACT - the mapping of polynomials to other rings
7*/
8
9#include "mod2.h"
10#include "structs.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"
17#include "omalloc.h"
18#include "kstd1.h"
19#include "longalg.h"
20#include "maps.h"
21#include "prCopy.h"
22
23// This is a very dirty way to "normalize" numbers w.r.t. a
24// MinPoly
25
26/* debug output: Tok2Cmdname in maApplyFetch*/
27//#include "ipshell.h"
28
29#define MAX_MAP_DEG 128
30
31/*2
32* copy a map
33*/
34map maCopy(map theMap)
35{
36  int i;
37  map m=(map)idInit(IDELEMS(theMap),0);
38  for (i=IDELEMS(theMap)-1; i>=0; i--)
39      m->m[i] = pCopy(theMap->m[i]);
40  m->preimage=omStrDup(theMap->preimage);
41  return m;
42}
43
44
45/*2
46* return the image of var(v)^pExp, where var(v) maps to p
47*/
48poly maEvalVariable(poly p, int v,int pExp,matrix s)
49{
50  if (pExp==1)
51    return pCopy(p);
52
53  poly res;
54
55  if((s!=NULL)&&(pExp<MAX_MAP_DEG))
56  {
57    int j=2;
58    poly p0=p;
59    // find starting point
60    if(MATELEM(s,v,1)==NULL)
61    {
62      MATELEM(s,v,1)=pCopy(p/*theMap->m[v-1]*/);
63    }
64    else
65    {
66      while((j<=pExp)&&(MATELEM(s,v,j)!=NULL))
67      {
68        j++;
69      }
70      p0=MATELEM(s,v,j-1);
71    }
72    // multiply
73    for(;j<=pExp;j++)
74    {
75      p0=MATELEM(s,v,j)=ppMult_qq(p0, p);
76      pNormalize(p0);
77    }
78    res=pCopy(p0/*MATELEM(s,v,pExp)*/);
79  }
80  else //if ((p->next!=NULL)&&(p->next->next==NULL))
81  {
82    res=pPower(pCopy(p),pExp);
83  }
84  return res;
85}
86
87static poly maEvalMonom(map theMap, poly p,ring preimage_r,matrix s, nMapFunc nMap)
88{
89    poly q=pNSet(nMap(pGetCoeff(p)));
90
91    int i;
92    for(i=1;i<=preimage_r->N; i++)
93    {
94      int pExp=p_GetExp( p,i,preimage_r);
95      if (pExp != 0)
96      {
97        if (theMap->m[i-1]!=NULL)
98        {
99          poly p1=theMap->m[i-1];
100          poly pp=maEvalVariable(p1,i,pExp,s);
101          q = pMult(q,pp);
102        }
103        else
104        {
105          pDelete(&q);
106          break;
107        }
108      }
109    }
110    int modulComp = p_GetComp( p,preimage_r);
111    if (q!=NULL) pSetCompP(q,modulComp);
112  return q;
113}
114
115poly maEval(map theMap, poly p,ring preimage_r,nMapFunc nMap,matrix s)
116{
117  poly result = NULL;
118  int i;
119
120//  for(i=1; i<=preimage_r->N; i++)
121//  {
122//    pTest(theMap->m[i-1]);
123//  }
124//  while (p!=NULL)
125//  {
126//    poly q=maEvalMonom(theMap,p,preimage_r,s);
127//    result = pAdd(result,q);
128//    pIter(p);
129//  }
130  if (p!=NULL)
131  {
132    int l = pLength(p)-1;
133    poly* monoms;
134    if (l>0)
135    {
136      monoms = (poly*) omAlloc(l*sizeof(poly));
137
138      for (i=0; i<l; i++)
139      {
140        monoms[i]=maEvalMonom(theMap,p,preimage_r,s, nMap);
141        pIter(p);
142      }
143    }
144    result=maEvalMonom(theMap,p,preimage_r,s, nMap);
145    if (l>0)
146    {
147      for(i = l-1; i>=0; i--)
148      {
149        result=pAdd(result, monoms[i]);
150      }
151      omFreeSize((ADDRESS)monoms,l*sizeof(poly));
152    }
153    if (currRing->minpoly!=NULL) result=pMinPolyNormalize(result);
154  }
155  pTest(result);
156  return result;
157}
158
159/*2
160*shifts the variables between minvar and maxvar of p  \in p_ring to the
161*first maxvar-minvar+1 variables in the actual ring
162*be carefull: there is no range check for the variables of p
163*/
164static poly pChangeSizeOfPoly(ring p_ring, poly p,int minvar,int maxvar)
165{
166  int i;
167  poly result = NULL,resultWorkP;
168  number n;
169
170  if (p==NULL) return result;
171  else result = pInit();
172  resultWorkP = result;
173  while (p!=NULL)
174  {
175    for (i=minvar;i<=maxvar;i++)
176      pSetExp(resultWorkP,i-minvar+1,p_GetExp(p,i,p_ring));
177    pSetComp(resultWorkP,p_GetComp(p,p_ring));
178    n=nCopy(pGetCoeff(p));
179    pSetCoeff(resultWorkP,n);
180    pSetm(resultWorkP);
181    pIter(p);
182    if (p!=NULL)
183    {
184      pNext(resultWorkP) = pInit();
185      pIter(resultWorkP);
186    }
187  }
188  return result;
189}
190
191
192/*2
193*returns the preimage of id under theMap,
194*if id is empty or zero the kernel is computed
195*/
196ideal maGetPreimage(ring theImageRing, map theMap, ideal id)
197{
198  int i,j;
199  int ordersize = rBlocks(currRing) + 1;
200  poly p,pp,q;
201  ideal temp1;
202  ideal temp2;
203  int *orders = (int*) omAlloc0(sizeof(int)*(ordersize));
204  int *block0 = (int*) omAlloc0(sizeof(int)*(ordersize));
205  int *block1 = (int*) omAlloc0(sizeof(int)*(ordersize));
206  int **wv = (int **) omAlloc0(ordersize * sizeof(int *));
207
208  int imagepvariables = theImageRing->N;
209  ring sourcering = currRing;
210  int N = pVariables+imagepvariables;
211  char** names = (char**) omAlloc0(N*sizeof(char*));
212
213  memcpy(names, currRing->names, currRing->N*sizeof(char*));
214  memcpy(&(names[currRing->N]), theImageRing->names,
215          (theImageRing->N*sizeof(char*)));
216  sip_sring tmpR;
217
218  //if (theImageRing->OrdSgn == 1)s
219     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  */
232  for (i=0; i<ordersize - 1; i++)
233  {
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];
238  }
239  tmpR = *currRing;
240  tmpR.N = N;
241  tmpR.order = orders;
242  tmpR.block0 = block0;
243  tmpR.block1 = block1;
244  tmpR.wvhdl = wv;
245  tmpR.names = names;
246  rComplete(&tmpR, 1);
247  rTest(&tmpR);
248
249#ifdef HAVE_PLURAL
250  if (sourcering->nc!= NULL)
251  {
252    rUnComplete(&tmpR);
253    omFreeSize(orders, sizeof(int)*(ordersize));
254    omFreeSize(block0, sizeof(int)*(ordersize));
255    omFreeSize(block1, sizeof(int)*(ordersize));
256    omFreeSize(wv, sizeof(int*)*(ordersize));
257    omFreeSize(names, (currRing->N)*sizeof(char*));
258    if (sourcering->nc->type!=nc_comm)
259    {
260      Werror("Sorry, not yet implemented for noncomm. rings");
261      return NULL;
262    }   
263    if ( rSum(theImageRing, sourcering, &tmpR ) !=1 )
264    {
265      /* something is wrong with the rings... */
266      Werror("Error in rSum");
267      return NULL;
268    }
269  }
270#endif
271
272  // change to new ring
273  rChangeCurrRing(&tmpR);
274  if (id==NULL)
275    j = 0;
276  else
277    j = IDELEMS(id);
278  int j0=j;
279  if (theImageRing->qideal!=NULL) j+=IDELEMS(theImageRing->qideal);
280  temp1 = idInit(sourcering->N+j,1);
281  for (i=0;i<sourcering->N;i++)
282  {
283    q = pISet(-1);
284    pSetExp(q,i+1+imagepvariables,1);
285    pSetm(q);
286    if ((i<IDELEMS(theMap)) && (theMap->m[i]!=NULL))
287    {
288      p = pSort(pChangeSizeOfPoly(theImageRing,theMap->m[i],1,imagepvariables));
289      p=pAdd(p,q);
290    }
291    else
292    {
293      p = q;
294    }
295    temp1->m[i] = p;
296  }
297  idTest(temp1);
298  for (i=sourcering->N;i<sourcering->N+j0;i++)
299  {
300    temp1->m[i] = pSort(pChangeSizeOfPoly(theImageRing,
301                                    id->m[i-sourcering->N],1,imagepvariables));
302  }
303  for (i=sourcering->N+j0;i<sourcering->N+j;i++)
304  {
305    temp1->m[i] = pSort(pChangeSizeOfPoly(theImageRing,
306                                    theImageRing->qideal->m[i-sourcering->N-j0],
307                                    1,imagepvariables));
308  }
309  // we ignore here homogenity - may be changed later:
310  temp2 = kStd(temp1,NULL,isNotHomog,NULL);
311  idDelete(&temp1);
312  for (i=0;i<IDELEMS(temp2);i++)
313  {
314    if (pLowVar(temp2->m[i])<imagepvariables) pDelete(&(temp2->m[i]));
315  }
316
317  // let's get back to the original ring
318  rChangeCurrRing(sourcering);
319  temp1 = idInit(5,1);
320  j = 0;
321  for (i=0;i<IDELEMS(temp2);i++)
322  {
323    p = temp2->m[i];
324    if (p!=NULL)
325    {
326      q = pChangeSizeOfPoly(&tmpR, p,imagepvariables+1,N);
327      if (j>=IDELEMS(temp1))
328      {
329        pEnlargeSet(&(temp1->m),IDELEMS(temp1),5);
330        IDELEMS(temp1)+=5;
331      }
332      temp1->m[j] = q;
333      j++;
334    }
335  }
336  id_Delete(&temp2, &tmpR);
337  idSkipZeroes(temp1);
338  rUnComplete(&tmpR);
339  omFreeSize(orders, sizeof(int)*(ordersize));
340  omFreeSize(block0, sizeof(int)*(ordersize));
341  omFreeSize(block1, sizeof(int)*(ordersize));
342  omFreeSize(wv, sizeof(int*)*(ordersize));
343  omFreeSize(names, (currRing->N)*sizeof(char*));
344  return temp1;
345}
346
347void maFindPerm(char **preim_names, int preim_n, char **preim_par, int preim_p,
348                char **names,       int n,       char **par,       int nop,
349                int * perm, int *par_perm, int ch)
350{
351  int i,j;
352  /* find correspondig vars */
353  for (i=0; i<preim_n; i++)
354  {
355    for(j=0; j<n; j++)
356    {
357      if (strcmp(preim_names[i],names[j])==0)
358      {
359        if (BVERBOSE(V_IMAP))
360          Print("// var %s: nr %d -> nr %d\n",preim_names[i],i+1,j+1);
361        /* var i+1 from preimage ring is var j+1  (index j+1) from image ring */
362        perm[i+1]=j+1;
363        break;
364      }
365    }
366    if ((perm[i+1]==0)&&(par!=NULL)
367        // do not consider par of Fq
368         && (ch < 2))
369    {
370      for(j=0; j<nop; j++)
371      {
372        if (strcmp(preim_names[i],par[j])==0)
373        {
374          if (BVERBOSE(V_IMAP))
375            Print("// var %s: nr %d -> par %d\n",preim_names[i],i+1,j+1);
376          /* var i+1 from preimage ring is par j+1 (index j) from image ring */
377          perm[i+1]=-(j+1);
378        }
379      }
380    }
381  }
382  if (par_perm!=NULL)
383  {
384    for (i=0; i<preim_p; i++)
385    {
386      for(j=0; j<n; j++)
387      {
388        if (strcmp(preim_par[i],names[j])==0)
389        {
390          if (BVERBOSE(V_IMAP))
391            Print("// par %s: par %d -> nr %d\n",preim_par[i],i+1,j+1);
392          /*par i+1 from preimage ring is var j+1  (index j+1) from image ring*/
393          par_perm[i]=j+1;
394          break;
395        }
396      }
397      if ((par!=NULL) && (par_perm[i]==0))
398      {
399        for(j=0; j<nop; j++)
400        {
401          if (strcmp(preim_par[i],par[j])==0)
402          {
403            if (BVERBOSE(V_IMAP))
404              Print("// par %s: nr %d -> par %d\n",preim_par[i],i+1,j+1);
405            /*par i+1 from preimage ring is par j+1 (index j) from image ring */
406            par_perm[i]=-(j+1);
407          }
408        }
409      }
410    }
411  }
412}
413
414/*2
415* embeds poly p from the subring r into the current ring
416*/
417poly maIMap(ring r, poly p)
418{
419  /* the simplest case:*/
420  if(r==currRing) return pCopy(p);
421  nMapFunc nMap=nSetMap(r);
422  int *perm=(int *)omAlloc0((r->N+1)*sizeof(int));
423  //int *par_perm=(int *)omAlloc0(rPar(r)*sizeof(int));
424  maFindPerm(r->names,r->N, r->parameter, r->P,
425             currRing->names,currRing->N,currRing->parameter, currRing->P,
426             perm,NULL, currRing->ch);
427  poly res=pPermPoly(p,perm,r, nMap /*,par_perm,rPar(r)*/);
428  omFreeSize((ADDRESS)perm,(r->N+1)*sizeof(int));
429  //omFreeSize((ADDRESS)par_perm,rPar(r)*sizeof(int));
430  return res;
431}
432
433/*3
434* find the max. degree in one variable, but not larger than MAX_MAP_DEG
435*/
436int maMaxDeg_Ma(ideal a,ring preimage_r)
437{
438  int i,j;
439  int N = preimage_r->N;
440  poly p;
441  int *m=(int *)omAlloc0(N*sizeof(int));
442
443  for (i=MATROWS(a)*MATCOLS(a)-1;i>=0;i--)
444  {
445    p=a->m[i];
446    //pTest(p); // cannot test p because it is from another ring
447    while(p!=NULL)
448    {
449      for(j=N-1;j>=0;j--)
450      {
451        m[j]=si_max(m[j],p_GetExp( p,j+1,preimage_r));
452        if (m[j]>=MAX_MAP_DEG)
453        {
454          i=MAX_MAP_DEG;
455          goto max_deg_fertig_id;
456        }
457      }
458      pIter(p);
459    }
460  }
461  i=m[0];
462  for(j=N-1;j>0;j--)
463  {
464    i=si_max(i,m[j]);
465  }
466max_deg_fertig_id:
467  omFreeSize((ADDRESS)m,N*sizeof(int));
468  return i;
469}
470
471/*3
472* find the max. degree in one variable, but not larger than MAX_MAP_DEG
473*/
474int maMaxDeg_P(poly p,ring preimage_r)
475{
476  int i,j;
477  int N = preimage_r->N;
478  int *m=(int *)omAlloc0(N*sizeof(int));
479
480//  pTest(p);
481  while(p!=NULL)
482  {
483    for(j=N-1;j>=0;j--)
484    {
485      m[j]=si_max(m[j],p_GetExp(p,j+1,preimage_r));
486      if (m[j]>=MAX_MAP_DEG)
487      {
488        i=MAX_MAP_DEG;
489        goto max_deg_fertig_p;
490      }
491    }
492    pIter(p);
493  }
494  i=m[0];
495  for(j=N-1;j>0;j--)
496  {
497    i=si_max(i,m[j]);
498  }
499max_deg_fertig_p:
500  omFreeSize((ADDRESS)m,N*sizeof(int));
501  return i;
502}
503
504// This is a very dirty way to cancel monoms whose number equals the
505// MinPoly
506poly pMinPolyNormalize(poly p)
507{
508  number one = nInit(1);
509  spolyrec rp;
510
511  poly q = &rp;
512
513  while (p != NULL)
514  {
515    // this returns 0, if p == MinPoly
516    number product = nMult(pGetCoeff(p), one);
517    if (product == NULL)
518    {
519      pDeleteLm(&p);
520    }
521    else
522    {
523      pSetCoeff(p, product);
524      pNext(q) = p;
525      q = p;
526      p = pNext(p);
527    }
528  }
529  pNext(q) = NULL;
530  return rp.next;
531}
Note: See TracBrowser for help on using the repository browser.