source: git/kernel/maps.cc @ 4bbe3b

spielwiese
Last change on this file since 4bbe3b was 35aab3, checked in by Hans Schönemann <hannes@…>, 21 years ago
This commit was generated by cvs2svn to compensate for changes in r6879, which included commits to RCS files with non-trunk default branches. git-svn-id: file:///usr/local/Singular/svn/trunk@6880 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 11.5 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id: maps.cc,v 1.1.1.1 2003-10-06 12:15:54 Singular 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  // change to new ring
250  rChangeCurrRing(&tmpR);
251  if (id==NULL)
252    j = 0;
253  else
254    j = IDELEMS(id);
255  int j0=j;
256  if (theImageRing->qideal!=NULL) j+=IDELEMS(theImageRing->qideal);
257  temp1 = idInit(sourcering->N+j,1);
258  for (i=0;i<sourcering->N;i++)
259  {
260    q = pISet(-1);
261    pSetExp(q,i+1+imagepvariables,1);
262    pSetm(q);
263    if ((i<IDELEMS(theMap)) && (theMap->m[i]!=NULL))
264    {
265      p = pSort(pChangeSizeOfPoly(theImageRing,theMap->m[i],1,imagepvariables));
266      p=pAdd(p,q);
267    }
268    else
269    {
270      p = q;
271    }
272    temp1->m[i] = p;
273  }
274  idTest(temp1);
275  for (i=sourcering->N;i<sourcering->N+j0;i++)
276  {
277    temp1->m[i] = pSort(pChangeSizeOfPoly(theImageRing,
278                                    id->m[i-sourcering->N],1,imagepvariables));
279  }
280  for (i=sourcering->N+j0;i<sourcering->N+j;i++)
281  {
282    temp1->m[i] = pSort(pChangeSizeOfPoly(theImageRing,
283                                    theImageRing->qideal->m[i-sourcering->N-j0],
284                                    1,imagepvariables));
285  }
286  // we ignore here homogenity - may be changed later:
287  temp2 = kStd(temp1,NULL,isNotHomog,NULL);
288  idDelete(&temp1);
289  for (i=0;i<IDELEMS(temp2);i++)
290  {
291    if (pLowVar(temp2->m[i])<imagepvariables) pDelete(&(temp2->m[i]));
292  }
293
294  // let's get back to the original ring
295  rChangeCurrRing(sourcering);
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    {
303      q = pChangeSizeOfPoly(&tmpR, p,imagepvariables+1,N);
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  }
313  id_Delete(&temp2, &tmpR);
314  idSkipZeroes(temp1);
315  rUnComplete(&tmpR);
316  omFreeSize(orders, sizeof(int)*(ordersize));
317  omFreeSize(block0, sizeof(int)*(ordersize));
318  omFreeSize(block1, sizeof(int)*(ordersize));
319  omFreeSize(wv, sizeof(int*)*(ordersize));
320  omFreeSize(names, (currRing->N)*sizeof(char*));
321  return temp1;
322}
323
324void maFindPerm(char **preim_names, int preim_n, char **preim_par, int preim_p,
325                char **names,       int n,       char **par,       int nop,
326                int * perm, int *par_perm, int ch)
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    }
343    if ((perm[i+1]==0)&&(par!=NULL)
344        // do not consider par of Fq
345         && (ch < 2))
346    {
347      for(j=0; j<nop; j++)
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  {
361    for (i=0; i<preim_p; i++)
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          /*par 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      {
376        for(j=0; j<nop; j++)
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            /*par 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*/
394poly maIMap(ring r, poly p)
395{
396  /* the simplest case:*/
397  if(r==currRing) return pCopy(p);
398  nMapFunc nMap=nSetMap(r);
399  int *perm=(int *)omAlloc0((r->N+1)*sizeof(int));
400  //int *par_perm=(int *)omAlloc0(rPar(r)*sizeof(int));
401  maFindPerm(r->names,r->N, r->parameter, r->P,
402             currRing->names,currRing->N,currRing->parameter, currRing->P,
403             perm,NULL, currRing->ch);
404  poly res=pPermPoly(p,perm,r, nMap /*,par_perm,rPar(r)*/);
405  omFreeSize((ADDRESS)perm,(r->N+1)*sizeof(int));
406  //omFreeSize((ADDRESS)par_perm,rPar(r)*sizeof(int));
407  return res;
408}
409
410/*3
411* find the max. degree in one variable, but not larger than MAX_MAP_DEG
412*/
413int maMaxDeg_Ma(ideal a,ring preimage_r)
414{
415  int i,j;
416  int N = preimage_r->N;
417  poly p;
418  int *m=(int *)omAlloc0(N*sizeof(int));
419
420  for (i=MATROWS(a)*MATCOLS(a)-1;i>=0;i--)
421  {
422    p=a->m[i];
423    //pTest(p); // cannot test p because it is from another ring
424    while(p!=NULL)
425    {
426      for(j=N-1;j>=0;j--)
427      {
428        m[j]=si_max(m[j],p_GetExp( p,j+1,preimage_r));
429        if (m[j]>=MAX_MAP_DEG)
430        {
431          i=MAX_MAP_DEG;
432          goto max_deg_fertig_id;
433        }
434      }
435      pIter(p);
436    }
437  }
438  i=m[0];
439  for(j=N-1;j>0;j--)
440  {
441    i=si_max(i,m[j]);
442  }
443max_deg_fertig_id:
444  omFreeSize((ADDRESS)m,N*sizeof(int));
445  return i;
446}
447
448/*3
449* find the max. degree in one variable, but not larger than MAX_MAP_DEG
450*/
451int maMaxDeg_P(poly p,ring preimage_r)
452{
453  int i,j;
454  int N = preimage_r->N;
455  int *m=(int *)omAlloc0(N*sizeof(int));
456
457//  pTest(p);
458  while(p!=NULL)
459  {
460    for(j=N-1;j>=0;j--)
461    {
462      m[j]=si_max(m[j],p_GetExp(p,j+1,preimage_r));
463      if (m[j]>=MAX_MAP_DEG)
464      {
465        i=MAX_MAP_DEG;
466        goto max_deg_fertig_p;
467      }
468    }
469    pIter(p);
470  }
471  i=m[0];
472  for(j=N-1;j>0;j--)
473  {
474    i=si_max(i,m[j]);
475  }
476max_deg_fertig_p:
477  omFreeSize((ADDRESS)m,N*sizeof(int));
478  return i;
479}
480
481// This is a very dirty way to cancel monoms whose number equals the
482// MinPoly
483poly pMinPolyNormalize(poly p)
484{
485  number one = nInit(1);
486  spolyrec rp;
487
488  poly q = &rp;
489
490  while (p != NULL)
491  {
492    // this returns 0, if p == MinPoly
493    number product = nMult(pGetCoeff(p), one);
494    if (product == NULL)
495    {
496      pDeleteLm(&p);
497    }
498    else
499    {
500      pSetCoeff(p, product);
501      pNext(q) = p;
502      q = p;
503      p = pNext(p);
504    }
505  }
506  pNext(q) = NULL;
507  return rp.next;
508}
Note: See TracBrowser for help on using the repository browser.