[9127cc] | 1 | /**************************************** |
---|
| 2 | * Computer Algebra System SINGULAR * |
---|
| 3 | ****************************************/ |
---|
| 4 | /* |
---|
| 5 | * ABSTRACT: class bigintmat: matrizes of big integers |
---|
| 6 | */ |
---|
[fe02b1] | 7 | |
---|
| 8 | #ifndef BIGINTMAT_H |
---|
| 9 | #define BIGINTMAT_H |
---|
| 10 | |
---|
[9127cc] | 11 | #include <omalloc/omalloc.h> |
---|
[fe02b1] | 12 | #include <findexec/feFopen.h> |
---|
[9127cc] | 13 | #include <coeffs/coeffs.h> |
---|
| 14 | |
---|
[fe02b1] | 15 | /// matrix of numbers |
---|
| 16 | /// NOTE: no reference counting!!! |
---|
[9127cc] | 17 | class bigintmat |
---|
| 18 | { |
---|
[fe02b1] | 19 | private: |
---|
| 20 | coeffs m_coeffs; |
---|
| 21 | number *v; |
---|
| 22 | int row; |
---|
| 23 | int col; |
---|
| 24 | public: |
---|
| 25 | |
---|
| 26 | bigintmat(): m_coeffs(NULL), v(NULL), row(1), col(0){} |
---|
| 27 | |
---|
[510dbc] | 28 | bigintmat * transpose(); |
---|
| 29 | |
---|
[fe02b1] | 30 | bigintmat(int r, int c, const coeffs n): m_coeffs(n), v(NULL), row(r), col(c) |
---|
[9127cc] | 31 | { |
---|
[fe02b1] | 32 | assume (rows() > 0); |
---|
| 33 | assume (cols() > 0); |
---|
| 34 | |
---|
| 35 | const int l = r*c; |
---|
| 36 | |
---|
| 37 | if (l>0) /*(r>0) && (c>0) */ |
---|
[9127cc] | 38 | { |
---|
[fe02b1] | 39 | v = (number *)omAlloc(sizeof(number)*l); |
---|
| 40 | |
---|
| 41 | assume (basecoeffs() != NULL); |
---|
| 42 | for (int i = l - 1; i>=0; i--) |
---|
| 43 | { |
---|
| 44 | v[i] = n_Init(0, basecoeffs()); |
---|
| 45 | } |
---|
[9127cc] | 46 | } |
---|
| 47 | } |
---|
| 48 | |
---|
[fe02b1] | 49 | bigintmat(const bigintmat *m): m_coeffs(m->basecoeffs()), v(NULL), row(m->rows()), col(m->cols()) |
---|
| 50 | { |
---|
| 51 | const int l = row*col; |
---|
| 52 | |
---|
| 53 | if (l > 0) |
---|
| 54 | { |
---|
| 55 | assume (rows() > 0); |
---|
| 56 | assume (cols() > 0); |
---|
[9127cc] | 57 | |
---|
[fe02b1] | 58 | assume (m->v != NULL); |
---|
| 59 | |
---|
| 60 | v = (number *)omAlloc(sizeof(number)*row*col); |
---|
| 61 | |
---|
| 62 | assume (basecoeffs() != NULL); |
---|
| 63 | |
---|
| 64 | for (int i = l-1; i>=0; i--) |
---|
| 65 | { |
---|
| 66 | v[i] = n_Copy((*m)[i], basecoeffs()); |
---|
| 67 | } |
---|
| 68 | } |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | inline number& operator[](int i) |
---|
[9127cc] | 72 | { |
---|
| 73 | #ifndef NDEBUG |
---|
| 74 | if((i<0)||(i>=row*col)) |
---|
| 75 | { |
---|
| 76 | Werror("wrong bigintmat index:%d\n",i); |
---|
| 77 | } |
---|
| 78 | #endif |
---|
[fe02b1] | 79 | assume ( !((i<0)||(i>=row*col)) ); |
---|
| 80 | |
---|
[9127cc] | 81 | return v[i]; // Hier sollte imho kein nlCopy rein... |
---|
| 82 | } |
---|
[fe02b1] | 83 | inline const number& operator[](int i) const |
---|
[9127cc] | 84 | { |
---|
| 85 | #ifndef NDEBUG |
---|
| 86 | if((i<0)||(i>=row*col)) |
---|
| 87 | { |
---|
| 88 | Werror("wrong bigintmat index:%d\n",i); |
---|
| 89 | } |
---|
| 90 | #endif |
---|
[fe02b1] | 91 | assume ( !((i<0)||(i>=row*col)) ); |
---|
| 92 | |
---|
[9127cc] | 93 | return v[i]; |
---|
| 94 | } |
---|
[75f10d] | 95 | #define BIMATELEM(M,I,J) (M)[(I-1)*(M).cols()+J-1] |
---|
[9127cc] | 96 | |
---|
[fe02b1] | 97 | /// UEberladener *=-Operator (fuer int und bigint) |
---|
| 98 | /// Frage hier: *= verwenden oder lieber = und * einzeln? |
---|
| 99 | void operator*=(int intop); |
---|
[75f10d] | 100 | |
---|
[fe02b1] | 101 | void inpMult(number bintop, const coeffs C = NULL); |
---|
| 102 | |
---|
[ebbb9c] | 103 | inline int length() { return col*row; } |
---|
[fe02b1] | 104 | inline int cols() const { return col; } |
---|
| 105 | inline int rows() const { return row; } |
---|
| 106 | inline coeffs basecoeffs() const { return m_coeffs; } |
---|
| 107 | |
---|
| 108 | ~bigintmat() |
---|
[9127cc] | 109 | { |
---|
| 110 | if (v!=NULL) |
---|
| 111 | { |
---|
[fe02b1] | 112 | for (int i=0; i<row*col; i++) { n_Delete(&(v[i]), basecoeffs()); } |
---|
| 113 | omFreeSize((ADDRESS)v, sizeof(number)*row*col); |
---|
[9127cc] | 114 | v=NULL; |
---|
| 115 | } |
---|
| 116 | } |
---|
[fe02b1] | 117 | |
---|
| 118 | int index(int r, int c) const |
---|
| 119 | { |
---|
| 120 | assume (rows() >= 0 && cols() >= 0); |
---|
[75f10d] | 121 | |
---|
[fe02b1] | 122 | assume (r > 0 && c > 0); |
---|
[aaf761] | 123 | assume (r <= rows() && c <= cols()); |
---|
[fe02b1] | 124 | |
---|
| 125 | const int index = ((r-1)*cols() + (c-1)); |
---|
| 126 | |
---|
| 127 | assume (index >= 0 && index < rows() * cols()); |
---|
| 128 | return index; |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | /// get a copy of an entry. NOTE: starts at [1,1] |
---|
| 132 | number get(int i, int j) const; |
---|
| 133 | |
---|
| 134 | /// get a copy of an entry. NOTE: starts at [0] |
---|
| 135 | number get(int i) const; |
---|
| 136 | |
---|
| 137 | /// replace an entry with a copy (delete old + copy new!). |
---|
| 138 | /// NOTE: starts at [1,1] |
---|
| 139 | void set(int i, int j, number n, const coeffs C = NULL); |
---|
[75f10d] | 140 | |
---|
[fe02b1] | 141 | /// replace an entry with a copy (delete old + copy new!). |
---|
| 142 | /// NOTE: starts at [0] |
---|
| 143 | void set(int i, number n, const coeffs C = NULL); |
---|
| 144 | |
---|
| 145 | |
---|
| 146 | /// replace an entry with the given number n (only delete old). |
---|
| 147 | /// NOTE: starts at [0] |
---|
[2e4ec14] | 148 | #ifdef NDEBUG |
---|
| 149 | inline void rawset(int i, number n, const coeffs /*C = NULL*/) |
---|
| 150 | #else |
---|
[fe02b1] | 151 | inline void rawset(int i, number n, const coeffs C = NULL) |
---|
[2e4ec14] | 152 | #endif |
---|
[fe02b1] | 153 | { |
---|
| 154 | assume (C == NULL || C == basecoeffs()); |
---|
| 155 | assume (i >= 0); |
---|
| 156 | const int l = rows() * cols(); |
---|
| 157 | assume (i<l); |
---|
| 158 | |
---|
| 159 | if (i < l) |
---|
| 160 | { |
---|
| 161 | n_Delete(&(v[i]), basecoeffs()); v[i] = n; |
---|
[75f10d] | 162 | } |
---|
[fe02b1] | 163 | #ifndef NDEBUG |
---|
[75f10d] | 164 | else |
---|
| 165 | { |
---|
[fe02b1] | 166 | Werror("wrong bigintmat index:%d\n",i); |
---|
| 167 | } |
---|
[75f10d] | 168 | #endif |
---|
[fe02b1] | 169 | } |
---|
[75f10d] | 170 | |
---|
[fe02b1] | 171 | inline void rawset(int i, int j, number n, const coeffs C = NULL) |
---|
| 172 | { |
---|
| 173 | rawset( index(i,j), n, C); |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | char * String(); |
---|
[81384b] | 177 | /*** |
---|
| 178 | * Returns a string as it would have been printed in the interpreter. |
---|
| 179 | * Used e.g. in print functions of various blackbox types. |
---|
| 180 | **/ |
---|
| 181 | char * StringAsPrinted(); |
---|
[fe02b1] | 182 | void pprint(int maxwid); |
---|
| 183 | int compare(const bigintmat* op) const; |
---|
[510dbc] | 184 | int * getwid(int maxwid); |
---|
[9127cc] | 185 | }; |
---|
[fe02b1] | 186 | |
---|
| 187 | bool operator==(const bigintmat & lhr, const bigintmat & rhr); |
---|
| 188 | bool operator!=(const bigintmat & lhr, const bigintmat & rhr); |
---|
| 189 | |
---|
| 190 | /// Matrix-Add/-Sub/-Mult so oder mit operator+/-/* ? |
---|
| 191 | /// NOTE: NULL as a result means an error (non-compatible matrices?) |
---|
[9127cc] | 192 | bigintmat * bimAdd(bigintmat * a, bigintmat * b); |
---|
[75f10d] | 193 | bigintmat * bimAdd(bigintmat * a, int b); |
---|
[9127cc] | 194 | bigintmat * bimSub(bigintmat * a, bigintmat * b); |
---|
[75f10d] | 195 | bigintmat * bimSub(bigintmat * a, int b); |
---|
[9127cc] | 196 | bigintmat * bimMult(bigintmat * a, bigintmat * b); |
---|
[75f10d] | 197 | bigintmat * bimMult(bigintmat * a, int b); |
---|
| 198 | bigintmat * bimMult(bigintmat * a, number b, const coeffs cf); |
---|
[9127cc] | 199 | bigintmat * bimCopy(const bigintmat * b); |
---|
[fe02b1] | 200 | |
---|
[510dbc] | 201 | int getShorter (int * a, int l, int j, int cols, int rows); |
---|
| 202 | int findLongest(int * a, int length); |
---|
| 203 | int intArrSum(int * a, int length); |
---|
[fe02b1] | 204 | |
---|
| 205 | class intvec; |
---|
| 206 | intvec * bim2iv(bigintmat * b); |
---|
| 207 | bigintmat * iv2bim(intvec * b, const coeffs C); |
---|
| 208 | |
---|
| 209 | |
---|
| 210 | #endif // #ifndef BIGINTMAT_H |
---|