parse_expr bug with splitting symbols and undefined function

36 views
Skip to first unread message

Duane Nykamp

unread,
Oct 24, 2014, 8:55:12 AM10/24/14
to sy...@googlegroups.com
The following throws an error

In [27]: f=Function('f')

In [28]: parse_expr('af(t)', local_dict={'f': f }, transformations=(auto_symbol, auto_number, split_symbols, implicit_multiplication))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)

[snip]

TypeError: unbound method as_base_exp() must be called with f instance as first argument (got nothing instead)


The code it is trying to evaluate is

Symbol ('a' )*(f )*(Symbol ('t' ))

It seems the problem is the parentheses around f that were inserted in line 394:
                            result.extend([(OP, '('), (NAME, "%s" % char), (OP, ')'),
                                           (NAME, 'Symbol'), (OP, '(')])

Is there any reason to add those parenthesis?  Certainly, if char is a function, they should not be added.  I'm not sure if there are cases where one does need those parenthesis.

I can fix the problem by changing this line to
                                result.extend([(NAME, "%s" % char),
                                           (NAME, 'Symbol'), (OP, '(')])
but I'm not sure if I should test if local_dict[char] or global_dict[char] is callable (similar to _get_token_callable) and only remove the parenthesis if it is callable.

For this to work, I also have to modify the approach to remove the last 2 tokens (rather than 3) and then skip the following token so that the close parenthesis from the original Symbol is not included.

Does this fix seem reasonable?

Duane

Aaron Meurer

unread,
Oct 24, 2014, 5:24:41 PM10/24/14
to sy...@googlegroups.com
Go ahead and open a pull request with this change, and we can review
it, and see if it breaks any tests, and so on.

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 http://groups.google.com/group/sympy.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sympy/5a8ffc92-3be1-4c60-aa0e-0765b20048f3%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Duane Nykamp

unread,
Oct 24, 2014, 6:06:48 PM10/24/14
to sy...@googlegroups.com
OK. I added a pull request: https://github.com/sympy/sympy/pull/8307
Reply all
Reply to author
Forward
0 new messages