File: aring-zzp-ffpack.cpp

package info (click to toggle)
macaulay2 1.21%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 133,096 kB
  • sloc: cpp: 110,377; ansic: 16,306; javascript: 4,193; makefile: 3,821; sh: 3,580; lisp: 764; yacc: 590; xml: 177; python: 140; perl: 114; lex: 65; awk: 3
file content (290 lines) | stat: -rw-r--r-- 7,514 bytes parent folder | download
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
// Copyright 2011 Michael E. Stillman

#include "aring-zzp-ffpack.hpp"
#include "exceptions.hpp"
#include "ringmap.hpp"

namespace M2 {

ARingZZpFFPACK::ARingZZpFFPACK(UTT charact)
    : mFfpackField(FieldType(static_cast<double>(charact))),
      mFfpackRandomIterator(mFfpackField),
      mCharac(charact),
      mDimension(1),
      mGeneratorComputed(false)
{
  assert(FieldType::maxCardinality() >= mCharac);
}

void ARingZZpFFPACK::elem_text_out(buffer &o,
                                   const ElementType elem,
                                   bool print_one,
                                   bool print_plus,
                                   bool print_parens) const
{
  STT a = static_cast<STT>(elem);
  if (a == 1 and not print_one) return;
  if (a > 0 and print_plus) o << "+";
  o << a;
}

/**  @todo Remove this function?  IE: rewrite it?
 //  http://www.johnkerl.org/doc/ffcomp.pdf
 */

ARingZZpFFPACK::ElementType ARingZZpFFPACK::computeGenerator() const
{
  for (UTT currIntElem = 2; currIntElem < mCharac; currIntElem++)
    {
      ElementType currElem;
      set_from_long(currElem, currIntElem);
      bool found = true;
      ElementType tmpElem = currElem;
      for (UTT count = 0; count < mCharac - 2; count++)
        {
          mult(tmpElem, tmpElem, currElem);
          if (is_equal(currElem, tmpElem)) found = false;
        }
      if (found)
        {
          std::cerr << "generator = " << currElem << std::endl;
          return currElem;
        }
    }
  throw exc::internal_error("program logic in ffpack code is wrong if we get to this error");
}

bool ARingZZpFFPACK::is_unit(const ElementType f) const
{
  return not mFfpackField.isZero(f);
}

bool ARingZZpFFPACK::is_zero(const ElementType f) const
{
  return mFfpackField.isZero(f);
}

bool ARingZZpFFPACK::is_equal(const ElementType f, const ElementType g) const
{
  return mFfpackField.areEqual(f, g);
}

/// compare exponents of the used generator
/// @return -1: f < g, 1: f>g; 0: f==g;
int ARingZZpFFPACK::compare_elems(const ElementType f,
                                  const ElementType g) const
{
  if (f < g) return -1;
  if (f > g) return 1;

  return 0;
}

// 'init', 'init_set' functions

void ARingZZpFFPACK::init(ElementType &result) const
{
  //    assert(0 == mFfpackField.zero);
  result = 0;
}

void ARingZZpFFPACK::clear(ElementType &result) const { /* nothing */}
void ARingZZpFFPACK::set_zero(ElementType &result) const
{
  // assert(0 == mFfpackField.zero);
  result = 0;
}

void ARingZZpFFPACK::copy(ElementType &result, const ElementType a) const
{
  result = a;
}

/// @todo possible problem if type UTT is smaller than an int?
void ARingZZpFFPACK::set_from_long(ElementType &result, long a) const
{
  mFfpackField.init(result, a);
}

void ARingZZpFFPACK::set_from_mpz(ElementType &result, mpz_srcptr a) const
{
  unsigned long b = static_cast<UTT>(mpz_fdiv_ui(a, mCharac));
  mFfpackField.init(result, b);
}

bool ARingZZpFFPACK::set_from_mpq(ElementType &result, mpq_srcptr a) const
{
  ElementType n, d;
  set_from_mpz(n, mpq_numref(a));
  set_from_mpz(d, mpq_denref(a));
  if (is_zero(d)) return false;
  divide(result, n, d);
  return true;
}

// arithmetic
void ARingZZpFFPACK::negate(ElementType &result, const ElementType a) const
{
  mFfpackField.neg(result, a);
}

/// if a is zero, the result is 1 , but is that what we expect?
/// I vote for two invert functions, one with this check and one without.(Jakob)
void ARingZZpFFPACK::invert(ElementType &result, const ElementType a) const
{
  if (mFfpackField.isZero(a)) throw exc::division_by_zero_error();
  mFfpackField.inv(result, a);
}

void ARingZZpFFPACK::unsafeInvert(ElementType &result,
                                  const ElementType a) const
{
  assert(not is_zero(a));
  mFfpackField.inv(result, a);
}

void ARingZZpFFPACK::add(ElementType &result,
                         const ElementType a,
                         const ElementType b) const
{
  mFfpackField.add(result, a, b);
}

void ARingZZpFFPACK::subtract(ElementType &result,
                              const ElementType a,
                              const ElementType b) const
{
  mFfpackField.sub(result, a, b);
}

/// @param c[in][out] c = c - a*b
void ARingZZpFFPACK::subtract_multiple(ElementType &c,
                                       const ElementType a,
                                       const ElementType b) const
{
  ElementType nega = a;
  mFfpackField.negin(nega);
  mFfpackField.axpyin(c, nega, b);
}

void ARingZZpFFPACK::mult(ElementType &result,
                          const ElementType a,
                          const ElementType b) const
{
  mFfpackField.mul(result, a, b);
}

void ARingZZpFFPACK::divide(ElementType &result,
                            const ElementType a,
                            const ElementType b) const
{
  if (mFfpackField.isZero(b))
    throw exc::division_by_zero_error();
  mFfpackField.div(result, a, b);
}

void ARingZZpFFPACK::power(ElementType &result,
                           const ElementType a,
                           STT n) const
{
  if (is_zero(a))
    {
      if (n < 0)
        throw exc::division_by_zero_error();
      else if (n == 0)
        set_from_long(result, 1);
      else
        set_zero(result);
      return;
    }
  ElementType base;
  set(base, a);
  if (n < 0)
    {
      invert(base, base);
      n = -n;
    }
  n = n % (mCharac - 1);
  set_from_long(result, 1);
  if (n == 0) return;

  // Now use doubling algorithm
  for (;;)
    {
      if ((n % 2) != 0) mFfpackField.mulin(result, base);  // result *= base
      n >>= 1;
      if (n == 0)
        return;
      else
        mFfpackField.mulin(base, base);  // base = base^2
    }
}

/// @pre ensure that  mFfpackField.cardinality() fits in a unsigned long,
/// otherwise instead of mpz_fdiv_ui a different function has to be called)
void ARingZZpFFPACK::power_mpz(ElementType &result,
                               const ElementType a,
                               mpz_srcptr n) const
{
  if (is_zero(a))
    {
      if (mpz_sgn(n) < 0)
        throw exc::division_by_zero_error();
      else if (mpz_sgn(n) == 0)
        set_from_long(result, 1);
      else
        set_zero(result);
    }
  else
    {
      // a != 0
      STT n1 = static_cast<STT>(mpz_fdiv_ui(n, mFfpackField.cardinality() - 1));
      power(result, a, n1);
    }
}

///@note duplicate code
void ARingZZpFFPACK::swap(ElementType &a, ElementType &b) const
{
  ElementType tmp = a;
  a = b;
  b = tmp;
}

/** @brief returns x,y  s.y.  x*a + y*b == 0.
    if possible, x is set to 1.
    no need to consider the case a==0 or b==0.
*/

void ARingZZpFFPACK::syzygy(const ElementType a,
                            const ElementType b,
                            ElementType &x,
                            ElementType &y) const
{
  // x = mFfpackField.one;
  x = 1.;
  divide(y, a, b);
  negate(y, y);
}

/// @jakob document possible overflow and other nasty things
void ARingZZpFFPACK::random(ElementType &result) const
{
  mFfpackRandomIterator.random(result);
}

void ARingZZpFFPACK::eval(const RingMap *map,
                          const ElementType f,
                          int first_var,  // not used here. See ringmap.cpp
                          ring_elem &result) const
{
  // translate f to map->target()
  long a = static_cast<long>(f);
  result = map->get_ring()->from_long(a);
}
};

// Local Variables:
// compile-command: "make -C $M2BUILDDIR/Macaulay2/e  "
// indent-tabs-mode: nil
// End: