Strange behavior from custom matrix printer

39 views
Skip to first unread message

Alan Bromborsky

unread,
Jul 1, 2015, 9:09:24 PM7/1/15
to sy...@googlegroups.com
I have implemented a custom matrix printer with "_print_MatrixBase(self, expr)."  I am running "jupyter notebook" with the output of 
"_print_MatrixBase(self, expr)" being given by -

    def _print_MatrixBase(self, expr):
        rows = expr.rows
        cols = expr.cols

        out_str = ' \\left [ \\begin{array}{' + (cols * 'c') + '} '
        for row in range(rows):
            for col in range(cols):
                out_str += latex(expr[row,col]) + ' & '
            out_str = out_str[:-2] + ' \\\\ '
        out_str = out_str[:-4] + ' \\end{array}\\right ] '

        if isinteractive():
            return display(Math(out_str))
        else:
            return out_str

for "jupyter notebook" the output is "return display(Math(out_str))" where the out_str ins the tex output for the matrix.  The notebook output is -

see Capture.PNG

Where  sp3.g is a sympy Matrix.  Why is "None"  output along with the correct matrix output?

Capture.PNG

Aaron Meurer

unread,
Jul 7, 2015, 2:34:13 PM7/7/15
to sy...@googlegroups.com
A better way to do this would be to define the LaTeX printer for the
object (which should be a separate printer from the pretty printer),
and enable LaTeX printing with init_printing().

Aaron Meurer

On Thu, Jul 2, 2015 at 10:30 AM, brombo <abrom...@gmail.com> wrote:
> I have implemented a custom matrix printer with "_print_MatrixBase(self,
> expr)." I am running "jupyter notebook" with the output of
> "_print_MatrixBase(self, expr)" being given by -
>
> def _print_MatrixBase(self, expr):
> rows = expr.rows
> cols = expr.cols
>
> out_str = ' \\left [ \\begin{array}{' + (cols * 'c') + '} '
> for row in range(rows):
> for col in range(cols):
> out_str += latex(expr[row,col]) + ' & '
> out_str = out_str[:-2] + ' \\\\ '
> out_str = out_str[:-4] + ' \\end{array}\\right ] '
>
> if isinteractive():
> return display(Math(out_str))
> else:
> return out_str
>
> for "jupyter notebook" the output is "return display(Math(out_str))" where
> the out_str ins the tex output for the matrix. The notebook output is -
>
> See attached file
>
> Where sp3.g is a sympy Matrix. Why is "None" output along with the
> correct matrix output?
>
> --
> 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 http://groups.google.com/group/sympy.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sympy/2d60f6fc-a7bc-41e7-a82a-86cad4b7d096%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Alan Bromborsky

unread,
Jul 10, 2015, 6:11:17 PM7/10/15
to sy...@googlegroups.com
Using init_printing(use_latex= 'mathjax') solves the problem, but the person who found the problem said it was due to a bug in sympy.  I suggested that he send his observations to this group.

Aaron Meurer

unread,
Jul 10, 2015, 6:47:11 PM7/10/15
to sy...@googlegroups.com
This might be because without use_latex='mathjax', it tries to
generate the latex using matplotlib's TeX renderer, which doesn't
support all of latex.

Aaron Meurer
> https://groups.google.com/d/msgid/sympy/CALOxT-m4ue-%2Bwc5Mh_qZ05DDDxLs%3DRS9DvovFVvYFJn0rq4KVQ%40mail.gmail.com.

Jason Moore

unread,
Jul 11, 2015, 1:58:03 AM7/11/15
to sy...@googlegroups.com
We really need to fix this bug. It came up multiple times over SciPy.

Aaron Meurer

unread,
Jul 13, 2015, 1:31:59 PM7/13/15
to sy...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages