Changing symbol printing for adjoint

68 views
Skip to first unread message

Michael Hansen

unread,
Jan 31, 2016, 3:04:32 PM1/31/16
to sympy
Quick question. Is there a way to change what symbols are used for things like adjoint for a matrix. In latex, it is currently \dagger but I would rather use "H" or something like that.

Jason Moore

unread,
Jan 31, 2016, 7:55:41 PM1/31/16
to sy...@googlegroups.com
You can subclass the LatexPrinter and modify the method that controls the adjoint printing to your liking.

On Sun, Jan 31, 2016 at 12:01 PM, Michael Hansen <michael.sch...@gmail.com> wrote:
Quick question. Is there a way to change what symbols are used for things like adjoint for a matrix. In latex, it is currently \dagger but I would rather use "H" or something like that.

--
You received this message because you are subscribed to the Google Groups "sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
To post to this group, send email to sy...@googlegroups.com.
Visit this group at https://groups.google.com/group/sympy.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/b5a31085-9549-4cce-915a-5cf060bf2ee5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Michael Hansen

unread,
Jan 31, 2016, 8:31:39 PM1/31/16
to sympy
Based on the manual I did something like:

import sympy
from sympy.printing.latex import LatexPrinter

class CustomLatexPrinter(LatexPrinter):
    def _print_Adjoint(self, expr):
        mat = expr.arg
        from sympy.matrices import MatrixSymbol
        if not isinstance(mat, MatrixSymbol):
            return r"\left(%s\right)^H" % self._print(mat)
        else:
            return "%s^H" % self._print(mat)


sympy.Basic.__str__ = lambda self: CustomLatexPrinter().doprint(self)
sympy.init_session()


But that doesn't really change it. How exactly do I overload the LatexPrinter?

Jason Moore

unread,
Jan 31, 2016, 8:58:25 PM1/31/16
to sy...@googlegroups.com
Can you please paste your full code that checks whether this works?

Michael Hansen

unread,
Jan 31, 2016, 9:03:19 PM1/31/16
to sympy

import sympy
from sympy.printing.latex import LatexPrinter

class CustomLatexPrinter(LatexPrinter):
    def _print_Adjoint(self, expr):
        mat = expr.arg
        from sympy.matrices import MatrixSymbol
        if not isinstance(mat, MatrixSymbol):
            return r"\left(%s\right)^H" % self._print(mat)
        else:
            return "%s^H" % self._print(mat)


sympy.init_session()
sympy.Basic.__str__ = lambda self: CustomLatexPrinter().doprint(self)

Nk, Nx = sympy.symbols('N_k N_x')
FT = sympy.MatrixSymbol('\mathcal{F}', Nx, Nk)
s = sympy.MatrixSymbol('s',Nk,1)
Sigmax = sympy.MatrixSymbol('\Sigma_x', Nx, Nx)
Sigmak = sympy.MatrixSymbol('\Sigma_k', Nk, Nk)
W = sympy.MatrixSymbol('W',Nk,Nk)
F = FT*W
m = sympy.MatrixSymbol('m',1,Nx)

Sigmax = m*F*Sigmak*F.adjoint()*m.adjoint()

latex(Sigmax)

Last line's result is:

'm \\mathcal{F} W \\Sigma_{k} W^\\dag \\mathcal{F}^\\dag m^\\dag'

If I do:

tmp = CustomLatexPrinter()
tmp.doprint(Sigmax)

The result is:

'm \\mathcal{F} W \\Sigma_{k} W^H \\mathcal{F}^H m^H'

I think I just don't know what to overload. 

Jason Moore

unread,
Jan 31, 2016, 9:27:19 PM1/31/16
to sy...@googlegroups.com
This is what you want to do:

In [1]: %paste

import sympy
from sympy.printing.latex import LatexPrinter

class CustomLatexPrinter(LatexPrinter):
    def _print_Adjoint(self, expr):
        mat = expr.arg
        from sympy.matrices import MatrixSymbol
        if not isinstance(mat, MatrixSymbol):
            return r"\left(%s\right)^H" % self._print(mat)
        else:
            return "%s^H" % self._print(mat)

## -- End pasted text --

In [2]: def my_latex(expr, **settings):
   ...:     return CustomLatexPrinter(settings).doprint(expr)
   ...:

In [3]: %paste

Nk, Nx = sympy.symbols('N_k N_x')
FT = sympy.MatrixSymbol('\mathcal{F}', Nx, Nk)
s = sympy.MatrixSymbol('s',Nk,1)
Sigmax = sympy.MatrixSymbol('\Sigma_x', Nx, Nx)
Sigmak = sympy.MatrixSymbol('\Sigma_k', Nk, Nk)
W = sympy.MatrixSymbol('W',Nk,Nk)
F = FT*W
m = sympy.MatrixSymbol('m',1,Nx)

Sigmax = m*F*Sigmak*F.adjoint()*m.adjoint()

## -- End pasted text --

In [4]: my_latex(Sigmax)
Out[4]: 'm \\mathcal{F} W \\Sigma_{k} W^H \\mathcal{F}^H m^H'

--
You received this message because you are subscribed to the Google Groups "sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
To post to this group, send email to sy...@googlegroups.com.
Visit this group at https://groups.google.com/group/sympy.

Michael Hansen

unread,
Jan 31, 2016, 10:30:09 PM1/31/16
to sympy
That will not work if I want to use it in an interactive session. There I need the actual latex function overloaded since that is what it seems to call. Is there a way to do that?

Jason Moore

unread,
Jan 31, 2016, 11:06:52 PM1/31/16
to sy...@googlegroups.com
You can pass in the custom latex printer to init_printing or init_session as a kwarg.

Jason
On Sun, Jan 31, 2016 at 7:30 PM, Michael Hansen <michael.sch...@gmail.com> wrote:
That will not work if I want to use it in an interactive session. There I need the actual latex function overloaded since that is what it seems to call. Is there a way to do that?
--
You received this message because you are subscribed to the Google Groups "sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
To post to this group, send email to sy...@googlegroups.com.
Visit this group at https://groups.google.com/group/sympy.

Michael Hansen

unread,
Feb 1, 2016, 10:44:03 AM2/1/16
to sympy
Great. That worked:

import sympy
from sympy.printing.latex import LatexPrinter

class CustomLatexPrinter(LatexPrinter):
    def _print_Adjoint(self, expr):
        mat = expr.arg
        from sympy.matrices import MatrixSymbol
        if not isinstance(mat, MatrixSymbol):
            return r"\left(%s\right)^H" % self._print(mat)
        else:
            return "%s^H" % self._print(mat)


def my_latex(expr, **settings):
    return CustomLatexPrinter(settings).doprint(expr)

sympy.init_printing(latex_printer=my_latex)

One remaining question. init_session() does not seem to accept the latex_printer argument. Is it passed in some other argument?

Aaron Meurer

unread,
Feb 1, 2016, 12:35:22 PM2/1/16
to sy...@googlegroups.com
On Mon, Feb 1, 2016 at 10:44 AM, Michael Hansen <michael.sch...@gmail.com> wrote:
Great. That worked:

import sympy
from sympy.printing.latex import LatexPrinter

class CustomLatexPrinter(LatexPrinter):
    def _print_Adjoint(self, expr):
        mat = expr.arg
        from sympy.matrices import MatrixSymbol
        if not isinstance(mat, MatrixSymbol):
            return r"\left(%s\right)^H" % self._print(mat)
        else:
            return "%s^H" % self._print(mat)


def my_latex(expr, **settings):
    return CustomLatexPrinter(settings).doprint(expr)

sympy.init_printing(latex_printer=my_latex)

One remaining question. init_session() does not seem to accept the latex_printer argument. Is it passed in some other argument?

I guess that's a bug. init_session() should have all the same keyword args as init_printing().

Aaron Meurer
 


On Sunday, January 31, 2016 at 11:06:52 PM UTC-5, Jason Moore wrote:
You can pass in the custom latex printer to init_printing or init_session as a kwarg.

Jason


On Sun, Jan 31, 2016 at 7:30 PM, Michael Hansen <michael.sch...@gmail.com> wrote:
That will not work if I want to use it in an interactive session. There I need the actual latex function overloaded since that is what it seems to call. Is there a way to do that?

--
You received this message because you are subscribed to the Google Groups "sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
To post to this group, send email to sy...@googlegroups.com.
Visit this group at https://groups.google.com/group/sympy.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/2b5347c3-8ebb-41b2-83d2-ead33860c6e7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
To post to this group, send email to sy...@googlegroups.com.
Visit this group at https://groups.google.com/group/sympy.

Michael Hansen

unread,
Feb 1, 2016, 2:56:55 PM2/1/16
to sympy
Yes, I can see that the parameters are not passes on to init_printing:


I am new to the sympy community. Do they prefer a bug report, an issue, or a pull request to fix?

Aaron Meurer

unread,
Feb 1, 2016, 3:13:37 PM2/1/16
to sy...@googlegroups.com
Issues are fine, pull requests are better. :)

Aaron Meurer

Michael Hansen

unread,
Feb 1, 2016, 8:42:56 PM2/1/16
to sympy
Reply all
Reply to author
Forward
0 new messages