1 | /**************************************** |
---|
2 | * Computer Algebra System SINGULAR * |
---|
3 | ****************************************/ |
---|
4 | /* |
---|
5 | * ABSTRACT: class bigintmat: matrizes of big integers |
---|
6 | */ |
---|
7 | |
---|
8 | #ifndef BIGINTMAT_H |
---|
9 | #define BIGINTMAT_H |
---|
10 | |
---|
11 | #include <omalloc/omalloc.h> |
---|
12 | #include <findexec/feFopen.h> |
---|
13 | #include <coeffs/coeffs.h> |
---|
14 | |
---|
15 | /// matrix of numbers |
---|
16 | /// NOTE: no reference counting!!! |
---|
17 | class bigintmat |
---|
18 | { |
---|
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 | |
---|
28 | bigintmat * transpose(); |
---|
29 | |
---|
30 | bigintmat(int r, int c, const coeffs n): m_coeffs(n), v(NULL), row(r), col(c) |
---|
31 | { |
---|
32 | assume (rows() > 0); |
---|
33 | assume (cols() > 0); |
---|
34 | |
---|
35 | const int l = r*c; |
---|
36 | |
---|
37 | if (l>0) /*(r>0) && (c>0) */ |
---|
38 | { |
---|
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 | } |
---|
46 | } |
---|
47 | } |
---|
48 | |
---|
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); |
---|
57 | |
---|
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) |
---|
72 | { |
---|
73 | #ifndef NDEBUG |
---|
74 | if((i<0)||(i>=row*col)) |
---|
75 | { |
---|
76 | Werror("wrong bigintmat index:%d\n",i); |
---|
77 | } |
---|
78 | #endif |
---|
79 | assume ( !((i<0)||(i>=row*col)) ); |
---|
80 | |
---|
81 | return v[i]; // Hier sollte imho kein nlCopy rein... |
---|
82 | } |
---|
83 | inline const number& operator[](int i) const |
---|
84 | { |
---|
85 | #ifndef NDEBUG |
---|
86 | if((i<0)||(i>=row*col)) |
---|
87 | { |
---|
88 | Werror("wrong bigintmat index:%d\n",i); |
---|
89 | } |
---|
90 | #endif |
---|
91 | assume ( !((i<0)||(i>=row*col)) ); |
---|
92 | |
---|
93 | return v[i]; |
---|
94 | } |
---|
95 | #define BIMATELEM(M,I,J) (M)[(I-1)*(M).cols()+J-1] |
---|
96 | |
---|
97 | /// UEberladener *=-Operator (fuer int und bigint) |
---|
98 | /// Frage hier: *= verwenden oder lieber = und * einzeln? |
---|
99 | void operator*=(int intop); |
---|
100 | |
---|
101 | void inpMult(number bintop, const coeffs C = NULL); |
---|
102 | |
---|
103 | inline int length() { return col*row; } |
---|
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() |
---|
109 | { |
---|
110 | if (v!=NULL) |
---|
111 | { |
---|
112 | for (int i=0; i<row*col; i++) { n_Delete(&(v[i]), basecoeffs()); } |
---|
113 | omFreeSize((ADDRESS)v, sizeof(number)*row*col); |
---|
114 | v=NULL; |
---|
115 | } |
---|
116 | } |
---|
117 | |
---|
118 | int index(int r, int c) const |
---|
119 | { |
---|
120 | assume (rows() >= 0 && cols() >= 0); |
---|
121 | |
---|
122 | assume (r > 0 && c > 0); |
---|
123 | assume (r <= rows() && r <= cols()); |
---|
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); |
---|
140 | |
---|
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] |
---|
148 | inline void rawset(int i, number n, const coeffs C = NULL) |
---|
149 | { |
---|
150 | assume (C == NULL || C == basecoeffs()); |
---|
151 | assume (i >= 0); |
---|
152 | const int l = rows() * cols(); |
---|
153 | assume (i<l); |
---|
154 | |
---|
155 | if (i < l) |
---|
156 | { |
---|
157 | n_Delete(&(v[i]), basecoeffs()); v[i] = n; |
---|
158 | } |
---|
159 | #ifndef NDEBUG |
---|
160 | else |
---|
161 | { |
---|
162 | Werror("wrong bigintmat index:%d\n",i); |
---|
163 | } |
---|
164 | #endif |
---|
165 | } |
---|
166 | |
---|
167 | inline void rawset(int i, int j, number n, const coeffs C = NULL) |
---|
168 | { |
---|
169 | rawset( index(i,j), n, C); |
---|
170 | } |
---|
171 | |
---|
172 | char * String(); |
---|
173 | void pprint(int maxwid); |
---|
174 | int compare(const bigintmat* op) const; |
---|
175 | int * getwid(int maxwid); |
---|
176 | }; |
---|
177 | |
---|
178 | bool operator==(const bigintmat & lhr, const bigintmat & rhr); |
---|
179 | bool operator!=(const bigintmat & lhr, const bigintmat & rhr); |
---|
180 | |
---|
181 | /// Matrix-Add/-Sub/-Mult so oder mit operator+/-/* ? |
---|
182 | /// NOTE: NULL as a result means an error (non-compatible matrices?) |
---|
183 | bigintmat * bimAdd(bigintmat * a, bigintmat * b); |
---|
184 | bigintmat * bimAdd(bigintmat * a, int b); |
---|
185 | bigintmat * bimSub(bigintmat * a, bigintmat * b); |
---|
186 | bigintmat * bimSub(bigintmat * a, int b); |
---|
187 | bigintmat * bimMult(bigintmat * a, bigintmat * b); |
---|
188 | bigintmat * bimMult(bigintmat * a, int b); |
---|
189 | bigintmat * bimMult(bigintmat * a, number b, const coeffs cf); |
---|
190 | bigintmat * bimCopy(const bigintmat * b); |
---|
191 | |
---|
192 | int getShorter (int * a, int l, int j, int cols, int rows); |
---|
193 | int findLongest(int * a, int length); |
---|
194 | int intArrSum(int * a, int length); |
---|
195 | |
---|
196 | |
---|
197 | // Ungetestet |
---|
198 | static void bimRowContent(bigintmat *bimat, int rowpos, int colpos); |
---|
199 | static void bimReduce(bigintmat *bimat, int rpiv, int colpos, |
---|
200 | int ready, int all); |
---|
201 | |
---|
202 | class intvec; |
---|
203 | intvec * bim2iv(bigintmat * b); |
---|
204 | bigintmat * iv2bim(intvec * b, const coeffs C); |
---|
205 | |
---|
206 | |
---|
207 | #endif // #ifndef BIGINTMAT_H |
---|