source: git/kernel/int64vec.cc @ 5a9e7b

spielwiese
Last change on this file since 5a9e7b was d14343a, checked in by Hans Schönemann <hannes@…>, 19 years ago
*hannes: code cleanup git-svn-id: file:///usr/local/Singular/svn/trunk@8110 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 4.6 KB
Line 
1/*****************************************
2*  Computer Algebra System SINGULAR      *
3*****************************************/
4/* $Id: int64vec.cc,v 1.3 2005-05-09 13:47:29 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 r, int c, int64 init)
41{
42  row = r;
43  col = c;
44  int l = r*c;
45  if ((r>0) && (c>0))
46    v = (int64 *)omAlloc(sizeof(int64)*l);
47  else
48    v = NULL;
49  for (int i=0; i<l; i++)
50  {
51    v[i] = init;
52  }
53}
54
55char * int64vec::iv64String(int not_mat,int mat,int spaces, int dim)
56{
57  StringSetS("");
58  if ((col == 1)&&(not_mat))
59  {
60    int i=0;
61    for (; i<row-1; i++)
62    {
63      StringAppend("%lld,",v[i]);
64    }
65    if (i<row)
66    {
67      StringAppend("%lld",v[i]);
68    }
69  }
70  else
71  {
72    for (int j=0; j<row; j++)
73    {
74      if (j<row-1)
75      {
76        for (int i=0; i<col; i++)
77        {
78          StringAppend("%lld%c",v[j*col+i],',');
79        }
80      }
81      else
82      {
83        for (int i=0; i<col; i++)
84        {
85          StringAppend("%lld%c",v[j*col+i],i<col-1 ? ',' : ' ');
86        }
87      }
88      if (j+1<row)
89      {
90        if (dim > 1) StringAppendS("\n");
91        if (spaces>0) StringAppend("%-*.*s",spaces,spaces," ");
92      }
93    }
94  }
95  return StringAppendS("");
96}
97
98char * int64vec::String(int dim)
99{
100  return omStrDup(iv64String(0, 0, dim));
101}
102
103void int64vec::show(int mat,int spaces)
104{
105  if (spaces>0)
106    Print("%-*.*s%s",spaces,spaces," ",iv64String(mat,spaces));
107  else
108    PrintS(iv64String(mat,0));
109}
110
111void int64vec::operator*=(int64 intop)
112{
113  for (int i=row*col-1; i>=0; i--) { v[i] *= intop; }
114}
115
116void int64vec::operator/=(int64 intop)
117{
118  if (intop == 0) return;
119  int64 bb=ABS(intop);
120  for (int i=row*col-1; i>=0; i--)
121  {
122    int64 r=v[i];
123    int64 c=r%bb;
124    if (c<0) c+=bb;
125    r=(r-c)/intop;
126    v[i]=r;
127  }
128}
129
130int int64vec::compare(int64vec* op)
131{
132  if ((col!=1) ||(op->cols()!=1))
133  {
134    if((col!=op->cols())
135    || (row!=op->rows()))
136      return -2;
137  }
138  int i;
139  for (i=0; i<si_min(length(),op->length()); i++)
140  {
141    if (v[i] > (*op)[i])
142      return 1;
143    if (v[i] < (*op)[i])
144      return -1;
145  }
146  // this can only happen for int64vec: (i.e. col==1)
147  for (; i<row; i++)
148  {
149    if (v[i] > 0)
150      return 1;
151    if (v[i] < 0)
152      return -1;
153  }
154  for (; i<op->rows(); i++)
155  {
156    if (0 > (*op)[i])
157      return 1;
158    if (0 < (*op)[i])
159      return -1;
160  }
161  return 0;
162}
163
164int64vec * iv64Add(int64vec * a, int64vec * b)
165{
166  int64vec * iv;
167  int64 mn, ma, i;
168  if (a->cols() != b->cols()) return NULL;
169  mn = si_min(a->rows(),b->rows());
170  ma = si_max(a->rows(),b->rows());
171  if (a->cols() == 1)
172  {
173    iv = new int64vec(ma);
174    for (i=0; i<mn; i++) (*iv)[i] = (*a)[i] + (*b)[i];
175    if (ma > mn)
176    {
177      if (ma == a->rows())
178      {
179        for(i=mn; i<ma; i++) (*iv)[i] = (*a)[i];
180      }
181      else
182      {
183        for(i=mn; i<ma; i++) (*iv)[i] = (*b)[i];
184      }
185    }
186    return iv;
187  }
188  if (mn != ma) return NULL;
189  iv = new int64vec(a);
190  for (i=0; i<mn*a->cols(); i++) { (*iv)[i] += (*b)[i]; }
191  return iv;
192}
193
194int64vec * iv64Sub(int64vec * a, int64vec * b)
195{
196  int64vec * iv;
197  int mn, ma,i;
198  if (a->cols() != b->cols()) return NULL;
199  mn = si_min(a->rows(),b->rows());
200  ma = si_max(a->rows(),b->rows());
201  if (a->cols() == 1)
202  {
203    iv = new int64vec(ma);
204    for (i=0; i<mn; i++) (*iv)[i] = (*a)[i] - (*b)[i];
205    if (ma > mn)
206    {
207      if (ma == a->rows())
208      {
209        for(i=mn; i<ma; i++) (*iv)[i] = (*a)[i];
210      }
211      else
212      {
213        for(i=mn; i<ma; i++) (*iv)[i] = -(*b)[i];
214      }
215    }
216    return iv;
217  }
218  if (mn != ma) return NULL;
219  iv = new int64vec(a);
220  for (i=0; i<mn*a->cols(); i++) { (*iv)[i] -= (*b)[i]; }
221  return iv;
222}
223
224/* def. internals */
225static int64 iv64Gcd(int, int);
226static int64 iv64L1Norm(intvec *);
227
228static int64 iv64Gcd(int64 a,int64 b)
229{
230  int64 x;
231
232  if (a<0) a=-a;
233  if (b<0) b=-b;
234  if (b>a)
235  {
236    x=b;
237    b=a;
238    a=x;
239  }
240  while (b!=0)
241  {
242    x = a % b;
243    a = b;
244    b = x;
245  }
246  return a;
247}
248
249static int64 iv64L1Norm(int64vec *w)
250{
251  int i;
252  int64 j, s = 0;
253
254  for (i=w->rows()-1;i>=0;i--)
255  {
256    j = (*w)[i];
257    if (j>0)
258      s += j;
259    else
260      s -= j;
261  }
262  return s;
263}
264
Note: See TracBrowser for help on using the repository browser.