File: vec.cc

package info (click to toggle)
eclib 20250122-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 5,916 kB
  • sloc: cpp: 45,414; makefile: 272; sh: 127
file content (362 lines) | stat: -rw-r--r-- 9,957 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
// vec.cc: implementation of integer vector classes
//////////////////////////////////////////////////////////////////////////
//
// Copyright 1990-2023 John Cremona
// 
// This file is part of the eclib package.
// 
// eclib is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation; either version 2 of the License, or (at your
// option) any later version.
// 
// eclib is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
// for more details.
// 
// You should have received a copy of the GNU General Public License
// along with eclib; if not, write to the Free Software Foundation,
// Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
// 
//////////////////////////////////////////////////////////////////////////
 
// Only to be included by vector.cc

// Definitions of member operators and functions:

vec::vec(long n)
{
  entries.resize(n, scalar(0));
}

vec::vec(const vector<scalar>& arr) :entries(arr) {}

vec::vec(const vec& v) :entries(v.entries) {} // copy constructor

void vec::init(long n)                 // (re)-initializes
{
  entries.resize(n, scalar(0));
}

vec& vec::operator=(const vec& v)                    // assignment
{
 if (this==&v) return *this;
 entries = v.entries;
 return *this;
}

scalar& vec::operator[](long i)
{
  return entries.at(i-1);
}

scalar vec::operator[](long i) const
{
  return entries.at(i-1);
}

vec& vec::operator+=(const vec& w)
{
  std::transform(w.entries.begin(), w.entries.end(), entries.begin(), entries.begin(),
                 [](const scalar& wi, const scalar& vi) { return vi + wi;});
  return *this;
}

void vec::addmodp(const vec& w, const scalar& pr)
{
  std::transform(w.entries.begin(), w.entries.end(), entries.begin(), entries.begin(),
                 [pr](const scalar& wi, const scalar& vi) { return mod(wi+vi,pr);});
}

vec& vec::operator-=(const vec& w)
{
  std::transform(w.entries.begin(), w.entries.end(), entries.begin(), entries.begin(),
                 [](const scalar& wi, const scalar& vi) { return vi - wi;});
  return *this;
}

vec& vec::operator*=(const scalar& scal)
{
  std::transform(entries.begin(), entries.end(), entries.begin(),
                 [scal](const scalar& vi) {return vi * scal;});
  return *this;
}

vec& vec::operator/=(const scalar& scal)
{
  std::transform(entries.begin(), entries.end(), entries.begin(),
                 [scal](const scalar& vi) {return vi / scal;});
  return *this;
}

vec vec::slice(long first, long last) const       // returns subvector
{
 if (last==-1) {last=first; first=1;}
 vec ans(last-first+1);
 std::copy(entries.begin()+first-1, entries.begin()+last, ans.entries.begin());
 return ans;
}

vec vec::operator[](const vec_i& index) const  // returns v[index[j]]
{
  vec w(dim(index));
  const vector<int>& vi = index.get_entries();
  std::transform(vi.begin(), vi.end(), w.entries.begin(),
                 [this](const int& i) {return entries.at(i-1);});
  return w;
}

vec vec::operator[](const vec_l& index) const  // returns v[index[j]]
{
  vec w(dim(index));
  const vector<long>& vi = index.get_entries();
  std::transform(vi.begin(), vi.end(), w.entries.begin(),
                 [this](const int& i) {return entries.at(i-1);});
  return w;
}

scalar vec::sub(long i) const
{
  return entries.at(i-1);
}

void vec::set(long i, const scalar& x)
{
  entries.at(i-1) = x;
}

void vec::add(long i, const scalar& x)
{
  entries.at(i-1) += x;
}

void vec::add_modp(long i, const scalar& x, const scalar& p)
{
  entries.at(i-1) = mod(entries.at(i-1)+x,p);
}

void vec::red_modp(const scalar& p)
{
  if (p==0) return;
  std::transform(entries.begin(), entries.end(), entries.begin(),
                 [p](const scalar& vi) {return mod(vi,p);});
}

vec vec::iota(long n)
{
  vec v(n);
  std::iota(v.entries.begin(), v.entries.end(), scalar(1));
  return v;
}

// Definitions of non-member, friend operators and functions

scalar operator*(const vec& v, const vec& w)
{
  return std::inner_product(v.entries.begin(), v.entries.end(), w.entries.begin(), scalar(0));
}

int operator==(const vec& v, const vec& w)
{
  return v.entries == w.entries;
}

int trivial(const vec& v)
{
  return std::all_of(v.entries.begin(), v.entries.end(), [](const scalar& vi) {return vi==0;});
}

ostream& operator<<(ostream& s, const vec& v)
{
  s << "[";
  long i=0;
  for ( const auto& vi : v.entries)
    {
      if(i++)
        s<<",";
      s<<vi;
    }
  s << "]";
  return s;
}

istream& operator>>(istream& s, vec& v)
{
  for (scalar& vi : v.entries)
    s>>vi;
  return s;
}

// Definition of non-friend operators and functions

scalar content(const vec& v)
{
  return v.entries.empty()?
    scalar(1) :
    std::accumulate(v.entries.begin(), v.entries.end(), scalar(0),
                    [](const scalar& x, const scalar& y) {return gcd(x,y);});
}

scalar maxabs(const vec& v)
{
  return v.entries.empty()?
    scalar(0) :
    std::accumulate(v.entries.begin(), v.entries.end(), scalar(0),
                    [](const scalar& x, const scalar& y) {return max(x,abs(y));});
}

void swapvec(vec& v, vec& w)
{
  std::swap(v.entries, w.entries);
}

int member(const scalar& a, const vec& v)
{
  return std::find(v.entries.begin(), v.entries.end(), a) != v.entries.end();
}

vec reverse(const vec& order)
{
  vec ans(order);
  std::reverse(ans.entries.begin(), ans.entries.end());
  return ans;
}

vec express(const vec& v, const vec& v1, const vec& v2)
{
   scalar v1v1 = v1 * v1;
   scalar v1v2 = v1 * v2;
   scalar v2v2 = v2 * v2;
   scalar vv1 = v * v1;
   scalar vv2 = v * v2;
   vec ans({vv1*v2v2 - vv2*v1v2,  vv2*v1v1 - vv1*v1v2, v1v1*v2v2 - v1v2*v1v2});
   makeprimitive(ans);
   if (ans[3]*v!=ans[1]*v1+ans[2]*v2)
     cerr << "Error in express: v is not in <v1,v2>"<<endl;
   return ans;
}

//#define DEBUG_LIFT

// int lift(const vec& v, const scalar& pr, vec& w)
// {
//   w = v;
//   w.red_modp(pr);
  
// }

int lift(const vec& v, const scalar& pr, vec& ans)
{
  long i0, i, j, d = dim(v);
  scalar nu, de;
  scalar lim = sqrt(pr>>1)-1;
  scalar maxallowed = 10*lim;
#ifdef DEBUG_LIFT
  cout<<"Lifting vector v = "<<v<<" mod "<<pr<<" (lim = "<<lim<<")"<<endl;
#endif
 // NB We do *not* make cumulative rescalings, since it is possible
 // for an apparently successful modrat reconstruction to give an
 // incorrect denominator.  I have an example with pr=2^30-35 where
 // the correct denominator is 4666 and one entry of the correct
 // primitive scaled vector is 47493 (greater than lim = 23170) but
 // since 47493/4666 = 587037152 = -10193/21607 (mod pr), rational
 // reconstruction returned nu=-10193, de = 21607.  If we kept the
 // (unsuccessful) scaling by 21607, all subsequent numerators would
 // be multiplied by this and we would never succeed.

 // This code allows for some entries to be >lim, and works as long as
 // (1) there is a lift with all entries at most 10*lim, (2) at least
 // one entry has the correct denominator, which is equaivalent to
 // requiring that in the primitive rescaling, there is an entry
 // coprime to the first non-zero entry.

 ans = reduce_modp(v, pr); // starts as a copy, and will be rescaled in place
#ifdef DEBUG_LIFT
  cout<<"After reduce_modp: v = "<<ans<<endl;
#endif
 if (maxabs(ans) <= maxallowed)
   {
#ifdef DEBUG_LIFT
     cout<<"No scaling needed, lift is "<<ans<<endl;
#endif
     return 1;
   }
 scalar vi0, inv_vi0, vi, maxvi(0);
 for(i0=1; i0<=d; i0++)
   {
     // scale so that i0'th entry is 1 mod p, then reduce vector
     // entries mod p to lie in (-p/2,p/2), and find the maximum
     // entry:
     while((vi0=ans[i0])==0) {i0++;} // skip over any zero entries
     inv_vi0=invmod(vi0,pr);
#ifdef DEBUG_LIFT
     cout<<"Scaling by "<<inv_vi0<<" (inverse of "<<vi0<<")"<<endl;
#endif
     for (i=1; i<=d; i++)
       {
         ans[i]=vi=mod(xmodmul(inv_vi0,ans[i],pr),pr);
         maxvi=max(maxvi,abs(vi));
       }
#ifdef DEBUG_LIFT
     cout<<"Reduced v = "<<ans<<", with max entry "<<maxvi<<endl;
#endif
     if(maxvi<=maxallowed) // no scaling needed!
           {
             // Normalize so first nonzero entry is positive:
             for(i0=1; i0<=d; i0++)
               {
                 while(ans[i0]==0) {i0++;}
                 if(ans[i0]<0) ans=-ans;
                 return 1;
               }
             return 0; // should not happen: means v==0!
           }

     for(i=1; (i<=d); i++)
       {
         modrat(ans[i],pr,nu,de);
         de=abs(de);
         if (de==1) continue; // loop on i
         // scale by de & recompute max entry:
#ifdef DEBUG_LIFT
         cout<<"Scaling by d="<<de<<endl;
#endif
         maxvi = 0;
         for (j=1; j<=d; j++)
           {
             ans[j] = vi = mod(xmodmul(de,ans[j],pr),pr);
             maxvi=max(maxvi,abs(vi));
           }
#ifdef DEBUG_LIFT
         cout<<"Now v = "<<ans<<", with max entry "<<maxvi<<endl;
#endif
         if(maxvi<=maxallowed)
           {
             // Normalize so first nonzero entry is positive:
             for(i0=1; i0<=d; i0++)
               {
                 while(ans[i0]==0) {i0++;}
                 if(ans[i0]<0) ans=-ans;
                 return 1;
               }
             return 0; // should not happen: means v==0!
           }
       }
   }
 // Normalize so first nonzero entry is positive:
 for(i0=1; i0<=d; i0++)
   {
     while(ans[i0]==0) {i0++;}
     if(ans[i0]<0) ans=-ans;
     return (maxvi<=lim);
   }
 return 0;
}

scalar dotmodp(const vec& v1, const vec& v2, const scalar& pr)
{
  auto a = [pr] (const scalar& x, const scalar& y) {return mod(x+y,pr);};
  auto m = [pr] (const scalar& x, const scalar& y) {return xmodmul(x,y,pr);};
  return std::inner_product(v1.entries.begin(), v1.entries.end(), v2.entries.begin(), scalar(0), a, m);
}