Macaulay2 Engine
Loading...
Searching...
No Matches
aring-qq-flint.cpp
Go to the documentation of this file.
1// Copyright 2013 Michael E. Stillman
2
3#include "aring-qq-flint.hpp"
4#include "ringmap.hpp"
5
6#include <iostream>
7namespace M2 {
8
14
15// This function will likely not ever get called.
20
22 const ElementType& f,
23 int first_var,
24 ring_elem& result) const
25{
26 mpq_t temp;
27
28 (void) first_var;
29 flint_mpq_init_set_readonly(temp, &f);
30 bool ok = map->get_ring()->from_rational(temp, result);
31 flint_mpq_clear_readonly(temp);
32 if (!ok)
33 {
34 // if there is already an error message don't add in another
35 throw exc::engine_error("cannot map rational to this ring");
36 }
37}
38
40 const ElementType& a,
41 bool p_one,
42 bool p_plus,
43 bool p_parens) const
44{
45 char s[1000];
46 char* str;
47
48 (void) p_parens;
49 bool is_neg = (fmpq_sgn(&a) == -1);
50 bool one = is_pm_one(a);
51
52 size_t size = fmpz_sizeinbase(fmpq_numref(&a), 10) +
53 fmpz_sizeinbase(fmpq_denref(&a), 10) + 3;
54
55 char* allocstr = (size > 1000 ? newarray_atomic(char, size) : s);
56
57 // std::cout << "size = " << size << std::endl;
58 if (!is_neg && p_plus) o << '+';
59 if (one)
60 {
61 if (is_neg) o << '-';
62 if (p_one) o << '1';
63 }
64 else
65 {
66 str = fmpq_get_str(allocstr, 10, &a);
67 o << str;
68 }
69 // std::cout << "output = " << o.str() << std::endl;
70 if (size > 1000) freemem(allocstr);
71}
72
74 const ElementType& b,
76 ElementType& y) const
77{
78 assert(!is_zero(b));
79 set_from_long(x, 1);
80 divide(y, a, b);
81 negate(y, y);
82}
83};
84
85// Local Variables:
86// compile-command: "make -C $M2BUILDDIR/Macaulay2/e "
87// indent-tabs-mode: nil
88// End:
M2::ARingQQFlint — rationals backed by FLINT's fmpq with small-value inlining.
#define FLINT_RAND_INIT(x)
Definition aring.hpp:59
#define FLINT_RAND_CLEAR(x)
Definition aring.hpp:60
flint_rand_t mRandomState
void divide(ElementType &result, const ElementType &a, const ElementType &b) const
test doc
void eval(const RingMap *map, const ElementType &f, int first_var, ring_elem &result) const
void elem_text_out(buffer &o, const ElementType &a, bool p_one=true, bool p_plus=false, bool p_parens=false) const
void negate(ElementType &result, const ElementType &a) const
bool is_pm_one(const ElementType &f) const
void set_from_long(ElementType &result, long a) const
void syzygy(const ElementType &a, const ElementType &b, ElementType &x, ElementType &y) const
bool is_zero(const ElementType &f) const
virtual bool from_rational(const mpq_srcptr q, ring_elem &result) const =0
const Ring * get_ring() const
Definition ringmap.hpp:111
Engine-side ring homomorphism: stores, for each source-ring variable, the target-ring element it maps...
Definition ringmap.hpp:60
void freemem(void *s)
Definition m2-mem.cpp:103
void size_t s
Definition m2-mem.cpp:271
VALGRIND_MAKE_MEM_DEFINED & result(result)
Definition aring-CC.cpp:3
#define newarray_atomic(T, len)
Definition newdelete.hpp:91
volatile int x
RingMap — engine representation of a ring homomorphism.