Changeset fe02b1 in git for libpolys/coeffs/bigintmat.h


Ignore:
Timestamp:
Apr 25, 2012, 8:38:08 PM (11 years ago)
Author:
Oleksandr Motsak <motsak@…>
Branches:
(u'spielwiese', '8e0ad00ce244dfd0756200662572aef8402f13d5')
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!)
File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.