source: git/libpolys/misc/int64vec.cc @ fe2e01

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