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