Macaulay2 Engine
Loading...
Searching...
No Matches
ntl-internal.cpp
Go to the documentation of this file.
1// Copyright 2005, Michael Stillman
2
3// This file contains routines which often conflict with our names
4
5#include <cstdio>
6
7#include <M2/config.h>
8#include <M2/gc-include.h>
9#include <stddef.h>
10#include "ntl-interface.hpp"
11
12void ntl_ZZ_to_mpz(mpz_t result, const NTL::ZZ &a)
13// Assumption: 'result' is already 'init'ed
14// I could imagine there is a faster way to do this, but currently the only
15// place
16// this code is used is with NTL LLL, and even then, only to transfer elements
17// to/from ntl integers
18// at the beginning and at the end.
19{
20 if (a == 0)
21 {
22 mpz_set_ui(result, 0);
23 return;
24 }
25
26 long len = NTL::NumBytes(a);
27 unsigned char *byte_array = new unsigned char[len];
28 bool is_neg = (a < 0);
29 NTL::BytesFromZZ(byte_array, a, len);
30 mpz_import(result, len, -1, sizeof(byte_array[0]), 0, 0, byte_array);
31 delete[] byte_array;
32 if (is_neg) mpz_neg(result, result);
33}
34
35NTL::ZZ ntl_ZZ_from_mpz(mpz_srcptr a)
36{
37 int sgn = mpz_sgn(a);
38 if (sgn == 0) return NTL::ZZ::zero();
39 long len = mpz_sizeinbase(a, 8);
40 unsigned char *byte_array = new unsigned char[len];
41 size_t written_len;
42 mpz_export(byte_array, &written_len, -1, sizeof(byte_array[0]), 0, 0, a);
43 NTL::ZZ result = NTL::ZZFromBytes(byte_array, written_len);
44 delete[] byte_array;
45 return (sgn > 0 ? result : -result);
46}
47
48NTL::mat_ZZ *makeNTLMatrixZZ(int nrows, int ncols)
49{
50 NTL::mat_ZZ *X = new NTL::mat_ZZ;
51 X->SetDims(nrows, ncols);
52 return X;
53}
54
55void mat_ZZ_set_entry(NTL::mat_ZZ *A, long i, long j, mpz_srcptr a)
56{
57 NTL::ZZ b = ntl_ZZ_from_mpz(a);
58 (*A)(i + 1, j + 1) = b;
59}
60
61void mat_ZZ_get_entry(const NTL::mat_ZZ *A, long i, long j, mpz_t result)
62{
63 NTL::ZZ t = (*A)(i + 1, j + 1);
65}
66
67// Local Variables:
68// compile-command: "make -C $M2BUILDDIR/Macaulay2/e "
69// indent-tabs-mode: nil
70// End:
VALGRIND_MAKE_MEM_DEFINED & result(result)
Engine bridge into NTL — type conversions and the NTL-backed LLL entry point.
NTL::ZZ ntl_ZZ_from_mpz(mpz_srcptr a)
void mat_ZZ_get_entry(const NTL::mat_ZZ *A, long i, long j, mpz_t result)
void mat_ZZ_set_entry(NTL::mat_ZZ *A, long i, long j, mpz_srcptr a)
NTL::mat_ZZ * makeNTLMatrixZZ(int nrows, int ncols)
void ntl_ZZ_to_mpz(mpz_t result, const NTL::ZZ &a)