source: git/Singular/maps_ip.cc @ 665ca8

spielwiese
Last change on this file since 665ca8 was dc79bd, checked in by Oleksandr Motsak <motsak@…>, 12 years ago
Major Update for Enumerators + Fixes for Algrbraic & Transcendental extensions General: chg: cleanup + documentation + additional assumes Enumerators: chg: some polish on Enumerators add: CRecursivePolyCoeffsEnumerator<ConverterPolicy> for recursive treatment of converted coeffs as Enumerators Coeffs: chg: use mpz_lcm for readability in nlClearDenominators + cleanup add: nlClear*NoPositiveLead variants should not make LC positive chg: all nlClear* are not static in order to be usable from alg / trans exts. fix: fixed a bug in ndClearContent AlgExt: add: nCoeff_is_Q_algext for checking an alg. ext. of Q add: naClear* for alg. ext. over Q NOTE: coeffs are polynomials in Q[a] - one should simply consider each of them recursively as a collection of numbers... NOTE: compute GCDs over Alg. Ext... + gcds of (int.) numbers!? NOTE: trying to be conform with older Singular: no negative leading coeff. normalization chg: Alg. Ext: use singclap_*gcd (instead of Frank's gcd-stuff) p_poly: add: p_Cleardenom_n/p_Cleardenom also clear content afterwards... chg: major and minor changes to p_Content/p_Cleardenom_n/p_Cleardenom + cleanup add: additionally trying to assure positive leading coeff. after p_Content/p_Cleardenom_n(/p_Cleardenom?) NOTE: which should not be needed as n_ClearDenominators/n_ClearContent are supposed to assure that themselves! add: more assumes to p_polys.cc NOTE: massive usage of enumerators form p_* causes problems - only doing that for Q_a()! NOTE: do -normalization over Q(x...) TransExt: add: ntClear* for trans. ext's fix: correct ntGetDenom/ntGetNumerator (thanks to pSubstPar), NOTE: no negative denominator out of ntGetNumerator/ntGetDenom! add: first inefficient ntClearContent/Q and ntClearDenominators/Q & F_p impl. NOTE: careful with the use of nlClear* ! (only over Q!) add: added ntTest to transext.cc on most in/outs + use ntInit(poly)! NOTE: trying to fix the monic-poly-gcd problem in ntClearDenominators!
  • Property mode set to 100644
File size: 9.8 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/*
5* ABSTRACT - the mapping of polynomials to other rings
6*/
7#define TRANSEXT_PRIVATES
8
9#include "config.h"
10#include <kernel/mod2.h>
11#include <omalloc/omalloc.h>
12
13#include <coeffs/numbers.h>
14#include <coeffs/coeffs.h>
15
16#include <polys/monomials/ring.h>
17#include <polys/monomials/maps.h>
18#include <polys/matpol.h>
19#include <polys/prCopy.h>
20#include <polys/ext_fields/transext.h>
21
22//#include <libpolys/polys/ext_fields/longtrans.h>
23// #include <kernel/longalg.h>
24
25#include <kernel/febase.h>
26#include <kernel/kstd1.h>
27
28#include "maps_ip.h"
29#include "ipid.h"
30
31
32#include "lists.h"
33#include "tok.h"
34
35/* debug output: Tok2Cmdname in maApplyFetch*/
36#include "ipshell.h"
37
38// define this if you want to use the fast_map routine for mapping ideals
39//#define FAST_MAP
40
41#ifdef FAST_MAP
42#include <polys/monomials/maps.h>
43#endif
44
45
46/*2
47* maps the expression w to res,
48* switch what: MAP_CMD: use theMap for mapping, N for preimage ring
49*              //FETCH_CMD: use pOrdPoly for mapping
50*              IMAP_CMD: use perm for mapping, N for preimage ring
51*              default: map only poly-structures,
52*                       use perm and par_perm, N and P,
53*/
54BOOLEAN maApplyFetch(int what,map theMap,leftv res, leftv w, ring preimage_r,
55                     int *perm, int *par_perm, int P, nMapFunc nMap)
56{
57  int i;
58  int N = preimage_r->N;
59#if 0
60  Print("N=%d what=%s ",N,Tok2Cmdname(what));
61  if (perm!=NULL) for(i=1;i<=N;i++) Print("%d -> %d ",i,perm[i]);
62  PrintS("\n");
63  Print("P=%d ",P);
64  if (par_perm!=NULL) for(i=0;i<P;i++) Print("%d -> %d ",i,par_perm[i]);
65  PrintS("\n");
66#endif
67
68  void *data=w->Data();
69  res->rtyp = w->rtyp;
70  switch (w->rtyp)
71  {
72    case NUMBER_CMD:
73      if (P!=0)
74      {
75//        WerrorS("Sorry 'napPermNumber' was lost in the refactoring process (due to Frank): needs to be fixed");
76//        return TRUE;
77#if 1
78// poly n_PermNumber(const number z, const int *par_perm, const int OldPar, const ring src, const ring dst);
79        res->data= (void *) n_PermNumber((number)data, par_perm, P, preimage_r, currRing);
80#endif
81        res->rtyp=POLY_CMD;
82        if (nCoeff_is_Extension(currRing->cf))
83          res->data=(void *)p_MinPolyNormalize((poly)res->data, currRing);
84        pTest((poly) res->data);       
85      }
86      else
87      {
88        assume( nMap != NULL );
89         
90        number a = nMap((number)data, preimage_r->cf, currRing->cf);
91       
92 
93        if (nCoeff_is_Extension(currRing->cf))
94        {
95          n_Normalize(a, currRing->cf); // ???
96/*
97          number a = (number)res->data;
98          number one = nInit(1);
99          number product = nMult(a, one );
100          nDelete(&one);
101          nDelete(&a);
102          res->data=(void *)product;
103 */
104        }
105        #ifdef LDEBUG
106        n_Test(a, currRing->cf);
107        #endif
108        res->data=(void *)a;
109
110      }
111      break;
112    case POLY_CMD:
113    case VECTOR_CMD:
114      if ((what==FETCH_CMD)&& (preimage_r->cf==currRing->cf))
115        res->data=(void *)prCopyR( (poly)data, preimage_r, currRing);
116      else
117      if ((what==IMAP_CMD) || ((what==FETCH_CMD) /* && (nMap!=nCopy)*/))
118        res->data=(void *)p_PermPoly((poly)data,perm,preimage_r,currRing, nMap,par_perm,P);
119      else /*if (what==MAP_CMD)*/
120      {
121        matrix s=mpNew(N,maMaxDeg_P((poly)data, preimage_r));
122        res->data=(void *)maEval(theMap, (poly)data, preimage_r, nMap, (ideal)s, currRing);
123        idDelete((ideal *)&s);
124      }
125      if (nCoeff_is_Extension(currRing->cf))
126        res->data=(void *)p_MinPolyNormalize((poly)res->data, currRing);
127      pTest((poly)res->data);
128      break;
129    case MODUL_CMD:
130    case MATRIX_CMD:
131    case IDEAL_CMD:
132    case MAP_CMD:
133    {
134      int C=((matrix)data)->cols();
135      int R;
136      if (w->rtyp==MAP_CMD) R=1;
137      else R=((matrix)data)->rows();
138      matrix m=mpNew(R,C);
139      char *tmpR=NULL;
140      if(w->rtyp==MAP_CMD)
141      {
142        tmpR=((map)data)->preimage;
143        ((matrix)data)->rank=((matrix)data)->rows();
144      }
145      if ((what==FETCH_CMD)&& (preimage_r->cf == currRing->cf))
146      {
147        for (i=R*C-1;i>=0;i--)
148        {
149          m->m[i]=prCopyR(((ideal)data)->m[i], preimage_r, currRing);
150          pTest(m->m[i]);
151        }
152      }
153      else
154      if ((what==IMAP_CMD) || ((what==FETCH_CMD) /* && (nMap!=nCopy)*/))
155      {
156        for (i=R*C-1;i>=0;i--)
157        {
158          m->m[i]=p_PermPoly(((ideal)data)->m[i],perm,preimage_r,currRing,
159                          nMap,par_perm,P);
160          pTest(m->m[i]);
161        }
162      }
163      else /* if(what==MAP_CMD) */
164      {
165        matrix s=mpNew(N,maMaxDeg_Ma((ideal)data,preimage_r));
166        for (i=R*C-1;i>=0;i--)
167        {
168          m->m[i]=maEval(theMap, ((ideal)data)->m[i], preimage_r, nMap, (ideal)s, currRing);
169          pTest(m->m[i]);
170        }
171        idDelete((ideal *)&s);
172      }
173      if (nCoeff_is_Extension(currRing->cf))
174      {
175        for (i=R*C-1;i>=0;i--)
176        {
177          m->m[i]=p_MinPolyNormalize(m->m[i], currRing);
178          pTest(m->m[i]);
179        }
180      }
181      if(w->rtyp==MAP_CMD)
182      {
183        ((map)data)->preimage=tmpR;
184        ((map)m)->preimage=omStrDup(tmpR);
185      }
186      else
187      {
188        m->rank=((matrix)data)->rank;
189      }
190      res->data=(char *)m;
191      idTest((ideal) m);
192      break;
193    }
194
195    case LIST_CMD:
196    {
197      lists l=(lists)data;
198      lists ml=(lists)omAllocBin(slists_bin);
199      ml->Init(l->nr+1);
200      for(i=0;i<=l->nr;i++)
201      {
202        if (((l->m[i].rtyp>BEGIN_RING)&&(l->m[i].rtyp<END_RING))
203        ||(l->m[i].rtyp==LIST_CMD))
204        {
205          if (maApplyFetch(what,theMap,&ml->m[i],&l->m[i],
206                           preimage_r,perm,par_perm,P,nMap))
207          {
208            ml->Clean();
209            omFreeBin((ADDRESS)ml, slists_bin);
210            res->rtyp=0;
211            return TRUE;
212          }
213        }
214        else
215        {
216          ml->m[i].Copy(&l->m[i]);
217        }
218      }
219      res->data=(char *)ml;
220      break;
221    }
222    default:
223    {
224      return TRUE;
225    }
226  }
227  return FALSE;
228}
229
230/*2
231* substitutes the parameter par (from 1..N) by image,
232* does not destroy p and  image
233*/
234poly pSubstPar(poly p, int par, poly image)
235{
236  ideal theMapI = idInit(rPar(currRing),1);
237  nMapFunc nMap = n_SetMap(currRing->cf->extRing->cf, currRing->cf);
238
239  int i;
240  for(i = rPar(currRing);i>0;i--)
241  {
242    if (i != par)
243      theMapI->m[i-1]= p_NSet(n_Param(i, currRing), currRing);
244    else
245      theMapI->m[i-1] = p_Copy(image, currRing);
246  }
247 
248
249  map theMap=(map)theMapI;
250  theMap->preimage=NULL;
251
252  leftv v=(leftv)omAllocBin(sleftv_bin);
253  sleftv tmpW;
254  poly res=NULL;
255
256  while (p!=NULL)
257  {
258    memset(v,0,sizeof(sleftv));
259
260    number d = n_GetDenom(p_GetCoeff(p, currRing), currRing);
261    if (!n_IsOne (d, currRing->cf))
262      WarnS("ignoring denominators of coefficients...");
263    n_Delete(&d, currRing);
264
265    number num = n_GetNumerator(p_GetCoeff(p, currRing), currRing); 
266
267    memset(&tmpW,0,sizeof(sleftv));
268    tmpW.rtyp = POLY_CMD;
269    tmpW.data = NUM (num); // a copy of this poly will be used
270     
271    if (maApplyFetch(MAP_CMD,theMap,v,&tmpW,currRing->cf->extRing,NULL,NULL,0,nMap))
272    {
273      WerrorS("map failed");
274      v->data=NULL;
275    }
276    n_Delete(&num, currRing);
277
278    //TODO check for memory leaks
279    poly pp = pHead(p);
280    //PrintS("map:");pWrite(pp);
281    pSetCoeff(pp, nInit(1));
282    //PrintS("->");pWrite((poly)(v->data));
283    poly ppp = pMult((poly)(v->data),pp);
284    //PrintS("->");pWrite(ppp);
285    res=pAdd(res,ppp);
286    pIter(p);
287  }
288  idDelete((ideal *)(&theMap));
289  omFreeBin((ADDRESS)v, sleftv_bin);
290  return res;
291}
292
293/*2
294* substitute the n-th parameter by the poly e in id
295* does not destroy id and e
296*/
297ideal  idSubstPar(ideal id, int n, poly e)
298{
299  int k=MATROWS((matrix)id)*MATCOLS((matrix)id);
300  ideal res=(ideal)mpNew(MATROWS((matrix)id),MATCOLS((matrix)id));
301
302  res->rank = id->rank;
303  for(k--;k>=0;k--)
304  {
305    res->m[k]=pSubstPar(id->m[k],n,e);
306  }
307  return res;
308}
309
310/*2
311* substitutes the variable var (from 1..N) by image,
312* does not destroy p and  image
313*/
314poly pSubstPoly(poly p, int var, poly image)
315{
316  if (p==NULL) return NULL;
317#ifdef HAVE_PLURAL
318  if (rIsPluralRing(currRing))
319  {
320    return pSubst(pCopy(p),var,image);
321  }
322#endif
323  map theMap=(map)idMaxIdeal(1);
324  theMap->preimage=NULL;
325  pDelete(&(theMap->m[var-1]));
326  theMap->m[var-1]=pCopy(image);
327
328  poly res=NULL;
329#ifdef FAST_MAP
330  if (pGetComp(p)==0)
331  {
332    ideal src_id=idInit(1,1);
333    src_id->m[0]=p;
334    ideal res_id=fast_map(src_id,currRing,(ideal)theMap,currRing);
335    res=res_id->m[0];
336    res_id->m[0]=NULL; idDelete(&res_id);
337    src_id->m[0]=NULL; idDelete(&src_id);
338  }
339  else
340#endif
341  {
342    sleftv tmpW;
343    memset(&tmpW,0,sizeof(sleftv));
344    tmpW.rtyp=POLY_CMD;
345    tmpW.data=p;
346    leftv v=(leftv)omAlloc0Bin(sleftv_bin);
347    if (maApplyFetch(MAP_CMD,theMap,v,&tmpW,currRing,NULL,NULL,0,
348                            n_SetMap(currRing->cf, currRing->cf)))
349    {
350      WerrorS("map failed");
351      v->data=NULL;
352    }
353    res=(poly)(v->data);
354    omFreeBin((ADDRESS)v, sleftv_bin);
355  }
356  idDelete((ideal *)(&theMap));
357  return res;
358}
359
360/*2
361* substitute the n-th variable by the poly e in id
362* does not destroy id and e
363*/
364ideal  idSubstPoly(ideal id, int n, poly e)
365{
366
367#ifdef HAVE_PLURAL
368  if (rIsPluralRing(currRing))
369  {
370    int k=MATROWS((matrix)id)*MATCOLS((matrix)id);
371    ideal res=(ideal)mpNew(MATROWS((matrix)id),MATCOLS((matrix)id));
372    res->rank = id->rank;
373    for(k--;k>=0;k--)
374    {
375      res->m[k]=pSubst(pCopy(id->m[k]),n,e);
376    }
377    return res;
378  }
379#endif
380  map theMap=(map)idMaxIdeal(1);
381  theMap->preimage=NULL;
382  pDelete(&(theMap->m[n-1]));
383  theMap->m[n-1]=pCopy(e);
384
385  leftv v=(leftv)omAlloc0Bin(sleftv_bin);
386  sleftv tmpW;
387  memset(&tmpW,0,sizeof(sleftv));
388  tmpW.rtyp=IDEAL_CMD;
389  tmpW.data=id;
390  if (maApplyFetch(MAP_CMD,theMap,v,&tmpW,currRing,NULL,NULL,0,
391                          n_SetMap(currRing->cf, currRing->cf)))
392  {
393    WerrorS("map failed");
394    v->data=NULL;
395  }
396  ideal res=(ideal)(v->data);
397  idDelete((ideal *)(&theMap));
398  omFreeBin((ADDRESS)v, sleftv_bin);
399  return res;
400}
Note: See TracBrowser for help on using the repository browser.