Can attributes of sage classes be changed?
I want to change the way vectors are rendered in sagetex. For example, consider v = vector([1, 2, 3])
. In sagetex, the command $v=\sage{v}$
renders as
$$
v=(1, 2, 3)
$$
Instead, I want my vectors to take the form
$$
v=\left[\begin{array}{rrr}1&2&3\end{array}\right]^\intercal
$$
I thought I could do this by resetting the _latex_
method of the sage.modules.free_module_element.FreeModuleElement
class as follows.
def my_vector_latex(self):
return matrix(v)._latex_() + r'^\intercal'
setattr(sage.modules.free_module_element.FreeModuleElement, '_latex_', my_vector_latex)
However, this code throws the following error:
TypeError: can't set attributes of built-in/extension type 'sage.modules.free_module_element.FreeModuleElement'
So, it looks like I can't modify the methods of FreeModuleElement
in this way. Is there anything else I could do?
The problem is that
FreeModuleElement
is a Cython class, and attributes of those can't be modified, I believe. See https://cython.readthedocs.io/en/late....Are there any workarounds?
Cython classes can't be monkey-patched, indeed. ISTR to have tried that, unsuccessfully...
<handwaving>
You might try creating a subclass of the relevant base class (in your case
sage.modules.vector_integer_dense.Vector_integer_dense
), with updatedlatex
-related methods.But that should be done for any vector class you wish to use...</handwaving>
Or propose a patch allowing setting
_latex_
attribute of vectors at run time, as it is done forvar
andfunction
. But that could be (run time) costly...