Macaulay2 Engine
Loading...
Searching...
No Matches
aring-zzp.cpp
Go to the documentation of this file.
1// Copyright 2011 Michael E. Stillman
2
3#include "aring-zzp.hpp"
4#include "ringmap.hpp"
5
6namespace M2 {
8{
9 int i, j, q;
10
11 int prim_root;
12 if (P == 2)
13 prim_root = 1;
14 else
15 {
16 j = 1;
17 for (i = 2; (i < P && j < P - 1); i++)
18 for (q = i, j = 1; (q != 1 && j < P); q = (q * i) % P, j++)
19 ;
20 prim_root = i - 1;
21 }
22 return prim_root;
23}
24
26{
27 int i, n;
28
30
33
34 for (i = 0, n = 1; i < p - 1; i++, n = (n * prim_root) % p)
35 {
36 log_table[n] = i; // i = log_(base _prim_root)(n)
37 exp_table[i] = n; // n = (_prim_root)^i
38 }
39 exp_table[p1] = 1;
40 exp_table[0] = 0;
41 log_table[1] = p1;
42 log_table[0] = 0;
43
44#if 0
45 fprintf(stderr, "char %d primitive %d\n", p, prim_root);
46 fprintf(stderr, "exp: ");
47 for (int i=0; i<p; i++)
48 fprintf(stderr, "%d ", exp_table[i]);
49 fprintf(stderr, "\nlog: ");
50 for (int i=0; i<p; i++)
51 fprintf(stderr, "%d ", log_table[i]);
52 fprintf(stderr, "\n");
53#endif
54}
55
56ARingZZp::ARingZZp(size_t p0) : charac(p0), p(static_cast<int>(p0)), p1(p - 1)
57{
58 if (p == 2)
59 minus_one = 1;
60 else
61 minus_one = (p - 1) / 2;
62
64}
65
71
72void ARingZZp::text_out(buffer &o) const { o << "AZZ/" << characteristic(); }
73
76 bool p_one,
77 bool p_plus,
78 bool p_parens) const
79{
80 (void) p_parens;
81 long n = coerceToLongInteger(a);
82 if (n < 0)
83 {
84 o << '-';
85 n = -n;
86 }
87 else if (p_plus)
88 o << '+';
89 if (p_one || n != 1) o << n;
90}
91
92void ARingZZp::eval(const RingMap *map,
93 const elem f,
94 int first_var,
95 ring_elem &result) const
96{
97 (void) first_var;
98 long a = coerceToLongInteger(f);
99 result = map->get_ring()->from_long(a);
100}
101};
102
103// Local Variables:
104// compile-command: "make -C $M2BUILDDIR/Macaulay2/e "
105// indent-tabs-mode: nil
106// End:
M2::ARingZZp — portable Z/p for small primes via log / exp tables.
static int findPrimitiveRoot(int P)
Definition aring-zzp.cpp:7
ARingZZp(size_t prime)
Definition aring-zzp.cpp:56
size_t characteristic() const
Definition aring-zzp.hpp:90
void elem_text_out(buffer &o, ElementType a, bool p_one=true, bool p_plus=false, bool p_parens=false) const
Definition aring-zzp.cpp:74
void text_out(buffer &o) const
Definition aring-zzp.cpp:72
long coerceToLongInteger(const elem &f) const
void initialize_tables()
Definition aring-zzp.cpp:25
void eval(const RingMap *map, const elem f, int first_var, ring_elem &result) const
Definition aring-zzp.cpp:92
virtual ring_elem from_long(long n) const =0
const Ring * get_ring() const
Definition ringmap.hpp:111
Engine-side ring homomorphism: stores, for each source-ring variable, the target-ring element it maps...
Definition ringmap.hpp:60
void freemem(void *s)
Definition m2-mem.cpp:103
VALGRIND_MAKE_MEM_DEFINED & result(result)
Definition aring-CC.cpp:3
#define newarray_atomic(T, len)
Definition newdelete.hpp:91
RingMap — engine representation of a ring homomorphism.