Macaulay2 Engine
Loading...
Searching...
No Matches
flint.cpp
Go to the documentation of this file.
1#include "interface/flint.h"
3
4#include <iostream>
5#include <vector>
6
7// The following needs to be included before any flint files are included.
8#include <M2/gc-include.h>
9
10#pragma GCC diagnostic push
11#pragma GCC diagnostic ignored "-Wconversion"
12#include <flint/fmpz.h> // for fmpz_t, fmpz, fmpz_clear, fmpz_init...
13#include <flint/fmpz_factor.h> // for fmpz_factor, fmpz_factor_clear, fmpz...
14#pragma GCC diagnostic pop
15
16#include "error.h"
17
19{
20 fmpz_t n;
21 fmpz_init(n);
22 fmpz_set_mpz(n, a);
23 auto ret = fmpz_is_prime(n);
24 fmpz_clear(n);
25 if(ret<0) {
26 ERROR("flint's is_prime failed");
27 std::cout << "fmpz_is_prime returned " << ret << std::endl;
28 }
29 return ret;
30}
31
33{
34 fmpz_t n;
35 fmpz_init(n);
36 fmpz_set_mpz(n, a);
37 int ret = fmpz_is_probabprime(n);
38 fmpz_clear(n);
39 return ret;
40}
41
42// TODO: not yet implemented or connected to top level
43// This function is intended to copy data to front end type.
44gmp_arrayZZ flintToFrontend(std::vector<fmpz_t>);
45
46gmp_arrayZZ rawZZfactor(gmp_ZZ x)
47{
48 fmpz_t n;
49 fmpz_init(n);
50 fmpz_set_mpz(n, x);
51 //std::cout << "factoring fmpz " << static_cast<void*>(n) << std::endl;
52 fmpz_factor_t factor;
53 fmpz_factor_init(factor);
54 fmpz_factor(factor,n);
55 int len = factor->num;
56 gmp_arrayZZ result = getmemarraytype(gmp_arrayZZ,2*len+1);
57 result->len = 2*len+1;
58 __mpz_struct *tmp;
59 // The sign is the first element of the result.
60 tmp = newitem(__mpz_struct);
61 mpz_init(tmp);
62 mpz_set_si(tmp, factor->sign);
64 result->array[0] = tmp;
65 for (int i=0; i<len; i++) {
66 // Get the i-th factor
67 tmp = newitem(__mpz_struct);
68 mpz_init(tmp);
69 fmpz_get_mpz(tmp,factor->p + i);
71 result->array[2*i+1] = tmp;
72 // Get the i-th factor and its exponent
73 tmp = newitem(__mpz_struct);
74 mpz_init(tmp);
75 fmpz_get_mpz(tmp,(fmpz *)(factor->exp + i));
77 result->array[2*i+2] = tmp;
78 }
79 fmpz_factor_clear(factor);
80 fmpz_clear(n);
81 return result;
82}
83
84// Local Variables:
85// indent-tabs-mode: nil
86// End:
Engine error-reporting primitives: ERROR, INTERNAL_ERROR, error, error_message.
gmp_arrayZZ flintToFrontend(std::vector< fmpz_t >)
gmp_arrayZZ rawZZfactor(gmp_ZZ x)
Definition flint.cpp:46
M2_bool rawZZisPrime(gmp_ZZ a)
Definition flint.cpp:18
M2_bool rawZZisProbablePrime(gmp_ZZ a)
Definition flint.cpp:32
Engine-boundary C API exposing FLINT's integer primality and factorisation services.
void mpz_reallocate_limbs(mpz_ptr _z)
Definition gmp-util.h:46
Inline helpers that move GMP / MPFR / MPFI limbs from malloc-managed storage into the bdwgc heap.
const int ERROR
Definition m2-mem.cpp:55
VALGRIND_MAKE_MEM_DEFINED & result(result)
#define getmemarraytype(S, len)
Definition m2-mem.h:142
char M2_bool
Definition m2-types.h:82
mpz_srcptr gmp_ZZ
Definition m2-types.h:141
#define newitem(T)
Definition newdelete.hpp:86
volatile int x