source: git/kernel/matpol.cc @ 9c6789

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