Macaulay2 Engine
Loading...
Searching...
No Matches
ARingRRRTest.cpp
Go to the documentation of this file.
1// Copyright 2012-2013 Michael E. Stillman
2
33
34#include <cstdio>
35#include <string>
36#include <iostream>
37#include <sstream>
38#include <memory>
39#include <gtest/gtest.h>
40#include <mpfr.h>
41
42#include "aring-RRR.hpp"
43#include "ARingTest.hpp"
44
46 int nbits,
49{
50 mpfr_t epsilon;
51 mpfr_init2(epsilon, R.get_precision());
52 mpfr_set_ui_2exp(epsilon, 1, -nbits, MPFR_RNDN);
53
55 R.init(c);
56 R.subtract(c, a, b);
57 bool ret = mpfr_cmpabs(&c, epsilon) < 0;
58
59 R.clear(c);
60 mpfr_clear(epsilon);
61 return ret;
62}
63
64template <>
66 int index,
68{
69 if (index < 50)
70 R.set_from_long(result, index - 25);
71 else
72 R.random(result);
73}
74
75// void getElementRRR(const M2::ARingRRR& R, int index,
76// M2::ARingRRR::ElementType& result)
77//{
78// if (index < 50) R.set_from_long(result, index-25);
79// else R.random(result);
80//}
81
82TEST(ARingRRR, create)
83{
84 M2::ARingRRR R(100);
85 EXPECT_EQ(ringName(R), "ARRR_100");
86 EXPECT_EQ(R.characteristic(), 0);
87}
88
90{
93 R.init(a);
94 R.init(b);
95 R.init(c);
96 for (int i = 0; i < ntrials; i++)
97 {
98 // test: (-a) + (a) == 0
99 gen.nextElement(a);
100 R.negate(b, a);
101 R.add(c, a, b);
102 EXPECT_TRUE(R.is_zero(c));
103 }
104 R.clear(c);
105 R.clear(b);
106 R.clear(a);
107}
108
109TEST(ARingRRR, negate)
110{
111 M2::ARingRRR R(100);
113}
114
115TEST(ARingRRR, add)
116{
117 M2::ARingRRR R(100);
119 M2::ARingRRR::ElementType a, b, c, d, e;
120 R.init(a);
121 R.init(b);
122 R.init(c);
123 R.init(d);
124 R.init(e);
125 for (int i = 0; i < ntrials; i++)
126 {
127 // test: (a+b) + (-b) == a
128 gen.nextElement(a);
129 gen.nextElement(b);
130 R.add(c, a, b);
131 R.negate(d, b);
132 R.add(e, c, d); // should be a
133 EXPECT_TRUE(almostEqual(R, 98, a, e));
134 }
135 R.clear(e);
136 R.clear(d);
137 R.clear(c);
138 R.clear(b);
139 R.clear(a);
140}
141
142TEST(ARingRRR, subtract)
143{
144 M2::ARingRRR R(100);
146 M2::ARingRRR::ElementType a, b, c, e;
147 R.init(a);
148 R.init(b);
149 R.init(c);
150 R.init(e);
151 for (int i = 0; i < ntrials; i++)
152 {
153 // test: (a-b) + (b) == a
154 gen.nextElement(a);
155 gen.nextElement(b);
156 R.subtract(c, a, b);
157 R.add(e, c, b); // should be a
158 EXPECT_TRUE(almostEqual(R, 98, a, e));
159 R.mult(e, a, b);
160 R.subtract_multiple(e, a, b);
161 EXPECT_TRUE(R.is_zero(e));
162 }
163 R.clear(e);
164 R.clear(c);
165 R.clear(b);
166 R.clear(a);
167}
168
169TEST(ARingRRR, multDivide)
170{
171 M2::ARingRRR R(100);
173 M2::ARingRRR::ElementType a, b, c, d;
174 R.init(a);
175 R.init(b);
176 R.init(c);
177 R.init(d);
178 for (int i = 0; i < ntrials; i++)
179 {
180 // test: (a*b) // b == a
181 gen.nextElement(a);
182 gen.nextElement(b);
183 R.mult(c, a, b);
184 if (R.is_zero(b))
185 EXPECT_TRUE(R.is_zero(c));
186 else
187 {
188 R.divide(d, c, b);
189 EXPECT_TRUE(almostEqual(R, 94, d, a));
190 }
191 }
192 R.clear(d);
193 R.clear(c);
194 R.clear(b);
195 R.clear(a);
196}
197
198TEST(ARingRRR, axioms)
199{
200 M2::ARingRRR R(100);
202 M2::ARingRRR::ElementType a, b, c, d, e;
203 R.init(a);
204 R.init(b);
205 R.init(c);
206 R.init(d);
207 R.init(e);
208 for (int i = 0; i < ntrials; i++)
209 {
210 gen.nextElement(a);
211 gen.nextElement(b);
212 gen.nextElement(c);
213 // Test commutativity
214 // test: a*b = b*a
215 // test: a+b == b+a
216 R.add(d, a, b);
217 R.add(e, b, a);
218 EXPECT_TRUE(almostEqual(R, 98, d, e));
219 R.mult(d, a, b);
220 R.mult(e, b, a);
221 EXPECT_TRUE(almostEqual(R, 98, d, e));
222
223 // Test associativity
224 // test: a+(b+c) == (a+b)+c
225 // test: a*(b*c) == (a*b)*c
226 R.add(e, b, c);
227 R.add(d, a, e); // a+(b+c)
228 R.add(e, a, b);
229 R.add(e, e, c); // (a+b)+c
230 EXPECT_TRUE(almostEqual(R, 94, d, e));
231 R.mult(e, b, c);
232 R.mult(d, a, e); // a*(b*c)
233 R.mult(e, a, b);
234 R.mult(e, e, c); // (a*b)*c
235 EXPECT_TRUE(almostEqual(R, 94, d, e));
236
237 // Test distributivity
238 // test: a*(b+c) == a*b + a*c
239 R.add(e, b, c);
240 R.mult(d, a, e); // a*(b+c)
241 R.mult(b, a, b);
242 R.mult(c, a, c);
243 R.add(e, b, c); // a*b + a*c
244 EXPECT_TRUE(almostEqual(R, 94, d, e));
245 }
246 R.clear(e);
247 R.clear(d);
248 R.clear(c);
249 R.clear(b);
250 R.clear(a);
251}
252
253TEST(ARingRRR, power_and_invert)
254{
255 M2::ARingRRR R(100);
257 M2::ARingRRR::ElementType a, b, c, d;
258 R.init(a);
259 R.init(b);
260 R.init(c);
261 R.init(d);
262 mpz_t gmp1;
263 mpz_init(gmp1);
264 for (int i = 0; i < ntrials; i++)
265 {
266 gen.nextElement(a);
267 // TODO: what should the answer here be?
268 // EXPECT_TRUE(R->is_equal(R->power(a, 0), R->one())); // 0^0 == 1 too?
269 R.power(b, a, 1);
270 EXPECT_TRUE(R.is_equal(b, a));
271
272 int e1 = rawRandomInt(10) + 1;
273 int e2 = rawRandomInt(10) + 1;
274 R.power(b, a, e1);
275 R.power(c, a, e2);
276 R.power(d, a, e1 + e2);
277 R.mult(c, b, c);
278 EXPECT_TRUE(almostEqual(R, 96, c, d));
279
280 // Make sure that powers via mpz work (at least for small exponents)
281 mpz_set_si(gmp1, e1);
282 R.power_mpz(d, a, gmp1);
283 EXPECT_TRUE(R.is_equal(d, b));
284 }
285 mpz_clear(gmp1);
286 R.clear(d);
287 R.clear(c);
288 R.clear(b);
289 R.clear(a);
290}
291
292// TODO: syzygy?
293
294// Local Variables:
295// compile-command: "make -C $M2BUILDDIR/Macaulay2/e/unit-tests check "
296// indent-tabs-mode: nil
297// End:
void getElement< M2::ARingRRR >(const M2::ARingRRR &R, int index, M2::ARingRRR::ElementType &result)
void testRingNegateRRR(const M2::ARingRRR &R, int ntrials)
TEST(ARingRRR, create)
bool almostEqual(const M2::ARingRRR &R, int nbits, const M2::ARingRRR::ElementType &a, const M2::ARingRRR::ElementType &b)
const int ntrials
Definition ARingTest.hpp:42
std::string ringName(const T &R)
Shared gtest harness for the ARing*Test.cpp suite.
M2::ARingRRR — arbitrary-precision real numbers backed by MPFR.
void nextElement(typename RingType::ElementType &result)
Definition ARingTest.hpp:56
size_t characteristic() const
Definition aring-RRR.hpp:81
void power_mpz(ElementType &result, const ElementType &a, mpz_srcptr n) const
void random(ElementType &result) const
void negate(ElementType &result, const ElementType &a) const
bool is_equal(const ElementType &f, const ElementType &g) const
Definition aring-RRR.hpp:97
void subtract_multiple(ElementType &result, const ElementType &a, const ElementType &b) const
bool is_zero(const ElementType &f) const
Definition aring-RRR.hpp:96
void set_from_long(ElementType &result, long a) const
void divide(ElementType &result, const ElementType &a, const ElementType &b) const
void subtract(ElementType &result, const ElementType &a, const ElementType &b) const
void mult(ElementType &result, const ElementType &a, const ElementType &b) const
static void clear(ElementType &result)
void power(ElementType &result, const ElementType &a, int n) const
void init(ElementType &result) const
void add(ElementType &result, const ElementType &a, const ElementType &b) const
unsigned long get_precision() const
Definition aring-RRR.hpp:82
aring-style adapter for arbitrary-precision real numbers, backed by MPFR.
Definition aring-RRR.hpp:70
void subtract(int &result, int a, int b)
VALGRIND_MAKE_MEM_DEFINED & result(result)
int32_t rawRandomInt(int32_t max)
Definition random.cpp:44