Well in theory it's easy. You just need to edit the exclude part of
the iterable definition in sympy/core/compatiblity.py to include
IndexedBase. But this is actually not so easy because there's no way
to import IndexedBase for use in the function, since it is used in the
import process before IndexedBase is defined.

So probably what should be done is some kind of _not_iterable property
put on IndexedBase and iterable() changed to look for it.

By the way, I think there are other objects that do the same sort of
thing, like MatrixSymbol, which also need to be disabled, since they
iterate forever. I seem to remember a few places in the code just
check for them explicitly, but we should standardize on iterable(),
and make iterable() know what does and doesn't work.

Aaron Meurer


On Sat, Jan 25, 2014 at 12:45 PM, Björn Dahlgren <[email protected]> wrote:
> Ok, I poked around a little didn't get it to work on my first try.
> Do you agree that it would be the preferred behaviour that cse should work
> with Indexed instances out of the box?
> Is it trivial fix for someone - if not I could try to get this to work but
> it will take me quite a bit of work I think.
>
> Thanks!
> /Björn
>
>
> On Saturday, 25 January 2014 19:26:47 UTC+1, Aaron Meurer wrote:
>>
>> The problem is that the iterable() function in compatibility thinks
>> that y is iterable, so it's trying to iterate through y. The fix is to
>> add IndexedBase to the set of ignored iterables in the function.
>>
>> Aaron Meurer
>>
>> On Sat, Jan 25, 2014 at 5:04 AM, Björn Dahlgren <[email protected]> wrote:
>> > Hi!
>> >
>> > while using the Indexed class from sympy.tensor I have run into the
>> > following problem:
>> >
>> > cse(...) fails for expressions having "Indexed" instances. They are not
>> > handled correctly in opt_cse() in cse_main.py
>> > A possible work-around is to substitute the Indexed instances with
>> > dummies,
>> > perform the CSE elimination and then
>> > resubstitute the dummies for the indexed symbols. But this is not pretty
>> > and
>> > I think that cse(...) should handle this
>> > on its own.
>> >
>> > Example:
>> >
>> >     from sympy import *
>> >
>> >     len_y = 5
>> >     y = IndexedBase('y', shape=(len_y,))
>> >     x = IndexedBase('x', shape=(len_y,))
>> >     Dy = IndexedBase('Dy', shape=(len_y-1,))
>> >     i = Idx('i', len_y-1)
>> >
>> >     expr1 = (y[i+1]-y[i])/(x[i+1]-x[i])
>> >     expr2 = 1/(x[i+1]-x[i])
>> >     print(cse([expr1, expr2]))
>> >
>> > this will cause an infinite recursive call stack to _find_opts raising:
>> >     RuntimeError: maximum recursion depth exceeded in __instancecheck__
>> >
>> >
>> > Does anybody know what the best approach here would be? Add something to
>> > the
>> > Indexed class or modify
>> > opt_cse?
>> >
>> > Best regards
>> > /Björn
>> >
>> > --
>> > 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 [email protected].
>> > To post to this group, send email to [email protected].
>> > Visit this group at http://groups.google.com/group/sympy.
>> > For more options, visit https://groups.google.com/groups/opt_out.
>
> --
> 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 [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/sympy.
> For more options, visit https://groups.google.com/groups/opt_out.

-- 
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sympy.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to