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