1 | /***************************************** |
---|
2 | * Computer Algebra System SINGULAR * |
---|
3 | *****************************************/ |
---|
4 | /* |
---|
5 | * ABSTRACT: class int64vec: lists/vectors of int64 |
---|
6 | */ |
---|
7 | |
---|
8 | #ifdef HAVE_CONFIG_H |
---|
9 | #include "config.h" |
---|
10 | #endif /* HAVE_CONFIG_H */ |
---|
11 | |
---|
12 | |
---|
13 | #include <misc/int64vec.h> |
---|
14 | #include <misc/intvec.h> |
---|
15 | #include <omalloc/omalloc.h> |
---|
16 | |
---|
17 | /*0 implementation*/ |
---|
18 | |
---|
19 | |
---|
20 | int64vec::int64vec(int64vec* iv) |
---|
21 | { |
---|
22 | row = iv->rows(); |
---|
23 | col = iv->cols(); |
---|
24 | v = (int64 *)omAlloc(sizeof(int64)*row*col); |
---|
25 | for (int i=0; i<row*col; i++) |
---|
26 | { |
---|
27 | v[i] = (*iv)[i]; |
---|
28 | } |
---|
29 | } |
---|
30 | |
---|
31 | int64vec::int64vec(intvec* iv) |
---|
32 | { |
---|
33 | row = iv->rows(); |
---|
34 | col = iv->cols(); |
---|
35 | v = (int64 *)omAlloc(sizeof(int64)*row*col); |
---|
36 | for (int i=0; i<row*col; i++) |
---|
37 | { |
---|
38 | v[i] = (int64)((*iv)[i]); |
---|
39 | } |
---|
40 | } |
---|
41 | |
---|
42 | int64vec::int64vec(int r, int c, int64 init) |
---|
43 | { |
---|
44 | row = r; |
---|
45 | col = c; |
---|
46 | int l = r*c; |
---|
47 | if ((r>0) && (c>0)) |
---|
48 | v = (int64 *)omAlloc(sizeof(int64)*l); |
---|
49 | else |
---|
50 | v = NULL; |
---|
51 | for (int i=0; i<l; i++) |
---|
52 | { |
---|
53 | v[i] = init; |
---|
54 | } |
---|
55 | } |
---|
56 | |
---|
57 | char * int64vec::iv64String(int not_mat, int /*mat*/, int spaces, int dim) |
---|
58 | { |
---|
59 | StringSetS(""); |
---|
60 | if ((col == 1)&&(not_mat)) |
---|
61 | { |
---|
62 | int i=0; |
---|
63 | for (; i<row-1; i++) |
---|
64 | { |
---|
65 | StringAppend("%lld,",v[i]); |
---|
66 | } |
---|
67 | if (i<row) |
---|
68 | { |
---|
69 | StringAppend("%lld",v[i]); |
---|
70 | } |
---|
71 | } |
---|
72 | else |
---|
73 | { |
---|
74 | for (int j=0; j<row; j++) |
---|
75 | { |
---|
76 | if (j<row-1) |
---|
77 | { |
---|
78 | for (int i=0; i<col; i++) |
---|
79 | { |
---|
80 | StringAppend("%lld%c",v[j*col+i],','); |
---|
81 | } |
---|
82 | } |
---|
83 | else |
---|
84 | { |
---|
85 | for (int i=0; i<col; i++) |
---|
86 | { |
---|
87 | StringAppend("%lld%c",v[j*col+i],i<col-1 ? ',' : ' '); |
---|
88 | } |
---|
89 | } |
---|
90 | if (j+1<row) |
---|
91 | { |
---|
92 | if (dim > 1) StringAppendS("\n"); |
---|
93 | if (spaces>0) StringAppend("%-*.*s",spaces,spaces," "); |
---|
94 | } |
---|
95 | } |
---|
96 | } |
---|
97 | return StringEndS(); |
---|
98 | } |
---|
99 | |
---|
100 | char * int64vec::String(int dim) |
---|
101 | { |
---|
102 | return iv64String(0, 0, dim); |
---|
103 | } |
---|
104 | |
---|
105 | void int64vec::show(int notmat,int spaces) |
---|
106 | { |
---|
107 | char *s=iv64String(notmat,spaces); |
---|
108 | if (spaces>0) |
---|
109 | { |
---|
110 | PrintNSpaces(spaces); |
---|
111 | PrintS(s); |
---|
112 | } |
---|
113 | else |
---|
114 | { |
---|
115 | PrintS(s); |
---|
116 | } |
---|
117 | omFree(s); |
---|
118 | } |
---|
119 | |
---|
120 | void int64vec::operator*=(int64 intop) |
---|
121 | { |
---|
122 | for (int i=row*col-1; i>=0; i--) { v[i] *= intop; } |
---|
123 | } |
---|
124 | |
---|
125 | void int64vec::operator/=(int64 intop) |
---|
126 | { |
---|
127 | if (intop == 0) return; |
---|
128 | int64 bb=ABS(intop); |
---|
129 | for (int i=row*col-1; i>=0; i--) |
---|
130 | { |
---|
131 | int64 r=v[i]; |
---|
132 | int64 c=r%bb; |
---|
133 | if (c<0) c+=bb; |
---|
134 | r=(r-c)/intop; |
---|
135 | v[i]=r; |
---|
136 | } |
---|
137 | } |
---|
138 | |
---|
139 | int int64vec::compare(const int64vec* op) const |
---|
140 | { |
---|
141 | if ((col!=1) ||(op->cols()!=1)) |
---|
142 | { |
---|
143 | if((col!=op->cols()) |
---|
144 | || (row!=op->rows())) |
---|
145 | return -2; |
---|
146 | } |
---|
147 | int i; |
---|
148 | for (i=0; i<si_min(length(),op->length()); i++) |
---|
149 | { |
---|
150 | if (v[i] > (*op)[i]) |
---|
151 | return 1; |
---|
152 | if (v[i] < (*op)[i]) |
---|
153 | return -1; |
---|
154 | } |
---|
155 | // this can only happen for int64vec: (i.e. col==1) |
---|
156 | for (; i<row; i++) |
---|
157 | { |
---|
158 | if (v[i] > 0) |
---|
159 | return 1; |
---|
160 | if (v[i] < 0) |
---|
161 | return -1; |
---|
162 | } |
---|
163 | for (; i<op->rows(); i++) |
---|
164 | { |
---|
165 | if (0 > (*op)[i]) |
---|
166 | return 1; |
---|
167 | if (0 < (*op)[i]) |
---|
168 | return -1; |
---|
169 | } |
---|
170 | return 0; |
---|
171 | } |
---|
172 | |
---|
173 | int64vec * iv64Add(int64vec * a, int64vec * b) |
---|
174 | { |
---|
175 | int64vec * iv; |
---|
176 | int64 mn, ma, i; |
---|
177 | if (a->cols() != b->cols()) return NULL; |
---|
178 | mn = si_min(a->rows(),b->rows()); |
---|
179 | ma = si_max(a->rows(),b->rows()); |
---|
180 | if (a->cols() == 1) |
---|
181 | { |
---|
182 | iv = new int64vec(ma); |
---|
183 | for (i=0; i<mn; i++) (*iv)[i] = (*a)[i] + (*b)[i]; |
---|
184 | if (ma > mn) |
---|
185 | { |
---|
186 | if (ma == a->rows()) |
---|
187 | { |
---|
188 | for(i=mn; i<ma; i++) (*iv)[i] = (*a)[i]; |
---|
189 | } |
---|
190 | else |
---|
191 | { |
---|
192 | for(i=mn; i<ma; i++) (*iv)[i] = (*b)[i]; |
---|
193 | } |
---|
194 | } |
---|
195 | return iv; |
---|
196 | } |
---|
197 | if (mn != ma) return NULL; |
---|
198 | iv = new int64vec(a); |
---|
199 | for (i=0; i<mn*a->cols(); i++) { (*iv)[i] += (*b)[i]; } |
---|
200 | return iv; |
---|
201 | } |
---|
202 | |
---|
203 | int64vec * iv64Sub(int64vec * a, int64vec * b) |
---|
204 | { |
---|
205 | int64vec * iv; |
---|
206 | int mn, ma,i; |
---|
207 | if (a->cols() != b->cols()) return NULL; |
---|
208 | mn = si_min(a->rows(),b->rows()); |
---|
209 | ma = si_max(a->rows(),b->rows()); |
---|
210 | if (a->cols() == 1) |
---|
211 | { |
---|
212 | iv = new int64vec(ma); |
---|
213 | for (i=0; i<mn; i++) (*iv)[i] = (*a)[i] - (*b)[i]; |
---|
214 | if (ma > mn) |
---|
215 | { |
---|
216 | if (ma == a->rows()) |
---|
217 | { |
---|
218 | for(i=mn; i<ma; i++) (*iv)[i] = (*a)[i]; |
---|
219 | } |
---|
220 | else |
---|
221 | { |
---|
222 | for(i=mn; i<ma; i++) (*iv)[i] = -(*b)[i]; |
---|
223 | } |
---|
224 | } |
---|
225 | return iv; |
---|
226 | } |
---|
227 | if (mn != ma) return NULL; |
---|
228 | iv = new int64vec(a); |
---|
229 | for (i=0; i<mn*a->cols(); i++) { (*iv)[i] -= (*b)[i]; } |
---|
230 | return iv; |
---|
231 | } |
---|
232 | |
---|
233 | |
---|
234 | /* |
---|
235 | * The following two functions are never used. |
---|
236 | * Remove? |
---|
237 | |
---|
238 | // def. internals |
---|
239 | static int64 iv64Gcd(int, int); |
---|
240 | static int64 iv64L1Norm(intvec *); |
---|
241 | |
---|
242 | |
---|
243 | // The following two functions seem to be never used. Remove? |
---|
244 | static int64 iv64Gcd(int64 a,int64 b) |
---|
245 | { |
---|
246 | int64 x; |
---|
247 | |
---|
248 | if (a<0) a=-a; |
---|
249 | if (b<0) b=-b; |
---|
250 | if (b>a) |
---|
251 | { |
---|
252 | x=b; |
---|
253 | b=a; |
---|
254 | a=x; |
---|
255 | } |
---|
256 | while (b!=0) |
---|
257 | { |
---|
258 | x = a % b; |
---|
259 | a = b; |
---|
260 | b = x; |
---|
261 | } |
---|
262 | return a; |
---|
263 | } |
---|
264 | |
---|
265 | static int64 iv64L1Norm(int64vec *w) |
---|
266 | { |
---|
267 | int i; |
---|
268 | int64 j, s = 0; |
---|
269 | |
---|
270 | for (i=w->rows()-1;i>=0;i--) |
---|
271 | { |
---|
272 | j = (*w)[i]; |
---|
273 | if (j>0) |
---|
274 | s += j; |
---|
275 | else |
---|
276 | s -= j; |
---|
277 | } |
---|
278 | return s; |
---|
279 | } |
---|
280 | */ |
---|