File: aring-qq-flint.cpp

package info (click to toggle)
macaulay2 1.21%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 133,096 kB
  • sloc: cpp: 110,377; ansic: 16,306; javascript: 4,193; makefile: 3,821; sh: 3,580; lisp: 764; yacc: 590; xml: 177; python: 140; perl: 114; lex: 65; awk: 3
file content (85 lines) | stat: -rw-r--r-- 2,066 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright 2013 Michael E. Stillman

#include "aring-qq-flint.hpp"
#include "ringmap.hpp"

#include <iostream>
namespace M2 {

ARingQQFlint::ARingQQFlint()
{
  flint_randinit(mRandomState);
  mMaxHeight = 50;
}

// This function will likely not ever get called.
ARingQQFlint::~ARingQQFlint()
{
  flint_randclear(mRandomState);
}

void ARingQQFlint::eval(const RingMap* map,
                        const ElementType& f,
                        int first_var,
                        ring_elem& result) const
{
  mpq_t temp;
  flint_mpq_init_set_readonly(temp, &f);
  bool ok = map->get_ring()->from_rational(temp, result);
  flint_mpq_clear_readonly(temp);
  if (!ok)
    {
      // if there is already an error message don't add in another
      throw exc::engine_error("cannot map rational to this ring");
    }
}

void ARingQQFlint::elem_text_out(buffer& o,
                                 const ElementType& a,
                                 bool p_one,
                                 bool p_plus,
                                 bool p_parens) const
{
  char s[1000];
  char* str;

  bool is_neg = (fmpq_sgn(&a) == -1);
  bool one = is_pm_one(a);

  size_t size = fmpz_sizeinbase(fmpq_numref(&a), 10) +
                fmpz_sizeinbase(fmpq_denref(&a), 10) + 3;

  char* allocstr = (size > 1000 ? newarray_atomic(char, size) : s);

  // std::cout << "size = " << size << std::endl;
  if (!is_neg && p_plus) o << '+';
  if (one)
    {
      if (is_neg) o << '-';
      if (p_one) o << '1';
    }
  else
    {
      str = fmpq_get_str(allocstr, 10, &a);
      o << str;
    }
  // std::cout << "output = " << o.str() << std::endl;
  if (size > 1000) freemem(allocstr);
}

void ARingQQFlint::syzygy(const ElementType& a,
                          const ElementType& b,
                          ElementType& x,
                          ElementType& y) const
{
  assert(!is_zero(b));
  set_from_long(x, 1);
  divide(y, a, b);
  negate(y, y);
}
};

// Local Variables:
// compile-command: "make -C $M2BUILDDIR/Macaulay2/e  "
// indent-tabs-mode: nil
// End: