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 omStrDup(iv64String(0, 0, dim)); |
---|
103 | } |
---|
104 | |
---|
105 | void int64vec::show(int mat,int spaces) |
---|
106 | { |
---|
107 | if (spaces>0) |
---|
108 | { |
---|
109 | PrintNSpaces(spaces); |
---|
110 | PrintS(iv64String(mat,spaces)); |
---|
111 | } |
---|
112 | else |
---|
113 | PrintS(iv64String(mat,0)); |
---|
114 | } |
---|
115 | |
---|
116 | void int64vec::operator*=(int64 intop) |
---|
117 | { |
---|
118 | for (int i=row*col-1; i>=0; i--) { v[i] *= intop; } |
---|
119 | } |
---|
120 | |
---|
121 | void int64vec::operator/=(int64 intop) |
---|
122 | { |
---|
123 | if (intop == 0) return; |
---|
124 | int64 bb=ABS(intop); |
---|
125 | for (int i=row*col-1; i>=0; i--) |
---|
126 | { |
---|
127 | int64 r=v[i]; |
---|
128 | int64 c=r%bb; |
---|
129 | if (c<0) c+=bb; |
---|
130 | r=(r-c)/intop; |
---|
131 | v[i]=r; |
---|
132 | } |
---|
133 | } |
---|
134 | |
---|
135 | int int64vec::compare(const int64vec* op) const |
---|
136 | { |
---|
137 | if ((col!=1) ||(op->cols()!=1)) |
---|
138 | { |
---|
139 | if((col!=op->cols()) |
---|
140 | || (row!=op->rows())) |
---|
141 | return -2; |
---|
142 | } |
---|
143 | int i; |
---|
144 | for (i=0; i<si_min(length(),op->length()); i++) |
---|
145 | { |
---|
146 | if (v[i] > (*op)[i]) |
---|
147 | return 1; |
---|
148 | if (v[i] < (*op)[i]) |
---|
149 | return -1; |
---|
150 | } |
---|
151 | // this can only happen for int64vec: (i.e. col==1) |
---|
152 | for (; i<row; i++) |
---|
153 | { |
---|
154 | if (v[i] > 0) |
---|
155 | return 1; |
---|
156 | if (v[i] < 0) |
---|
157 | return -1; |
---|
158 | } |
---|
159 | for (; i<op->rows(); i++) |
---|
160 | { |
---|
161 | if (0 > (*op)[i]) |
---|
162 | return 1; |
---|
163 | if (0 < (*op)[i]) |
---|
164 | return -1; |
---|
165 | } |
---|
166 | return 0; |
---|
167 | } |
---|
168 | |
---|
169 | int64vec * iv64Add(int64vec * a, int64vec * b) |
---|
170 | { |
---|
171 | int64vec * iv; |
---|
172 | int64 mn, ma, i; |
---|
173 | if (a->cols() != b->cols()) return NULL; |
---|
174 | mn = si_min(a->rows(),b->rows()); |
---|
175 | ma = si_max(a->rows(),b->rows()); |
---|
176 | if (a->cols() == 1) |
---|
177 | { |
---|
178 | iv = new int64vec(ma); |
---|
179 | for (i=0; i<mn; i++) (*iv)[i] = (*a)[i] + (*b)[i]; |
---|
180 | if (ma > mn) |
---|
181 | { |
---|
182 | if (ma == a->rows()) |
---|
183 | { |
---|
184 | for(i=mn; i<ma; i++) (*iv)[i] = (*a)[i]; |
---|
185 | } |
---|
186 | else |
---|
187 | { |
---|
188 | for(i=mn; i<ma; i++) (*iv)[i] = (*b)[i]; |
---|
189 | } |
---|
190 | } |
---|
191 | return iv; |
---|
192 | } |
---|
193 | if (mn != ma) return NULL; |
---|
194 | iv = new int64vec(a); |
---|
195 | for (i=0; i<mn*a->cols(); i++) { (*iv)[i] += (*b)[i]; } |
---|
196 | return iv; |
---|
197 | } |
---|
198 | |
---|
199 | int64vec * iv64Sub(int64vec * a, int64vec * b) |
---|
200 | { |
---|
201 | int64vec * iv; |
---|
202 | int mn, ma,i; |
---|
203 | if (a->cols() != b->cols()) return NULL; |
---|
204 | mn = si_min(a->rows(),b->rows()); |
---|
205 | ma = si_max(a->rows(),b->rows()); |
---|
206 | if (a->cols() == 1) |
---|
207 | { |
---|
208 | iv = new int64vec(ma); |
---|
209 | for (i=0; i<mn; i++) (*iv)[i] = (*a)[i] - (*b)[i]; |
---|
210 | if (ma > mn) |
---|
211 | { |
---|
212 | if (ma == a->rows()) |
---|
213 | { |
---|
214 | for(i=mn; i<ma; i++) (*iv)[i] = (*a)[i]; |
---|
215 | } |
---|
216 | else |
---|
217 | { |
---|
218 | for(i=mn; i<ma; i++) (*iv)[i] = -(*b)[i]; |
---|
219 | } |
---|
220 | } |
---|
221 | return iv; |
---|
222 | } |
---|
223 | if (mn != ma) return NULL; |
---|
224 | iv = new int64vec(a); |
---|
225 | for (i=0; i<mn*a->cols(); i++) { (*iv)[i] -= (*b)[i]; } |
---|
226 | return iv; |
---|
227 | } |
---|
228 | |
---|
229 | |
---|
230 | /* |
---|
231 | * The following two functions are never used. |
---|
232 | * Remove? |
---|
233 | |
---|
234 | // def. internals |
---|
235 | static int64 iv64Gcd(int, int); |
---|
236 | static int64 iv64L1Norm(intvec *); |
---|
237 | |
---|
238 | |
---|
239 | // The following two functions seem to be never used. Remove? |
---|
240 | static int64 iv64Gcd(int64 a,int64 b) |
---|
241 | { |
---|
242 | int64 x; |
---|
243 | |
---|
244 | if (a<0) a=-a; |
---|
245 | if (b<0) b=-b; |
---|
246 | if (b>a) |
---|
247 | { |
---|
248 | x=b; |
---|
249 | b=a; |
---|
250 | a=x; |
---|
251 | } |
---|
252 | while (b!=0) |
---|
253 | { |
---|
254 | x = a % b; |
---|
255 | a = b; |
---|
256 | b = x; |
---|
257 | } |
---|
258 | return a; |
---|
259 | } |
---|
260 | |
---|
261 | static int64 iv64L1Norm(int64vec *w) |
---|
262 | { |
---|
263 | int i; |
---|
264 | int64 j, s = 0; |
---|
265 | |
---|
266 | for (i=w->rows()-1;i>=0;i--) |
---|
267 | { |
---|
268 | j = (*w)[i]; |
---|
269 | if (j>0) |
---|
270 | s += j; |
---|
271 | else |
---|
272 | s -= j; |
---|
273 | } |
---|
274 | return s; |
---|
275 | } |
---|
276 | */ |
---|