Conversion Methods
A python object could interact with gmpy2 if it implements one of the following methods:
__mpz__: return an object of typempz.__mpq__: return an object of typempq.__mpfr__: return an object of typempfr.__mpc__: return an object of typempc.
Implementing on of these methods allow gmpy2 to convert a python object into a gmpy2 type. Example:
>>> from gmpy2 import mpz
>>> class CustInt:
... def __init__(self, x):
... self.x = x
... def __mpz__(self):
... return mpz(self.x)
...
>>> ci = CustInt(5)
>>> z = mpz(ci); z
mpz(5)
>>> type(z)
<class 'gmpy2.mpz'>
Arithmetic operations
gmpy2 allow arithmetic operations between gmpy2 numbers and objects with conversion methods. Operation with object that implements floating conversion and exact conversion methods are not supported. That means that only the following cases are supported:
An integer type have to implement
__mpz__A rational type have to implement
__mpq__and can implement__mpz__A real type have to implement
__mpfr__A complex type have to implement
__mpc__and can implement__mpfr__
Examples:
>>> import gmpy2
>>> from gmpy2 import mpz, mpq, mpfr, mpc
>>> gmpy2.set_context(gmpy2.context())
>>> class Q:
... def __mpz__(self): return mpz(1)
... def __mpq__(self): return mpq(3,2)
>>> q = Q()
>>> mpz(2) + q
mpq(7,2)
>>> mpq(1,2) * q
mpq(3,4)
>>> mpfr(10) * q
mpfr('15.0')