source: git/libpolys/polys/matpol.cc @ 405407

spielwiese
Last change on this file since 405407 was 040c71, checked in by Hans Schoenemann <hannes@…>, 11 years ago
g: prefer StringAppendS over StringAppend (from master)
  • Property mode set to 100644
File size: 33.2 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4
5/*
6* ABSTRACT:
7*/
8
9#include <stdio.h>
10#include <math.h>
11
12#ifdef HAVE_CONFIG_H
13#include "libpolysconfig.h"
14#endif /* HAVE_CONFIG_H */
15#include <misc/auxiliary.h>
16
17#include <omalloc/omalloc.h>
18#include <misc/mylimits.h>
19
20
21// #include <kernel/structs.h>
22// #include <kernel/kstd1.h>
23// #include <kernel/polys.h>
24
25#include <misc/intvec.h>
26#include <coeffs/numbers.h>
27
28#include <reporter/reporter.h>
29
30
31#include "monomials/ring.h"
32#include "monomials/p_polys.h"
33
34#include "coeffrings.h"
35#include "simpleideals.h"
36#include "matpol.h"
37#include "prCopy.h"
38
39#include "sparsmat.h"
40
41//omBin sip_sideal_bin = omGetSpecBin(sizeof(ip_smatrix));
42/*0 implementation*/
43
44static poly mp_Exdiv ( poly m, poly d, poly vars, const ring);
45static poly mp_Select (poly fro, poly what, const ring);
46
47/// create a r x c zero-matrix
48matrix mpNew(int r, int c)
49{
50  if (r<=0) r=1;
51  if ( (((int)(MAX_INT_VAL/sizeof(poly))) / r) <= c)
52  {
53    Werror("internal error: creating matrix[%d][%d]",r,c);
54    return NULL;
55  }
56  matrix rc = (matrix)omAllocBin(sip_sideal_bin);
57  rc->nrows = r;
58  rc->ncols = c;
59  rc->rank = r;
60  if (c != 0)
61  {
62    int s=r*c*sizeof(poly);
63    rc->m = (poly*)omAlloc0(s);
64    //if (rc->m==NULL)
65    //{
66    //  Werror("internal error: creating matrix[%d][%d]",r,c);
67    //  return NULL;
68    //}
69  }
70  return rc;
71}
72
73/// copies matrix a (from ring r to r)
74matrix mp_Copy (matrix a, const ring r)
75{
76  id_Test((ideal)a, r);
77  poly t;
78  int i, m=MATROWS(a), n=MATCOLS(a);
79  matrix b = mpNew(m, n);
80
81  for (i=m*n-1; i>=0; i--)
82  {
83    t = a->m[i];
84    if (t!=NULL)
85    {
86      p_Normalize(t, r);
87      b->m[i] = p_Copy(t, r);
88    }
89  }
90  b->rank=a->rank;
91  return b;
92}
93
94/// copies matrix a from rSrc into rDst
95matrix mp_Copy(const matrix a, const ring rSrc, const ring rDst)
96{
97  id_Test((ideal)a, rSrc);
98
99  poly t;
100  int i, m=MATROWS(a), n=MATCOLS(a);
101
102  matrix b = mpNew(m, n);
103
104  for (i=m*n-1; i>=0; i--)
105  {
106    t = a->m[i];
107    if (t!=NULL)
108    {
109      b->m[i] = prCopyR_NoSort(t, rSrc, rDst);
110      p_Normalize(b->m[i], rDst);
111    }
112  }
113  b->rank=a->rank;
114
115  id_Test((ideal)b, rDst);
116
117  return b;
118}
119
120
121
122/// make it a p * unit matrix
123matrix mp_InitP(int r, int c, poly p, const ring R)
124{
125  matrix rc = mpNew(r,c);
126  int i=si_min(r,c), n = c*(i-1)+i-1, inc = c+1;
127
128  p_Normalize(p, R);
129  while (n>0)
130  {
131    rc->m[n] = p_Copy(p, R);
132    n -= inc;
133  }
134  rc->m[0]=p;
135  return rc;
136}
137
138/// make it a v * unit matrix
139matrix mp_InitI(int r, int c, int v, const ring R)
140{
141  return mp_InitP(r, c, p_ISet(v, R), R);
142}
143
144/// c = f*a
145matrix mp_MultI(matrix a, int f, const ring R)
146{
147  int k, n = a->nrows, m = a->ncols;
148  poly p = p_ISet(f, R);
149  matrix c = mpNew(n,m);
150
151  for (k=m*n-1; k>0; k--)
152    c->m[k] = pp_Mult_qq(a->m[k], p, R);
153  c->m[0] = p_Mult_q(p_Copy(a->m[0], R), p, R);
154  return c;
155}
156
157/// multiply a matrix 'a' by a poly 'p', destroy the args
158matrix mp_MultP(matrix a, poly p, const ring R)
159{
160  int k, n = a->nrows, m = a->ncols;
161
162  p_Normalize(p, R);
163  for (k=m*n-1; k>0; k--)
164  {
165    if (a->m[k]!=NULL)
166      a->m[k] = p_Mult_q(a->m[k], p_Copy(p, R), R);
167  }
168  a->m[0] = p_Mult_q(a->m[0], p, R);
169  return a;
170}
171
172/*2
173* multiply a poly 'p' by a matrix 'a', destroy the args
174*/
175matrix pMultMp(poly p, matrix a, const ring R)
176{
177  int k, n = a->nrows, m = a->ncols;
178
179  p_Normalize(p, R);
180  for (k=m*n-1; k>0; k--)
181  {
182    if (a->m[k]!=NULL)
183      a->m[k] = p_Mult_q(p_Copy(p, R), a->m[k], R);
184  }
185  a->m[0] = p_Mult_q(p, a->m[0], R);
186  return a;
187}
188
189matrix mp_Add(matrix a, matrix b, const ring R)
190{
191  int k, n = a->nrows, m = a->ncols;
192  if ((n != b->nrows) || (m != b->ncols))
193  {
194/*
195*    Werror("cannot add %dx%d matrix and %dx%d matrix",
196*      m,n,b->cols(),b->rows());
197*/
198    return NULL;
199  }
200  matrix c = mpNew(n,m);
201  for (k=m*n-1; k>=0; k--)
202    c->m[k] = p_Add_q(p_Copy(a->m[k], R), p_Copy(b->m[k], R), R);
203  return c;
204}
205
206matrix mp_Sub(matrix a, matrix b, const ring R)
207{
208  int k, n = a->nrows, m = a->ncols;
209  if ((n != b->nrows) || (m != b->ncols))
210  {
211/*
212*    Werror("cannot sub %dx%d matrix and %dx%d matrix",
213*      m,n,b->cols(),b->rows());
214*/
215    return NULL;
216  }
217  matrix c = mpNew(n,m);
218  for (k=m*n-1; k>=0; k--)
219    c->m[k] = p_Sub(p_Copy(a->m[k], R), p_Copy(b->m[k], R), R);
220  return c;
221}
222
223matrix mp_Mult(matrix a, matrix b, const ring R)
224{
225  int i, j, k;
226  int m = MATROWS(a);
227  int p = MATCOLS(a);
228  int q = MATCOLS(b);
229
230  if (p!=MATROWS(b))
231  {
232/*
233*   Werror("cannot multiply %dx%d matrix and %dx%d matrix",
234*     m,p,b->rows(),q);
235*/
236    return NULL;
237  }
238  matrix c = mpNew(m,q);
239
240  for (i=1; i<=m; i++)
241  {
242    for (k=1; k<=p; k++)
243    {
244      poly aik;
245      if ((aik=MATELEM(a,i,k))!=NULL)
246      {
247        for (j=1; j<=q; j++)
248        {
249          poly bkj;
250          if ((bkj=MATELEM(b,k,j))!=NULL)
251          {
252            poly *cij=&(MATELEM(c,i,j));
253            poly s = pp_Mult_qq(aik /*MATELEM(a,i,k)*/, bkj/*MATELEM(b,k,j)*/, R);
254            if (/*MATELEM(c,i,j)*/ (*cij)==NULL) (*cij)=s;
255            else (*cij) = p_Add_q((*cij) /*MATELEM(c,i,j)*/ ,s, R);
256          }
257        }
258      }
259    //  pNormalize(t);
260    //  MATELEM(c,i,j) = t;
261    }
262  }
263  for(i=m*q-1;i>=0;i--) p_Normalize(c->m[i], R);
264  return c;
265}
266
267matrix mp_Transp(matrix a, const ring R)
268{
269  int    i, j, r = MATROWS(a), c = MATCOLS(a);
270  poly *p;
271  matrix b =  mpNew(c,r);
272
273  p = b->m;
274  for (i=0; i<c; i++)
275  {
276    for (j=0; j<r; j++)
277    {
278      if (a->m[j*c+i]!=NULL) *p = p_Copy(a->m[j*c+i], R);
279      p++;
280    }
281  }
282  return b;
283}
284
285/*2
286*returns the trace of matrix a
287*/
288poly mp_Trace ( matrix a, const ring R)
289{
290  int i;
291  int n = (MATCOLS(a)<MATROWS(a)) ? MATCOLS(a) : MATROWS(a);
292  poly  t = NULL;
293
294  for (i=1; i<=n; i++)
295    t = p_Add_q(t, p_Copy(MATELEM(a,i,i), R), R);
296  return t;
297}
298
299/*2
300*returns the trace of the product of a and b
301*/
302poly TraceOfProd ( matrix a, matrix b, int n, const ring R)
303{
304  int i, j;
305  poly  p, t = NULL;
306
307  for (i=1; i<=n; i++)
308  {
309    for (j=1; j<=n; j++)
310    {
311      p = pp_Mult_qq(MATELEM(a,i,j), MATELEM(b,j,i), R);
312      t = p_Add_q(t, p, R);
313    }
314  }
315  return t;
316}
317
318// #ifndef SIZE_OF_SYSTEM_PAGE
319// #define SIZE_OF_SYSTEM_PAGE 4096
320// #endif
321
322/*2
323* corresponds to Maple's coeffs:
324* var has to be the number of a variable
325*/
326matrix mp_Coeffs (ideal I, int var, const ring R)
327{
328  poly h,f;
329  int l, i, c, m=0;
330  matrix co;
331  /* look for maximal power m of x_var in I */
332  for (i=IDELEMS(I)-1; i>=0; i--)
333  {
334    f=I->m[i];
335    while (f!=NULL)
336    {
337      l=p_GetExp(f,var, R);
338      if (l>m) m=l;
339      pIter(f);
340    }
341  }
342  co=mpNew((m+1)*I->rank,IDELEMS(I));
343  /* divide each monomial by a power of x_var,
344  * remember the power in l and the component in c*/
345  for (i=IDELEMS(I)-1; i>=0; i--)
346  {
347    f=I->m[i];
348    I->m[i]=NULL;
349    while (f!=NULL)
350    {
351      l=p_GetExp(f,var, R);
352      p_SetExp(f,var,0, R);
353      c=si_max((int)p_GetComp(f, R),1);
354      p_SetComp(f,0, R);
355      p_Setm(f, R);
356      /* now add the resulting monomial to co*/
357      h=pNext(f);
358      pNext(f)=NULL;
359      //MATELEM(co,c*(m+1)-l,i+1)
360      //  =p_Add_q(MATELEM(co,c*(m+1)-l,i+1),f, R);
361      MATELEM(co,(c-1)*(m+1)+l+1,i+1)
362        =p_Add_q(MATELEM(co,(c-1)*(m+1)+l+1,i+1),f, R);
363      /* iterate f*/
364      f=h;
365    }
366  }
367  id_Delete(&I, R);
368  return co;
369}
370
371/*2
372* given the result c of mpCoeffs(ideal/module i, var)
373* i of rank r
374* build the matrix of the corresponding monomials in m
375*/
376void   mp_Monomials(matrix c, int r, int var, matrix m, const ring R)
377{
378  /* clear contents of m*/
379  int k,l;
380  for (k=MATROWS(m);k>0;k--)
381  {
382    for(l=MATCOLS(m);l>0;l--)
383    {
384      p_Delete(&MATELEM(m,k,l), R);
385    }
386  }
387  omfreeSize((ADDRESS)m->m,MATROWS(m)*MATCOLS(m)*sizeof(poly));
388  /* allocate monoms in the right size r x MATROWS(c)*/
389  m->m=(poly*)omAlloc0(r*MATROWS(c)*sizeof(poly));
390  MATROWS(m)=r;
391  MATCOLS(m)=MATROWS(c);
392  m->rank=r;
393  /* the maximal power p of x_var: MATCOLS(m)=r*(p+1) */
394  int p=MATCOLS(m)/r-1;
395  /* fill in the powers of x_var=h*/
396  poly h=p_One(R);
397  for(k=r;k>0; k--)
398  {
399    MATELEM(m,k,k*(p+1))=p_One(R);
400  }
401  for(l=p;l>=0; l--)
402  {
403    p_SetExp(h,var,p-l, R);
404    p_Setm(h, R);
405    for(k=r;k>0; k--)
406    {
407      MATELEM(m,k,k*(p+1)-l)=p_Copy(h, R);
408    }
409  }
410  p_Delete(&h, R);
411}
412
413matrix mp_CoeffProc (poly f, poly vars, const ring R)
414{
415  assume(vars!=NULL);
416  poly sel, h;
417  int l, i;
418  int pos_of_1 = -1;
419  matrix co;
420
421  if (f==NULL)
422  {
423    co = mpNew(2, 1);
424    MATELEM(co,1,1) = p_One(R);
425    MATELEM(co,2,1) = NULL;
426    return co;
427  }
428  sel = mp_Select(f, vars, R);
429  l = pLength(sel);
430  co = mpNew(2, l);
431
432  if (rHasLocalOrMixedOrdering(R))
433  {
434    for (i=l; i>=1; i--)
435    {
436      h = sel;
437      pIter(sel);
438      pNext(h)=NULL;
439      MATELEM(co,1,i) = h;
440      MATELEM(co,2,i) = NULL;
441      if (p_IsConstant(h, R)) pos_of_1 = i;
442    }
443  }
444  else
445  {
446    for (i=1; i<=l; i++)
447    {
448      h = sel;
449      pIter(sel);
450      pNext(h)=NULL;
451      MATELEM(co,1,i) = h;
452      MATELEM(co,2,i) = NULL;
453      if (p_IsConstant(h, R)) pos_of_1 = i;
454    }
455  }
456  while (f!=NULL)
457  {
458    i = 1;
459    loop
460    {
461      if (i!=pos_of_1)
462      {
463        h = mp_Exdiv(f, MATELEM(co,1,i),vars, R);
464        if (h!=NULL)
465        {
466          MATELEM(co,2,i) = p_Add_q(MATELEM(co,2,i), h, R);
467          break;
468        }
469      }
470      if (i == l)
471      {
472        // check monom 1 last:
473        if (pos_of_1 != -1)
474        {
475          h = mp_Exdiv(f, MATELEM(co,1,pos_of_1),vars, R);
476          if (h!=NULL)
477          {
478            MATELEM(co,2,pos_of_1) = p_Add_q(MATELEM(co,2,pos_of_1), h, R);
479          }
480        }
481        break;
482      }
483      i ++;
484    }
485    pIter(f);
486  }
487  return co;
488}
489
490/*2
491*exact divisor: let d  == x^i*y^j, m is thought to have only one term;
492*    return m/d iff d divides m, and no x^k*y^l (k>i or l>j) divides m
493* consider all variables in vars
494*/
495static poly mp_Exdiv ( poly m, poly d, poly vars, const ring R)
496{
497  int i;
498  poly h = p_Head(m, R);
499  for (i=1; i<=rVar(R); i++)
500  {
501    if (p_GetExp(vars,i, R) > 0)
502    {
503      if (p_GetExp(d,i, R) != p_GetExp(h,i, R))
504      {
505        p_Delete(&h, R);
506        return NULL;
507      }
508      p_SetExp(h,i,0, R);
509    }
510  }
511  p_Setm(h, R);
512  return h;
513}
514
515void mp_Coef2(poly v, poly mon, matrix *c, matrix *m, const ring R)
516{
517  poly* s;
518  poly p;
519  int sl,i,j;
520  int l=0;
521  poly sel=mp_Select(v,mon, R);
522
523  p_Vec2Polys(sel,&s,&sl, R);
524  for (i=0; i<sl; i++)
525    l=si_max(l,pLength(s[i]));
526  *c=mpNew(sl,l);
527  *m=mpNew(sl,l);
528  poly h;
529  int isConst;
530  for (j=1; j<=sl;j++)
531  {
532    p=s[j-1];
533    if (p_IsConstant(p, R)) /*p != NULL */
534    {
535      isConst=-1;
536      i=l;
537    }
538    else
539    {
540      isConst=1;
541      i=1;
542    }
543    while(p!=NULL)
544    {
545      h = p_Head(p, R);
546      MATELEM(*m,j,i) = h;
547      i+=isConst;
548      p = p->next;
549    }
550  }
551  while (v!=NULL)
552  {
553    i = 1;
554    j = p_GetComp(v, R);
555    loop
556    {
557      poly mp=MATELEM(*m,j,i);
558      if (mp!=NULL)
559      {
560        h = mp_Exdiv(v, mp /*MATELEM(*m,j,i)*/, mp, R);
561        if (h!=NULL)
562        {
563          p_SetComp(h,0, R);
564          MATELEM(*c,j,i) = p_Add_q(MATELEM(*c,j,i), h, R);
565          break;
566        }
567      }
568      if (i < l)
569        i++;
570      else
571        break;
572    }
573    v = v->next;
574  }
575}
576
577
578BOOLEAN mp_Equal(matrix a, matrix b, const ring R)
579{
580  if ((MATCOLS(a)!=MATCOLS(b)) || (MATROWS(a)!=MATROWS(b)))
581    return FALSE;
582  int i=MATCOLS(a)*MATROWS(b)-1;
583  while (i>=0)
584  {
585    if (a->m[i]==NULL)
586    {
587      if (b->m[i]!=NULL) return FALSE;
588    }
589    else
590      if (b->m[i]==NULL) return FALSE;
591      else if (p_Cmp(a->m[i],b->m[i], R)!=0) return FALSE;
592    i--;
593  }
594  i=MATCOLS(a)*MATROWS(b)-1;
595  while (i>=0)
596  {
597#if 0
598    poly tt=p_Sub(p_Copy(a->m[i], R),p_Copy(b->m[i], R), R);
599    if (tt!=NULL)
600    {
601      p_Delete(&tt, R);
602      return FALSE;
603    }
604#else
605    if(!p_EqualPolys(a->m[i],b->m[i], R)) return FALSE;
606#endif
607    i--;
608  }
609  return TRUE;
610}
611
612static poly minuscopy (poly p, const ring R)
613{
614  poly w;
615  number  e;
616  e = n_Init(-1, R);
617  w = p_Copy(p, R);
618  p_Mult_nn(w, e, R);
619  n_Delete(&e, R);
620  return w;
621}
622
623/*2
624* insert a monomial into a list, avoid duplicates
625* arguments are destroyed
626*/
627static poly p_Insert(poly p1, poly p2, const ring R)
628{
629  poly a1, p, a2, a;
630  int c;
631
632  if (p1==NULL) return p2;
633  if (p2==NULL) return p1;
634  a1 = p1;
635  a2 = p2;
636  a = p  = p_One(R);
637  loop
638  {
639    c = p_Cmp(a1, a2, R);
640    if (c == 1)
641    {
642      a = pNext(a) = a1;
643      pIter(a1);
644      if (a1==NULL)
645      {
646        pNext(a) = a2;
647        break;
648      }
649    }
650    else if (c == -1)
651    {
652      a = pNext(a) = a2;
653      pIter(a2);
654      if (a2==NULL)
655      {
656        pNext(a) = a1;
657        break;
658      }
659    }
660    else
661    {
662      p_LmDelete(&a2, R);
663      a = pNext(a) = a1;
664      pIter(a1);
665      if (a1==NULL)
666      {
667        pNext(a) = a2;
668        break;
669      }
670      else if (a2==NULL)
671      {
672        pNext(a) = a1;
673        break;
674      }
675    }
676  }
677  p_LmDelete(&p, R);
678  return p;
679}
680
681/*2
682*if what == xy the result is the list of all different power products
683*    x^i*y^j (i, j >= 0) that appear in fro
684*/
685static poly mp_Select (poly fro, poly what, const ring R)
686{
687  int i;
688  poly h, res;
689  res = NULL;
690  while (fro!=NULL)
691  {
692    h = p_One(R);
693    for (i=1; i<=rVar(R); i++)
694      p_SetExp(h,i, p_GetExp(fro,i, R) * p_GetExp(what, i, R), R);
695    p_SetComp(h, p_GetComp(fro, R), R);
696    p_Setm(h, R);
697    res = p_Insert(h, res, R);
698    fro = fro->next;
699  }
700  return res;
701}
702
703/*
704*static void ppp(matrix a)
705*{
706*  int j,i,r=a->nrows,c=a->ncols;
707*  for(j=1;j<=r;j++)
708*  {
709*    for(i=1;i<=c;i++)
710*    {
711*      if(MATELEM(a,j,i)!=NULL) Print("X");
712*      else Print("0");
713*    }
714*    Print("\n");
715*  }
716*}
717*/
718
719static void mp_PartClean(matrix a, int lr, int lc, const ring R)
720{
721  poly *q1;
722  int i,j;
723
724  for (i=lr-1;i>=0;i--)
725  {
726    q1 = &(a->m)[i*a->ncols];
727    for (j=lc-1;j>=0;j--) if(q1[j]) p_Delete(&q1[j], R);
728  }
729}
730
731static void mp_FinalClean(matrix a, const ring)
732{
733  omFreeSize((ADDRESS)a->m,a->nrows*a->ncols*sizeof(poly));
734  omFreeBin((ADDRESS)a, sip_sideal_bin);
735}
736
737BOOLEAN mp_IsDiagUnit(matrix U, const ring R)
738{
739  if(MATROWS(U)!=MATCOLS(U))
740    return FALSE;
741  for(int i=MATCOLS(U);i>=1;i--)
742  {
743    for(int j=MATCOLS(U); j>=1; j--)
744    {
745      if (i==j)
746      {
747        if (!p_IsUnit(MATELEM(U,i,i), R)) return FALSE;
748      }
749      else if (MATELEM(U,i,j)!=NULL) return FALSE;
750    }
751  }
752  return TRUE;
753}
754
755void iiWriteMatrix(matrix im, const char *n, int dim, const ring r, int spaces)
756{
757  int i,ii = MATROWS(im)-1;
758  int j,jj = MATCOLS(im)-1;
759  poly *pp = im->m;
760
761  for (i=0; i<=ii; i++)
762  {
763    for (j=0; j<=jj; j++)
764    {
765      if (spaces>0)
766        Print("%-*.*s",spaces,spaces," ");
767      if (dim == 2) Print("%s[%u,%u]=",n,i+1,j+1);
768      else if (dim == 1) Print("%s[%u]=",n,j+1);
769      else if (dim == 0) Print("%s=",n);
770      if ((i<ii)||(j<jj)) p_Write(*pp++, r);
771      else                p_Write0(*pp, r);
772    }
773  }
774}
775
776char * iiStringMatrix(matrix im, int dim, const ring r, char ch)
777{
778  int i,ii = MATROWS(im);
779  int j,jj = MATCOLS(im);
780  poly *pp = im->m;
781  char ch_s[2];
782  ch_s[0]=ch;
783  ch_s[1]='\0';
784
785  StringSetS("");
786
787  for (i=0; i<ii; i++)
788  {
789    for (j=0; j<jj; j++)
790    {
791      p_String0(*pp++, r);
792      StringAppendS(ch_s);
793      if (dim > 1) StringAppendS("\n");
794    }
795  }
796  char *s=StringEndS();
797  s[strlen(s)- (dim > 1 ? 2 : 1)]='\0';
798  return s;
799}
800
801void   mp_Delete(matrix* a, const ring r)
802{
803  id_Delete((ideal *) a, r);
804}
805
806/*
807* C++ classes for Bareiss algorithm
808*/
809class row_col_weight
810{
811  private:
812  int ym, yn;
813  public:
814  float *wrow, *wcol;
815  row_col_weight() : ym(0) {}
816  row_col_weight(int, int);
817  ~row_col_weight();
818};
819
820row_col_weight::row_col_weight(int i, int j)
821{
822  ym = i;
823  yn = j;
824  wrow = (float *)omAlloc(i*sizeof(float));
825  wcol = (float *)omAlloc(j*sizeof(float));
826}
827row_col_weight::~row_col_weight()
828{
829  if (ym!=0)
830  {
831    omFreeSize((ADDRESS)wcol, yn*sizeof(float));
832    omFreeSize((ADDRESS)wrow, ym*sizeof(float));
833  }
834}
835
836/*2
837*  a submatrix M of a matrix X[m,n]:
838*    0 <= i < s_m <= a_m
839*    0 <= j < s_n <= a_n
840*    M = ( Xarray[qrow[i],qcol[j]] )
841*    if a_m = a_n and s_m = s_n
842*      det(X) = sign*div^(s_m-1)*det(M)
843*    resticted pivot for elimination
844*      0 <= j < piv_s
845*/
846class mp_permmatrix
847{
848  private:
849  int       a_m, a_n, s_m, s_n, sign, piv_s;
850  int       *qrow, *qcol;
851  poly      *Xarray;
852  ring _R;
853  void mpInitMat();
854  poly * mpRowAdr(int r)
855  { return &(Xarray[a_n*qrow[r]]); }
856  poly * mpColAdr(int c)
857  { return &(Xarray[qcol[c]]); }
858  void mpRowWeight(float *);
859  void mpColWeight(float *);
860  void mpRowSwap(int, int);
861  void mpColSwap(int, int);
862  public:
863  mp_permmatrix() : a_m(0) {}
864  mp_permmatrix(matrix, ring);
865  mp_permmatrix(mp_permmatrix *);
866  ~mp_permmatrix();
867  int mpGetRow();
868  int mpGetCol();
869  int mpGetRdim() { return s_m; }
870  int mpGetCdim() { return s_n; }
871  int mpGetSign() { return sign; }
872  void mpSetSearch(int s);
873  void mpSaveArray() { Xarray = NULL; }
874  poly mpGetElem(int, int);
875  void mpSetElem(poly, int, int);
876  void mpDelElem(int, int);
877  void mpElimBareiss(poly);
878  int mpPivotBareiss(row_col_weight *);
879  int mpPivotRow(row_col_weight *, int);
880  void mpToIntvec(intvec *);
881  void mpRowReorder();
882  void mpColReorder();
883};
884mp_permmatrix::mp_permmatrix(matrix A, ring R) : sign(1)
885{
886  a_m = A->nrows;
887  a_n = A->ncols;
888  this->mpInitMat();
889  Xarray = A->m;
890  _R=R;
891}
892
893mp_permmatrix::mp_permmatrix(mp_permmatrix *M)
894{
895  poly p, *athis, *aM;
896  int i, j;
897
898  _R=M->_R;
899  a_m = M->s_m;
900  a_n = M->s_n;
901  sign = M->sign;
902  this->mpInitMat();
903  Xarray = (poly *)omAlloc0(a_m*a_n*sizeof(poly));
904  for (i=a_m-1; i>=0; i--)
905  {
906    athis = this->mpRowAdr(i);
907    aM = M->mpRowAdr(i);
908    for (j=a_n-1; j>=0; j--)
909    {
910      p = aM[M->qcol[j]];
911      if (p)
912      {
913        athis[j] = p_Copy(p,_R);
914      }
915    }
916  }
917}
918
919mp_permmatrix::~mp_permmatrix()
920{
921  int k;
922
923  if (a_m != 0)
924  {
925    omFreeSize((ADDRESS)qrow,a_m*sizeof(int));
926    omFreeSize((ADDRESS)qcol,a_n*sizeof(int));
927    if (Xarray != NULL)
928    {
929      for (k=a_m*a_n-1; k>=0; k--)
930        p_Delete(&Xarray[k],_R);
931      omFreeSize((ADDRESS)Xarray,a_m*a_n*sizeof(poly));
932    }
933  }
934}
935
936
937static float mp_PolyWeight(poly p, const ring r);
938void mp_permmatrix::mpColWeight(float *wcol)
939{
940  poly p, *a;
941  int i, j;
942  float count;
943
944  for (j=s_n; j>=0; j--)
945  {
946    a = this->mpColAdr(j);
947    count = 0.0;
948    for(i=s_m; i>=0; i--)
949    {
950      p = a[a_n*qrow[i]];
951      if (p)
952        count += mp_PolyWeight(p,_R);
953    }
954    wcol[j] = count;
955  }
956}
957void mp_permmatrix::mpRowWeight(float *wrow)
958{
959  poly p, *a;
960  int i, j;
961  float count;
962
963  for (i=s_m; i>=0; i--)
964  {
965    a = this->mpRowAdr(i);
966    count = 0.0;
967    for(j=s_n; j>=0; j--)
968    {
969      p = a[qcol[j]];
970      if (p)
971        count += mp_PolyWeight(p,_R);
972    }
973    wrow[i] = count;
974  }
975}
976
977void mp_permmatrix::mpRowSwap(int i1, int i2)
978{
979   poly p, *a1, *a2;
980   int j;
981
982   a1 = &(Xarray[a_n*i1]);
983   a2 = &(Xarray[a_n*i2]);
984   for (j=a_n-1; j>= 0; j--)
985   {
986     p = a1[j];
987     a1[j] = a2[j];
988     a2[j] = p;
989   }
990}
991
992void mp_permmatrix::mpColSwap(int j1, int j2)
993{
994   poly p, *a1, *a2;
995   int i, k = a_n*a_m;
996
997   a1 = &(Xarray[j1]);
998   a2 = &(Xarray[j2]);
999   for (i=0; i< k; i+=a_n)
1000   {
1001     p = a1[i];
1002     a1[i] = a2[i];
1003     a2[i] = p;
1004   }
1005}
1006void mp_permmatrix::mpInitMat()
1007{
1008  int k;
1009
1010  s_m = a_m;
1011  s_n = a_n;
1012  piv_s = 0;
1013  qrow = (int *)omAlloc(a_m*sizeof(int));
1014  qcol = (int *)omAlloc(a_n*sizeof(int));
1015  for (k=a_m-1; k>=0; k--) qrow[k] = k;
1016  for (k=a_n-1; k>=0; k--) qcol[k] = k;
1017}
1018
1019void mp_permmatrix::mpColReorder()
1020{
1021  int k, j, j1, j2;
1022
1023  if (a_n > a_m)
1024    k = a_n - a_m;
1025  else
1026    k = 0;
1027  for (j=a_n-1; j>=k; j--)
1028  {
1029    j1 = qcol[j];
1030    if (j1 != j)
1031    {
1032      this->mpColSwap(j1, j);
1033      j2 = 0;
1034      while (qcol[j2] != j) j2++;
1035      qcol[j2] = j1;
1036    }
1037  }
1038}
1039
1040void mp_permmatrix::mpRowReorder()
1041{
1042  int k, i, i1, i2;
1043
1044  if (a_m > a_n)
1045    k = a_m - a_n;
1046  else
1047    k = 0;
1048  for (i=a_m-1; i>=k; i--)
1049  {
1050    i1 = qrow[i];
1051    if (i1 != i)
1052    {
1053      this->mpRowSwap(i1, i);
1054      i2 = 0;
1055      while (qrow[i2] != i) i2++;
1056      qrow[i2] = i1;
1057    }
1058  }
1059}
1060
1061/*
1062* perform replacement for pivot strategy in Bareiss algorithm
1063* change sign of determinant
1064*/
1065static void mpReplace(int j, int n, int &sign, int *perm)
1066{
1067  int k;
1068
1069  if (j != n)
1070  {
1071    k = perm[n];
1072    perm[n] = perm[j];
1073    perm[j] = k;
1074    sign = -sign;
1075  }
1076}
1077/*2
1078* pivot strategy for Bareiss algorithm
1079*/
1080int mp_permmatrix::mpPivotBareiss(row_col_weight *C)
1081{
1082  poly p, *a;
1083  int i, j, iopt, jopt;
1084  float sum, f1, f2, fo, r, ro, lp;
1085  float *dr = C->wrow, *dc = C->wcol;
1086
1087  fo = 1.0e20;
1088  ro = 0.0;
1089  iopt = jopt = -1;
1090
1091  s_n--;
1092  s_m--;
1093  if (s_m == 0)
1094    return 0;
1095  if (s_n == 0)
1096  {
1097    for(i=s_m; i>=0; i--)
1098    {
1099      p = this->mpRowAdr(i)[qcol[0]];
1100      if (p)
1101      {
1102        f1 = mp_PolyWeight(p,_R);
1103        if (f1 < fo)
1104        {
1105          fo = f1;
1106          if (iopt >= 0)
1107            p_Delete(&(this->mpRowAdr(iopt)[qcol[0]]),_R);
1108          iopt = i;
1109        }
1110        else
1111          p_Delete(&(this->mpRowAdr(i)[qcol[0]]),_R);
1112      }
1113    }
1114    if (iopt >= 0)
1115      mpReplace(iopt, s_m, sign, qrow);
1116    return 0;
1117  }
1118  this->mpRowWeight(dr);
1119  this->mpColWeight(dc);
1120  sum = 0.0;
1121  for(i=s_m; i>=0; i--)
1122    sum += dr[i];
1123  for(i=s_m; i>=0; i--)
1124  {
1125    r = dr[i];
1126    a = this->mpRowAdr(i);
1127    for(j=s_n; j>=0; j--)
1128    {
1129      p = a[qcol[j]];
1130      if (p)
1131      {
1132        lp = mp_PolyWeight(p,_R);
1133        ro = r - lp;
1134        f1 = ro * (dc[j]-lp);
1135        if (f1 != 0.0)
1136        {
1137          f2 = lp * (sum - ro - dc[j]);
1138          f2 += f1;
1139        }
1140        else
1141          f2 = lp-r-dc[j];
1142        if (f2 < fo)
1143        {
1144          fo = f2;
1145          iopt = i;
1146          jopt = j;
1147        }
1148      }
1149    }
1150  }
1151  if (iopt < 0)
1152    return 0;
1153  mpReplace(iopt, s_m, sign, qrow);
1154  mpReplace(jopt, s_n, sign, qcol);
1155  return 1;
1156}
1157poly mp_permmatrix::mpGetElem(int r, int c)
1158{
1159  return Xarray[a_n*qrow[r]+qcol[c]];
1160}
1161
1162/*
1163* the Bareiss-type elimination with division by div (div != NULL)
1164*/
1165void mp_permmatrix::mpElimBareiss(poly div)
1166{
1167  poly piv, elim, q1, q2, *ap, *a;
1168  int i, j, jj;
1169
1170  ap = this->mpRowAdr(s_m);
1171  piv = ap[qcol[s_n]];
1172  for(i=s_m-1; i>=0; i--)
1173  {
1174    a = this->mpRowAdr(i);
1175    elim = a[qcol[s_n]];
1176    if (elim != NULL)
1177    {
1178      elim = p_Neg(elim,_R);
1179      for (j=s_n-1; j>=0; j--)
1180      {
1181        q2 = NULL;
1182        jj = qcol[j];
1183        if (ap[jj] != NULL)
1184        {
1185          q2 = SM_MULT(ap[jj], elim, div,_R);
1186          if (a[jj] != NULL)
1187          {
1188            q1 = SM_MULT(a[jj], piv, div,_R);
1189            p_Delete(&a[jj],_R);
1190            q2 = p_Add_q(q2, q1, _R);
1191          }
1192        }
1193        else if (a[jj] != NULL)
1194        {
1195          q2 = SM_MULT(a[jj], piv, div, _R);
1196        }
1197        if ((q2!=NULL) && div)
1198          SM_DIV(q2, div, _R);
1199        a[jj] = q2;
1200      }
1201      p_Delete(&a[qcol[s_n]], _R);
1202    }
1203    else
1204    {
1205      for (j=s_n-1; j>=0; j--)
1206      {
1207        jj = qcol[j];
1208        if (a[jj] != NULL)
1209        {
1210          q2 = SM_MULT(a[jj], piv, div, _R);
1211          p_Delete(&a[jj], _R);
1212          if (div)
1213            SM_DIV(q2, div, _R);
1214          a[jj] = q2;
1215        }
1216      }
1217    }
1218  }
1219}
1220/*
1221* weigth of a polynomial, for pivot strategy
1222*/
1223static float mp_PolyWeight(poly p, const ring r)
1224{
1225  int i;
1226  float res;
1227
1228  if (pNext(p) == NULL)
1229  {
1230    res = (float)n_Size(pGetCoeff(p),r->cf);
1231    for (i=rVar(r);i>0;i--)
1232    {
1233      if(p_GetExp(p,i,r)!=0)
1234      {
1235        res += 2.0;
1236        break;
1237      }
1238    }
1239  }
1240  else
1241  {
1242    res = 0.0;
1243    do
1244    {
1245      res += (float)n_Size(pGetCoeff(p),r->cf)+2.0;
1246      pIter(p);
1247    }
1248    while (p);
1249  }
1250  return res;
1251}
1252/*
1253* find best row
1254*/
1255static int mp_PivBar(matrix a, int lr, int lc, const ring r)
1256{
1257  float f1, f2;
1258  poly *q1;
1259  int i,j,io;
1260
1261  io = -1;
1262  f1 = 1.0e30;
1263  for (i=lr-1;i>=0;i--)
1264  {
1265    q1 = &(a->m)[i*a->ncols];
1266    f2 = 0.0;
1267    for (j=lc-1;j>=0;j--)
1268    {
1269      if (q1[j]!=NULL)
1270        f2 += mp_PolyWeight(q1[j],r);
1271    }
1272    if ((f2!=0.0) && (f2<f1))
1273    {
1274      f1 = f2;
1275      io = i;
1276    }
1277  }
1278  if (io<0) return 0;
1279  else return io+1;
1280}
1281
1282static void mpSwapRow(matrix a, int pos, int lr, int lc)
1283{
1284  poly sw;
1285  int j;
1286  poly* a2 = a->m;
1287  poly* a1 = &a2[a->ncols*(pos-1)];
1288
1289  a2 = &a2[a->ncols*(lr-1)];
1290  for (j=lc-1; j>=0; j--)
1291  {
1292    sw = a1[j];
1293    a1[j] = a2[j];
1294    a2[j] = sw;
1295  }
1296}
1297
1298/*2
1299*  prepare one step of 'Bareiss' algorithm
1300*  for application in minor
1301*/
1302static int mp_PrepareRow (matrix a, int lr, int lc, const ring R)
1303{
1304  int r;
1305
1306  r = mp_PivBar(a,lr,lc,R);
1307  if(r==0) return 0;
1308  if(r<lr) mpSwapRow(a, r, lr, lc);
1309  return 1;
1310}
1311
1312/*
1313* find pivot in the last row
1314*/
1315static int mp_PivRow(matrix a, int lr, int lc, const ring r)
1316{
1317  float f1, f2;
1318  poly *q1;
1319  int j,jo;
1320
1321  jo = -1;
1322  f1 = 1.0e30;
1323  q1 = &(a->m)[(lr-1)*a->ncols];
1324  for (j=lc-1;j>=0;j--)
1325  {
1326    if (q1[j]!=NULL)
1327    {
1328      f2 = mp_PolyWeight(q1[j],r);
1329      if (f2<f1)
1330      {
1331        f1 = f2;
1332        jo = j;
1333      }
1334    }
1335  }
1336  if (jo<0) return 0;
1337  else return jo+1;
1338}
1339
1340static void mpSwapCol(matrix a, int pos, int lr, int lc)
1341{
1342  poly sw;
1343  int j;
1344  poly* a2 = a->m;
1345  poly* a1 = &a2[pos-1];
1346
1347  a2 = &a2[lc-1];
1348  for (j=a->ncols*(lr-1); j>=0; j-=a->ncols)
1349  {
1350    sw = a1[j];
1351    a1[j] = a2[j];
1352    a2[j] = sw;
1353  }
1354}
1355
1356/*2
1357*  prepare one step of 'Bareiss' algorithm
1358*  for application in minor
1359*/
1360static int mp_PreparePiv (matrix a, int lr, int lc,const ring r)
1361{
1362  int c;
1363
1364  c = mp_PivRow(a, lr, lc,r);
1365  if(c==0) return 0;
1366  if(c<lc) mpSwapCol(a, c, lr, lc);
1367  return 1;
1368}
1369
1370static inline BOOLEAN smSmaller(poly a, poly b)
1371{
1372  loop
1373  {
1374    pIter(b);
1375    if (b == NULL) return TRUE;
1376    pIter(a);
1377    if (a == NULL) return FALSE;
1378  }
1379}
1380
1381static BOOLEAN sm_IsNegQuot(poly a, const poly b, const poly c, const ring R)
1382{
1383  if (p_LmDivisibleByNoComp(c, b, R))
1384  {
1385    p_ExpVectorDiff(a, b, c, R);
1386    // Hmm: here used to be a pSetm(a): but it is unnecessary,
1387    // if b and c are correct
1388    return FALSE;
1389  }
1390  else
1391  {
1392    int i;
1393    for (i=rVar(R); i>0; i--)
1394    {
1395      if(p_GetExp(c,i,R) > p_GetExp(b,i,R))
1396        p_SetExp(a,i,p_GetExp(c,i,R)-p_GetExp(b,i,R),R);
1397      else
1398        p_SetExp(a,i,0,R);
1399    }
1400    // here we actually might need a pSetm, if a is to be used in
1401    // comparisons
1402    return TRUE;
1403  }
1404}
1405
1406static void mp_ElimBar(matrix a0, matrix re, poly div, int lr, int lc, const ring R)
1407{
1408  int r=lr-1, c=lc-1;
1409  poly *b = a0->m, *x = re->m;
1410  poly piv, elim, q1, q2, *ap, *a, *q;
1411  int i, j;
1412
1413  ap = &b[r*a0->ncols];
1414  piv = ap[c];
1415  for(j=c-1; j>=0; j--)
1416    if (ap[j] != NULL) ap[j] = p_Neg(ap[j],R);
1417  for(i=r-1; i>=0; i--)
1418  {
1419    a = &b[i*a0->ncols];
1420    q = &x[i*re->ncols];
1421    if (a[c] != NULL)
1422    {
1423      elim = a[c];
1424      for (j=c-1; j>=0; j--)
1425      {
1426        q1 = NULL;
1427        if (a[j] != NULL)
1428        {
1429          q1 = sm_MultDiv(a[j], piv, div,R);
1430          if (ap[j] != NULL)
1431          {
1432            q2 = sm_MultDiv(ap[j], elim, div, R);
1433            q1 = p_Add_q(q1,q2,R);
1434          }
1435        }
1436        else if (ap[j] != NULL)
1437          q1 = sm_MultDiv(ap[j], elim, div, R);
1438        if (q1 != NULL)
1439        {
1440          if (div)
1441            sm_SpecialPolyDiv(q1, div,R);
1442          q[j] = q1;
1443        }
1444      }
1445    }
1446    else
1447    {
1448      for (j=c-1; j>=0; j--)
1449      {
1450        if (a[j] != NULL)
1451        {
1452          q1 = sm_MultDiv(a[j], piv, div, R);
1453          if (div)
1454            sm_SpecialPolyDiv(q1, div, R);
1455          q[j] = q1;
1456        }
1457      }
1458    }
1459  }
1460}
1461
1462/*2*/
1463/// entries of a are minors and go to result (only if not in R)
1464void mp_MinorToResult(ideal result, int &elems, matrix a, int r, int c,
1465                     ideal R, const ring)
1466{
1467  poly *q1;
1468  int e=IDELEMS(result);
1469  int i,j;
1470
1471  if (R != NULL)
1472  {
1473    for (i=r-1;i>=0;i--)
1474    {
1475      q1 = &(a->m)[i*a->ncols];
1476      //for (j=c-1;j>=0;j--)
1477      //{
1478      //  if (q1[j]!=NULL) q1[j] = kNF(R,currQuotient,q1[j]);
1479      //}
1480    }
1481  }
1482  for (i=r-1;i>=0;i--)
1483  {
1484    q1 = &(a->m)[i*a->ncols];
1485    for (j=c-1;j>=0;j--)
1486    {
1487      if (q1[j]!=NULL)
1488      {
1489        if (elems>=e)
1490        {
1491          pEnlargeSet(&(result->m),e,e);
1492          e += e;
1493          IDELEMS(result) =e;
1494        }
1495        result->m[elems] = q1[j];
1496        q1[j] = NULL;
1497        elems++;
1498      }
1499    }
1500  }
1501}
1502/*
1503// from  linalg_from_matpol.cc: TODO: compare with above & remove...
1504void mp_MinorToResult(ideal result, int &elems, matrix a, int r, int c,
1505                     ideal R, const ring R)
1506{
1507  poly *q1;
1508  int e=IDELEMS(result);
1509  int i,j;
1510
1511  if (R != NULL)
1512  {
1513    for (i=r-1;i>=0;i--)
1514    {
1515      q1 = &(a->m)[i*a->ncols];
1516      for (j=c-1;j>=0;j--)
1517      {
1518        if (q1[j]!=NULL) q1[j] = kNF(R,currQuotient,q1[j]);
1519      }
1520    }
1521  }
1522  for (i=r-1;i>=0;i--)
1523  {
1524    q1 = &(a->m)[i*a->ncols];
1525    for (j=c-1;j>=0;j--)
1526    {
1527      if (q1[j]!=NULL)
1528      {
1529        if (elems>=e)
1530        {
1531          if(e<SIZE_OF_SYSTEM_PAGE)
1532          {
1533            pEnlargeSet(&(result->m),e,e);
1534            e += e;
1535          }
1536          else
1537          {
1538            pEnlargeSet(&(result->m),e,SIZE_OF_SYSTEM_PAGE);
1539            e += SIZE_OF_SYSTEM_PAGE;
1540          }
1541          IDELEMS(result) =e;
1542        }
1543        result->m[elems] = q1[j];
1544        q1[j] = NULL;
1545        elems++;
1546      }
1547    }
1548  }
1549}
1550*/
1551
1552static void mpFinalClean(matrix a)
1553{
1554  omFreeSize((ADDRESS)a->m,a->nrows*a->ncols*sizeof(poly));
1555  omFreeBin((ADDRESS)a, sip_sideal_bin);
1556}
1557
1558/*2*/
1559/// produces recursively the ideal of all arxar-minors of a
1560void mp_RecMin(int ar,ideal result,int &elems,matrix a,int lr,int lc,
1561              poly barDiv, ideal R, const ring r)
1562{
1563  int k;
1564  int kr=lr-1,kc=lc-1;
1565  matrix nextLevel=mpNew(kr,kc);
1566
1567  loop
1568  {
1569/*--- look for an optimal row and bring it to last position ------------*/
1570    if(mp_PrepareRow(a,lr,lc,r)==0) break;
1571/*--- now take all pivots from the last row ------------*/
1572    k = lc;
1573    loop
1574    {
1575      if(mp_PreparePiv(a,lr,k,r)==0) break;
1576      mp_ElimBar(a,nextLevel,barDiv,lr,k,r);
1577      k--;
1578      if (ar>1)
1579      {
1580        mp_RecMin(ar-1,result,elems,nextLevel,kr,k,a->m[kr*a->ncols+k],R,r);
1581        mp_PartClean(nextLevel,kr,k, r);
1582      }
1583      else mp_MinorToResult(result,elems,nextLevel,kr,k,R,r);
1584      if (ar>k-1) break;
1585    }
1586    if (ar>=kr) break;
1587/*--- now we have to take out the last row...------------*/
1588    lr = kr;
1589    kr--;
1590  }
1591  mpFinalClean(nextLevel);
1592}
1593/*
1594// from  linalg_from_matpol.cc: TODO: compare with above & remove...
1595void mp_RecMin(int ar,ideal result,int &elems,matrix a,int lr,int lc,
1596              poly barDiv, ideal R, const ring R)
1597{
1598  int k;
1599  int kr=lr-1,kc=lc-1;
1600  matrix nextLevel=mpNew(kr,kc);
1601
1602  loop
1603  {
1604// --- look for an optimal row and bring it to last position ------------
1605    if(mpPrepareRow(a,lr,lc)==0) break;
1606// --- now take all pivots from the last row ------------
1607    k = lc;
1608    loop
1609    {
1610      if(mpPreparePiv(a,lr,k)==0) break;
1611      mpElimBar(a,nextLevel,barDiv,lr,k);
1612      k--;
1613      if (ar>1)
1614      {
1615        mpRecMin(ar-1,result,elems,nextLevel,kr,k,a->m[kr*a->ncols+k],R);
1616        mpPartClean(nextLevel,kr,k);
1617      }
1618      else mpMinorToResult(result,elems,nextLevel,kr,k,R);
1619      if (ar>k-1) break;
1620    }
1621    if (ar>=kr) break;
1622// --- now we have to take out the last row...------------
1623    lr = kr;
1624    kr--;
1625  }
1626  mpFinalClean(nextLevel);
1627}
1628*/
1629
1630/*2*/
1631/// returns the determinant of the matrix m;
1632/// uses Bareiss algorithm
1633poly mp_DetBareiss (matrix a, const ring r)
1634{
1635  int s;
1636  poly div, res;
1637  if (MATROWS(a) != MATCOLS(a))
1638  {
1639    Werror("det of %d x %d matrix",MATROWS(a),MATCOLS(a));
1640    return NULL;
1641  }
1642  matrix c = mp_Copy(a,r);
1643  mp_permmatrix *Bareiss = new mp_permmatrix(c,r);
1644  row_col_weight w(Bareiss->mpGetRdim(), Bareiss->mpGetCdim());
1645
1646  /* Bareiss */
1647  div = NULL;
1648  while(Bareiss->mpPivotBareiss(&w))
1649  {
1650    Bareiss->mpElimBareiss(div);
1651    div = Bareiss->mpGetElem(Bareiss->mpGetRdim(), Bareiss->mpGetCdim());
1652  }
1653  Bareiss->mpRowReorder();
1654  Bareiss->mpColReorder();
1655  Bareiss->mpSaveArray();
1656  s = Bareiss->mpGetSign();
1657  delete Bareiss;
1658
1659  /* result */
1660  res = MATELEM(c,1,1);
1661  MATELEM(c,1,1) = NULL;
1662  id_Delete((ideal *)&c,r);
1663  if (s < 0)
1664    res = p_Neg(res,r);
1665  return res;
1666}
1667/*
1668// from  linalg_from_matpol.cc: TODO: compare with above & remove...
1669poly mp_DetBareiss (matrix a, const ring R)
1670{
1671  int s;
1672  poly div, res;
1673  if (MATROWS(a) != MATCOLS(a))
1674  {
1675    Werror("det of %d x %d matrix",MATROWS(a),MATCOLS(a));
1676    return NULL;
1677  }
1678  matrix c = mp_Copy(a, R);
1679  mp_permmatrix *Bareiss = new mp_permmatrix(c, R);
1680  row_col_weight w(Bareiss->mpGetRdim(), Bareiss->mpGetCdim());
1681
1682  // Bareiss
1683  div = NULL;
1684  while(Bareiss->mpPivotBareiss(&w))
1685  {
1686    Bareiss->mpElimBareiss(div);
1687    div = Bareiss->mpGetElem(Bareiss->mpGetRdim(), Bareiss->mpGetCdim());
1688  }
1689  Bareiss->mpRowReorder();
1690  Bareiss->mpColReorder();
1691  Bareiss->mpSaveArray();
1692  s = Bareiss->mpGetSign();
1693  delete Bareiss;
1694
1695  // result
1696  res = MATELEM(c,1,1);
1697  MATELEM(c,1,1) = NULL;
1698  id_Delete((ideal *)&c, R);
1699  if (s < 0)
1700    res = p_Neg(res, R);
1701  return res;
1702}
1703*/
1704
1705/*2
1706* compute all ar-minors of the matrix a
1707*/
1708matrix mp_Wedge(matrix a, int ar, const ring R)
1709{
1710  int     i,j,k,l;
1711  int *rowchoise,*colchoise;
1712  BOOLEAN rowch,colch;
1713  matrix result;
1714  matrix tmp;
1715  poly p;
1716
1717  i = binom(a->nrows,ar);
1718  j = binom(a->ncols,ar);
1719
1720  rowchoise=(int *)omAlloc(ar*sizeof(int));
1721  colchoise=(int *)omAlloc(ar*sizeof(int));
1722  result = mpNew(i,j);
1723  tmp    = mpNew(ar,ar);
1724  l = 1; /* k,l:the index in result*/
1725  idInitChoise(ar,1,a->nrows,&rowch,rowchoise);
1726  while (!rowch)
1727  {
1728    k=1;
1729    idInitChoise(ar,1,a->ncols,&colch,colchoise);
1730    while (!colch)
1731    {
1732      for (i=1; i<=ar; i++)
1733      {
1734        for (j=1; j<=ar; j++)
1735        {
1736          MATELEM(tmp,i,j) = MATELEM(a,rowchoise[i-1],colchoise[j-1]);
1737        }
1738      }
1739      p = mp_DetBareiss(tmp, R);
1740      if ((k+l) & 1) p=p_Neg(p, R);
1741      MATELEM(result,l,k) = p;
1742      k++;
1743      idGetNextChoise(ar,a->ncols,&colch,colchoise);
1744    }
1745    idGetNextChoise(ar,a->nrows,&rowch,rowchoise);
1746    l++;
1747  }
1748
1749  /*delete the matrix tmp*/
1750  for (i=1; i<=ar; i++)
1751  {
1752    for (j=1; j<=ar; j++) MATELEM(tmp,i,j) = NULL;
1753  }
1754  id_Delete((ideal *) &tmp, R);
1755  return (result);
1756}
Note: See TracBrowser for help on using the repository browser.