source: git/kernel/int64vec.cc @ d51339

spielwiese
Last change on this file since d51339 was 058624, checked in by Hans Schönemann <hannes@…>, 16 years ago
*hannes: Print, PrintNSpaces and monitor git-svn-id: file:///usr/local/Singular/svn/trunk@10392 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.4 2007-11-08 09:47:13 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  {
107    PrintNSpaces(spaces);
108    PrintS(iv64String(mat,spaces));
109  }
110  else
111    PrintS(iv64String(mat,0));
112}
113
114void int64vec::operator*=(int64 intop)
115{
116  for (int i=row*col-1; i>=0; i--) { v[i] *= intop; }
117}
118
119void int64vec::operator/=(int64 intop)
120{
121  if (intop == 0) return;
122  int64 bb=ABS(intop);
123  for (int i=row*col-1; i>=0; i--)
124  {
125    int64 r=v[i];
126    int64 c=r%bb;
127    if (c<0) c+=bb;
128    r=(r-c)/intop;
129    v[i]=r;
130  }
131}
132
133int int64vec::compare(int64vec* op)
134{
135  if ((col!=1) ||(op->cols()!=1))
136  {
137    if((col!=op->cols())
138    || (row!=op->rows()))
139      return -2;
140  }
141  int i;
142  for (i=0; i<si_min(length(),op->length()); i++)
143  {
144    if (v[i] > (*op)[i])
145      return 1;
146    if (v[i] < (*op)[i])
147      return -1;
148  }
149  // this can only happen for int64vec: (i.e. col==1)
150  for (; i<row; i++)
151  {
152    if (v[i] > 0)
153      return 1;
154    if (v[i] < 0)
155      return -1;
156  }
157  for (; i<op->rows(); i++)
158  {
159    if (0 > (*op)[i])
160      return 1;
161    if (0 < (*op)[i])
162      return -1;
163  }
164  return 0;
165}
166
167int64vec * iv64Add(int64vec * a, int64vec * b)
168{
169  int64vec * iv;
170  int64 mn, ma, i;
171  if (a->cols() != b->cols()) return NULL;
172  mn = si_min(a->rows(),b->rows());
173  ma = si_max(a->rows(),b->rows());
174  if (a->cols() == 1)
175  {
176    iv = new int64vec(ma);
177    for (i=0; i<mn; i++) (*iv)[i] = (*a)[i] + (*b)[i];
178    if (ma > mn)
179    {
180      if (ma == a->rows())
181      {
182        for(i=mn; i<ma; i++) (*iv)[i] = (*a)[i];
183      }
184      else
185      {
186        for(i=mn; i<ma; i++) (*iv)[i] = (*b)[i];
187      }
188    }
189    return iv;
190  }
191  if (mn != ma) return NULL;
192  iv = new int64vec(a);
193  for (i=0; i<mn*a->cols(); i++) { (*iv)[i] += (*b)[i]; }
194  return iv;
195}
196
197int64vec * iv64Sub(int64vec * a, int64vec * b)
198{
199  int64vec * iv;
200  int mn, ma,i;
201  if (a->cols() != b->cols()) return NULL;
202  mn = si_min(a->rows(),b->rows());
203  ma = si_max(a->rows(),b->rows());
204  if (a->cols() == 1)
205  {
206    iv = new int64vec(ma);
207    for (i=0; i<mn; i++) (*iv)[i] = (*a)[i] - (*b)[i];
208    if (ma > mn)
209    {
210      if (ma == a->rows())
211      {
212        for(i=mn; i<ma; i++) (*iv)[i] = (*a)[i];
213      }
214      else
215      {
216        for(i=mn; i<ma; i++) (*iv)[i] = -(*b)[i];
217      }
218    }
219    return iv;
220  }
221  if (mn != ma) return NULL;
222  iv = new int64vec(a);
223  for (i=0; i<mn*a->cols(); i++) { (*iv)[i] -= (*b)[i]; }
224  return iv;
225}
226
227/* def. internals */
228static int64 iv64Gcd(int, int);
229static int64 iv64L1Norm(intvec *);
230
231static int64 iv64Gcd(int64 a,int64 b)
232{
233  int64 x;
234
235  if (a<0) a=-a;
236  if (b<0) b=-b;
237  if (b>a)
238  {
239    x=b;
240    b=a;
241    a=x;
242  }
243  while (b!=0)
244  {
245    x = a % b;
246    a = b;
247    b = x;
248  }
249  return a;
250}
251
252static int64 iv64L1Norm(int64vec *w)
253{
254  int i;
255  int64 j, s = 0;
256
257  for (i=w->rows()-1;i>=0;i--)
258  {
259    j = (*w)[i];
260    if (j>0)
261      s += j;
262    else
263      s -= j;
264  }
265  return s;
266}
267
Note: See TracBrowser for help on using the repository browser.