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

spielwiese
Last change on this file since ba5e9e was ba5e9e, checked in by Oleksandr Motsak <motsak@…>, 11 years ago
Changed configure-scripts to generate individual public config files for each package: resources, libpolys, singular (main) fix: sources should include correct corresponding config headers.
  • 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  StringSetS("");
782
783  for (i=0; i<ii; i++)
784  {
785    for (j=0; j<jj; j++)
786    {
787      p_String0(*pp++, r);
788      StringAppend("%c",ch);
789      if (dim > 1) StringAppendS("\n");
790    }
791  }
792  char *s=StringEndS();
793  s[strlen(s)- (dim > 1 ? 2 : 1)]='\0';
794  return s;
795}
796
797void   mp_Delete(matrix* a, const ring r)
798{
799  id_Delete((ideal *) a, r);
800}
801
802/*
803* C++ classes for Bareiss algorithm
804*/
805class row_col_weight
806{
807  private:
808  int ym, yn;
809  public:
810  float *wrow, *wcol;
811  row_col_weight() : ym(0) {}
812  row_col_weight(int, int);
813  ~row_col_weight();
814};
815
816row_col_weight::row_col_weight(int i, int j)
817{
818  ym = i;
819  yn = j;
820  wrow = (float *)omAlloc(i*sizeof(float));
821  wcol = (float *)omAlloc(j*sizeof(float));
822}
823row_col_weight::~row_col_weight()
824{
825  if (ym!=0)
826  {
827    omFreeSize((ADDRESS)wcol, yn*sizeof(float));
828    omFreeSize((ADDRESS)wrow, ym*sizeof(float));
829  }
830}
831
832/*2
833*  a submatrix M of a matrix X[m,n]:
834*    0 <= i < s_m <= a_m
835*    0 <= j < s_n <= a_n
836*    M = ( Xarray[qrow[i],qcol[j]] )
837*    if a_m = a_n and s_m = s_n
838*      det(X) = sign*div^(s_m-1)*det(M)
839*    resticted pivot for elimination
840*      0 <= j < piv_s
841*/
842class mp_permmatrix
843{
844  private:
845  int       a_m, a_n, s_m, s_n, sign, piv_s;
846  int       *qrow, *qcol;
847  poly      *Xarray;
848  ring _R;
849  void mpInitMat();
850  poly * mpRowAdr(int r)
851  { return &(Xarray[a_n*qrow[r]]); }
852  poly * mpColAdr(int c)
853  { return &(Xarray[qcol[c]]); }
854  void mpRowWeight(float *);
855  void mpColWeight(float *);
856  void mpRowSwap(int, int);
857  void mpColSwap(int, int);
858  public:
859  mp_permmatrix() : a_m(0) {}
860  mp_permmatrix(matrix, ring);
861  mp_permmatrix(mp_permmatrix *);
862  ~mp_permmatrix();
863  int mpGetRow();
864  int mpGetCol();
865  int mpGetRdim() { return s_m; }
866  int mpGetCdim() { return s_n; }
867  int mpGetSign() { return sign; }
868  void mpSetSearch(int s);
869  void mpSaveArray() { Xarray = NULL; }
870  poly mpGetElem(int, int);
871  void mpSetElem(poly, int, int);
872  void mpDelElem(int, int);
873  void mpElimBareiss(poly);
874  int mpPivotBareiss(row_col_weight *);
875  int mpPivotRow(row_col_weight *, int);
876  void mpToIntvec(intvec *);
877  void mpRowReorder();
878  void mpColReorder();
879};
880mp_permmatrix::mp_permmatrix(matrix A, ring R) : sign(1)
881{
882  a_m = A->nrows;
883  a_n = A->ncols;
884  this->mpInitMat();
885  Xarray = A->m;
886  _R=R;
887}
888
889mp_permmatrix::mp_permmatrix(mp_permmatrix *M)
890{
891  poly p, *athis, *aM;
892  int i, j;
893
894  _R=M->_R;
895  a_m = M->s_m;
896  a_n = M->s_n;
897  sign = M->sign;
898  this->mpInitMat();
899  Xarray = (poly *)omAlloc0(a_m*a_n*sizeof(poly));
900  for (i=a_m-1; i>=0; i--)
901  {
902    athis = this->mpRowAdr(i);
903    aM = M->mpRowAdr(i);
904    for (j=a_n-1; j>=0; j--)
905    {
906      p = aM[M->qcol[j]];
907      if (p)
908      {
909        athis[j] = p_Copy(p,_R);
910      }
911    }
912  }
913}
914
915mp_permmatrix::~mp_permmatrix()
916{
917  int k;
918
919  if (a_m != 0)
920  {
921    omFreeSize((ADDRESS)qrow,a_m*sizeof(int));
922    omFreeSize((ADDRESS)qcol,a_n*sizeof(int));
923    if (Xarray != NULL)
924    {
925      for (k=a_m*a_n-1; k>=0; k--)
926        p_Delete(&Xarray[k],_R);
927      omFreeSize((ADDRESS)Xarray,a_m*a_n*sizeof(poly));
928    }
929  }
930}
931
932
933static float mp_PolyWeight(poly p, const ring r);
934void mp_permmatrix::mpColWeight(float *wcol)
935{
936  poly p, *a;
937  int i, j;
938  float count;
939
940  for (j=s_n; j>=0; j--)
941  {
942    a = this->mpColAdr(j);
943    count = 0.0;
944    for(i=s_m; i>=0; i--)
945    {
946      p = a[a_n*qrow[i]];
947      if (p)
948        count += mp_PolyWeight(p,_R);
949    }
950    wcol[j] = count;
951  }
952}
953void mp_permmatrix::mpRowWeight(float *wrow)
954{
955  poly p, *a;
956  int i, j;
957  float count;
958
959  for (i=s_m; i>=0; i--)
960  {
961    a = this->mpRowAdr(i);
962    count = 0.0;
963    for(j=s_n; j>=0; j--)
964    {
965      p = a[qcol[j]];
966      if (p)
967        count += mp_PolyWeight(p,_R);
968    }
969    wrow[i] = count;
970  }
971}
972
973void mp_permmatrix::mpRowSwap(int i1, int i2)
974{
975   poly p, *a1, *a2;
976   int j;
977
978   a1 = &(Xarray[a_n*i1]);
979   a2 = &(Xarray[a_n*i2]);
980   for (j=a_n-1; j>= 0; j--)
981   {
982     p = a1[j];
983     a1[j] = a2[j];
984     a2[j] = p;
985   }
986}
987
988void mp_permmatrix::mpColSwap(int j1, int j2)
989{
990   poly p, *a1, *a2;
991   int i, k = a_n*a_m;
992
993   a1 = &(Xarray[j1]);
994   a2 = &(Xarray[j2]);
995   for (i=0; i< k; i+=a_n)
996   {
997     p = a1[i];
998     a1[i] = a2[i];
999     a2[i] = p;
1000   }
1001}
1002void mp_permmatrix::mpInitMat()
1003{
1004  int k;
1005
1006  s_m = a_m;
1007  s_n = a_n;
1008  piv_s = 0;
1009  qrow = (int *)omAlloc(a_m*sizeof(int));
1010  qcol = (int *)omAlloc(a_n*sizeof(int));
1011  for (k=a_m-1; k>=0; k--) qrow[k] = k;
1012  for (k=a_n-1; k>=0; k--) qcol[k] = k;
1013}
1014
1015void mp_permmatrix::mpColReorder()
1016{
1017  int k, j, j1, j2;
1018
1019  if (a_n > a_m)
1020    k = a_n - a_m;
1021  else
1022    k = 0;
1023  for (j=a_n-1; j>=k; j--)
1024  {
1025    j1 = qcol[j];
1026    if (j1 != j)
1027    {
1028      this->mpColSwap(j1, j);
1029      j2 = 0;
1030      while (qcol[j2] != j) j2++;
1031      qcol[j2] = j1;
1032    }
1033  }
1034}
1035
1036void mp_permmatrix::mpRowReorder()
1037{
1038  int k, i, i1, i2;
1039
1040  if (a_m > a_n)
1041    k = a_m - a_n;
1042  else
1043    k = 0;
1044  for (i=a_m-1; i>=k; i--)
1045  {
1046    i1 = qrow[i];
1047    if (i1 != i)
1048    {
1049      this->mpRowSwap(i1, i);
1050      i2 = 0;
1051      while (qrow[i2] != i) i2++;
1052      qrow[i2] = i1;
1053    }
1054  }
1055}
1056
1057/*
1058* perform replacement for pivot strategy in Bareiss algorithm
1059* change sign of determinant
1060*/
1061static void mpReplace(int j, int n, int &sign, int *perm)
1062{
1063  int k;
1064
1065  if (j != n)
1066  {
1067    k = perm[n];
1068    perm[n] = perm[j];
1069    perm[j] = k;
1070    sign = -sign;
1071  }
1072}
1073/*2
1074* pivot strategy for Bareiss algorithm
1075*/
1076int mp_permmatrix::mpPivotBareiss(row_col_weight *C)
1077{
1078  poly p, *a;
1079  int i, j, iopt, jopt;
1080  float sum, f1, f2, fo, r, ro, lp;
1081  float *dr = C->wrow, *dc = C->wcol;
1082
1083  fo = 1.0e20;
1084  ro = 0.0;
1085  iopt = jopt = -1;
1086
1087  s_n--;
1088  s_m--;
1089  if (s_m == 0)
1090    return 0;
1091  if (s_n == 0)
1092  {
1093    for(i=s_m; i>=0; i--)
1094    {
1095      p = this->mpRowAdr(i)[qcol[0]];
1096      if (p)
1097      {
1098        f1 = mp_PolyWeight(p,_R);
1099        if (f1 < fo)
1100        {
1101          fo = f1;
1102          if (iopt >= 0)
1103            p_Delete(&(this->mpRowAdr(iopt)[qcol[0]]),_R);
1104          iopt = i;
1105        }
1106        else
1107          p_Delete(&(this->mpRowAdr(i)[qcol[0]]),_R);
1108      }
1109    }
1110    if (iopt >= 0)
1111      mpReplace(iopt, s_m, sign, qrow);
1112    return 0;
1113  }
1114  this->mpRowWeight(dr);
1115  this->mpColWeight(dc);
1116  sum = 0.0;
1117  for(i=s_m; i>=0; i--)
1118    sum += dr[i];
1119  for(i=s_m; i>=0; i--)
1120  {
1121    r = dr[i];
1122    a = this->mpRowAdr(i);
1123    for(j=s_n; j>=0; j--)
1124    {
1125      p = a[qcol[j]];
1126      if (p)
1127      {
1128        lp = mp_PolyWeight(p,_R);
1129        ro = r - lp;
1130        f1 = ro * (dc[j]-lp);
1131        if (f1 != 0.0)
1132        {
1133          f2 = lp * (sum - ro - dc[j]);
1134          f2 += f1;
1135        }
1136        else
1137          f2 = lp-r-dc[j];
1138        if (f2 < fo)
1139        {
1140          fo = f2;
1141          iopt = i;
1142          jopt = j;
1143        }
1144      }
1145    }
1146  }
1147  if (iopt < 0)
1148    return 0;
1149  mpReplace(iopt, s_m, sign, qrow);
1150  mpReplace(jopt, s_n, sign, qcol);
1151  return 1;
1152}
1153poly mp_permmatrix::mpGetElem(int r, int c)
1154{
1155  return Xarray[a_n*qrow[r]+qcol[c]];
1156}
1157
1158/*
1159* the Bareiss-type elimination with division by div (div != NULL)
1160*/
1161void mp_permmatrix::mpElimBareiss(poly div)
1162{
1163  poly piv, elim, q1, q2, *ap, *a;
1164  int i, j, jj;
1165
1166  ap = this->mpRowAdr(s_m);
1167  piv = ap[qcol[s_n]];
1168  for(i=s_m-1; i>=0; i--)
1169  {
1170    a = this->mpRowAdr(i);
1171    elim = a[qcol[s_n]];
1172    if (elim != NULL)
1173    {
1174      elim = p_Neg(elim,_R);
1175      for (j=s_n-1; j>=0; j--)
1176      {
1177        q2 = NULL;
1178        jj = qcol[j];
1179        if (ap[jj] != NULL)
1180        {
1181          q2 = SM_MULT(ap[jj], elim, div,_R);
1182          if (a[jj] != NULL)
1183          {
1184            q1 = SM_MULT(a[jj], piv, div,_R);
1185            p_Delete(&a[jj],_R);
1186            q2 = p_Add_q(q2, q1, _R);
1187          }
1188        }
1189        else if (a[jj] != NULL)
1190        {
1191          q2 = SM_MULT(a[jj], piv, div, _R);
1192        }
1193        if ((q2!=NULL) && div)
1194          SM_DIV(q2, div, _R);
1195        a[jj] = q2;
1196      }
1197      p_Delete(&a[qcol[s_n]], _R);
1198    }
1199    else
1200    {
1201      for (j=s_n-1; j>=0; j--)
1202      {
1203        jj = qcol[j];
1204        if (a[jj] != NULL)
1205        {
1206          q2 = SM_MULT(a[jj], piv, div, _R);
1207          p_Delete(&a[jj], _R);
1208          if (div)
1209            SM_DIV(q2, div, _R);
1210          a[jj] = q2;
1211        }
1212      }
1213    }
1214  }
1215}
1216/*
1217* weigth of a polynomial, for pivot strategy
1218*/
1219static float mp_PolyWeight(poly p, const ring r)
1220{
1221  int i;
1222  float res;
1223
1224  if (pNext(p) == NULL)
1225  {
1226    res = (float)n_Size(pGetCoeff(p),r->cf);
1227    for (i=rVar(r);i>0;i--)
1228    {
1229      if(p_GetExp(p,i,r)!=0)
1230      {
1231        res += 2.0;
1232        break;
1233      }
1234    }
1235  }
1236  else
1237  {
1238    res = 0.0;
1239    do
1240    {
1241      res += (float)n_Size(pGetCoeff(p),r->cf)+2.0;
1242      pIter(p);
1243    }
1244    while (p);
1245  }
1246  return res;
1247}
1248/*
1249* find best row
1250*/
1251static int mp_PivBar(matrix a, int lr, int lc, const ring r)
1252{
1253  float f1, f2;
1254  poly *q1;
1255  int i,j,io;
1256
1257  io = -1;
1258  f1 = 1.0e30;
1259  for (i=lr-1;i>=0;i--)
1260  {
1261    q1 = &(a->m)[i*a->ncols];
1262    f2 = 0.0;
1263    for (j=lc-1;j>=0;j--)
1264    {
1265      if (q1[j]!=NULL)
1266        f2 += mp_PolyWeight(q1[j],r);
1267    }
1268    if ((f2!=0.0) && (f2<f1))
1269    {
1270      f1 = f2;
1271      io = i;
1272    }
1273  }
1274  if (io<0) return 0;
1275  else return io+1;
1276}
1277
1278static void mpSwapRow(matrix a, int pos, int lr, int lc)
1279{
1280  poly sw;
1281  int j;
1282  poly* a2 = a->m;
1283  poly* a1 = &a2[a->ncols*(pos-1)];
1284
1285  a2 = &a2[a->ncols*(lr-1)];
1286  for (j=lc-1; j>=0; j--)
1287  {
1288    sw = a1[j];
1289    a1[j] = a2[j];
1290    a2[j] = sw;
1291  }
1292}
1293
1294/*2
1295*  prepare one step of 'Bareiss' algorithm
1296*  for application in minor
1297*/
1298static int mp_PrepareRow (matrix a, int lr, int lc, const ring R)
1299{
1300  int r;
1301
1302  r = mp_PivBar(a,lr,lc,R);
1303  if(r==0) return 0;
1304  if(r<lr) mpSwapRow(a, r, lr, lc);
1305  return 1;
1306}
1307
1308/*
1309* find pivot in the last row
1310*/
1311static int mp_PivRow(matrix a, int lr, int lc, const ring r)
1312{
1313  float f1, f2;
1314  poly *q1;
1315  int j,jo;
1316
1317  jo = -1;
1318  f1 = 1.0e30;
1319  q1 = &(a->m)[(lr-1)*a->ncols];
1320  for (j=lc-1;j>=0;j--)
1321  {
1322    if (q1[j]!=NULL)
1323    {
1324      f2 = mp_PolyWeight(q1[j],r);
1325      if (f2<f1)
1326      {
1327        f1 = f2;
1328        jo = j;
1329      }
1330    }
1331  }
1332  if (jo<0) return 0;
1333  else return jo+1;
1334}
1335
1336static void mpSwapCol(matrix a, int pos, int lr, int lc)
1337{
1338  poly sw;
1339  int j;
1340  poly* a2 = a->m;
1341  poly* a1 = &a2[pos-1];
1342
1343  a2 = &a2[lc-1];
1344  for (j=a->ncols*(lr-1); j>=0; j-=a->ncols)
1345  {
1346    sw = a1[j];
1347    a1[j] = a2[j];
1348    a2[j] = sw;
1349  }
1350}
1351
1352/*2
1353*  prepare one step of 'Bareiss' algorithm
1354*  for application in minor
1355*/
1356static int mp_PreparePiv (matrix a, int lr, int lc,const ring r)
1357{
1358  int c;
1359
1360  c = mp_PivRow(a, lr, lc,r);
1361  if(c==0) return 0;
1362  if(c<lc) mpSwapCol(a, c, lr, lc);
1363  return 1;
1364}
1365
1366static inline BOOLEAN smSmaller(poly a, poly b)
1367{
1368  loop
1369  {
1370    pIter(b);
1371    if (b == NULL) return TRUE;
1372    pIter(a);
1373    if (a == NULL) return FALSE;
1374  }
1375}
1376
1377static BOOLEAN sm_IsNegQuot(poly a, const poly b, const poly c, const ring R)
1378{
1379  if (p_LmDivisibleByNoComp(c, b, R))
1380  {
1381    p_ExpVectorDiff(a, b, c, R);
1382    // Hmm: here used to be a pSetm(a): but it is unnecessary,
1383    // if b and c are correct
1384    return FALSE;
1385  }
1386  else
1387  {
1388    int i;
1389    for (i=rVar(R); i>0; i--)
1390    {
1391      if(p_GetExp(c,i,R) > p_GetExp(b,i,R))
1392        p_SetExp(a,i,p_GetExp(c,i,R)-p_GetExp(b,i,R),R);
1393      else
1394        p_SetExp(a,i,0,R);
1395    }
1396    // here we actually might need a pSetm, if a is to be used in
1397    // comparisons
1398    return TRUE;
1399  }
1400}
1401
1402static void mp_ElimBar(matrix a0, matrix re, poly div, int lr, int lc, const ring R)
1403{
1404  int r=lr-1, c=lc-1;
1405  poly *b = a0->m, *x = re->m;
1406  poly piv, elim, q1, q2, *ap, *a, *q;
1407  int i, j;
1408
1409  ap = &b[r*a0->ncols];
1410  piv = ap[c];
1411  for(j=c-1; j>=0; j--)
1412    if (ap[j] != NULL) ap[j] = p_Neg(ap[j],R);
1413  for(i=r-1; i>=0; i--)
1414  {
1415    a = &b[i*a0->ncols];
1416    q = &x[i*re->ncols];
1417    if (a[c] != NULL)
1418    {
1419      elim = a[c];
1420      for (j=c-1; j>=0; j--)
1421      {
1422        q1 = NULL;
1423        if (a[j] != NULL)
1424        {
1425          q1 = sm_MultDiv(a[j], piv, div,R);
1426          if (ap[j] != NULL)
1427          {
1428            q2 = sm_MultDiv(ap[j], elim, div, R);
1429            q1 = p_Add_q(q1,q2,R);
1430          }
1431        }
1432        else if (ap[j] != NULL)
1433          q1 = sm_MultDiv(ap[j], elim, div, R);
1434        if (q1 != NULL)
1435        {
1436          if (div)
1437            sm_SpecialPolyDiv(q1, div,R);
1438          q[j] = q1;
1439        }
1440      }
1441    }
1442    else
1443    {
1444      for (j=c-1; j>=0; j--)
1445      {
1446        if (a[j] != NULL)
1447        {
1448          q1 = sm_MultDiv(a[j], piv, div, R);
1449          if (div)
1450            sm_SpecialPolyDiv(q1, div, R);
1451          q[j] = q1;
1452        }
1453      }
1454    }
1455  }
1456}
1457
1458/*2*/
1459/// entries of a are minors and go to result (only if not in R)
1460void mp_MinorToResult(ideal result, int &elems, matrix a, int r, int c,
1461                     ideal R, const ring)
1462{
1463  poly *q1;
1464  int e=IDELEMS(result);
1465  int i,j;
1466
1467  if (R != NULL)
1468  {
1469    for (i=r-1;i>=0;i--)
1470    {
1471      q1 = &(a->m)[i*a->ncols];
1472      //for (j=c-1;j>=0;j--)
1473      //{
1474      //  if (q1[j]!=NULL) q1[j] = kNF(R,currQuotient,q1[j]);
1475      //}
1476    }
1477  }
1478  for (i=r-1;i>=0;i--)
1479  {
1480    q1 = &(a->m)[i*a->ncols];
1481    for (j=c-1;j>=0;j--)
1482    {
1483      if (q1[j]!=NULL)
1484      {
1485        if (elems>=e)
1486        {
1487          pEnlargeSet(&(result->m),e,e);
1488          e += e;
1489          IDELEMS(result) =e;
1490        }
1491        result->m[elems] = q1[j];
1492        q1[j] = NULL;
1493        elems++;
1494      }
1495    }
1496  }
1497}
1498/*
1499// from  linalg_from_matpol.cc: TODO: compare with above & remove...
1500void mp_MinorToResult(ideal result, int &elems, matrix a, int r, int c,
1501                     ideal R, const ring R)
1502{
1503  poly *q1;
1504  int e=IDELEMS(result);
1505  int i,j;
1506
1507  if (R != NULL)
1508  {
1509    for (i=r-1;i>=0;i--)
1510    {
1511      q1 = &(a->m)[i*a->ncols];
1512      for (j=c-1;j>=0;j--)
1513      {
1514        if (q1[j]!=NULL) q1[j] = kNF(R,currQuotient,q1[j]);
1515      }
1516    }
1517  }
1518  for (i=r-1;i>=0;i--)
1519  {
1520    q1 = &(a->m)[i*a->ncols];
1521    for (j=c-1;j>=0;j--)
1522    {
1523      if (q1[j]!=NULL)
1524      {
1525        if (elems>=e)
1526        {
1527          if(e<SIZE_OF_SYSTEM_PAGE)
1528          {
1529            pEnlargeSet(&(result->m),e,e);
1530            e += e;
1531          }
1532          else
1533          {
1534            pEnlargeSet(&(result->m),e,SIZE_OF_SYSTEM_PAGE);
1535            e += SIZE_OF_SYSTEM_PAGE;
1536          }
1537          IDELEMS(result) =e;
1538        }
1539        result->m[elems] = q1[j];
1540        q1[j] = NULL;
1541        elems++;
1542      }
1543    }
1544  }
1545}
1546*/
1547
1548static void mpFinalClean(matrix a)
1549{
1550  omFreeSize((ADDRESS)a->m,a->nrows*a->ncols*sizeof(poly));
1551  omFreeBin((ADDRESS)a, sip_sideal_bin);
1552}
1553
1554/*2*/
1555/// produces recursively the ideal of all arxar-minors of a
1556void mp_RecMin(int ar,ideal result,int &elems,matrix a,int lr,int lc,
1557              poly barDiv, ideal R, const ring r)
1558{
1559  int k;
1560  int kr=lr-1,kc=lc-1;
1561  matrix nextLevel=mpNew(kr,kc);
1562
1563  loop
1564  {
1565/*--- look for an optimal row and bring it to last position ------------*/
1566    if(mp_PrepareRow(a,lr,lc,r)==0) break;
1567/*--- now take all pivots from the last row ------------*/
1568    k = lc;
1569    loop
1570    {
1571      if(mp_PreparePiv(a,lr,k,r)==0) break;
1572      mp_ElimBar(a,nextLevel,barDiv,lr,k,r);
1573      k--;
1574      if (ar>1)
1575      {
1576        mp_RecMin(ar-1,result,elems,nextLevel,kr,k,a->m[kr*a->ncols+k],R,r);
1577        mp_PartClean(nextLevel,kr,k, r);
1578      }
1579      else mp_MinorToResult(result,elems,nextLevel,kr,k,R,r);
1580      if (ar>k-1) break;
1581    }
1582    if (ar>=kr) break;
1583/*--- now we have to take out the last row...------------*/
1584    lr = kr;
1585    kr--;
1586  }
1587  mpFinalClean(nextLevel);
1588}
1589/*
1590// from  linalg_from_matpol.cc: TODO: compare with above & remove...
1591void mp_RecMin(int ar,ideal result,int &elems,matrix a,int lr,int lc,
1592              poly barDiv, ideal R, const ring R)
1593{
1594  int k;
1595  int kr=lr-1,kc=lc-1;
1596  matrix nextLevel=mpNew(kr,kc);
1597
1598  loop
1599  {
1600// --- look for an optimal row and bring it to last position ------------
1601    if(mpPrepareRow(a,lr,lc)==0) break;
1602// --- now take all pivots from the last row ------------
1603    k = lc;
1604    loop
1605    {
1606      if(mpPreparePiv(a,lr,k)==0) break;
1607      mpElimBar(a,nextLevel,barDiv,lr,k);
1608      k--;
1609      if (ar>1)
1610      {
1611        mpRecMin(ar-1,result,elems,nextLevel,kr,k,a->m[kr*a->ncols+k],R);
1612        mpPartClean(nextLevel,kr,k);
1613      }
1614      else mpMinorToResult(result,elems,nextLevel,kr,k,R);
1615      if (ar>k-1) break;
1616    }
1617    if (ar>=kr) break;
1618// --- now we have to take out the last row...------------
1619    lr = kr;
1620    kr--;
1621  }
1622  mpFinalClean(nextLevel);
1623}
1624*/
1625
1626/*2*/
1627/// returns the determinant of the matrix m;
1628/// uses Bareiss algorithm
1629poly mp_DetBareiss (matrix a, const ring r)
1630{
1631  int s;
1632  poly div, res;
1633  if (MATROWS(a) != MATCOLS(a))
1634  {
1635    Werror("det of %d x %d matrix",MATROWS(a),MATCOLS(a));
1636    return NULL;
1637  }
1638  matrix c = mp_Copy(a,r);
1639  mp_permmatrix *Bareiss = new mp_permmatrix(c,r);
1640  row_col_weight w(Bareiss->mpGetRdim(), Bareiss->mpGetCdim());
1641
1642  /* Bareiss */
1643  div = NULL;
1644  while(Bareiss->mpPivotBareiss(&w))
1645  {
1646    Bareiss->mpElimBareiss(div);
1647    div = Bareiss->mpGetElem(Bareiss->mpGetRdim(), Bareiss->mpGetCdim());
1648  }
1649  Bareiss->mpRowReorder();
1650  Bareiss->mpColReorder();
1651  Bareiss->mpSaveArray();
1652  s = Bareiss->mpGetSign();
1653  delete Bareiss;
1654
1655  /* result */
1656  res = MATELEM(c,1,1);
1657  MATELEM(c,1,1) = NULL;
1658  id_Delete((ideal *)&c,r);
1659  if (s < 0)
1660    res = p_Neg(res,r);
1661  return res;
1662}
1663/*
1664// from  linalg_from_matpol.cc: TODO: compare with above & remove...
1665poly mp_DetBareiss (matrix a, const ring R)
1666{
1667  int s;
1668  poly div, res;
1669  if (MATROWS(a) != MATCOLS(a))
1670  {
1671    Werror("det of %d x %d matrix",MATROWS(a),MATCOLS(a));
1672    return NULL;
1673  }
1674  matrix c = mp_Copy(a, R);
1675  mp_permmatrix *Bareiss = new mp_permmatrix(c, R);
1676  row_col_weight w(Bareiss->mpGetRdim(), Bareiss->mpGetCdim());
1677
1678  // Bareiss
1679  div = NULL;
1680  while(Bareiss->mpPivotBareiss(&w))
1681  {
1682    Bareiss->mpElimBareiss(div);
1683    div = Bareiss->mpGetElem(Bareiss->mpGetRdim(), Bareiss->mpGetCdim());
1684  }
1685  Bareiss->mpRowReorder();
1686  Bareiss->mpColReorder();
1687  Bareiss->mpSaveArray();
1688  s = Bareiss->mpGetSign();
1689  delete Bareiss;
1690
1691  // result
1692  res = MATELEM(c,1,1);
1693  MATELEM(c,1,1) = NULL;
1694  id_Delete((ideal *)&c, R);
1695  if (s < 0)
1696    res = p_Neg(res, R);
1697  return res;
1698}
1699*/
1700
1701/*2
1702* compute all ar-minors of the matrix a
1703*/
1704matrix mp_Wedge(matrix a, int ar, const ring R)
1705{
1706  int     i,j,k,l;
1707  int *rowchoise,*colchoise;
1708  BOOLEAN rowch,colch;
1709  matrix result;
1710  matrix tmp;
1711  poly p;
1712
1713  i = binom(a->nrows,ar);
1714  j = binom(a->ncols,ar);
1715
1716  rowchoise=(int *)omAlloc(ar*sizeof(int));
1717  colchoise=(int *)omAlloc(ar*sizeof(int));
1718  result = mpNew(i,j);
1719  tmp    = mpNew(ar,ar);
1720  l = 1; /* k,l:the index in result*/
1721  idInitChoise(ar,1,a->nrows,&rowch,rowchoise);
1722  while (!rowch)
1723  {
1724    k=1;
1725    idInitChoise(ar,1,a->ncols,&colch,colchoise);
1726    while (!colch)
1727    {
1728      for (i=1; i<=ar; i++)
1729      {
1730        for (j=1; j<=ar; j++)
1731        {
1732          MATELEM(tmp,i,j) = MATELEM(a,rowchoise[i-1],colchoise[j-1]);
1733        }
1734      }
1735      p = mp_DetBareiss(tmp, R);
1736      if ((k+l) & 1) p=p_Neg(p, R);
1737      MATELEM(result,l,k) = p;
1738      k++;
1739      idGetNextChoise(ar,a->ncols,&colch,colchoise);
1740    }
1741    idGetNextChoise(ar,a->nrows,&rowch,rowchoise);
1742    l++;
1743  }
1744
1745  /*delete the matrix tmp*/
1746  for (i=1; i<=ar; i++)
1747  {
1748    for (j=1; j<=ar; j++) MATELEM(tmp,i,j) = NULL;
1749  }
1750  id_Delete((ideal *) &tmp, R);
1751  return (result);
1752}
Note: See TracBrowser for help on using the repository browser.