Pretty printing a custom Function (hinge functions)

29 views
Skip to first unread message

Vladimir Perić

unread,
Apr 18, 2016, 5:27:17 PM4/18/16
to sy...@googlegroups.com
Hi all,

I want to define some (simple) custom functions, so I could provide my
own `_imp_` for lambdify and for other benefits. I managed to program
the numerical evolution, but I'm struggling with pretty printing. I'm
trying to emulate sympy/printing/pretty/pretty/_pretty_Function, but I
barely understand the code and it feels like a hack. I'd be very much
obliged if someone could give me a simple primer on how to accomplish
what I want:

Essentially, I have a hinge function, which is defined as `max(0,
x-thr)` , where x is a vector and thr is a predefined scalar (and can
be 0). This is the code I currently have:

def _pretty(self, printer, *args):
from sympy.printing.pretty.stringpict import prettyForm, stringPict
prettyFunc = printer._print('Max')
args = self.args + (0,)
prettyArgs = prettyForm(*printer._print_seq(args).parens())
pform = prettyForm(binding=prettyForm.FUNC,
*stringPict.next(prettyFunc, prettyArgs))
return pform

Which produces the results I want. However, I really don't understand
what most of the code actually does. Is there a simpler way of writing
this? It realls feels like there should be.

Thanks in advance,

--
Vladimir Perić

Aaron Meurer

unread,
Apr 18, 2016, 6:26:06 PM4/18/16
to sy...@googlegroups.com
Maybe just return printer._print(Function("Max")(*(self.args + (0,))),
that is, reuse the existing Function printer.

Aaron Meurer
> --
> 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/CAFu6gYZFQh41x%2BDgFOc51W7Vuu-ZAOXtQnV05tH_W1Yt9w%2Bt%3DQ%40mail.gmail.com.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages