Add fallback implementation in case gmpy is missing

It seems gmpy is hard to install on a Mac, so make it optional.