source: git/kernel/fast_maps.cc @ 3dfb74

spielwiese
Last change on this file since 3dfb74 was 3dfb74, checked in by Hans Schönemann <hannes@…>, 14 years ago
*hannes: memory leaks fixed (part of track 183 git-svn-id: file:///usr/local/Singular/svn/trunk@12261 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 17.4 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/***************************************************************
5 *  File:    fast_maps.cc
6 *  Purpose: implementation of fast maps
7 *  Author:  obachman (Olaf Bachmann)
8 *  Created: 02/01
9 *  Version: $Id$
10 *******************************************************************/
11#include "mod2.h"
12#include <omalloc.h>
13#include "p_polys.h"
14#include "prCopy.h"
15#include "ideals.h"
16#include "ring.h"
17#include "febase.h"
18#include "fast_maps.h"
19
20// define if you want to use special dest_ring
21#define HAVE_DEST_R 1
22// define if you want to use special src_ring
23#define HAVE_SRC_R 1
24// define if you want to use optimization step
25#define HAVE_MAP_OPTIMIZE 1
26
27/*******************************************************************************
28**
29*F  maMaxExp  . . . . . . . .  returns bound on maximal exponent of result of map
30*/
31// return maximal monomial if max_map_monomials are substituted into pi_m
32static poly maGetMaxExpP(poly* max_map_monomials,
33                         int n_max_map_monomials, ring map_r,
34                         poly pi_m, ring pi_r)
35{
36  int n = si_min(pi_r->N, n_max_map_monomials);
37  int i, j;
38  unsigned long e_i, e_j;
39  poly m_i=NULL;
40  poly map_j = p_Init(map_r);
41
42  for (i=1; i <= n; i++)
43  {
44    e_i = p_GetExp(pi_m, i, pi_r);
45    if (e_i==0) e_i=1;
46    m_i = max_map_monomials[i-1];
47    if (m_i != NULL && ! p_IsConstantComp(m_i, map_r))
48    {
49      for (j = 1; j<= map_r->N; j++)
50      {
51        e_j = p_GetExp(m_i, j, map_r);
52        if (e_j == 0) e_j=1;
53        p_AddExp(map_j, j, e_j*e_i, map_r);
54      }
55    }
56  }
57  return map_j;
58}
59
60// returns maximal exponent if map_id is applied to pi_id
61static unsigned long maGetMaxExp(ideal pi_id, ring pi_r, ideal map_id, ring map_r)
62{
63  unsigned long max=0;
64  poly* max_map_monomials = (poly*) omAlloc(IDELEMS(map_id)*sizeof(poly));
65  poly max_pi_i, max_map_i;
66
67  int i, j;
68  for (i=0; i<IDELEMS(map_id); i++)
69  {
70    max_map_monomials[i] = p_GetMaxExpP(map_id->m[i], map_r);
71  }
72
73  for (i=0; i<IDELEMS(pi_id); i++)
74  {
75    max_pi_i = p_GetMaxExpP(pi_id->m[i], pi_r);
76    max_map_i = maGetMaxExpP(max_map_monomials, IDELEMS(map_id), map_r,
77                              max_pi_i, pi_r);
78    unsigned long temp = p_GetMaxExp(max_map_i, map_r);
79    if (temp > max){ max=temp; }
80
81    p_LmFree(max_pi_i, pi_r);
82    p_LmFree(max_map_i, map_r);
83  }
84  for (i=0; i<IDELEMS(map_id); i++)
85  {
86    p_Delete(&max_map_monomials[i], map_r);
87  }
88  omFreeSize(max_map_monomials,IDELEMS(map_id)*sizeof(poly));
89
90  return max;
91}
92
93
94/*******************************************************************************
95**
96*F  debugging stuff
97*/
98#ifndef NDEBUG
99void maMonomial_Out(mapoly monomial, ring src_r, ring dest_r)
100{
101  p_wrp(monomial->src, src_r);
102  printf(" ref:%d", monomial->ref);
103  if (dest_r != NULL)
104  {
105    printf(" dest:");
106    p_wrp(monomial->dest, dest_r);
107  }
108  if (monomial->f1!=NULL) { printf(" f1:%lx", (long)monomial->f1->src);
109                            // p_wrp(monomial->f1->src, src_r);
110                          }
111  if (monomial->f2!=NULL) { printf(" f2:%lx",(long)monomial->f2->src);
112                            // p_wrp(monomial->f2->src, src_r);
113                          }
114  printf("\n");
115  fflush(stdout);
116}
117
118void maPoly_Out(mapoly mpoly, ring src_r, ring dest_r)
119{
120  while (mpoly != NULL)
121  {
122    maMonomial_Out(mpoly, src_r, dest_r);
123    mpoly = mpoly->next;
124  }
125}
126#endif
127
128/*******************************************************************************
129**
130*F  mapolyCreate  . . . . . . . . . . . . . . .  Creates mapoly
131*/
132static omBin mapolyBin = omGetSpecBin(sizeof(mapoly_s));
133static omBin macoeffBin = omGetSpecBin(sizeof(macoeff_s));
134
135mapoly maMonomial_Create(poly p, ring r_p, sBucket_pt bucket)
136{
137  mapoly mp = (mapoly) omAlloc0Bin(mapolyBin);
138  //p_wrp(p,r_p);printf(" (%x) created\n",mp);
139  mp->src = p;
140  p->next = NULL;
141
142  if (bucket != NULL)
143  {
144    mp->coeff = (macoeff) omAlloc0Bin(macoeffBin);
145    mp->coeff->bucket = bucket;
146    mp->coeff->n = pGetCoeff(p);
147  }
148  mp->ref = 1;
149  return mp;
150}
151
152void maMonomial_Destroy(mapoly mp, ring src_r, ring dest_r)
153{
154  if (mp != NULL)
155  {
156    p_LmFree(mp->src, src_r);
157    if (mp->coeff != NULL)
158    {
159      macoeff coeff, next = mp->coeff;
160      do
161      {
162        coeff = next;
163        next = coeff->next;
164        omFreeBin(coeff, macoeffBin);
165      }
166      while (next != NULL);
167      if (mp->dest != NULL)
168      {
169        assume(dest_r != NULL);
170        p_Delete(&(mp->dest), dest_r);
171      }
172    }
173    //if (mp->next!=NULL) maMonomial_Destroy(mp->next,dest_r);
174  }
175  omFreeBin(mp, mapolyBin);
176}
177
178/*******************************************************************************
179**
180*F  maPoly_InsertMonomial . . . . . . . . .insertion of a monomial into mapoly
181*/
182mapoly maPoly_InsertMonomial(mapoly &into, mapoly what, ring src_r)
183{
184  if (into == NULL)
185  {
186    into = what;
187    return what;
188  }
189
190  mapoly iter = into;
191  mapoly prev = NULL;
192
193  Top:
194  p_LmCmpAction(iter->src, what->src, src_r, goto Equal, goto Greater, goto Smaller);
195
196  Greater:
197  if (iter->next == NULL)
198  {
199    iter->next = what;
200    return what;
201  }
202  prev = iter;
203  iter = iter->next;
204  goto Top;
205
206  Smaller:
207  if (prev == NULL)
208  {
209    into = what;
210    what->next = iter;
211    return what;
212  }
213  prev->next = what;
214  what->next = iter;
215  return what;
216
217  Equal:
218  iter->ref += what->ref;
219  macoeff coeff = what->coeff;
220  if (coeff != NULL)
221  {
222    while (coeff->next != NULL) coeff = coeff->next;
223    coeff->next = iter->coeff;
224    iter->coeff = what->coeff;
225    what->coeff = NULL;
226  }
227  maMonomial_Free(what, src_r);
228  return iter;
229}
230
231mapoly maPoly_InsertMonomial(mapoly &into, poly p, ring src_r, sBucket_pt bucket)
232{
233  return maPoly_InsertMonomial(into, maMonomial_Create(p, src_r, bucket), src_r);
234}
235
236static void maPoly_InsertPoly(mapoly &into, poly what, ring src_r, sBucket_pt bucket)
237{
238  poly next;
239
240  while (what != NULL)
241  {
242    next = what->next;
243    maPoly_InsertMonomial(into, what, src_r, bucket);
244    what = next;
245  }
246}
247
248/*******************************************************************************
249**
250*F  maMap_Create . . . . . . . . . . . . . . . . . . . . create stuff
251*/
252
253void maMap_CreatePolyIdeal(ideal map_id, ring map_r, ring src_r, ring dest_r,
254                           mapoly &mp, maideal &mideal)
255{
256  mideal = (maideal) omAlloc0(sizeof(maideal_s));
257  mideal->n = IDELEMS(map_id);
258  mideal->buckets = (sBucket_pt*) omAlloc0(mideal->n*sizeof(sBucket_pt));
259  int i;
260  mp = NULL;
261
262  for (i=0; i<mideal->n; i++)
263  {
264    if (map_id->m[i] != NULL)
265    {
266      mideal->buckets[i] = sBucketCreate(dest_r);
267      maPoly_InsertPoly(mp,
268#ifdef PDEBUG
269                        prShallowCopyR(map_id->m[i], map_r, src_r),
270#else
271                        prShallowCopyR_NoSort(map_id->m[i], map_r, src_r),
272#endif
273                        src_r,
274                        mideal->buckets[i]);
275    }
276  }
277}
278
279void maMap_CreateRings(ideal map_id, ring map_r,
280                       ideal image_id, ring image_r,
281                       ring &src_r, ring &dest_r, BOOLEAN &simple)
282{
283#if HAVE_SRC_R > 0
284  int* weights = (int*) omAlloc0(map_r->N*sizeof(int));
285  int i;
286  int n = si_min(map_r->N, IDELEMS(image_id));
287
288  for (i=0; i<n; i++)
289  {
290    weights[i] = pLength(image_id->m[i])+1;
291  }
292  src_r = rModifyRing_Wp(map_r, weights);
293#else
294  src_r = map_r;
295#endif
296
297#if HAVE_DEST_R > 0
298  unsigned long maxExp = maGetMaxExp(map_id, map_r, image_id, image_r);
299  if (maxExp <=  1) maxExp = 2;
300  else if (maxExp > (unsigned long) image_r->bitmask)
301    maxExp = (unsigned long) image_r->bitmask;
302  dest_r = rModifyRing_Simple(image_r, TRUE, TRUE, maxExp,  simple);
303#else
304  dest_r = image_r;
305#endif
306}
307
308static void maMap_KillRings(ring map_r, ring image_r, ring src_r, ring dest_r)
309{
310  if (map_r != src_r)
311    rKillModified_Wp_Ring(src_r);
312  if (image_r != dest_r)
313    rKillModifiedRing_Simple(dest_r);
314}
315
316/*******************************************************************************
317**
318*F  misc  . . . . . . . . . . . . . . . . . . . . . . . . . . . .  misc  stuff
319*/
320
321ideal maIdeal_2_Ideal(maideal m_id, ring dest_r)
322{
323  ideal res = idInit(m_id->n, 1);
324  int l;
325
326  for (int i= 0; i < m_id->n; i++)
327  {
328    if (m_id->buckets[i]!=NULL)
329      sBucketDestroyAdd(m_id->buckets[i], &(res->m[i]), &l);
330  }
331  omFreeSize(m_id->buckets,m_id->n*sizeof(sBucket_pt));
332  omFree(m_id);
333  return res;
334}
335
336void maPoly_GetLength(mapoly mp, int &length)
337{
338  length = 0;
339  while (mp != NULL)
340  {
341    length++;
342    mp = mp->next;
343  }
344}
345
346
347/*******************************************************************************
348**
349*F  fast_map  . . . . . . . . . . . . . . . . . . . . . . . . . .the real thing
350*/
351
352ideal fast_map(ideal map_id, ring map_r, ideal image_id, ring image_r)
353{
354  ring src_r, dest_r;
355  ideal dest_id, res_id;
356  int length = 0;
357  BOOLEAN no_sort;
358
359  // construct rings we work in:
360  // src_r: Wp with Weights set to length of poly in image_id
361  // dest_r: Simple ring without degree ordering and short exponents
362  maMap_CreateRings(map_id, map_r, image_id, image_r, src_r, dest_r, no_sort);
363
364  // construct dest_id
365  if (dest_r != image_r)
366    dest_id = idrShallowCopyR(image_id, image_r, dest_r);
367  else
368    dest_id = image_id;
369
370  // construct mpoly and mideal
371  mapoly mp;
372  maideal mideal;
373  maMap_CreatePolyIdeal(map_id, map_r, src_r, dest_r, mp, mideal);
374
375  if (TEST_OPT_PROT)
376  {
377    maPoly_GetLength(mp, length);
378    Print("map[%ld:%d]{%d:", dest_r->bitmask, dest_r->ExpL_Size, length);
379  }
380
381  // do the optimization step
382#if HAVE_MAP_OPTIMIZE > 0
383  if (mp!=NULL) maPoly_Optimize(mp, src_r);
384#endif
385  if (TEST_OPT_PROT)
386  {
387    maPoly_GetLength(mp, length);
388    Print("%d}", length);
389  }
390
391  // do the actual evaluation
392  maPoly_Eval(mp, src_r, dest_id, dest_r, length);
393  if (TEST_OPT_PROT) Print(".");
394
395  // collect the results back into an ideal
396  ideal res_dest_id = maIdeal_2_Ideal(mideal, dest_r);
397  if (TEST_OPT_PROT) Print(".");
398
399  // convert result back to image_r
400  ideal res_image_id;
401  if (dest_r != image_r)
402  {
403    //if (no_sort) see Old/m134si.tst
404    //  res_image_id = idrShallowCopyR_NoSort(res_dest_id, dest_r, image_r);
405    //else
406      res_image_id = idrShallowCopyR(res_dest_id, dest_r, image_r);
407    id_ShallowDelete(&res_dest_id, dest_r);
408  }
409  else
410    res_image_id = res_dest_id;
411
412  if (TEST_OPT_PROT) Print(".");
413
414  // clean-up the rings
415  maMap_KillRings(map_r, image_r, src_r, dest_r);
416
417  if (TEST_OPT_PROT)
418    Print("\n");
419
420  idTest(res_image_id);
421  return res_image_id;
422}
423
424
425/**********************************************************************
426* Evaluation stuff                                                    *
427**********************************************************************/
428
429// substitute p everywhere the monomial occours,
430// return the number of substitutions
431static int maPoly_Substitute(macoeff c, poly p, ring dest_r)
432{
433  // substitute the monomial: go through macoeff
434  int len=pLength(p);
435  int done=0;
436  while (c!=NULL)
437  {
438    done++;
439    poly t=pp_Mult_nn(p,c->n,dest_r);
440    sBucket_Add_p(c->bucket, t, len);
441    c=c->next;
442  }
443  return done;
444}
445
446static poly maPoly_EvalMon(poly src, ring src_r, poly* dest_id, ring dest_r)
447{
448  int i;
449  int e;
450  poly p=NULL;
451  poly pp;
452  for(i=1;i<=src_r->N;i++)
453  {
454    e=p_GetExp(src,i,src_r);
455    if (e>0)
456    {
457      pp=dest_id[i-1];
458      if (pp==NULL)
459      {
460        p_Delete(&p,dest_r);
461        return NULL;
462      }
463      if ((p==NULL) /* && (e>0)*/)
464      {
465        p=p_Copy(pp /*dest_id[i-1]*/,dest_r);
466        e--;
467      }
468      while (e>0)
469      {
470        p=p_Mult_q(p,p_Copy(pp /*dest_id[i-1]*/,dest_r),dest_r);
471        e--;
472      }
473    }
474  }
475  if (p==NULL) p=p_ISet(1,dest_r);
476  return p;
477}
478
479void maPoly_Eval(mapoly root, ring src_r, ideal dest_id, ring dest_r, int total_cost)
480{
481  // invert the list rooted at root:
482  if ((root!=NULL) && (root->next!=NULL))
483  {
484    mapoly q=root->next;
485    mapoly qn;
486    root->next=NULL;
487    do
488    {
489      qn=q->next;
490      q->next=root;
491      root=q;
492      q=qn;
493    }
494    while (qn !=NULL);
495  }
496
497  total_cost /= 10;
498  int next_print_cost = total_cost;
499
500  // the evaluation -----------------------------------------
501  mapoly p=root;
502  int cost = 0;
503
504  while (p!=NULL)
505  {
506    // look at each mapoly: compute its value in ->dest
507    assume (p->dest==NULL);
508    {
509      if ((p->f1!=NULL)&&(p->f2!=NULL))
510      {
511#if 0
512        printf("found prod:");
513        p_wrp(p->src,src_r);printf("=");
514        p_wrp(p->f1->src,src_r);printf(" * ");
515        p_wrp(p->f2->src,src_r);printf("\n");
516#endif
517        poly f1=p->f1->dest;
518        poly f2=p->f2->dest;
519        if (p->f1->ref>0) f1=p_Copy(f1,dest_r);
520        else
521        {
522          // we own p->f1->dest now (in f1)
523          p->f1->dest=NULL;
524        }
525        if (p->f2->ref>0) f2=p_Copy(f2,dest_r);
526        else
527        {
528          // we own p->f2->dest now (in f2)
529          p->f2->dest=NULL;
530        }
531        maMonomial_Free(p->f1,src_r, dest_r);
532        maMonomial_Free(p->f2,src_r, dest_r);
533        p->dest=p_Mult_q(f1,f2,dest_r);
534      } /* factors : 2 */
535      else
536      {
537        //printf("compute "); p_wrp(p->src,src_r);printf("\n");
538        assume((p->f1==NULL) && (p->f2==NULL));
539        //if(p->f1!=NULL) { printf(" but f1="); p_wrp(p->f1->src,src_r);printf("\n"); }
540        //if(p->f2!=NULL) { printf(" but f2="); p_wrp(p->f2->src,src_r);printf("\n"); }
541        // no factorization provided, use the classical method:
542        p->dest=maPoly_EvalMon(p->src,src_r,dest_id->m,dest_r);
543        //p_wrp(p->dest, dest_r); printf(" is p->dest\n");
544      }
545    } /* p->dest==NULL */
546    // substitute the monomial: go through macoeff
547    p->ref -= maPoly_Substitute(p->coeff, p->dest, dest_r);
548    //printf("subst done\n");
549    if (total_cost)
550    {
551      assume(TEST_OPT_PROT);
552      cost++;
553      if (cost > next_print_cost)
554      {
555        Print("-");
556        next_print_cost += total_cost;
557      }
558    }
559
560    mapoly pp=p;
561    p=p->next;
562    //p_wrp(pp->src, src_r);
563    if (pp->ref<=0)
564    {
565      //printf(" (%x) killed\n",pp);
566      maMonomial_Destroy(pp, src_r, dest_r);
567    }
568    //else
569    //  printf(" (%x) not killed, ref=%d\n",pp,pp->ref);
570  }
571}
572
573
574/*******************************************************************************
575**
576*F  maEggt  . . . . . . . . . . . . . . . . . . . . . . . .  returns extended ggt
577*/
578// return NULL if deg(ggt(m1, m2)) < 2
579// else return m = ggT(m1, m2) and q1, q2 such that m1 = q1*m m2 = q2*m
580static poly maEggT(const poly m1, const poly m2, poly &q1, poly &q2,const ring r)
581{
582
583  int i;
584  int dg = 0;
585  poly ggt = NULL;
586  q1 = p_Init(r);
587  q2 = p_Init(r);
588  ggt=p_Init(r);
589
590  for (i=1;i<=r->N;i++) {
591    unsigned long e1 = p_GetExp(m1, i, r);
592    unsigned long e2 = p_GetExp(m2, i, r);
593    if (e1 > 0 && e2 > 0)
594    {
595      unsigned long em = (e1 > e2 ? e2 : e1);
596      dg += em;
597      p_SetExp(ggt, i, em, r);
598      p_SetExp(q1, i, e1 - em, r);
599      p_SetExp(q2, i, e2 - em, r);
600    }
601    else
602    {
603      p_SetExp(q1, i, e1, r);
604      p_SetExp(q2, i, e2, r);
605    }
606  }
607  if (dg>1)
608  {
609    p_Setm(ggt, r);
610    p_Setm(q1, r);
611    p_Setm(q2, r);
612  }
613  else
614  {
615    p_LmFree(ggt, r);
616    p_LmFree(q1, r);
617    p_LmFree(q2, r);
618    ggt = NULL;
619  }
620  return ggt;
621}
622
623/********************************************************************
624 **                                                                 *
625 * maFindBestggT                                                    *
626 * finds ggT with the highest cost                                  *
627 *******************************************************************/
628
629static mapoly maFindBestggT(mapoly mp, mapoly & choice, mapoly & fp, mapoly & fq,const ring r)
630{
631  int ggt_deg = 0;
632  poly p = mp->src;
633  mapoly iter = choice;
634  poly ggT = NULL;
635  fp = NULL;
636  fq = NULL;
637  poly fp_p=NULL;
638  poly fq_p=NULL;
639  choice=NULL;
640  while ((iter != NULL) && (pDeg(iter->src, r) > ggt_deg))
641  {
642    //    maMonomial_Out(iter, r, NULL);
643    poly q1, q2, q;
644
645    q = maEggT(p, iter->src, q1, q2,r);
646    if (q != NULL)
647    {
648      assume((q1!=NULL)&&(q2!=NULL));
649      if (pDeg(q,r) > ggt_deg)
650      {
651        choice=iter;
652        if (ggT != NULL)
653        {
654          p_LmFree(ggT, r);
655          p_LmFree(fp_p, r);
656          p_LmFree(fq_p, r);
657
658        }
659        ggt_deg = pDeg(q, r);
660        ggT = q;
661        fp_p = q1;
662        fq_p = q2;
663      }
664      else
665      {
666        p_LmFree(q, r);
667        p_LmFree(q1, r);
668        p_LmFree(q2, r);
669      }
670    }
671    iter=iter->next;
672  }
673  if(ggT!=NULL)
674  {
675    int dq =pTotaldegree(fq_p,r);
676    if (dq!=0)
677    {
678      fq=maPoly_InsertMonomial(mp, fq_p, r, NULL);
679      fp=maPoly_InsertMonomial(mp, fp_p, r, NULL);
680      return maPoly_InsertMonomial(mp, ggT, r, NULL);
681    }
682    else
683    {
684      fq=NULL;
685      p_LmFree(fq_p, r);
686      p_LmFree(ggT, r);
687      fp=maPoly_InsertMonomial(mp, fp_p, r, NULL);
688      choice->ref++;
689      return choice;
690    }
691  }
692  else
693  {
694    return NULL;
695  }
696}
697
698/********************************************************************
699 **                                                                 *
700 * maPoly_Optimize                                                  *
701 * adds and integrates subexpressions                               *
702 *******************************************************************/
703
704void maPoly_Optimize(mapoly mpoly, ring src_r)
705{
706  assume(mpoly!=NULL && mpoly->src!=NULL);
707  mapoly iter = mpoly;
708  mapoly choice;
709  mapoly ggT=NULL;
710  mapoly fp=NULL;
711  mapoly fq=NULL;
712  while (iter->next!=NULL)
713  {
714    choice=iter->next;
715    if ((iter->f1==NULL))
716    {
717      ggT=maFindBestggT(iter, choice, fp, fq,src_r);
718      if (choice!=NULL)
719      {
720        iter->f1=fp;
721        iter->f2=ggT;
722        if (fq!=NULL)
723        {
724          ggT->ref++;
725          choice->f1=fq;
726          choice->f2=ggT;
727        }
728      }
729    }
730    iter=iter->next;
731  }
732}
Note: See TracBrowser for help on using the repository browser.