Macaulay2 Engine
Loading...
Searching...
No Matches
ARingRRiTest.cpp
Go to the documentation of this file.
1
30
31#include <cstdio>
32#include <string>
33#include <iostream>
34#include <sstream>
35#include <memory>
36#include <gtest/gtest.h>
37#include <mpfr.h>
38
39#include "aring-RRi.hpp"
40#include "aring-RRR.hpp"
41#include "aring-glue.hpp"
42#include "ARingTest.hpp"
43
44// For debugging purposes, use
45//mpfr_printf("a=(%.20Rf,%.20Rf)\n",&(a.left), &(a.right));
46
48 int nbits,
51{
52 mpfr_t epsilon;
53 mpfr_init2(epsilon, R.get_precision());
54 mpfr_set_ui_2exp(epsilon, 1, -nbits, MPFR_RNDN);
55
56 mpfr_t c,d;
57 mpfr_init2(c, R.get_precision());
58 mpfr_init2(d, R.get_precision());
59
60 mpfr_sub(c,&(a.left),&(b.left),MPFR_RNDN);
61 mpfr_sub(d,&(a.right),&(b.right),MPFR_RNDN);
62
63 bool retL = mpfr_cmpabs(c, epsilon) < 0,
64 retR = mpfr_cmpabs(d, epsilon) < 0;
65
66 mpfr_clear(d);
67 mpfr_clear(c);
68 mpfr_clear(epsilon);
69 return retL and retR;
70}
71
72template <>
74 int index,
76{
77 if (index < 50)
78 R.set_from_long(result, index - 25);
79 else
80 R.random(result);
81}
82
83TEST(ARingRRi, create)
84{
85 M2::ARingRRi R(100);
86 EXPECT_EQ(ringName(R), "ARRi_100");
87 EXPECT_EQ(R.characteristic(), 0);
88}
89
91{
94 R.init(a);
95 R.init(b);
96 R.init(c);
97
99 S.init(d);
100 for (int i = 0; i < ntrials; i++)
101 {
102 // test: (-a) + (a) == 0
103 gen.nextElement(a);
104 R.negate(b,a);
105 R.add(c,a,b);
106 R.midpoint(d,c);
107 EXPECT_TRUE(S.is_zero(d));
108 }
109 S.clear(d);
110 R.clear(c);
111 R.clear(b);
112 R.clear(a);
113}
114
115TEST(ARingRRi, negate)
116{
117 M2::ARingRRi R(100);
118 M2::ARingRRR S(100);
120}
121
122TEST(ARingRRi, add)
123{
124 M2::ARingRRi R(100);
126 M2::ARingRRi::ElementType a, b, c, d, e;
127 R.init(a);
128 R.init(b);
129 R.init(c);
130 R.init(d);
131 R.init(e);
132 for (int i = 0; i < ntrials; i++)
133 {
134 // test: (a+b) + (-b) == a
135 gen.nextElement(a);
136 gen.nextElement(b);
137 R.add(c, a, b);
138 R.negate(d, b);
139 R.add(e, c, d); // should be a
140 EXPECT_TRUE(R.is_subset(a,e));
141 }
142 R.clear(e);
143 R.clear(d);
144 R.clear(c);
145 R.clear(b);
146 R.clear(a);
147}
148
149TEST(ARingRRi, subtract)
150{
151 M2::ARingRRi R(100);
152 M2::ARingRRR S(100);
154 M2::ARingRRi::ElementType a, b, c, e;
155 R.init(a);
156 R.init(b);
157 R.init(c);
158 R.init(e);
159
161 S.init(f);
162 for (int i = 0; i < ntrials; i++)
163 {
164 // test: (a-b) + (b) == a
165 gen.nextElement(a);
166 gen.nextElement(b);
167 R.subtract(c, a, b);
168 R.add(e, c, b); // should be a
169 EXPECT_TRUE(R.is_subset(a,e));
170 R.mult(e, a, b);
171 R.subtract_multiple(e, a, b);
172 R.midpoint(f,e);
173 EXPECT_TRUE(S.is_zero(f));
174 }
175 S.clear(f);
176 R.clear(e);
177 R.clear(c);
178 R.clear(b);
179 R.clear(a);
180}
181
182TEST(ARingRRi, multDivide)
183{
184 M2::ARingRRi R(100);
186 M2::ARingRRi::ElementType a, b, c, d;
187 R.init(a);
188 R.init(b);
189 R.init(c);
190 R.init(d);
191 for (int i = 0; i < ntrials; i++)
192 {
193 // test: (a*b) // b == a
194 gen.nextElement(a);
195 gen.nextElement(b);
196 R.mult(c, a, b);
197 if (R.is_member(0L,b))
198 EXPECT_TRUE(R.is_member(0L,c));
199 else
200 {
201 R.divide(d, c, b);
202 EXPECT_TRUE(R.is_subset(a,d));
203 }
204 }
205 R.clear(d);
206 R.clear(c);
207 R.clear(b);
208 R.clear(a);
209}
210
211TEST(ARingRRi, axioms)
212{
213 M2::ARingRRi R(100);
214 M2::ARingRRR S(100);
216 M2::ARingRRi::ElementType a, b, c, d, e;
217 R.init(a);
218 R.init(b);
219 R.init(c);
220 R.init(d);
221 R.init(e);
222
223 for (int i = 0; i < ntrials; i++)
224 {
225 gen.nextElement(a);
226 gen.nextElement(b);
227 gen.nextElement(c);
228 // Test commutativity
229 // test: a*b = b*a
230 // test: a+b == b+a
231 R.add(d, a, b);
232 R.add(e, b, a);
233 EXPECT_TRUE(R.is_equal(d,e));
234 R.mult(d, a, b);
235 R.mult(e, b, a);
236 EXPECT_TRUE(R.is_equal(d,e));
237
238 // Test associativity
239 // test: a+(b+c) == (a+b)+c
240 // test: a*(b*c) == (a*b)*c
241 R.add(e, b, c);
242 R.add(d, a, e); // a+(b+c)
243 R.add(e, a, b);
244 R.add(e, e, c); // (a+b)+c
245 EXPECT_TRUE(almostEqual(R,-94,d,e));
246
247 R.mult(e, b, c);
248 R.mult(d, a, e); // a*(b*c)
249 R.mult(e, a, b);
250 R.mult(e, e, c); // (a*b)*c
251
252 EXPECT_TRUE(almostEqual(R,-94,d,e));
253
254 // Test distributivity
255 // test: a*(b+c) == a*b + a*c
256 R.add(e, b, c);
257 R.mult(d, a, e); // a*(b+c)
258 R.mult(b, a, b);
259 R.mult(c, a, c);
260 R.add(e, b, c); // a*b + a*c
261
262 EXPECT_TRUE(almostEqual(R,-94,d,e));
263 }
264 R.clear(e);
265 R.clear(d);
266 R.clear(c);
267 R.clear(b);
268 R.clear(a);
269}
270
271TEST(ARingRRi, power_and_invert)
272{
273 M2::ARingRRi R(100);
275 M2::ARingRRi::ElementType a, b, c, d;
276 R.init(a);
277 R.init(b);
278 R.init(c);
279 R.init(d);
280 mpz_t gmp1;
281 mpz_init(gmp1);
282
283 for (int i = 0; i < ntrials; i++)
284 {
285 gen.nextElement(a);
286 // TODO: what should the answer here be?
287 // EXPECT_TRUE(R->is_equal(R->power(a, 0), R->one())); // 0^0 == 1 too?
288 R.power(b, a, 1);
289 EXPECT_TRUE(R.is_equal(b, a));
290
291 int e1 = rawRandomInt(10) + 1;
292 int e2 = rawRandomInt(10) + 1;
293 R.power(b, a, e1);
294 R.power(c, a, e2);
295 R.power(d, a, e1 + e2);
296 R.mult(c, b, c);
297 EXPECT_TRUE(almostEqual(R,-94,c,d));
298
299 // Make sure that powers via mpz work (at least for small exponents)
300 mpz_set_si(gmp1, e1);
301 R.power_mpz(d, a, gmp1);
302 EXPECT_TRUE(R.is_equal(d, b));
303 }
304 mpz_clear(gmp1);
305 R.clear(d);
306 R.clear(c);
307 R.clear(b);
308 R.clear(a);
309}
310
311// Local Variables:
312// compile-command: "make -C $M2BUILDDIR/Macaulay2/e/unit-tests check "
313// indent-tabs-mode: nil
314// End:
void getElement< M2::ARingRRi >(const M2::ARingRRi &R, int index, M2::ARingRRi::ElementType &result)
TEST(ARingRRi, create)
bool almostEqual(const M2::ARingRRi &R, int nbits, const M2::ARingRRi::ElementType &a, const M2::ARingRRi::ElementType &b)
void testRingNegateRRi(const M2::ARingRRi &R, const M2::ARingRRR &S, int ntrials)
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.
M2::ARingRRi — certified real intervals [a, b] with MPFR endpoints, MPFI arithmetic.
ConcreteRing<RingType> — the templated bridge between aring and the legacy Ring API.
void nextElement(typename RingType::ElementType &result)
Definition ARingTest.hpp:56
bool is_zero(const ElementType &f) const
Definition aring-RRR.hpp:96
static void clear(ElementType &result)
void init(ElementType &result) const
aring-style adapter for arbitrary-precision real numbers, backed by MPFR.
Definition aring-RRR.hpp:70
void divide(ElementType &result, const ElementType &a, const ElementType &b) const
static void clear(ElementType &result)
void subtract(ElementType &result, const ElementType &a, const ElementType &b) const
size_t characteristic() const
Definition aring-RRi.hpp:80
void midpoint(ARingRRR::ElementType &a, const ElementType &b) const
void init(ElementType &result) const
void random(ElementType &result) const
void negate(ElementType &result, const ElementType &a) const
void subtract_multiple(ElementType &result, const ElementType &a, const ElementType &b) const
bool is_subset(const ElementType &g, const ElementType &f) const
bool is_member(const ARingRRR::ElementType &a, const ElementType &f) const
void set_from_long(ElementType &result, long a) const
void add(ElementType &result, const ElementType &a, const ElementType &b) const
bool is_equal(const ElementType &f, const ElementType &g) const
Definition aring-RRi.hpp:96
void power(ElementType &result, const ElementType &a, int n) const
unsigned long get_precision() const
Definition aring-RRi.hpp:81
void mult(ElementType &result, const ElementType &a, const ElementType &b) const
void power_mpz(ElementType &result, const ElementType &a, mpz_srcptr n) const
aring-style adapter for arbitrary-precision real intervals, backed by MPFI.
Definition aring-RRi.hpp:69
void subtract(int &result, int a, int b)
VALGRIND_MAKE_MEM_DEFINED & result(result)
int32_t rawRandomInt(int32_t max)
Definition random.cpp:44