Recommended way to implement division for RingElement

45 views
Skip to first unread message

Simon King

unread,
Sep 21, 2019, 4:44:28 AM9/21/19
to sage-...@googlegroups.com
Hi!

Let R be a ring that is an F-vector space, for some field F.
Let x,y be elements of R and c be an element of F.

If I understand correctly, in order to implement x/y, one should provide
a single underscore method x._div_. But what should one provide for x/c?
It seems that the default behaviour is to replace x/c by x*(~c), but
for efficiency I don't want that c is inverted in F before letting it
act on x.

Moreover, in my use case, R is not an integral domain (thus, has no fraction
field), and actually I don't want division in R at all. So, what do I
need to do in order to make x/y NOT work (and fail with a TypeError)?

In the above situation, is it recommended to override double underscore
__div__? Then what about Py3? Would it then be needed to override
__div__, __truediv__ and __floordiv__?

Best regards,
Simon

TB

unread,
Sep 22, 2019, 1:14:42 PM9/22/19
to sage-...@googlegroups.com
I don't know exactly what happens in Sage, except that the file
src/sage/structure/element.pyx is relevant. In general, for Python 2+3
compatibility it might help to have

from __future__ import division

at the top of your files, and then "a/b" uses __truediv__ in both Python
versions. You do not have to also implement __floordiv__ if "a//b" is
not needed (see
https://docs.python.org/2/reference/datamodel.html#object.__div__).

>
> Best regards,
> Simon
>

Regards,
TB
Reply all
Reply to author
Forward
0 new messages