File: aring-m2-gf.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 (208 lines) | stat: -rw-r--r-- 5,716 bytes parent folder | download | duplicates (3)
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
// Copyright 2012 Michael E. Stillman

#include <vector>
#include <iostream>

#include "interface/random.h"
#include "relem.hpp"
#include "polyring.hpp"
#include "aring-m2-gf.hpp"
#include "ringmap.hpp"
#include "monoid.hpp"
#include "interrupted.hpp"

extern "C" void dringelem(const Ring *R, const ring_elem f);

namespace M2 {

GaloisFieldTable::GaloisFieldTable(const PolynomialRing &R,
                                   const ring_elem prim)
    : mCharac(static_cast<int>(R.characteristic())),
      mOriginalRing(R),
      mPrimitiveElement(prim)
{
  assert(mOriginalRing.n_quotients() == 1);

  mGenerator = RingElement::make_raw(&R, R.copy(prim));
  ring_elem f = mOriginalRing.quotient_element(0);
  Nterm *t = f;
  mDimension = mOriginalRing.getMonoid()->primary_degree(t->monom);
  mOrder = mCharac;
  for (int i = 1; i < mDimension; i++) mOrder *= mCharac;
  mOne = mOrder - 1;      // representation for the number 1: p^n - 1.
  mOrderMinusOne = mOne;  // p^n - 1
  mMinusOne = (mCharac == 2 ? mOne : mOne / 2);

  // Get ready to create mOneTable.
  VECTOR(ring_elem) polys;
  polys.push_back(mOriginalRing.from_long(0));
  polys.push_back(mOriginalRing.copy(mPrimitiveElement));

  ring_elem oneR = mOriginalRing.from_long(1);

  mGeneratorExponent = static_cast<GFElement>(-1);
  ring_elem x = mOriginalRing.var(0);
  if (mOriginalRing.is_equal(mPrimitiveElement, x)) mGeneratorExponent = 1;
  for (GFElement i = 2; i <= mOne; i++)
    {
      ring_elem g = mOriginalRing.mult(polys[i - 1], mPrimitiveElement);
      polys.push_back(g);
      if (mOriginalRing.is_equal(g, oneR)) break;
      if (mOriginalRing.is_equal(g, x)) mGeneratorExponent = i;
    }

#if 0
    for (size_t i = 0; i < polys.size(); i++)
      {
        std::cerr << i << "  ";
        dringelem(&R, polys[i]);
        std::cerr << "\n";
      }
#endif
  assert(polys.size() == mOrder);
  assert(mGeneratorExponent != static_cast<GFElement>(-1));

  // Set 'one_table'.
  mOneTable = newarray_atomic(GFElement, mOrder);
  mOneTable[0] = mOrderMinusOne;

  for (GFElement i = 1; i <= mOrderMinusOne; i++)
    {
      if (system_interrupted())
        {
          // first clean up?
          return;
        }
      ring_elem f1 = mOriginalRing.add(polys[i], oneR);
      GFElement j;
      for (j = 0; j <= mOrderMinusOne; j++)
        if (mOriginalRing.is_equal(f1, polys[j])) break;
      if (j > mOrderMinusOne)
        {
          std::cout << "oops: didn't find element " << i << " !!" << std::endl;
        }
      mOneTable[i] = j;
    }

  // Create the ZZ/P ---> GF(Q) inclusion map
  mFromIntTable = newarray_atomic(GFElement, mCharac);
  GFElement a = mOne;
  ;
  mFromIntTable[0] = 0;
  for (GFElement i = 1; i < mCharac; i++)
    {
      mFromIntTable[i] = a;
      a = mOneTable[a];
    }
}

void GaloisFieldTable::display(std::ostream &o) const
{
  o << "GF(" << mCharac << "^" << mDimension << ")" << std::endl;
  o << " order = " << mOrder << std::endl;
  o << " 1     = " << mOne << std::endl;
  o << " -1    = " << mMinusOne << std::endl;
  o << " fromZZ: " << std::endl << "    ";
  for (GFElement i = 0; i < mCharac; i++)
    {
      if ((i + 1) % 10 == 0) o << std::endl << "    ";
      o << mFromIntTable[i] << " ";
    }
  o << std::endl << " oneTable: " << std::endl << "    ";
  for (GFElement i = 0; i < mOrder; i++)
    {
      if ((i + 1) % 10 == 0) o << std::endl << "    ";
      o << mOneTable[i] << " ";
    }
  o << std::endl;
}

ARingGFM2::ARingGFM2(const PolynomialRing &R, const ring_elem a) : mGF(R, a)
{
  // Nothing to do here.
}

void ARingGFM2::fromSmallIntegerCoefficients(
    ElementType &result,
    const std::vector<long> &poly) const
{
  result = 0;
  ElementType a, b;
  for (long i = 0; i < poly.size(); i++)
    if (poly[i] != 0)
      {
        set_from_long(a, poly[i]);
        power(b, mGF.generatorExponent(), i);
        mult(a, a, b);
        add(result, result, a);
      }
}

bool ARingGFM2::promote(const Ring *Rf, const ring_elem f, elem &result) const
{
  if (&mGF.ring() != Rf) return false;

  std::vector<long> poly;
  RingElement F(Rf, f);
  F.getSmallIntegerCoefficients(poly);
  fromSmallIntegerCoefficients(result, poly);
  return true;
}

void ARingGFM2::lift_to_original_ring(ring_elem &result,
                                      const ElementType &f) const
{
  if (f == 0)
    result = mGF.ring().from_long(0);
  else if (f == mGF.one())
    result = mGF.ring().from_long(1);
  else
    result = mGF.ring().power(mGF.primitiveElement(), f);
}

bool ARingGFM2::lift(const Ring *Rg, const elem f, ring_elem &result) const
{
  // Rg = Z/p[x]/F(x) ---> GF(p,n)
  // promotion: need to be able to know the value of 'x'.
  // lift: need to compute (primite_element)^e

  if (&mGF.ring() != Rg) return false;

  lift_to_original_ring(result, f);
  return true;
}

void ARingGFM2::eval(const RingMap *map,
                     const elem f,
                     int first_var,
                     ring_elem &result) const
{
  result = map->get_ring()->power(map->elem(first_var), f);
}

void ARingGFM2::text_out(buffer &o) const
{
  o << "GF(" << mGF.characteristic() << "^" << mGF.dimension() << ",M2)";
}

void ARingGFM2::elem_text_out(buffer &o,
                              elem a,
                              bool p_one,
                              bool p_plus,
                              bool p_parens) const
{
  if (a == 0)
    {
      o << "0";
      return;
    }
  ring_elem h = mGF.ring().power(mGF.primitiveElement(), a);
  mGF.ring().elem_text_out(o, h, p_one, p_plus, p_parens);
  mGF.ring().remove(h);
}
}; // namespace M2

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