Changeset fe02b1 in git


Ignore:
Timestamp:
Apr 25, 2012, 8:38:08 PM (12 years ago)
Author:
Oleksandr Motsak <motsak@…>
Branches:
(u'spielwiese', 'fe61d9c35bf7c61f2b6cbf1b56e25e2f08d536cc')
Children:
0fd1715860b463009cfd80025e81bd671f0a0a2d
Parents:
84fc1fddf725ce226a336195603ebcadbb650d4c
Message:
my rewrite of bigintmat as a matrix of numbers from any coeff. domain

fix: several bugs and mem. leaks
add: improved interface (mostly backward compatible)
chg: many minor improvements
add: started documenting with doxygen comments
add: lots of assumes

NOTE: NULL should NOT be used as a coeff. domain (use coeffs_BIGINT for Q!)
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • Singular/grammar.cc

    r84fc1f rfe02b1  
    31773177              h=(idhdl)v->data;
    31783178              delete IDBIMAT(h);
    3179               IDBIMAT(h) = new bigintmat(r,c);
     3179              IDBIMAT(h) = new bigintmat(r, c, coeffs_BIGINT);
    31803180              if (IDBIMAT(h)==NULL) YYERROR;
    31813181            }
  • Singular/grammar.y

    r84fc1f rfe02b1  
    935935              h=(idhdl)v->data;
    936936              delete IDBIMAT(h);
    937               IDBIMAT(h) = new bigintmat(r,c);
     937              IDBIMAT(h) = new bigintmat(r, c, coeffs_BIGINT);
    938938              if (IDBIMAT(h)==NULL) YYERROR;
    939939            }
  • libpolys/coeffs/bigintmat.cc

    r84fc1f rfe02b1  
    11/*****************************************
    2 *  Computer Algebra System SINGULAR      *
    3 *****************************************/
     2 *  Computer Algebra System SINGULAR      *
     3 *****************************************/
    44/*
    5 * ABSTRACT: class bigintmat: matrizes of big integers
    6 */
    7 #include <kernel/mod2.h>
     5 * ABSTRACT: class bigintmat: matrizes of big integers
     6 */
     7#include "bigintmat.h"
     8
     9#include <misc/intvec.h>
     10
     11//#include <kernel/mod2.h>
    812//#include <kernel/options.h>
    9 #include "bigintmat.h"
    10 #include <omalloc/omalloc.h>
    11 #include <coeffs/longrat.h>
    12 #include <misc/intvec.h>
     13
    1314#include <math.h>
    1415#include <string.h>
    1516
    16 #define BIGIMATELEM(M,I,J) (M)[(I-1)*(M).cols()+J-1]
     17#define BIMATELEM(M,I,J) (M)[ (M).index(I,J) ]
     18
     19
     20// Beginnt bei [0]
     21void bigintmat::set(int i, number n, const coeffs C)
     22{
     23  assume (C == NULL || C == basecoeffs());
     24
     25  rawset(i, n_Copy(n, basecoeffs()), basecoeffs());
     26}
    1727
    1828// Beginnt bei [1,1]
    19 void bigintmat::set(int i, int j, number n)
    20 {
    21   nlDelete(&(v[(i-1)*col+j-1]), NULL);
    22   v[(i-1)*col+j-1] = nlCopy(n,NULL);
    23 }
    24 
    25 // Beginnt bei [0]
    26 void bigintmat::set(int i, number n)
    27 {
    28   if (i<row*col)
    29   {
    30     nlDelete(&(v[i]), NULL);
    31     v[i] = nlCopy(n,NULL);
    32   }
    33 }
    34 
    35 number bigintmat::get(int i, int j)
    36 {
    37   return nlCopy(v[(i-1)*col+j-1],NULL);
    38 }
    39 
    40 number bigintmat::get(int i)
    41 {
    42   return nlCopy(v[i],NULL);
    43 }
    44 
    45 bigintmat::bigintmat(int r, int c)
    46 {
    47   row = r;
    48   col = c;
    49   int l = r*c;
    50   if (l>0) /*(r>0) && (c>0) */
    51     v = (number *)omAlloc(sizeof(number)*l);
    52   else
    53     v = NULL;
    54   for (int i=0; i<l; i++)
    55   {
    56     v[i] = nlInit(0, NULL);
    57   }
     29void bigintmat::set(int i, int j, number n, const coeffs C)
     30{
     31  assume (C == NULL || C == basecoeffs());
     32  assume (i > 0 && j > 0);
     33  assume (i <= rows() && j <= cols());
     34 
     35  set(index(i, j), n, C);
     36}
     37
     38number bigintmat::get(int i) const
     39{
     40  assume (i >= 0);
     41  assume (i<rows()*cols());
     42 
     43  return n_Copy(v[i], basecoeffs());
     44}
     45
     46number bigintmat::get(int i, int j) const
     47{
     48  assume (i > 0 && j > 0);
     49  assume (i <= rows() && j <= cols());
     50 
     51  return get(index(i, j));
    5852}
    5953
     
    6256void bigintmat::operator*=(int intop)
    6357{
    64   for (int i=0; i<row*col; i++)
    65   {
    66           number iop = nlInit(intop, NULL);
    67           number prod = nlMult(v[i], iop,NULL);
    68           nlDelete(&(v[i]), NULL);
    69           nlDelete(&iop, NULL);
    70           v[i] = prod;
    71   }
    72 }
    73 
    74 void bigintmat::operator*=(number bintop)
    75 {
    76   for (int i=0; i<row*col; i++)
    77   {
    78           number prod = nlMult(v[i], bintop,NULL);
    79           nlDelete(&(v[i]), NULL);
    80           v[i] = prod;
    81   }
     58  number iop = n_Init(intop, basecoeffs());
     59 
     60  inpMult(iop, basecoeffs());
     61 
     62  n_Delete(&iop, basecoeffs());
     63}
     64
     65void bigintmat::inpMult(number bintop, const coeffs C)
     66{
     67  assume (C == NULL || C == basecoeffs());
     68 
     69  const int l = rows() * cols();
     70
     71  for (int i=0; i < l; i++)
     72    n_InpMult(v[i], bintop, basecoeffs());
    8273}
    8374
     
    8677// Oder lieber eine comp-Funktion?
    8778
    88 bool operator==(bigintmat & lhr, bigintmat & rhr)
     79bool operator==(const bigintmat & lhr, const bigintmat & rhr)
    8980{
    9081  if (&lhr == &rhr) { return true; }
    9182  if (lhr.cols() != rhr.cols()) { return false; }
    9283  if (lhr.rows() != rhr.rows()) { return false; }
    93   for (int i=0; i<(lhr.rows())*(lhr.cols()); i++)
    94   {
    95     if (!nlEqual(lhr[i], rhr[i],NULL)) { return false; }
    96   }
     84  if (lhr.basecoeffs() != rhr.basecoeffs()) { return false; }
     85
     86  const int l = (lhr.rows())*(lhr.cols());
     87 
     88  for (int i=0; i < l; i++)
     89  {
     90    if (!n_Equal(lhr[i], rhr[i], lhr.basecoeffs())) { return false; }
     91  }
     92 
    9793  return true;
    9894}
    9995
    100 bool operator!=(bigintmat & lhr, bigintmat & rhr)
     96bool operator!=(const bigintmat & lhr, const bigintmat & rhr)
    10197{
    10298  return !(lhr==rhr);
     
    106102bigintmat * bimAdd(bigintmat * a, bigintmat * b)
    107103{
    108   bigintmat * bim;
    109   int mn, ma, i;
    110104  if (a->cols() != b->cols()) return NULL;
    111   mn = si_min(a->rows(),b->rows());
    112   ma = si_max(a->rows(),b->rows());
     105  if (a->basecoeffs() != b->basecoeffs()) { return NULL; }
     106 
     107  const int mn = si_min(a->rows(),b->rows());
     108  const int ma = si_max(a->rows(),b->rows());
     109
     110  const coeffs basecoeffs = a->basecoeffs();
     111 
     112  int i;
     113
    113114  if (a->cols() == 1)
    114115  {
    115     bim = new bigintmat(ma, 1);
     116    bigintmat * bim = new bigintmat(ma, 1, basecoeffs);
     117   
    116118    for (i=0; i<mn; i++)
    117     {
    118       number n = nlAdd((*a)[i], (*b)[i],NULL);
    119       bim->set(i, n);
    120       nlDelete(&n, NULL);
    121     }
     119      bim->rawset(i, n_Add((*a)[i], (*b)[i], basecoeffs), basecoeffs);
     120   
    122121    if (ma > mn)
    123122    {
    124123      if (ma == a->rows())
    125       {
     124        for(i=mn; i<ma; i++)
     125          bim->set(i, (*a)[i], basecoeffs);
     126      else
     127        for(i=mn; i<ma; i++)
     128          bim->set(i, (*b)[i], basecoeffs);
     129    }
     130    return bim;
     131  }
     132 
     133  if (mn != ma) return NULL;
     134
     135  bigintmat * bim = new bigintmat(mn, a->cols(), basecoeffs);
     136 
     137  for (i=0; i<mn*a->cols(); i++)
     138    bim->rawset(i, n_Add((*a)[i], (*b)[i], basecoeffs), basecoeffs);
     139 
     140  return bim;
     141}
     142
     143bigintmat * bimSub(bigintmat * a, bigintmat * b)
     144{
     145  if (a->cols() != b->cols()) return NULL;
     146  if (a->basecoeffs() != b->basecoeffs()) { return NULL; }
     147
     148  const int mn = si_min(a->rows(),b->rows());
     149  const int ma = si_max(a->rows(),b->rows());
     150
     151  const coeffs basecoeffs = a->basecoeffs();
     152
     153  int i;
     154
     155  if (a->cols() == 1)
     156  {
     157    bigintmat * bim = new bigintmat(ma, 1, basecoeffs);
     158   
     159    for (i=0; i<mn; i++)
     160      bim->rawset(i, n_Sub((*a)[i], (*b)[i], basecoeffs), basecoeffs);
     161
     162    if (ma > mn)
     163    {
     164      if (ma == a->rows())
     165      {
     166        for(i=mn; i<ma; i++)
     167          bim->set(i, (*a)[i], basecoeffs);
     168      }
     169      else
    126170        for(i=mn; i<ma; i++)
    127171        {
    128           bim->set(i, (*a)[i]);
     172          number n = b->get(i);
     173          n = n_Neg(n, basecoeffs);
     174          bim->rawset(i, n, basecoeffs);
    129175        }
    130       }
    131       else
    132       {
    133         for(i=mn; i<ma; i++)
    134         {
    135           bim->set(i, (*b)[i]);
    136         }
    137       }
    138176    }
    139177    return bim;
    140178  }
     179 
    141180  if (mn != ma) return NULL;
    142   bim = new bigintmat(mn, a->cols());
     181 
     182  bigintmat * bim = new bigintmat(mn, a->cols(), basecoeffs);
     183 
    143184  for (i=0; i<mn*a->cols(); i++)
    144   {
    145     number n = nlAdd((*a)[i], (*b)[i],NULL);
    146     bim->set(i, n);
    147     nlDelete(&n, NULL);
    148   }
     185    bim->rawset(i, n_Sub((*a)[i], (*b)[i], basecoeffs), basecoeffs);
     186 
    149187  return bim;
    150188}
    151189
    152 bigintmat * bimSub(bigintmat * a, bigintmat * b)
    153 {
    154   bigintmat * bim;
    155   int mn, ma, i;
    156   if (a->cols() != b->cols()) return NULL;
    157   mn = si_min(a->rows(),b->rows());
    158   ma = si_max(a->rows(),b->rows());
    159   if (a->cols() == 1)
    160   {
    161     bim = new bigintmat(ma, 1);
    162     for (i=0; i<mn; i++)
    163     {
    164       number n = nlSub((*a)[i], (*b)[i],NULL);
    165       bim->set(i, n);
    166       nlDelete(&n, NULL);
    167     }
    168     if (ma > mn)
    169     {
    170       if (ma == a->rows())
    171       {
    172         for(i=mn; i<ma; i++)
    173         {
    174           bim->set(i, (*a)[i]);
    175         }
    176       }
    177       else
    178       {
    179         for(i=mn; i<ma; i++)
    180         {
    181           bim->set(i, (*b)[i]);
    182         }
    183       }
    184     }
    185     return bim;
    186   }
    187   if (mn != ma) return NULL;
    188   bim = new bigintmat(mn, a->cols());
    189   for (i=0; i<mn*a->cols(); i++)
    190   {
    191     number n = nlSub((*a)[i], (*b)[i],NULL);
    192     bim->set(i, n);
    193     nlDelete(&n, NULL);
    194   }
     190bigintmat * bimMult(bigintmat * a, bigintmat * b)
     191{
     192  const int ca = a->cols();
     193  const int cb = b->cols();
     194
     195  const int ra = a->rows();
     196  const int rb = b->rows();
     197 
     198  if (ca != rb)
     199  {
     200#ifndef NDEBUG
     201    Werror("wrong bigintmat sizes at multiplication a * b: acols: %d != brows: %d\n", ca, rb);
     202#endif
     203    return NULL;
     204  }
     205 
     206  assume (ca == rb);
     207 
     208  if (a->basecoeffs() != b->basecoeffs()) { return NULL; }
     209 
     210  const coeffs basecoeffs = a->basecoeffs();
     211
     212  int i, j, k;
     213 
     214  number sum;
     215 
     216  bigintmat * bim = new bigintmat(ra, cb, basecoeffs);
     217 
     218  for (i=0; i<ra; i++)
     219    for (j=0; j<cb; j++)
     220    {
     221      sum = n_Init(0, basecoeffs);
     222     
     223      for (k=0; k<ca; k++)
     224      {
     225        number prod = n_Mult( BIMATELEM(*a, i, k), BIMATELEM(*b, k, j), basecoeffs);
     226       
     227        number sum2 = n_Add(sum, prod, basecoeffs); // no inplace add :(
     228       
     229        n_Delete(&sum, basecoeffs); n_Delete(&prod, basecoeffs);
     230       
     231        sum = sum2;
     232      }
     233      bim->rawset(i+1, j+1, sum, basecoeffs);
     234    }
    195235  return bim;
    196236}
    197237
    198 bigintmat * bimMult(bigintmat * a, bigintmat * b)
    199 {
    200   int i, j, k,
    201       ra = a->rows(), ca = a->cols(),
    202       rb = b->rows(), cb = b->cols();
    203   number sum;
    204   bigintmat * bim;
    205   if (ca != rb) return NULL;
    206   bim = new bigintmat(ra, cb);
    207   for (i=0; i<ra; i++)
    208   {
    209     for (j=0; j<cb; j++)
    210     {
    211       sum = nlInit(0, NULL);
    212       for (k=0; k<ca; k++)
    213       {
    214         number prod = nlMult((*a)[i*ca+k], (*b)[k*cb+j],NULL);
    215         number sum2 = nlAdd(sum, prod,NULL);
    216         nlDelete(&sum, NULL);
    217         sum = sum2;
    218         nlDelete(&prod, NULL);
    219       }
    220       bim->set(i+1, j+1, sum);
    221       nlDelete(&sum, NULL);
    222     }
    223   }
     238// ----------------------------------------------------------------- //
     239// Korrekt?
     240
     241intvec * bim2iv(bigintmat * b)
     242{
     243  intvec * iv = new intvec(b->rows(), b->cols(), 0);
     244  for (int i=0; i<(b->rows())*(b->cols()); i++)
     245    (*iv)[i] = n_Int((*b)[i], b->basecoeffs()); // Geht das so?
     246  return iv;
     247}
     248
     249bigintmat * iv2bim(intvec * b, const coeffs C)
     250{
     251  const int l = (b->rows())*(b->cols());
     252  bigintmat * bim = new bigintmat(b->rows(), b->cols(), C);
     253 
     254  for (int i=0; i < l; i++)
     255    bim->rawset(i, n_Init((*b)[i], C), C);
     256 
    224257  return bim;
    225258}
    226259
    227 // Korrekt?
    228 
    229 intvec * bim2iv(bigintmat * b)
    230 {
    231   intvec * iv;
    232   iv = new intvec(b->rows(), b->cols(), 0);
    233   for (int i=0; i<(b->rows())*(b->cols()); i++) { (*iv)[i] = nlInt((*b)[i], NULL); } // Geht das so?
    234   return iv;
    235 }
    236 
    237 bigintmat * iv2bim(intvec * b)
    238 {
    239   bigintmat * bim;
    240   bim = new bigintmat(b->rows(), b->cols());
    241   for (int i=0; i<(b->rows())*(b->cols()); i++)
    242   {
    243     number n = nlInit((*b)[i], NULL);
    244     bim->set(i, n);
    245     nlDelete(&n, NULL);
    246   }
    247   return bim;
    248 }
     260// ----------------------------------------------------------------- //
    249261
    250262int bigintmat::compare(const bigintmat* op) const
    251263{
     264  assume (basecoeffs() == op->basecoeffs() );
     265
     266#ifndef NDEBUG
     267  if (basecoeffs() != op->basecoeffs() )
     268    WerrorS("wrong bigintmat comparison: different basecoeffs!\n");
     269#endif
     270
    252271  if ((col!=1) ||(op->cols()!=1))
    253272  {
    254273    if((col!=op->cols())
    255     || (row!=op->rows()))
     274       || (row!=op->rows()))
    256275      return -2;
    257276  }
     277
    258278  int i;
    259279  for (i=0; i<si_min(row*col,op->rows()*op->cols()); i++)
    260280  {
    261     if (nlGreater(v[i], (*op)[i],NULL))
     281    if ( n_Greater(v[i], (*op)[i], basecoeffs()) )
    262282      return 1;
    263     else if (!nlEqual(v[i], (*op)[i],NULL))
     283    else if (! n_Equal(v[i], (*op)[i], basecoeffs()))
    264284      return -1;
    265285  }
     
    267287  for (; i<row; i++)
    268288  {
    269     if (nlGreaterZero(v[i],NULL))
     289    if ( n_GreaterZero(v[i], basecoeffs()) )
    270290      return 1;
    271     else if (!nlIsZero(v[i],NULL))
     291    else if (! n_IsZero(v[i], basecoeffs()) )
    272292      return -1;
    273293  }
    274294  for (; i<op->rows(); i++)
    275295  {
    276     if (nlGreaterZero((*op)[i],NULL))
     296    if ( n_GreaterZero((*op)[i], basecoeffs()) )
    277297      return -1;
    278     else if (!nlIsZero((*op)[i],NULL))
     298    else if (! n_IsZero((*op)[i], basecoeffs()) )
    279299      return 1;
    280300  }
     
    285305bigintmat * bimCopy(const bigintmat * b)
    286306{
    287   bigintmat * a=NULL;
    288   if (b!=NULL)
    289   {
    290     a = new bigintmat(b->rows(), b->cols());
    291     for (int i=0; i<(b->rows())*(b->cols()); i++)
    292     {
    293       a->set(i, (*b)[i]);
    294     }
    295   }
    296   return a;
     307  if (b == NULL)
     308    return NULL;
     309   
     310  return new bigintmat(b);
    297311}
    298312
    299313char* bigintmat::String()
    300314{
     315  assume (rows() > 0);
     316  assume (cols() > 0);
     317
    301318  StringSetS("");
    302   int i;
    303   for (i=0; i<col*row-1; i++)
    304   {
    305     nlWrite(v[i], NULL);
    306     StringAppendS(",");
    307   }
    308   nlWrite(v[i], NULL);
    309    /* if (i != col*row-1)
    310     {
    311       StringAppendS(",");
    312       if ((i+1)%col == 0)
    313         StringAppendS("\n");
    314     }   */
     319  const int l = cols()*rows();
     320
     321  n_Write(v[0], basecoeffs());
     322  for (int i = 1; i < l; i++)
     323  {
     324    StringAppendS(","); n_Write(v[i], basecoeffs());
     325  }
     326  /* if (i != col*row-1)
     327  {
     328  StringAppendS(",");
     329  if ((i+1)%col == 0)
     330  StringAppendS("\n");
     331  }   */
    315332  return StringAppendS("");
    316333}
     
    323340  {
    324341    StringSetS("");
    325     nlWrite(v[i], NULL);
     342    n_Write(v[i], basecoeffs());
    326343    char * tmp = StringAppendS("");
    327     char * ts = omStrDup(tmp);
    328     int nl = strlen(ts);
    329     if (nl > wid)
    330     {
    331       if (nl > colwid)
     344//    char * ts = omStrDup(tmp);
     345    const int _nl = strlen(tmp); // ts?
     346    if (_nl > wid)
     347    {
     348      if (_nl > colwid)
    332349      {
    333350        int phwid = floor(log10(row))+floor(log10(col))+5;
     
    336353      }
    337354      else
    338         wid = nl;
     355        wid = _nl;
    339356    }
    340357  }
     
    357374    {
    358375      StringSetS("");
    359       nlWrite(v[i], NULL);
     376      n_Write(v[i], basecoeffs());
    360377      char * temp = StringAppendS("");
    361378      char * ts = omStrDup(temp);
    362       int nl = strlen(ts);
    363       if (nl > colwid)
     379      const int _nl = strlen(ts);
     380      if (_nl > colwid)
    364381      {
    365382        StringSetS("");
     
    386403      else  // Mit Leerzeichen auffÃŒllen und zahl reinschreiben
    387404      {
    388         for (int j=0; j<colwid-nl; j++)
     405        for (int j=0; j<colwid-_nl; j++)
    389406          ps[pos+j] = ' ';
    390         for (int j=0; j<nl; j++)
    391           ps[pos+colwid-nl+j] = ts[j];
     407        for (int j=0; j<_nl; j++)
     408          ps[pos+colwid-_nl+j] = ts[j];
    392409      }
    393410      // ", " oder "\n" einfÃŒgen
     
    406423        pos += colwid+2;
    407424      }
    408     // Hier ts zerstören
     425      // Hier ts zerstören
    409426    }
    410427    PrintS(ps);
     
    416433static void bimRowContent(bigintmat *bimat, int rowpos, int colpos)
    417434{
     435  const coeffs basecoeffs = bimat->basecoeffs();
     436 
    418437  number tgcd, m;
    419438  int i=bimat->cols();
     
    421440  loop
    422441  {
    423     tgcd = nlCopy(BIMATELEM(*bimat,rowpos,i--),NULL);
    424     if (!nlIsZero(tgcd,NULL)) break;
     442    tgcd = n_Copy(BIMATELEM(*bimat,rowpos,i--), basecoeffs);
     443    if (! n_IsZero(tgcd, basecoeffs)) break;
    425444    if (i<colpos) return;
    426445  }
    427   if ((!nlGreaterZero(tgcd,NULL)) && (!nlIsZero(tgcd,NULL))) tgcd = nlNeg(tgcd,NULL);
    428   if (nlIsOne(tgcd,NULL)) return;
     446  if ((! n_GreaterZero(tgcd, basecoeffs)) && (! n_IsZero(tgcd, basecoeffs))) tgcd = n_Neg(tgcd, basecoeffs);
     447  if ( n_IsOne(tgcd,basecoeffs)) return;
    429448  loop
    430449  {
    431     m = nlCopy(BIMATELEM(*bimat,rowpos,i--),NULL);
    432     if (!nlIsZero(m,NULL))
    433     {
    434       number tp1 = nlGcd(tgcd, m, NULL);
    435       nlDelete(&tgcd, NULL);
     450    m = n_Copy(BIMATELEM(*bimat,rowpos,i--), basecoeffs);
     451    if (! n_IsZero(m,basecoeffs))
     452    {
     453      number tp1 = n_Gcd(tgcd, m, basecoeffs);
     454      n_Delete(&tgcd, basecoeffs);
    436455      tgcd = tp1;
    437456    }
    438     if (nlIsOne(tgcd,NULL)) return;
     457    if ( n_IsOne(tgcd,basecoeffs)) return;
    439458    if (i<colpos) break;
    440459  }
    441460  for (i=bimat->cols();i>=colpos;i--)
    442461  {
    443     number tp2 = nlDiv(BIMATELEM(*bimat,rowpos,i), tgcd,NULL);
    444     nlDelete(&BIMATELEM(*bimat,rowpos,i), NULL);
     462    number tp2 = n_Div(BIMATELEM(*bimat,rowpos,i), tgcd,basecoeffs);
     463    n_Delete(&BIMATELEM(*bimat,rowpos,i), basecoeffs);
    445464    BIMATELEM(*bimat,rowpos,i) = tp2;
    446465  }
    447   nlDelete(&tgcd, NULL);
    448   nlDelete(&m, NULL);
     466  n_Delete(&tgcd, basecoeffs);
     467  n_Delete(&m, basecoeffs);
    449468}
    450469
    451470static void bimReduce(bigintmat *bimat, int rpiv, int colpos,
    452                      int ready, int all)
    453 {
     471                      int ready, int all)
     472{
     473  const coeffs basecoeffs = bimat->basecoeffs();
     474
    454475  number tgcd, ce, m1, m2;
    455476  int j, i;
     
    458479  for (j=all;j>ready;j--)
    459480  {
    460     ce = nlCopy(BIMATELEM(*bimat,j,colpos),NULL);
    461     if (!nlIsZero(ce,NULL))
    462     {
    463       nlDelete(&BIMATELEM(*bimat,j,colpos), NULL);
    464       BIMATELEM(*bimat,j,colpos) = nlInit(0, NULL);
    465       m1 = nlCopy(piv,NULL);
    466       m2 = nlCopy(ce,NULL);
    467       tgcd = nlGcd(m1, m2, NULL);
    468       if (!nlIsOne(tgcd,NULL))
    469       {
    470         number tp1 = nlDiv(m1, tgcd,NULL);
    471         number tp2 = nlDiv(m2, tgcd,NULL);
    472         nlDelete(&m1, NULL);
    473         nlDelete(&m2, NULL);
     481    ce = n_Copy(BIMATELEM(*bimat,j,colpos),basecoeffs);
     482    if (! n_IsZero(ce, basecoeffs))
     483    {
     484      n_Delete(&BIMATELEM(*bimat,j,colpos), basecoeffs);
     485      BIMATELEM(*bimat,j,colpos) = n_Init(0, basecoeffs);
     486      m1 = n_Copy(piv,basecoeffs);
     487      m2 = n_Copy(ce,basecoeffs);
     488      tgcd = n_Gcd(m1, m2, basecoeffs);
     489      if (! n_IsOne(tgcd,basecoeffs))
     490      {
     491        number tp1 = n_Div(m1, tgcd,basecoeffs);
     492        number tp2 = n_Div(m2, tgcd,basecoeffs);
     493        n_Delete(&m1, basecoeffs);
     494        n_Delete(&m2, basecoeffs);
    474495        m1 = tp1;
    475496        m2 = tp2;
     
    477498      for (i=bimat->cols();i>colpos;i--)
    478499      {
    479         nlDelete(&BIMATELEM(*bimat,j,i), NULL);
    480         number tp1 = nlMult(BIMATELEM(*bimat,j,i), m1,NULL);
    481         number tp2 = nlMult(BIMATELEM(*bimat,rpiv,i), m2,NULL);
    482         BIMATELEM(*bimat,j,i) = nlSub(tp1, tp2,NULL);
    483         nlDelete(&tp1, NULL);
    484         nlDelete(&tp2, NULL);
     500        n_Delete(&BIMATELEM(*bimat,j,i), basecoeffs);
     501        number tp1 = n_Mult(BIMATELEM(*bimat,j,i), m1,basecoeffs);
     502        number tp2 = n_Mult(BIMATELEM(*bimat,rpiv,i), m2,basecoeffs);
     503        BIMATELEM(*bimat,j,i) = n_Sub(tp1, tp2,basecoeffs);
     504        n_Delete(&tp1, basecoeffs);
     505        n_Delete(&tp2, basecoeffs);
    485506      }
    486507      bimRowContent(bimat, j, colpos+1);
    487       nlDelete(&m1, NULL);
    488       nlDelete(&m2, NULL);
    489     }
    490     nlDelete(&ce, NULL);
    491   }
    492 }
    493 
    494 
    495 
    496 
    497 
     508      n_Delete(&m1, basecoeffs);
     509      n_Delete(&m2, basecoeffs);
     510    }
     511    n_Delete(&ce, basecoeffs);
     512  }
     513}
     514
     515
     516
     517
     518
  • libpolys/coeffs/bigintmat.h

    r84fc1f rfe02b1  
    1 #ifndef BIGINTMAT_H
    2 #define BIGINTMAT_H
    31/****************************************
    42*  Computer Algebra System SINGULAR     *
     
    75* ABSTRACT: class bigintmat: matrizes of big integers
    86*/
    9 #include <string.h>
     7
     8#ifndef BIGINTMAT_H
     9#define BIGINTMAT_H
     10
    1011#include <omalloc/omalloc.h>
     12#include <findexec/feFopen.h>
    1113#include <coeffs/coeffs.h>
    12 #include <coeffs/longrat.h>
    13 #include <misc/intvec.h>
    1414
    15 
     15/// matrix of numbers
     16/// NOTE: no reference counting!!!
    1617class bigintmat
    1718{
    18 private:
    19   number *v;
    20   int row;
    21   int col;
    22 public:
     19  private:
     20    coeffs m_coeffs;
     21    number *v;
     22    int row;
     23    int col;
     24  public:
    2325
    24   bigintmat()
    25   {
    26     row = 1;
    27     col = 0;
    28     v = NULL;
    29   }
    30   bigintmat(int r, int c);
     26    bigintmat(): m_coeffs(NULL), v(NULL), row(1), col(0){}
    3127
    32   bigintmat(const bigintmat *m)
    33   {
    34     row = m->rows();
    35     col = m->cols();
    36     if (row*col>0)
     28    bigintmat(int r, int c, const coeffs n): m_coeffs(n), v(NULL), row(r), col(c)
    3729    {
    38       v = (number *)omAlloc(sizeof(number)*row*col);
    39       for (int i=row*col-1; i>=0; i--)
     30      assume (rows() > 0);
     31      assume (cols() > 0);
     32
     33      const int l = r*c;
     34
     35      if (l>0) /*(r>0) && (c>0) */
    4036      {
    41         v[i] = nlCopy((*m)[i],NULL);
     37        v = (number *)omAlloc(sizeof(number)*l);
     38
     39        assume (basecoeffs() != NULL);
     40        for (int i = l - 1; i>=0; i--)
     41        {
     42          v[i] = n_Init(0, basecoeffs());
     43        }
    4244      }
    4345    }
    44   }
    4546
     47    bigintmat(const bigintmat *m): m_coeffs(m->basecoeffs()), v(NULL), row(m->rows()), col(m->cols())
     48    {
     49      const int l = row*col;
    4650
    47   inline number& operator[](int i)
     51      if (l > 0)
     52      {
     53        assume (rows() > 0);
     54        assume (cols() > 0);
     55
     56        assume (m->v != NULL);
     57
     58        v = (number *)omAlloc(sizeof(number)*row*col);
     59
     60        assume (basecoeffs() != NULL);
     61
     62        for (int i = l-1; i>=0; i--)
     63        {
     64          v[i] = n_Copy((*m)[i], basecoeffs());
     65        }
     66      }
     67    }
     68
     69    inline number& operator[](int i)
    4870    {
    4971#ifndef NDEBUG
     
    5375      }
    5476#endif
     77      assume ( !((i<0)||(i>=row*col)) );
     78
    5579      return v[i];  // Hier sollte imho kein nlCopy rein...
    5680    }
    57   inline const number& operator[](int i) const
     81    inline const number& operator[](int i) const
    5882    {
    5983#ifndef NDEBUG
     
    6387      }
    6488#endif
     89      assume ( !((i<0)||(i>=row*col)) );
     90
    6591      return v[i];
    6692    }
    6793
    68 #define BIMATELEM(M,I,J) (M)[(I-1)*(M).cols()+J-1]
    69   void operator*=(int intop);
    70   void operator*=(number bintop);
    71   inline int  cols() const { return col; }
    72   inline int  rows() const { return row; }
    73   inline ~bigintmat()
     94    /// UEberladener *=-Operator (fuer int und bigint)
     95    /// Frage hier: *= verwenden oder lieber = und * einzeln?
     96    void operator*=(int intop);
     97   
     98    void inpMult(number bintop, const coeffs C = NULL);
     99
     100    inline int  cols() const { return col; }
     101    inline int  rows() const { return row; }
     102    inline coeffs basecoeffs() const { return m_coeffs; }
     103
     104    ~bigintmat()
    74105    {
    75106      if (v!=NULL)
    76107      {
    77         for (int i=0; i<row*col; i++) { nlDelete(&(v[i]), NULL); }
    78         omFreeSize((ADDRESS)v,sizeof(number)*row*col);
     108        for (int i=0; i<row*col; i++) { n_Delete(&(v[i]), basecoeffs()); }
     109        omFreeSize((ADDRESS)v, sizeof(number)*row*col);
    79110        v=NULL;
    80111      }
    81112    }
    82   number get(int i, int j);
    83   number get(int i);
    84   void set(int i, int j, number n);
    85   void set(int i, number n);
    86   char * String();
    87   void pprint(int maxwid);
    88   int compare(const bigintmat* op) const;
    89   int getwid(int maxwid);
     113
     114    int index(int r, int c) const
     115    {
     116      assume (rows() >= 0 && cols() >= 0);
     117     
     118      assume (r > 0 && c > 0);
     119      assume (r <= rows() && r <= cols());
     120
     121      const int index = ((r-1)*cols() + (c-1));
     122
     123      assume (index >= 0 && index < rows() * cols());
     124      return index;
     125    }
     126
     127    /// get a copy of an entry. NOTE: starts at [1,1]
     128    number get(int i, int j) const;
     129
     130    /// get a copy of an entry. NOTE: starts at [0]
     131    number get(int i) const;
     132
     133    /// replace an entry with a copy (delete old + copy new!).
     134    /// NOTE: starts at [1,1]
     135    void set(int i, int j, number n, const coeffs C = NULL);
     136   
     137    /// replace an entry with a copy (delete old + copy new!).
     138    /// NOTE: starts at [0]
     139    void set(int i, number n, const coeffs C = NULL);
     140
     141
     142    /// replace an entry with the given number n (only delete old).
     143    /// NOTE: starts at [0]
     144    inline void rawset(int i, number n, const coeffs C = NULL)
     145    {
     146      assume (C == NULL || C == basecoeffs());
     147      assume (i >= 0);
     148      const int l = rows() * cols();
     149      assume (i<l);
     150
     151      if (i < l)
     152      {
     153        n_Delete(&(v[i]), basecoeffs()); v[i] = n;
     154      } else
     155      {
     156#ifndef NDEBUG
     157        Werror("wrong bigintmat index:%d\n",i);
     158#endif
     159      }
     160    }
     161   
     162    inline void rawset(int i, int j, number n, const coeffs C = NULL)
     163    {
     164      rawset( index(i,j), n, C);
     165    }
     166
     167    char * String();
     168    void pprint(int maxwid);
     169    int compare(const bigintmat* op) const;
     170    int getwid(int maxwid);
    90171};
    91 bool operator==(bigintmat & lhr, bigintmat & rhr);
    92 bool operator!=(bigintmat & lhr, bigintmat & rhr);
     172
     173bool operator==(const bigintmat & lhr, const bigintmat & rhr);
     174bool operator!=(const bigintmat & lhr, const bigintmat & rhr);
     175
     176/// Matrix-Add/-Sub/-Mult so oder mit operator+/-/* ?
     177/// NOTE: NULL as a result means an error (non-compatible matrices?)
    93178bigintmat * bimAdd(bigintmat * a, bigintmat * b);
    94179bigintmat * bimSub(bigintmat * a, bigintmat * b);
    95180bigintmat * bimMult(bigintmat * a, bigintmat * b);
    96 intvec * bim2iv(bigintmat * b);
    97181bigintmat * bimCopy(const bigintmat * b);
     182
     183
     184
     185// Ungetestet
    98186static void bimRowContent(bigintmat *bimat, int rowpos, int colpos);
    99187static void bimReduce(bigintmat *bimat, int rpiv, int colpos,
    100                      int ready, int all);
     188                      int ready, int all);
    101189
    102 bigintmat * iv2bim(intvec * b);
    103 #endif
     190class intvec;
     191intvec * bim2iv(bigintmat * b);
     192bigintmat * iv2bim(intvec * b, const coeffs C);
     193
     194
     195#endif // #ifndef BIGINTMAT_H
Note: See TracChangeset for help on using the changeset viewer.