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