Macaulay2 Engine
Loading...
Searching...
No Matches
ARingGFTest.cpp
Go to the documentation of this file.
1// Copyright 2013 Michael E. Stillman
2
32
33#include <cstdio>
34#include <string>
35#include <iostream>
36#include <sstream>
37#include <memory>
38#include <gtest/gtest.h>
39#include <mpfr.h>
40
41#include "debug.hpp"
42#include "aring-gf-flint.hpp"
43#include "ARingTest.hpp"
44
45static const int nelements = 200;
46static int randomVals[nelements] = {
47 2666036, 85344567, 71531106, 8755168, 53852118, 88167705, 22475268,
48 41297550, 91010248, 44033907, 33751711, 95042283, 701143, 62443598,
49 55048281, 17723663, 85240587, 25979106, 47819861, 90244528, 11275250,
50 22798387, 68996632, 76613634, 48760025, 24733508, 27522895, 56224559,
51 66749385, 49236242, 42314935, 56483458, 17870101, 84017847, 4627176,
52 94565033, 93275532, 76351828, 90611837, 77643518, 59276468, 26344784,
53 73201185, 91083238, 85501801, 85345398, 83160965, 89062810, 37815670,
54 8227378, 19118805, 63640713, 84232050, 24326912, 73922619, 70743421,
55 19090900, 59358457, 17634169, 86322817, 37134114, 97886677, 82613353,
56 13603908, 61119326, 40173934, 24107001, 90616769, 72088106, 47299394,
57 97111628, 22494627, 6547534, 68935260, 57921076, 3521860, 45351552,
58 87950309, 87755396, 43573207, 77361055, 97023982, 36678347, 66937610,
59 3708760, 36009386, 84861416, 84983584, 99073162, 25592499, 55121036,
60 217404, 45246974, 87694278, 18631417, 97176622, 66230759, 70817866,
61 48984802, 99776399, 11107075, 83804819, 1210456, 72894434, 36929177,
62 57482178, 26142753, 88954986, 53609557, 80607781, 98680672, 2989714,
63 89692551, 96341984, 70402005, 50025094, 3450389, 24030230, 94134112,
64 85324655, 35404622, 74519773, 85307471, 87613072, 80434774, 55494037,
65 34945135, 36300411, 44911039, 42734386, 69273889, 6537915, 18710819,
66 33093559, 68308135, 29544635, 65475048, 51852879, 85499965, 65398813,
67 61670710, 98961531, 70981648, 24941547, 8504747, 80452330, 45113018,
68 60607115, 65165225, 73257098, 2992669, 51802181, 65378474, 2825238,
69 78916325, 8474068, 2695455, 53942610, 94297201, 37662100, 45567374,
70 30141840, 32491957, 91837138, 28261048, 71359280, 11933270, 16587656,
71 64093661, 12235770, 16195573, 99396594, 35549941, 98074540, 45023021,
72 15205552, 3304979, 34666480, 89209262, 10261916, 35340937, 98935118,
73 77343644, 78522496, 46395773, 35429063, 54767177, 14130046, 2726640,
74 44257782, 31615869, 83095327, 15062803, 92772905, 25189126, 86464567,
75 43372313, 24240507, 96790882, 99639739};
76
77template <>
79 int index,
80 M2::ARingGFGivaro::ElementType& result)
81{
83 R.init(gen);
84 R.getGenerator(gen);
85 if (index >= nelements)
86 R.power(result, gen, rawRandomInt(static_cast<int32_t>(R.cardinality())));
87 else
88 R.power(result, gen, randomVals[index]);
89 R.clear(gen);
90}
91
92TEST(ARingGFFlint, create)
93{
94 M2::ARingGFFlint R(5, 3);
95
96 EXPECT_EQ(ringName(R), "GF(5,3,Flint)");
97 EXPECT_EQ(R.cardinality(), 125);
98 EXPECT_EQ(R.characteristic(), 5);
99
100 M2_arrayint gen_modpoly = R.getModPolynomialCoeffs();
101 std::cout << "minimal polynomial = ";
102 dintarray(gen_modpoly);
103 std::cout << std::endl;
104
105 M2_arrayint gen_coeffs = R.getGeneratorCoeffs();
106 std::cout << "generator polynomial = ";
107 dintarray(gen_coeffs);
108 std::cout << std::endl;
109
110 // Check what values integers go to
112 R.init(a);
113 // for (int i=-5; i<R.characteristic(); i++)
114 for (int i = -130; i < 130; i++)
115 {
116 R.set_from_long(a, i);
117 M2_arrayint coeffs = R.fieldElementToM2Array(a);
118 EXPECT_EQ(coeffs->len, 3);
119 int imodp = i % 5;
120 if (imodp < 0) imodp += 5;
121 EXPECT_EQ(coeffs->array[0], imodp);
122 for (int j = 1; j < 3; j++) EXPECT_EQ(coeffs->array[j], 0);
123 // std::cout << i << " = ";
124 // dintarray(coeffs);
125 // std::cout << std::endl;
126 }
127 R.clear(a);
128
129 testSomeMore(R);
130}
131
132TEST(ARingGFFlint, random)
133{
134 M2::ARingGFFlint R(7, 2);
136 R.init(a);
137 int counts[49];
138 for (int i = 0; i < 49; i++) counts[i] = 0;
139 for (int i = 0; i < 49 * 200; i++)
140 {
141 R.random(a);
142 EXPECT_TRUE(a >= 0);
143 EXPECT_TRUE(a < 49);
144 counts[a]++;
145 }
146#if 0
147 double sum = 0;
148 for (int i=0; i<49; i++)
149 {
150 std::cout << i << " " << counts[i] << std::endl;
151 sum += (counts[i]-200)^2;
152 }
153
154 sum /= (49*200);
155 std::cout << "chi = " << sum << std::endl;
156 for (int i=0; i<49; i++)
157 {
158 a = i;
159 M2_arrayint f = R.fieldElementToM2Array(a);
160 dintarray(f);
161 std::cout << std::endl;
162 }
163#endif
164 R.clear(a);
165}
166
167TEST(ARingGFFlint, arithmetic)
168{
169 M2::ARingGFFlint R(5, 3);
171}
172
173// Local Variables:
174// compile-command: "make -C $M2BUILDDIR/Macaulay2/e/unit-tests check "
175// indent-tabs-mode: nil
176// End:
static const int nelements
void getElement< M2::ARingGFFlint >(const M2::ARingGFFlint &R, int index, M2::ARingGFGivaro::ElementType &result)
TEST(ARingGFFlint, create)
static int randomVals[nelements]
void testFiniteField(const T &R, int ntrials)
const int ntrials
Definition ARingTest.hpp:42
void testSomeMore(const T &R)
Definition ARingTest.hpp:72
std::string ringName(const T &R)
Shared gtest harness for the ARing*Test.cpp suite.
M2::ARingGFFlint — small GF(p^k) via FLINT Zech-logarithm tables.
void getGenerator(ElementType &result_gen) const
fq_zech_struct ElementType
void init(ElementType &result) const
void clear(ElementType &result) const
void power(ElementType &result, const ElementType &a, int n) const
void set_from_long(ElementType &result, long a) const
long characteristic() const
void random(ElementType &result) const
aring-style adapter for FLINT's Zech-logarithm representation of small finite fields GF(p^n).
void dintarray(M2_arrayint a)
Definition debug.cpp:19
Debugger-callable d* helpers that pretty-print engine values to stderr.
VALGRIND_MAKE_MEM_DEFINED & result(result)
int32_t rawRandomInt(int32_t max)
Definition random.cpp:44