source: git/kernel/matpol.cc @ b4ffba

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