Ask Your Question
1

change how elements of an existing class are printed out

asked 2021-09-08 02:11:15 +0200

Max Alekseyev gravatar image

updated 2021-09-08 02:46:57 +0200

By default, the elements of Zmod are printed without the modulus - e.g.:

sage: a = mod(1,3)                                                                                                                                                                                                                                                            
sage: print(a)
1

so it's even hard to distinguish them from integers in the output. I want them to be always printed out like mod(1, 3) in the above example. It's not a big deal to do so for any particular element, but I'd like this be changed system-wide.

So, my question is how to re-define how elements of an existing class (such as Zmod) are printed out?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2021-09-08 03:58:58 +0200

You can patch Sage: make the change

diff --git a/src/sage/rings/finite_rings/integer_mod.pyx b/src/sage/rings/finite_rings/integer_mod.pyx
index 738d4b05ab..7e0eb624cd 100644
--- a/src/sage/rings/finite_rings/integer_mod.pyx
+++ b/src/sage/rings/finite_rings/integer_mod.pyx
@@ -1863,7 +1863,7 @@ cdef class IntegerMod_abstract(FiniteRingElement):
         return self._mul_(~right)

     def _repr_(self):
-        return str(self.lift())
+        return "mod({},{})".format(self.lift(), self.modulus())

     def _latex_(self):
         return str(self)

Then run "sage -b" to rebuild the affected files.

edit flag offensive delete link more

Comments

Thanks, but I hope for a more portable method from within Sage/Python.

Max Alekseyev gravatar imageMax Alekseyev ( 2021-09-08 05:04:52 +0200 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2021-09-08 02:11:15 +0200

Seen: 61 times

Last updated: Sep 08 '21