3

I'm extending a small library using NTL and GMP. I'm using NTL for convenience (and to conform to existing APIs), but I'll be switching to GMP (and MPFR) for the really performance-critical stuff.

I'm using GMP as a long integer backend for NTL (compiled using the NTL_GMP_LIP=on flag) and I'm hoping this means I can just access the underlying mpz_t from an NTL::ZZ object. However, I can't find any documentation or examples.

If someone could give me a small code snippet or explain how to convert between an NTL::ZZ and mpz_t I would be eternally grateful.

(Note: I know you can just use strings as an intermediate format and convert using string parsing, but I'd like something more performant.)

AbcAeffchen
  • 14,400
  • 15
  • 47
  • 66
pg1989
  • 1,010
  • 6
  • 13
  • AFAICS, NTL doesn't use mpz_t (well, temporary ones if you define NTL_GMP_HACK), it only uses the MPN layer of GMP. You should download the source for NTL and look at src/g_lip_impl.h to figure out what it looks like. – Marc Glisse Feb 01 '14 at 03:57

1 Answers1

2

You can use sstream or similar string utilities to convert vice versa. The below code be used to transfer from NTL to ZZ. Reverse is similar.

   ZZ a, b;

   cin >> a;
   cin >> b;

   mpz_t aa, bb;
   mpz_init(aa);
   mpz_init(bb);

   std::stringstream ssa;
   std::stringstream ssb;

   ssa << a;
   ssb << b;

   mpz_set_str( aa, ssa.str().c_str(),10);
   mpz_set_str( bb, ssb.str().c_str(),10);


   gmp_printf ("%Zd-", aa);
   gmp_printf ("%Zd\n", bb);

   cout << a  << "-" << b;
kelalaka
  • 5,064
  • 5
  • 27
  • 44