How to parse f(x) as a Function?

19 views
Skip to first unread message

Paul Royik

unread,
Nov 22, 2021, 2:00:13 AM11/22/21
to sympy
I want to parse something like `f(x) - 2 sin 3x`.

If I use `parse_expr` without `implicit_multiplication_application`, then `f(x)` is parsed correctly, but not `2 sin 3x`.

If I include `implicit_multiplication_application`, then `f(x)` is parsed as `fx`.

Is this a bug?

Paul Royik

unread,
Nov 22, 2021, 1:30:10 PM11/22/21
to sympy
I've found the following piece of code in _implicit_multiplication

elif (isinstance(tok, AppliedFunction) and
nextTok[0] == OP and nextTok[1] == '('):
# Applied function followed by an open parenthesis
if tok.function[1] == "Function":
result[-1].function = (result[-1].function[0], 'Symbol')
result.append((OP, '*'))

Is there any reason to substitute a Function with a Symbol?

Aaron Meurer

unread,
Nov 22, 2021, 5:35:13 PM11/22/21
to sy...@googlegroups.com
I'm not sure what that line of code is doing. You'd have to trace
through to see exactly what sequence of tokens can lead to it being
run.

However, I will say in general that it would make sense to me to split
out implicit multiplication via a space and implicit multiplication
via parentheses. It's only the latter that is ambiguous with function
application. Unfortunately, this is a general ambiguity that is
inherent in mathematical notation. Does x(y + z) mean x is a function
of y + z or does it mean xy + xz? There's no way to know from the
notation alone.

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 view this discussion on the web visit https://groups.google.com/d/msgid/sympy/7daad2ef-415d-46af-a9dc-92bb2006fa1bn%40googlegroups.com.

Paul Royik

unread,
Nov 23, 2021, 1:19:53 AM11/23/21
to sympy
You're correct. But symbols of the length one like f(x) are certaily function calls.
Reply all
Reply to author
Forward
0 new messages