source: git/kernel/matpol.cc @ 6b9532

spielwiese
Last change on this file since 6b9532 was 5f1edb, checked in by Hans Schönemann <hannes@…>, 20 years ago
*hannes: assume) git-svn-id: file:///usr/local/Singular/svn/trunk@7318 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.2 2004-08-05 08:57:26 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  assume(vars!=NULL);
716  poly sel, h;
717  int l, i;
718  int pos_of_1 = -1;
719  matrix co;
720
721  if (f==NULL)
722  {
723    co = mpNew(2, 1);
724    MATELEM(co,1,1) = pOne();
725    MATELEM(co,2,1) = NULL;
726    return co;
727  }
728  sel = mpSelect(f, vars);
729  l = pLength(sel);
730  co = mpNew(2, l);
731  if (pOrdSgn==-1)
732  {
733    for (i=l; i>=1; i--)
734    {
735      h = sel;
736      pIter(sel);
737      pNext(h)=NULL;
738      MATELEM(co,1,i) = h;
739      MATELEM(co,2,i) = NULL;
740      if (pIsConstant(h)) pos_of_1 = i;
741    }
742  }
743  else
744  {
745    for (i=1; i<=l; i++)
746    {
747      h = sel;
748      pIter(sel);
749      pNext(h)=NULL;
750      MATELEM(co,1,i) = h;
751      MATELEM(co,2,i) = NULL;
752      if (pIsConstant(h)) pos_of_1 = i;
753    }
754  }
755  while (f!=NULL)
756  {
757    i = 1;
758    loop
759    {
760      if (i!=pos_of_1)
761      {
762        h = mpExdiv(f, MATELEM(co,1,i),vars);
763        if (h!=NULL)
764        {
765          MATELEM(co,2,i) = pAdd(MATELEM(co,2,i), h);
766          break;
767        }
768      }
769      if (i == l)
770      {
771        // check monom 1 last:
772        if (pos_of_1 != -1)
773        {
774          h = mpExdiv(f, MATELEM(co,1,pos_of_1),vars);
775          if (h!=NULL)
776          {
777            MATELEM(co,2,pos_of_1) = pAdd(MATELEM(co,2,pos_of_1), h);
778          }
779        }
780        break;
781      }
782      i ++;
783    }
784    pIter(f);
785  }
786  return co;
787}
788
789/*2
790*exact divisor: let d  == x^i*y^j, m is thought to have only one term;
791*    return m/d iff d divides m, and no x^k*y^l (k>i or l>j) divides m
792* consider all variables in vars
793*/
794static poly mpExdiv ( poly m, poly d, poly vars)
795{
796  int i;
797  poly h = pHead(m);
798  for (i=1; i<=pVariables; i++)
799  {
800    if (pGetExp(vars,i) > 0)
801    {
802      if (pGetExp(d,i) != pGetExp(h,i))
803      {
804        pDelete(&h);
805        return NULL;
806      }
807      pSetExp(h,i,0);
808    }
809  }
810  pSetm(h);
811  return h;
812}
813
814void mpCoef2(poly v, poly mon, matrix *c, matrix *m)
815{
816  polyset s;
817  poly p;
818  int sl,i,j;
819  int l=0;
820  poly sel=mpSelect(v,mon);
821
822  pVec2Polys(sel,&s,&sl);
823  for (i=0; i<sl; i++)
824    l=si_max(l,pLength(s[i]));
825  *c=mpNew(sl,l);
826  *m=mpNew(sl,l);
827  poly h;
828  int isConst;
829  for (j=1; j<=sl;j++)
830  {
831    p=s[j-1];
832    if (pIsConstant(p)) /*p != NULL */
833    {
834      isConst=-1;
835      i=l;
836    }
837    else
838    {
839      isConst=1;
840      i=1;
841    }
842    while(p!=NULL)
843    {
844      h = pHead(p);
845      MATELEM(*m,j,i) = h;
846      i+=isConst;
847      p = p->next;
848    }
849  }
850  while (v!=NULL)
851  {
852    i = 1;
853    j = pGetComp(v);
854    loop
855    {
856      poly mp=MATELEM(*m,j,i);
857      if (mp!=NULL)
858      {
859        h = mpExdiv(v, mp /*MATELEM(*m,j,i)*/, mp);
860        if (h!=NULL)
861        {
862          pSetComp(h,0);
863          MATELEM(*c,j,i) = pAdd(MATELEM(*c,j,i), h);
864          break;
865        }
866      }
867      if (i < l)
868        i++;
869      else
870        break;
871    }
872    v = v->next;
873  }
874}
875
876
877BOOLEAN mpEqual(matrix a, matrix b)
878{
879  if ((MATCOLS(a)!=MATCOLS(b)) || (MATROWS(a)!=MATROWS(b)))
880    return FALSE;
881  int i=MATCOLS(a)*MATROWS(b)-1;
882  while (i>=0)
883  {
884    if (a->m[i]==NULL)
885    {
886      if (b->m[i]!=NULL) return FALSE;
887    }
888    else
889      if (pCmp(a->m[i],b->m[i])!=0) return FALSE;
890    i--;
891  }
892  i=MATCOLS(a)*MATROWS(b)-1;
893  while (i>=0)
894  {
895    if (pSub(pCopy(a->m[i]),pCopy(b->m[i]))!=NULL) return FALSE;
896    i--;
897  }
898  return TRUE;
899}
900
901/* --------------- internal stuff ------------------- */
902
903row_col_weight::row_col_weight(int i, int j)
904{
905  ym = i;
906  yn = j;
907  wrow = (float *)omAlloc(i*sizeof(float));
908  wcol = (float *)omAlloc(j*sizeof(float));
909}
910
911row_col_weight::~row_col_weight()
912{
913  if (ym!=0)
914  {
915    omFreeSize((ADDRESS)wcol, yn*sizeof(float));
916    omFreeSize((ADDRESS)wrow, ym*sizeof(float));
917  }
918}
919
920mp_permmatrix::mp_permmatrix(matrix A) : sign(1)
921{
922  a_m = A->nrows;
923  a_n = A->ncols;
924  this->mpInitMat();
925  Xarray = A->m;
926}
927
928mp_permmatrix::mp_permmatrix(mp_permmatrix *M)
929{
930  poly p, *athis, *aM;
931  int i, j;
932
933  a_m = M->s_m;
934  a_n = M->s_n;
935  sign = M->sign;
936  this->mpInitMat();
937  Xarray = (poly *)omAlloc0(a_m*a_n*sizeof(poly));
938  for (i=a_m-1; i>=0; i--)
939  {
940    athis = this->mpRowAdr(i);
941    aM = M->mpRowAdr(i);
942    for (j=a_n-1; j>=0; j--)
943    {
944      p = aM[M->qcol[j]];
945      if (p)
946      {
947        athis[j] = pCopy(p);
948      }
949    }
950  }
951}
952
953mp_permmatrix::~mp_permmatrix()
954{
955  int k;
956
957  if (a_m != 0)
958  {
959    omFreeSize((ADDRESS)qrow,a_m*sizeof(int));
960    omFreeSize((ADDRESS)qcol,a_n*sizeof(int));
961    if (Xarray != NULL)
962    {
963      for (k=a_m*a_n-1; k>=0; k--)
964        pDelete(&Xarray[k]);
965      omFreeSize((ADDRESS)Xarray,a_m*a_n*sizeof(poly));
966    }
967  }
968}
969
970int mp_permmatrix::mpGetRdim() { return s_m; }
971
972int mp_permmatrix::mpGetCdim() { return s_n; }
973
974int mp_permmatrix::mpGetSign() { return sign; }
975
976void mp_permmatrix::mpSetSearch(int s) { piv_s = s; }
977
978void mp_permmatrix::mpSaveArray() { Xarray = NULL; }
979
980poly mp_permmatrix::mpGetElem(int r, int c)
981{
982  return Xarray[a_n*qrow[r]+qcol[c]];
983}
984
985void mp_permmatrix::mpSetElem(poly p, int r, int c)
986{
987  Xarray[a_n*qrow[r]+qcol[c]] = p;
988}
989
990void mp_permmatrix::mpDelElem(int r, int c)
991{
992  pDelete(&Xarray[a_n*qrow[r]+qcol[c]]);
993}
994
995/*
996* the Bareiss-type elimination with division by div (div != NULL)
997*/
998void mp_permmatrix::mpElimBareiss(poly div)
999{
1000  poly piv, elim, q1, q2, *ap, *a;
1001  int i, j, jj;
1002
1003  ap = this->mpRowAdr(s_m);
1004  piv = ap[qcol[s_n]];
1005  for(i=s_m-1; i>=0; i--)
1006  {
1007    a = this->mpRowAdr(i);
1008    elim = a[qcol[s_n]];
1009    if (elim != NULL)
1010    {
1011      elim = pNeg(elim);
1012      for (j=s_n-1; j>=0; j--)
1013      {
1014        q2 = NULL;
1015        jj = qcol[j];
1016        if (ap[jj] != NULL)
1017        {
1018          q2 = SM_MULT(ap[jj], elim, div);
1019          if (a[jj] != NULL)
1020          {
1021            q1 = SM_MULT(a[jj], piv, div);
1022            pDelete(&a[jj]);
1023            q2 = pAdd(q2, q1);
1024          }
1025        }
1026        else if (a[jj] != NULL)
1027        {
1028          q2 = SM_MULT(a[jj], piv, div);
1029        }
1030        if ((q2!=NULL) && div)
1031          SM_DIV(q2, div);
1032        a[jj] = q2;
1033      }
1034      pDelete(&a[qcol[s_n]]);
1035    }
1036    else
1037    {
1038      for (j=s_n-1; j>=0; j--)
1039      {
1040        jj = qcol[j];
1041        if (a[jj] != NULL)
1042        {
1043          q2 = SM_MULT(a[jj], piv, div);
1044          pDelete(&a[jj]);
1045          if (div)
1046            SM_DIV(q2, div);
1047          a[jj] = q2;
1048        }
1049      }
1050    }
1051  }
1052}
1053
1054/*2
1055* pivot strategy for Bareiss algorithm
1056*/
1057int mp_permmatrix::mpPivotBareiss(row_col_weight *C)
1058{
1059  poly p, *a;
1060  int i, j, iopt, jopt;
1061  float sum, f1, f2, fo, r, ro, lp;
1062  float *dr = C->wrow, *dc = C->wcol;
1063
1064  fo = 1.0e20;
1065  ro = 0.0;
1066  iopt = jopt = -1;
1067
1068  s_n--;
1069  s_m--;
1070  if (s_m == 0)
1071    return 0;
1072  if (s_n == 0)
1073  {
1074    for(i=s_m; i>=0; i--)
1075    {
1076      p = this->mpRowAdr(i)[qcol[0]];
1077      if (p)
1078      {
1079        f1 = mpPolyWeight(p);
1080        if (f1 < fo)
1081        {
1082          fo = f1;
1083          if (iopt >= 0)
1084            pDelete(&(this->mpRowAdr(iopt)[qcol[0]]));
1085          iopt = i;
1086        }
1087        else
1088          pDelete(&(this->mpRowAdr(i)[qcol[0]]));
1089      }
1090    }
1091    if (iopt >= 0)
1092      mpReplace(iopt, s_m, sign, qrow);
1093    return 0;
1094  }
1095  this->mpRowWeight(dr);
1096  this->mpColWeight(dc);
1097  sum = 0.0;
1098  for(i=s_m; i>=0; i--)
1099    sum += dr[i];
1100  for(i=s_m; i>=0; i--)
1101  {
1102    r = dr[i];
1103    a = this->mpRowAdr(i);
1104    for(j=s_n; j>=0; j--)
1105    {
1106      p = a[qcol[j]];
1107      if (p)
1108      {
1109        lp = mpPolyWeight(p);
1110        ro = r - lp;
1111        f1 = ro * (dc[j]-lp);
1112        if (f1 != 0.0)
1113        {
1114          f2 = lp * (sum - ro - dc[j]);
1115          f2 += f1;
1116        }
1117        else
1118          f2 = lp-r-dc[j];
1119        if (f2 < fo)
1120        {
1121          fo = f2;
1122          iopt = i;
1123          jopt = j;
1124        }
1125      }
1126    }
1127  }
1128  if (iopt < 0)
1129    return 0;
1130  mpReplace(iopt, s_m, sign, qrow);
1131  mpReplace(jopt, s_n, sign, qcol);
1132  return 1;
1133}
1134
1135/*2
1136* pivot strategy for Bareiss algorithm with defined row
1137*/
1138int mp_permmatrix::mpPivotRow(row_col_weight *C, int row)
1139{
1140  poly p, *a;
1141  int j, iopt, jopt;
1142  float sum, f1, f2, fo, r, ro, lp;
1143  float *dr = C->wrow, *dc = C->wcol;
1144
1145  fo = 1.0e20;
1146  ro = 0.0;
1147  iopt = jopt = -1;
1148
1149  s_n--;
1150  s_m--;
1151  if (s_m == 0)
1152    return 0;
1153  if (s_n == 0)
1154  {
1155    p = this->mpRowAdr(row)[qcol[0]];
1156    if (p)
1157    {
1158      f1 = mpPolyWeight(p);
1159      if (f1 < fo)
1160      {
1161        fo = f1;
1162        if (iopt >= 0)
1163        pDelete(&(this->mpRowAdr(iopt)[qcol[0]]));
1164        iopt = row;
1165      }
1166      else
1167        pDelete(&(this->mpRowAdr(row)[qcol[0]]));
1168    }
1169    if (iopt >= 0)
1170      mpReplace(iopt, s_m, sign, qrow);
1171    return 0;
1172  }
1173  this->mpRowWeight(dr);
1174  this->mpColWeight(dc);
1175  sum = 0.0;
1176  for(j=s_m; j>=0; j--)
1177    sum += dr[j];
1178  r = dr[row];
1179  a = this->mpRowAdr(row);
1180  for(j=s_n; j>=0; j--)
1181  {
1182    p = a[qcol[j]];
1183    if (p)
1184    {
1185      lp = mpPolyWeight(p);
1186      ro = r - lp;
1187      f1 = ro * (dc[j]-lp);
1188      if (f1 != 0.0)
1189      {
1190        f2 = lp * (sum - ro - dc[j]);
1191        f2 += f1;
1192      }
1193      else
1194        f2 = lp-r-dc[j];
1195      if (f2 < fo)
1196      {
1197        fo = f2;
1198        iopt = row;
1199        jopt = j;
1200      }
1201    }
1202  }
1203  if (iopt < 0)
1204    return 0;
1205  mpReplace(iopt, s_m, sign, qrow);
1206  mpReplace(jopt, s_n, sign, qcol);
1207  return 1;
1208}
1209
1210void mp_permmatrix::mpToIntvec(intvec *v)
1211{
1212  int i;
1213
1214  for (i=v->rows()-1; i>=0; i--)
1215    (*v)[i] = qcol[i]+1;
1216}
1217
1218void mp_permmatrix::mpRowReorder()
1219{
1220  int k, i, i1, i2;
1221
1222  if (a_m > a_n)
1223    k = a_m - a_n;
1224  else
1225    k = 0;
1226  for (i=a_m-1; i>=k; i--)
1227  {
1228    i1 = qrow[i];
1229    if (i1 != i)
1230    {
1231      this->mpRowSwap(i1, i);
1232      i2 = 0;
1233      while (qrow[i2] != i) i2++;
1234      qrow[i2] = i1;
1235    }
1236  }
1237}
1238
1239void mp_permmatrix::mpColReorder()
1240{
1241  int k, j, j1, j2;
1242
1243  if (a_n > a_m)
1244    k = a_n - a_m;
1245  else
1246    k = 0;
1247  for (j=a_n-1; j>=k; j--)
1248  {
1249    j1 = qcol[j];
1250    if (j1 != j)
1251    {
1252      this->mpColSwap(j1, j);
1253      j2 = 0;
1254      while (qcol[j2] != j) j2++;
1255      qcol[j2] = j1;
1256    }
1257  }
1258}
1259
1260// private
1261void mp_permmatrix::mpInitMat()
1262{
1263  int k;
1264
1265  s_m = a_m;
1266  s_n = a_n;
1267  piv_s = 0;
1268  qrow = (int *)omAlloc(a_m*sizeof(int));
1269  qcol = (int *)omAlloc(a_n*sizeof(int));
1270  for (k=a_m-1; k>=0; k--) qrow[k] = k;
1271  for (k=a_n-1; k>=0; k--) qcol[k] = k;
1272}
1273
1274poly * mp_permmatrix::mpRowAdr(int r)
1275{
1276  return &(Xarray[a_n*qrow[r]]);
1277}
1278
1279poly * mp_permmatrix::mpColAdr(int c)
1280{
1281  return &(Xarray[qcol[c]]);
1282}
1283
1284void mp_permmatrix::mpRowWeight(float *wrow)
1285{
1286  poly p, *a;
1287  int i, j;
1288  float count;
1289
1290  for (i=s_m; i>=0; i--)
1291  {
1292    a = this->mpRowAdr(i);
1293    count = 0.0;
1294    for(j=s_n; j>=0; j--)
1295    {
1296      p = a[qcol[j]];
1297      if (p)
1298        count += mpPolyWeight(p);
1299    }
1300    wrow[i] = count;
1301  }
1302}
1303
1304void mp_permmatrix::mpColWeight(float *wcol)
1305{
1306  poly p, *a;
1307  int i, j;
1308  float count;
1309
1310  for (j=s_n; j>=0; j--)
1311  {
1312    a = this->mpColAdr(j);
1313    count = 0.0;
1314    for(i=s_m; i>=0; i--)
1315    {
1316      p = a[a_n*qrow[i]];
1317      if (p)
1318        count += mpPolyWeight(p);
1319    }
1320    wcol[j] = count;
1321  }
1322}
1323
1324void mp_permmatrix::mpRowSwap(int i1, int i2)
1325{
1326   poly p, *a1, *a2;
1327   int j;
1328
1329   a1 = &(Xarray[a_n*i1]);
1330   a2 = &(Xarray[a_n*i2]);
1331   for (j=a_n-1; j>= 0; j--)
1332   {
1333     p = a1[j];
1334     a1[j] = a2[j];
1335     a2[j] = p;
1336   }
1337}
1338
1339void mp_permmatrix::mpColSwap(int j1, int j2)
1340{
1341   poly p, *a1, *a2;
1342   int i, k = a_n*a_m;
1343
1344   a1 = &(Xarray[j1]);
1345   a2 = &(Xarray[j2]);
1346   for (i=0; i< k; i+=a_n)
1347   {
1348     p = a1[i];
1349     a1[i] = a2[i];
1350     a2[i] = p;
1351   }
1352}
1353
1354int mp_permmatrix::mpGetRow()
1355{
1356  return qrow[s_m];
1357}
1358
1359int mp_permmatrix::mpGetCol()
1360{
1361  return qcol[s_n];
1362}
1363
1364/*
1365* perform replacement for pivot strategy in Bareiss algorithm
1366* change sign of determinant
1367*/
1368static void mpReplace(int j, int n, int &sign, int *perm)
1369{
1370  int k;
1371
1372  if (j != n)
1373  {
1374    k = perm[n];
1375    perm[n] = perm[j];
1376    perm[j] = k;
1377    sign = -sign;
1378  }
1379}
1380
1381static int mpNextperm(perm * z, int max)
1382{
1383  int s, i, k, t;
1384  s = max;
1385  do
1386  {
1387    s--;
1388  }
1389  while ((s > 0) && ((*z)[s] >= (*z)[s+1]));
1390  if (s==0)
1391    return 0;
1392  do
1393  {
1394    (*z)[s]++;
1395    k = 0;
1396    do
1397    {
1398      k++;
1399    }
1400    while (((*z)[k] != (*z)[s]) && (k!=s));
1401  }
1402  while (k < s);
1403  for (i=s+1; i <= max; i++)
1404  {
1405    (*z)[i]=0;
1406    do
1407    {
1408      (*z)[i]++;
1409      k=0;
1410      do
1411      {
1412        k++;
1413      }
1414      while (((*z)[k] != (*z)[i]) && (k != i));
1415    }
1416    while (k < i);
1417  }
1418  s = max+1;
1419  do
1420  {
1421    s--;
1422  }
1423  while ((s > 0) && ((*z)[s] > (*z)[s+1]));
1424  t = 1;
1425  for (i=1; i<max; i++)
1426    for (k=i+1; k<=max; k++)
1427      if ((*z)[k] < (*z)[i])
1428        t = -t;
1429  (*z)[0] = t;
1430  return s;
1431}
1432
1433static poly mpLeibnitz(matrix a)
1434{
1435  int i, e, n;
1436  poly p, d;
1437  perm z;
1438
1439  n = MATROWS(a);
1440  memset(&z,0,(n+2)*sizeof(int));
1441  p = pOne();
1442  for (i=1; i <= n; i++)
1443    p = pMult(p, pCopy(MATELEM(a, i, i)));
1444  d = p;
1445  for (i=1; i<= n; i++)
1446    z[i] = i;
1447  z[0]=1;
1448  e = 1;
1449  if (n!=1)
1450  {
1451    while (e)
1452    {
1453      e = mpNextperm((perm *)&z, n);
1454      p = pOne();
1455      for (i = 1; i <= n; i++)
1456        p = pMult(p, pCopy(MATELEM(a, i, z[i])));
1457      if (z[0] > 0)
1458        d = pAdd(d, p);
1459      else
1460        d = pSub(d, p);
1461    }
1462  }
1463  return d;
1464}
1465
1466static poly minuscopy (poly p)
1467{
1468  poly w;
1469  number  e;
1470  e = nInit(-1);
1471  w = pCopy(p);
1472  pMult_nn(w, e);
1473  nDelete(&e);
1474  return w;
1475}
1476
1477/*2
1478* insert a monomial into a list, avoid duplicates
1479* arguments are destroyed
1480*/
1481static poly pInsert(poly p1, poly p2)
1482{
1483  poly a1, p, a2, a;
1484  int c;
1485
1486  if (p1==NULL) return p2;
1487  if (p2==NULL) return p1;
1488  a1 = p1;
1489  a2 = p2;
1490  a = p  = pOne();
1491  loop
1492  {
1493    c = pCmp(a1, a2);
1494    if (c == 1)
1495    {
1496      a = pNext(a) = a1;
1497      pIter(a1);
1498      if (a1==NULL)
1499      {
1500        pNext(a) = a2;
1501        break;
1502      }
1503    }
1504    else if (c == -1)
1505    {
1506      a = pNext(a) = a2;
1507      pIter(a2);
1508      if (a2==NULL)
1509      {
1510        pNext(a) = a1;
1511        break;
1512      }
1513    }
1514    else
1515    {
1516      pDeleteLm(&a2);
1517      a = pNext(a) = a1;
1518      pIter(a1);
1519      if (a1==NULL)
1520      {
1521        pNext(a) = a2;
1522        break;
1523      }
1524      else if (a2==NULL)
1525      {
1526        pNext(a) = a1;
1527        break;
1528      }
1529    }
1530  }
1531  pDeleteLm(&p);
1532  return p;
1533}
1534
1535/*2
1536*if what == xy the result is the list of all different power products
1537*    x^i*y^j (i, j >= 0) that appear in fro
1538*/
1539static poly mpSelect (poly fro, poly what)
1540{
1541  int i;
1542  poly h, res;
1543  res = NULL;
1544  while (fro!=NULL)
1545  {
1546    h = pOne();
1547    for (i=1; i<=pVariables; i++)
1548      pSetExp(h,i, pGetExp(fro,i) * pGetExp(what, i));
1549    pSetComp(h, pGetComp(fro));
1550    pSetm(h);
1551    res = pInsert(h, res);
1552    fro = fro->next;
1553  }
1554  return res;
1555}
1556
1557/*
1558*static void ppp(matrix a)
1559*{
1560*  int j,i,r=a->nrows,c=a->ncols;
1561*  for(j=1;j<=r;j++)
1562*  {
1563*    for(i=1;i<=c;i++)
1564*    {
1565*      if(MATELEM(a,j,i)!=NULL) Print("X");
1566*      else Print("0");
1567*    }
1568*    Print("\n");
1569*  }
1570*}
1571*/
1572
1573static void mpPartClean(matrix a, int lr, int lc)
1574{
1575  poly *q1;
1576  int i,j;
1577
1578  for (i=lr-1;i>=0;i--)
1579  {
1580    q1 = &(a->m)[i*a->ncols];
1581    for (j=lc-1;j>=0;j--) if(q1[j]) pDelete(&q1[j]);
1582  }
1583}
1584
1585static void mpFinalClean(matrix a)
1586{
1587  omFreeSize((ADDRESS)a->m,a->nrows*a->ncols*sizeof(poly));
1588  omFreeBin((ADDRESS)a, ip_smatrix_bin);
1589}
1590
1591/*2
1592*  prepare one step of 'Bareiss' algorithm
1593*  for application in minor
1594*/
1595static int mpPrepareRow (matrix a, int lr, int lc)
1596{
1597  int r;
1598
1599  r = mpPivBar(a,lr,lc);
1600  if(r==0) return 0;
1601  if(r<lr) mpSwapRow(a, r, lr, lc);
1602  return 1;
1603}
1604
1605/*2
1606*  prepare one step of 'Bareiss' algorithm
1607*  for application in minor
1608*/
1609static int mpPreparePiv (matrix a, int lr, int lc)
1610{
1611  int c;
1612
1613  c = mpPivRow(a, lr, lc);
1614  if(c==0) return 0;
1615  if(c<lc) mpSwapCol(a, c, lr, lc);
1616  return 1;
1617}
1618
1619/*
1620* find best row
1621*/
1622static int mpPivBar(matrix a, int lr, int lc)
1623{
1624  float f1, f2;
1625  poly *q1;
1626  int i,j,io;
1627
1628  io = -1;
1629  f1 = 1.0e30;
1630  for (i=lr-1;i>=0;i--)
1631  {
1632    q1 = &(a->m)[i*a->ncols];
1633    f2 = 0.0;
1634    for (j=lc-1;j>=0;j--)
1635    {
1636      if (q1[j]!=NULL)
1637        f2 += mpPolyWeight(q1[j]);
1638    }
1639    if ((f2!=0.0) && (f2<f1))
1640    {
1641      f1 = f2;
1642      io = i;
1643    }
1644  }
1645  if (io<0) return 0;
1646  else return io+1;
1647}
1648
1649/*
1650* find pivot in the last row
1651*/
1652static int mpPivRow(matrix a, int lr, int lc)
1653{
1654  float f1, f2;
1655  poly *q1;
1656  int j,jo;
1657
1658  jo = -1;
1659  f1 = 1.0e30;
1660  q1 = &(a->m)[(lr-1)*a->ncols];
1661  for (j=lc-1;j>=0;j--)
1662  {
1663    if (q1[j]!=NULL)
1664    {
1665      f2 = mpPolyWeight(q1[j]);
1666      if (f2<f1)
1667      {
1668        f1 = f2;
1669        jo = j;
1670      }
1671    }
1672  }
1673  if (jo<0) return 0;
1674  else return jo+1;
1675}
1676
1677/*
1678* weigth of a polynomial, for pivot strategy
1679*/
1680static float mpPolyWeight(poly p)
1681{
1682  int i;
1683  float res;
1684
1685  if (pNext(p) == NULL)
1686  {
1687    res = (float)nSize(pGetCoeff(p));
1688    for (i=pVariables;i>0;i--)
1689    {
1690      if(pGetExp(p,i)!=0)
1691      {
1692        res += 2.0;
1693        break;
1694      }
1695    }
1696  }
1697  else
1698  {
1699    res = 0.0;
1700    do
1701    {
1702      res += (float)nSize(pGetCoeff(p))+2.0;
1703      pIter(p);
1704    }
1705    while (p);
1706  }
1707  return res;
1708}
1709
1710static void mpSwapRow(matrix a, int pos, int lr, int lc)
1711{
1712  poly sw;
1713  int j;
1714  polyset a2 = a->m, a1 = &a2[a->ncols*(pos-1)];
1715
1716  a2 = &a2[a->ncols*(lr-1)];
1717  for (j=lc-1; j>=0; j--)
1718  {
1719    sw = a1[j];
1720    a1[j] = a2[j];
1721    a2[j] = sw;
1722  }
1723}
1724
1725static void mpSwapCol(matrix a, int pos, int lr, int lc)
1726{
1727  poly sw;
1728  int j;
1729  polyset a2 = a->m, a1 = &a2[pos-1];
1730
1731  a2 = &a2[lc-1];
1732  for (j=a->ncols*(lr-1); j>=0; j-=a->ncols)
1733  {
1734    sw = a1[j];
1735    a1[j] = a2[j];
1736    a2[j] = sw;
1737  }
1738}
1739
1740static void mpElimBar(matrix a0, matrix re, poly div, int lr, int lc)
1741{
1742  int r=lr-1, c=lc-1;
1743  poly *b = a0->m, *x = re->m;
1744  poly piv, elim, q1, q2, *ap, *a, *q;
1745  int i, j;
1746
1747  ap = &b[r*a0->ncols];
1748  piv = ap[c];
1749  for(j=c-1; j>=0; j--)
1750    if (ap[j] != NULL) ap[j] = pNeg(ap[j]);
1751  for(i=r-1; i>=0; i--)
1752  {
1753    a = &b[i*a0->ncols];
1754    q = &x[i*re->ncols];
1755    if (a[c] != NULL)
1756    {
1757      elim = a[c];
1758      for (j=c-1; j>=0; j--)
1759      {
1760        q1 = NULL;
1761        if (a[j] != NULL)
1762        {
1763          q1 = SM_MULT(a[j], piv, div);
1764          if (ap[j] != NULL)
1765          {
1766            q2 = SM_MULT(ap[j], elim, div);
1767            q1 = pAdd(q1,q2);
1768          }
1769        }
1770        else if (ap[j] != NULL)
1771          q1 = SM_MULT(ap[j], elim, div);
1772        if (q1 != NULL)
1773        {
1774          if (div)
1775            SM_DIV(q1, div);
1776          q[j] = q1;
1777        }
1778      }
1779    }
1780    else
1781    {
1782      for (j=c-1; j>=0; j--)
1783      {
1784        if (a[j] != NULL)
1785        {
1786          q1 = SM_MULT(a[j], piv, div);
1787          if (div)
1788            SM_DIV(q1, div);
1789          q[j] = q1;
1790        }
1791      }
1792    }
1793  }
1794}
1795
1796BOOLEAN mpIsDiagUnit(matrix U)
1797{
1798  if(MATROWS(U)!=MATCOLS(U))
1799    return FALSE;
1800  for(int i=MATCOLS(U);i>=1;i--)
1801  {
1802    for(int j=MATCOLS(U); j>=1; j--)
1803    {
1804      if (i==j)
1805      {
1806        if (!pIsUnit(MATELEM(U,i,i))) return FALSE;
1807      }
1808      else if (MATELEM(U,i,j)!=NULL) return FALSE;
1809    } 
1810  }
1811  return TRUE;
1812}
1813
1814void iiWriteMatrix(matrix im, const char *n, int dim,int spaces)
1815{
1816  int i,ii = MATROWS(im)-1;
1817  int j,jj = MATCOLS(im)-1;
1818  poly *pp = im->m;
1819
1820  for (i=0; i<=ii; i++)
1821  {
1822    for (j=0; j<=jj; j++)
1823    {
1824      if (spaces>0)
1825        Print("%-*.*s",spaces,spaces," ");
1826      if (dim == 2) Print("%s[%u,%u]=",n,i+1,j+1);
1827      else if (dim == 1) Print("%s[%u]=",n,j+1);
1828      else if (dim == 0) Print("%s=",n);
1829      if ((i<ii)||(j<jj)) pWrite(*pp++);
1830      else                pWrite0(*pp);
1831    }
1832  }
1833}
1834
1835char * iiStringMatrix(matrix im, int dim,char ch)
1836{
1837  int i,ii = MATROWS(im);
1838  int j,jj = MATCOLS(im);
1839  poly *pp = im->m;
1840  char *s=StringSetS("");
1841
1842  for (i=0; i<ii; i++)
1843  {
1844    for (j=0; j<jj; j++)
1845    {
1846      pString0(*pp++);
1847      s=StringAppend("%c",ch);
1848      if (dim > 1) s = StringAppendS("\n");
1849    }
1850  }
1851  s[strlen(s)- (dim > 1 ? 2 : 1)]='\0';
1852  return s;
1853}
1854
Note: See TracBrowser for help on using the repository browser.