source: git/kernel/matpol.cc @ 2971ea

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