2

Just as the title says I am trying to find a function in the C++ gmp library that does the same thing as the gmpy2 python library's divm(...) function.

I can't imagine that gmpy2 has this method and the gmp C++ library has nothing that can do this same calculation.

If this does not exist, I'd appreciate any advice on making the divm function from scratch (still must be using gmp as I'm working with values larger than standard integers via mpz_class).

Thanks!

1 Answer 1

2

The C source code for that function can be found here, in GMPy_MPZ_Function_Divm. As you can see, it's not really a one-to-one mapping to the underlying C code but, when you strip away all the Python reference counting stuff, it looks pretty basic:

// numz, denz, and modz are copies of the arguments.
// resz, gcdz ar temporaries.

if (mpz_invert(resz, denz, modz)) {    // inverse exists?
    ok = 1;
} else {                               // num, den AND mod have a gcd > 1?
    mpz_init(gcdz);
    mpz_gcd(gcdz, numz, denz);
    mpz_gcd(gcdz, gcdz, modz);
    mpz_divexact(numz, numz, gcdz);
    mpz_divexact(denz, denz, gcdz);
    mpz_divexact(modz, modz, gcdz);
    mpz_clear(gcdz);
    ok = mpz_invert(resz, denz, modz);
}
if (ok) {
    mpz_mul(resz, resz, numz);
    mpz_mod(resz, resz, modz);
    mpz_clear(numz);
    mpz_clear(denz);
    mpz_clear(modz);
    return resz;
} else {
    mpz_clear(numz);
    mpz_clear(denz);
    mpz_clear(modz);
    return NULL;
}

Mathematically, it just calculates the expression b-1 * a % m for divm(a, b, m), assuming of course that that expression is valid (eg, b is non-zero).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.