source: git/kernel/matpol.cc @ 68349d

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