source: git/kernel/maps.cc @ 171950

spielwiese
Last change on this file since 171950 was fd8c22, checked in by Hans Schönemann <hannes@…>, 16 years ago
*hannes: rTensor, maGetPreimage fixed git-svn-id: file:///usr/local/Singular/svn/trunk@10676 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 10.5 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id: maps.cc,v 1.8 2008-04-21 14:18:16 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* (assumes) that both ring have the same coeff.field
196*/
197ideal maGetPreimage(ring theImageRing, map theMap, ideal id)
198{
199  int i,j;
200  poly p,pp,q;
201  ideal temp1;
202  ideal temp2;
203
204  int imagepvariables = theImageRing->N;
205  ring sourcering = currRing;
206  int N = pVariables+imagepvariables;
207
208  ring tmpR;
209  if (rTensor(theImageRing,sourcering,tmpR,FALSE,TRUE)!=1)
210  {
211     WerrorS("rTensor error");
212     return NULL;
213  }
214
215#ifdef HAVE_PLURAL
216  if (rIsPluralRing(theImageRing))
217  {
218    if ((rIsPluralRing(sourcering)) && (ncRingType(sourcering)!=nc_comm)) 
219    {
220      Werror("Sorry, not yet implemented for noncomm. rings");
221      return NULL;
222    }   
223  }
224  if (nSetMap(theImageRing) != nCopy)
225  {
226    Werror("Coefficient fields must be equal");
227    return NULL;
228  }
229#endif
230
231  // change to new ring
232  rChangeCurrRing(tmpR);
233  if (id==NULL)
234    j = 0;
235  else
236    j = IDELEMS(id);
237  int j0=j;
238  if (theImageRing->qideal!=NULL) j+=IDELEMS(theImageRing->qideal);
239  temp1 = idInit(sourcering->N+j,1);
240  for (i=0;i<sourcering->N;i++)
241  {
242    q = pISet(-1);
243    pSetExp(q,i+1+imagepvariables,1);
244    pSetm(q);
245    if ((i<IDELEMS(theMap)) && (theMap->m[i]!=NULL))
246    {
247      p = pSort(pChangeSizeOfPoly(theImageRing,theMap->m[i],1,imagepvariables));
248      p=pAdd(p,q);
249    }
250    else
251    {
252      p = q;
253    }
254    temp1->m[i] = p;
255  }
256  idTest(temp1);
257  for (i=sourcering->N;i<sourcering->N+j0;i++)
258  {
259    temp1->m[i] = pSort(pChangeSizeOfPoly(theImageRing,
260                                    id->m[i-sourcering->N],1,imagepvariables));
261  }
262  for (i=sourcering->N+j0;i<sourcering->N+j;i++)
263  {
264    temp1->m[i] = pSort(pChangeSizeOfPoly(theImageRing,
265                                    theImageRing->qideal->m[i-sourcering->N-j0],
266                                    1,imagepvariables));
267  }
268  // we ignore here homogenity - may be changed later:
269  temp2 = kStd(temp1,NULL,isNotHomog,NULL);
270  idDelete(&temp1);
271  for (i=0;i<IDELEMS(temp2);i++)
272  {
273    if (pLowVar(temp2->m[i])<imagepvariables) pDelete(&(temp2->m[i]));
274  }
275
276  // let's get back to the original ring
277  rChangeCurrRing(sourcering);
278  temp1 = idInit(5,1);
279  j = 0;
280  for (i=0;i<IDELEMS(temp2);i++)
281  {
282    p = temp2->m[i];
283    if (p!=NULL)
284    {
285      q = pSort(pChangeSizeOfPoly(tmpR, p,imagepvariables+1,N));
286      if (j>=IDELEMS(temp1))
287      {
288        pEnlargeSet(&(temp1->m),IDELEMS(temp1),5);
289        IDELEMS(temp1)+=5;
290      }
291      temp1->m[j] = q;
292      j++;
293    }
294  }
295  id_Delete(&temp2, tmpR);
296  idSkipZeroes(temp1);
297  rKill(tmpR);
298  return temp1;
299}
300
301void maFindPerm(char **preim_names, int preim_n, char **preim_par, int preim_p,
302                char **names,       int n,       char **par,       int nop,
303                int * perm, int *par_perm, int ch)
304{
305  int i,j;
306  /* find correspondig vars */
307  for (i=0; i<preim_n; i++)
308  {
309    for(j=0; j<n; j++)
310    {
311      if (strcmp(preim_names[i],names[j])==0)
312      {
313        if (BVERBOSE(V_IMAP))
314          Print("// var %s: nr %d -> nr %d\n",preim_names[i],i+1,j+1);
315        /* var i+1 from preimage ring is var j+1  (index j+1) from image ring */
316        perm[i+1]=j+1;
317        break;
318      }
319    }
320    if ((perm[i+1]==0)&&(par!=NULL)
321        // do not consider par of Fq
322         && (ch < 2))
323    {
324      for(j=0; j<nop; j++)
325      {
326        if (strcmp(preim_names[i],par[j])==0)
327        {
328          if (BVERBOSE(V_IMAP))
329            Print("// var %s: nr %d -> par %d\n",preim_names[i],i+1,j+1);
330          /* var i+1 from preimage ring is par j+1 (index j) from image ring */
331          perm[i+1]=-(j+1);
332        }
333      }
334    }
335  }
336  if (par_perm!=NULL)
337  {
338    for (i=0; i<preim_p; i++)
339    {
340      for(j=0; j<n; j++)
341      {
342        if (strcmp(preim_par[i],names[j])==0)
343        {
344          if (BVERBOSE(V_IMAP))
345            Print("// par %s: par %d -> nr %d\n",preim_par[i],i+1,j+1);
346          /*par i+1 from preimage ring is var j+1  (index j+1) from image ring*/
347          par_perm[i]=j+1;
348          break;
349        }
350      }
351      if ((par!=NULL) && (par_perm[i]==0))
352      {
353        for(j=0; j<nop; j++)
354        {
355          if (strcmp(preim_par[i],par[j])==0)
356          {
357            if (BVERBOSE(V_IMAP))
358              Print("// par %s: nr %d -> par %d\n",preim_par[i],i+1,j+1);
359            /*par i+1 from preimage ring is par j+1 (index j) from image ring */
360            par_perm[i]=-(j+1);
361          }
362        }
363      }
364    }
365  }
366}
367
368/*2
369* embeds poly p from the subring r into the current ring
370*/
371poly maIMap(ring r, poly p)
372{
373  /* the simplest case:*/
374  if(r==currRing) return pCopy(p);
375  nMapFunc nMap=nSetMap(r);
376  int *perm=(int *)omAlloc0((r->N+1)*sizeof(int));
377  //int *par_perm=(int *)omAlloc0(rPar(r)*sizeof(int));
378  maFindPerm(r->names,r->N, r->parameter, r->P,
379             currRing->names,currRing->N,currRing->parameter, currRing->P,
380             perm,NULL, currRing->ch);
381  poly res=pPermPoly(p,perm,r, nMap /*,par_perm,rPar(r)*/);
382  omFreeSize((ADDRESS)perm,(r->N+1)*sizeof(int));
383  //omFreeSize((ADDRESS)par_perm,rPar(r)*sizeof(int));
384  return res;
385}
386
387/*3
388* find the max. degree in one variable, but not larger than MAX_MAP_DEG
389*/
390int maMaxDeg_Ma(ideal a,ring preimage_r)
391{
392  int i,j;
393  int N = preimage_r->N;
394  poly p;
395  int *m=(int *)omAlloc0(N*sizeof(int));
396
397  for (i=MATROWS(a)*MATCOLS(a)-1;i>=0;i--)
398  {
399    p=a->m[i];
400    //pTest(p); // cannot test p because it is from another ring
401    while(p!=NULL)
402    {
403      for(j=N-1;j>=0;j--)
404      {
405        m[j]=si_max(m[j],p_GetExp( p,j+1,preimage_r));
406        if (m[j]>=MAX_MAP_DEG)
407        {
408          i=MAX_MAP_DEG;
409          goto max_deg_fertig_id;
410        }
411      }
412      pIter(p);
413    }
414  }
415  i=m[0];
416  for(j=N-1;j>0;j--)
417  {
418    i=si_max(i,m[j]);
419  }
420max_deg_fertig_id:
421  omFreeSize((ADDRESS)m,N*sizeof(int));
422  return i;
423}
424
425/*3
426* find the max. degree in one variable, but not larger than MAX_MAP_DEG
427*/
428int maMaxDeg_P(poly p,ring preimage_r)
429{
430  int i,j;
431  int N = preimage_r->N;
432  int *m=(int *)omAlloc0(N*sizeof(int));
433
434//  pTest(p);
435  while(p!=NULL)
436  {
437    for(j=N-1;j>=0;j--)
438    {
439      m[j]=si_max(m[j],p_GetExp(p,j+1,preimage_r));
440      if (m[j]>=MAX_MAP_DEG)
441      {
442        i=MAX_MAP_DEG;
443        goto max_deg_fertig_p;
444      }
445    }
446    pIter(p);
447  }
448  i=m[0];
449  for(j=N-1;j>0;j--)
450  {
451    i=si_max(i,m[j]);
452  }
453max_deg_fertig_p:
454  omFreeSize((ADDRESS)m,N*sizeof(int));
455  return i;
456}
457
458// This is a very dirty way to cancel monoms whose number equals the
459// MinPoly
460poly pMinPolyNormalize(poly p)
461{
462  number one = nInit(1);
463  spolyrec rp;
464
465  poly q = &rp;
466
467  while (p != NULL)
468  {
469    // this returns 0, if p == MinPoly
470    number product = nMult(pGetCoeff(p), one);
471    if (product == NULL)
472    {
473      pDeleteLm(&p);
474    }
475    else
476    {
477      pSetCoeff(p, product);
478      pNext(q) = p;
479      q = p;
480      p = pNext(p);
481    }
482  }
483  pNext(q) = NULL;
484  return rp.next;
485}
Note: See TracBrowser for help on using the repository browser.