source: git/kernel/int64vec.cc @ 0f7b79

spielwiese
Last change on this file since 0f7b79 was fc5095, checked in by Hans Schönemann <hannes@…>, 19 years ago
*hannes: frwalk/walk stuff git-svn-id: file:///usr/local/Singular/svn/trunk@8041 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 6.4 KB
Line 
1/*****************************************
2*  Computer Algebra System SINGULAR      *
3*****************************************/
4/* $Id: int64vec.cc,v 1.1 2005-05-04 14:08:54 Singular Exp $ */
5/*
6* ABSTRACT: class int64vec: lists/vectors of int64
7*/
8#include "mod2.h"
9#include "structs.h"
10#include "febase.h"
11#include "int64vec.h"
12#include "intvec.h"
13#include "omalloc.h"
14
15/*0 implementation*/
16
17
18int64vec::int64vec(int64vec* iv)
19{
20  row = iv->rows();
21  col = iv->cols();
22  v   = (int64 *)omAlloc(sizeof(int64)*row*col);
23  for (int i=0; i<row*col; i++)
24  {
25    v[i] = (*iv)[i];
26  }
27}
28
29int64vec::int64vec(intvec* iv)
30{
31  row = iv->rows();
32  col = iv->cols();
33  v   = (int64 *)omAlloc(sizeof(int64)*row*col);
34  for (int i=0; i<row*col; i++)
35  {
36    v[i] = (int64)((*iv)[i]);
37  }
38}
39
40int64vec::int64vec(int s, int e)
41{
42  int inc;
43  col = 1;
44  if (s<e)
45  {
46    row =  e-s+1;
47    inc =  1;
48  }
49  else
50  {
51    row = s-e+1;
52    inc = -1;
53  }
54  v = (int64 *)omAlloc(sizeof(int64)*row);
55  for (int i=0; i<row; i++)
56  {
57    v[i] = (int64)s;
58    s+=inc;
59  }
60}
61
62int64vec::int64vec(int r, int c, int64 init)
63{
64  row = r;
65  col = c;
66  int l = r*c;
67  if ((r>0) && (c>0))
68    v = (int64 *)omAlloc(sizeof(int64)*l);
69  else
70    v = NULL;
71  for (int i=0; i<l; i++)
72  {
73    v[i] = init;
74  }
75}
76
77char * int64vec::iv64String(int not_mat,int mat,int spaces, int dim)
78{
79  StringSetS("");
80  if ((col == 1)&&(not_mat))
81  {
82    int i=0;
83    for (; i<row-1; i++)
84    {
85      StringAppend("%lld,",v[i]);
86    }
87    if (i<row)
88    {
89      StringAppend("%lld",v[i]);
90    }
91  }
92  else
93  {
94    for (int j=0; j<row; j++)
95    {
96      if (j<row-1)
97      {
98        for (int i=0; i<col; i++)
99        {
100          StringAppend("%lld%c",v[j*col+i],',');
101        }
102      }
103      else
104      {
105        for (int i=0; i<col; i++)
106        {
107          StringAppend("%lld%c",v[j*col+i],i<col-1 ? ',' : ' ');
108        }
109      }
110      if (j+1<row)
111      {
112        if (dim > 1) StringAppendS("\n");
113        if (spaces>0) StringAppend("%-*.*s",spaces,spaces," ");
114      }
115    }
116  }
117  return StringAppendS("");
118}
119
120void int64vec::resize(int new_length)
121{
122  assume(new_length > 0 && col == 1);
123  v = (int64*) omRealloc0Size(v, row*sizeof(int64), new_length*sizeof(int64));
124  row = new_length;
125}
126
127char * int64vec::String(int dim)
128{
129  return omStrDup(iv64String(0, 0, dim));
130}
131
132void int64vec::show(int mat,int spaces)
133{
134  if (spaces>0)
135    Print("%-*.*s%s",spaces,spaces," ",iv64String(mat,spaces));
136  else
137    PrintS(iv64String(mat,0));
138}
139
140void int64vec::operator+=(int64 intop)
141{
142  for (int i=0; i<row*col; i++) { v[i] += intop; }
143}
144
145void int64vec::operator-=(int64 intop)
146{
147  for (int i=0; i<row*col; i++) { v[i] -= intop; }
148}
149
150void int64vec::operator*=(int64 intop)
151{
152  for (int i=0; i<row*col; i++) { v[i] *= intop; }
153}
154
155void int64vec::operator/=(int64 intop)
156{
157  if (intop == 0) return;
158  int64 bb=ABS(intop);
159  for (int i=0; i<row*col; i++)
160  {
161    int64 r=v[i];
162    int64 c=r%bb;
163    if (c<0) c+=bb;
164    r=(r-c)/intop;
165    v[i]=r;
166  }
167}
168
169void int64vec::operator%=(int64 intop)
170{
171  if (intop == 0) return;
172  int64 bb=ABS(intop);
173  for (int i=0; i<row*col; i++)
174  {
175    int64 r=v[i];
176    int64 c=r%bb;
177    if (c<0) c+=bb;
178    v[i]=c;
179  }
180}
181
182int int64vec::compare(int64vec* op)
183{
184  if ((col!=1) ||(op->cols()!=1))
185  {
186    if((col!=op->cols())
187    || (row!=op->rows()))
188      return -2;
189  }
190  int i;
191  for (i=0; i<si_min(length(),op->length()); i++)
192  {
193    if (v[i] > (*op)[i])
194      return 1;
195    if (v[i] < (*op)[i])
196      return -1;
197  }
198  // this can only happen for int64vec: (i.e. col==1)
199  for (; i<row; i++)
200  {
201    if (v[i] > 0)
202      return 1;
203    if (v[i] < 0)
204      return -1;
205  }
206  for (; i<op->rows(); i++)
207  {
208    if (0 > (*op)[i])
209      return 1;
210    if (0 < (*op)[i])
211      return -1;
212  }
213  return 0;
214}
215int int64vec::compare(int64 o)
216{
217  for (int i=0; i<row*col; i++)
218  {
219    if (v[i] <o) return -1;
220    if (v[i] >o) return 1;
221  }
222  return 0;
223}
224
225int64vec * iv64Copy(int64vec * o)
226{
227  int64vec * iv=new int64vec(o);
228  return iv;
229}
230
231int64vec * iv64Add(int64vec * a, int64vec * b)
232{
233  int64vec * iv;
234  int64 mn, ma, i;
235  if (a->cols() != b->cols()) return NULL;
236  mn = si_min(a->rows(),b->rows());
237  ma = si_max(a->rows(),b->rows());
238  if (a->cols() == 1)
239  {
240    iv = new int64vec(ma);
241    for (i=0; i<mn; i++) (*iv)[i] = (*a)[i] + (*b)[i];
242    if (ma > mn)
243    {
244      if (ma == a->rows())
245      {
246        for(i=mn; i<ma; i++) (*iv)[i] = (*a)[i];
247      }
248      else
249      {
250        for(i=mn; i<ma; i++) (*iv)[i] = (*b)[i];
251      }
252    }
253    return iv;
254  }
255  if (mn != ma) return NULL;
256  iv = new int64vec(a);
257  for (i=0; i<mn*a->cols(); i++) { (*iv)[i] += (*b)[i]; }
258  return iv;
259}
260
261int64vec * iv64Sub(int64vec * a, int64vec * b)
262{
263  int64vec * iv;
264  int mn, ma,i;
265  if (a->cols() != b->cols()) return NULL;
266  mn = si_min(a->rows(),b->rows());
267  ma = si_max(a->rows(),b->rows());
268  if (a->cols() == 1)
269  {
270    iv = new int64vec(ma);
271    for (i=0; i<mn; i++) (*iv)[i] = (*a)[i] - (*b)[i];
272    if (ma > mn)
273    {
274      if (ma == a->rows())
275      {
276        for(i=mn; i<ma; i++) (*iv)[i] = (*a)[i];
277      }
278      else
279      {
280        for(i=mn; i<ma; i++) (*iv)[i] = -(*b)[i];
281      }
282    }
283    return iv;
284  }
285  if (mn != ma) return NULL;
286  iv = new int64vec(a);
287  for (i=0; i<mn*a->cols(); i++) { (*iv)[i] -= (*b)[i]; }
288  return iv;
289}
290
291int64vec * iv64Tranp(int64vec * o)
292{
293  int i, j, r = o->rows(), c = o->cols();
294  int64vec * iv= new int64vec(c, r, 0);
295  for (i=0; i<r; i++)
296  {
297    for (j=0; j<c; j++)
298      (*iv)[j*r+i] = (*o)[i*c+j];
299  }
300  return iv;
301}
302
303int64 iv64Trace(int64vec * o)
304{
305  int i, m = si_min(o->rows(),o->cols()), c = o->cols();
306  int64 s = 0;
307  for (i=0; i<m; i++)
308  {
309    s += (*o)[i*c+i];
310  }
311  return s;
312}
313
314int64vec * iv64Mult(int64vec * a, int64vec * b)
315{
316  int i, j, k,
317      ra = a->rows(), ca = a->cols(),
318      rb = b->rows(), cb = b->cols();
319  int64 sum;
320  int64vec * iv;
321  if (ca != rb) return NULL;
322  iv = new int64vec(ra, cb, 0);
323  for (i=0; i<ra; i++)
324  {
325    for (j=0; j<cb; j++)
326    {
327      sum = 0;
328      for (k=0; k<ca; k++)
329        sum += (*a)[i*ca+k]*(*b)[k*cb+j];
330      (*iv)[i*cb+j] = sum;
331    }
332  }
333  return iv;
334}
335
336/* def. internals */
337static int64 iv64Gcd(int, int);
338static int64 iv64L1Norm(intvec *);
339
340static int64 iv64Gcd(int64 a,int64 b)
341{
342  int64 x;
343
344  if (a<0) a=-a;
345  if (b<0) b=-b;
346  if (b>a)
347  {
348    x=b;
349    b=a;
350    a=x;
351  }
352  while (b!=0)
353  {
354    x = a % b;
355    a = b;
356    b = x;
357  }
358  return a;
359}
360
361static int64 iv64L1Norm(int64vec *w)
362{
363  int i;
364  int64 j, s = 0;
365
366  for (i=w->rows()-1;i>=0;i--)
367  {
368    j = (*w)[i];
369    if (j>0)
370      s += j;
371    else
372      s -= j;
373  }
374  return s;
375}
376
Note: See TracBrowser for help on using the repository browser.