Symbolic Output L^p norm

47 views
Skip to first unread message

Yang Liu

unread,
Sep 1, 2022, 5:42:33 PM9/1/22
to sympy

I am trying to define a Sympy function $F(p,f)=|f|{L^p}$ symbolically. So it take Symbol("p") and Function f and output is in latex form of $|f|{L^p}$. Any idea of how to do it?

Thanks a lot!

Aaron Meurer

unread,
Sep 1, 2022, 6:32:09 PM9/1/22
to sy...@googlegroups.com
It depends on what you intend to do with the expression. If you're
only interested in having it print correctly, you can write a custom
function that defines LaTeX printing in the way you want (see
https://docs.sympy.org/latest/guides/custom-functions.html#printing).
For example

class F(Function):
@classmethod
def eval(cls, p, f):
pass

def _latex(self, printer):
p, f = self.args
_p, _f = printer._print(p), printer._print(f)
return r'\left | %s \right | {L^{%s}}' % (_p, _f)

And use it like

p, f = symbols('p f')
F(p, f)

However, the issue here is that f isn't really a function, it's a
symbol. In SymPy it's currently not possible to represent Function
objects as symbolic objects (see
https://github.com/sympy/sympy/issues/4787). So if you want to later
use f as a SymPy function that you can call with other arguments,
you'll have to structure it differently.

Aaron Meurer


On Thu, Sep 1, 2022 at 3:42 PM Yang Liu <mica...@gmail.com> wrote:
>
> I am trying to define a Sympy function $F(p,f)=|f|{L^p}$ symbolically. So it take Symbol("p") and Function f and output is in latex form of $|f|{L^p}$. Any idea of how to do it?
>
> Thanks a lot!
>
> --
> 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 view this discussion on the web visit https://groups.google.com/d/msgid/sympy/fb7bab86-69f5-49a2-9a49-73b1a03d033fn%40googlegroups.com.

Yang Liu

unread,
Sep 1, 2022, 6:48:56 PM9/1/22
to sympy
Hi Aron,

Thank you very much, very appreciated. I am mainly want to construct a symbolic Sobolev inequality, so f is symbolic is fine at the moment.
Reply all
Reply to author
Forward
0 new messages