On Fri, Nov 30, 2012 at 8:00 PM, Shriramana Sharma <[email protected]> wrote:
> Hello. Trying to execute the following minimal example throws SymPy
> into an infinite loop. (Obviously this is an extract from my actual
> program.) I tried debugging using eric5 but beyond that the problem
> occurs within the Ra() function, I couldn't find out anything. Can
> please anyone tell me what I am doing wrong?
>
> from sympy import symbols,summation,binomial,Function
> i,n,j=symbols('i n j', integer=True)
> t=symbols('t',real=True)
> ival=Function('ival')
> def Ra(i) : return
> summation((-1)**(i+j)*binomial(i,j)*binomial(n,i)*ival(j),(j,0,i))

I'm assuming that this should be the return value of Ra.

> print(summation(t**i*Ra(i),(i,0,5)))

The problem seems to be with the printer.  Namely, the printer tries
to order the terms, which involves calling evalf on the summations.
You can see what you would get by
pprint(summation(t**i*Ra(i),(i,0,5)), order='none') (or
sstr(summation(t**i*Ra(i),(i,0,5)), order='none') for the 1-d format).

So there are a few issues here.

First, the printing methods should not call evalf to determine the
order to print things, as it obviously can be too slow.

Second, evalf should not be slow.  The summations are finite.

The real issue though is that when you call
summation(t**i*Ra(i),(i,0,5)), the Ra(i) returns the summation
unevaluated.  It then apparently doesn't evaluate when the outer
summation is evaluated (this might be considered a SymPy bug).  The
easy way to fix it is to use summation(t**i*Ra(i),(i,0,5)).doit().
The probably better way is to subclass Function for Ra, and define
eval to return the summation when the argument is an integer (that
way, it will evaluate when called).

In other words, use

class Ra(Function):
    nargs = 1

    @classmethod
    def eval(cls, i):
        if i.is_Integer:
            return
summation((-1)**(i+j)*binomial(i,j)*binomial(n,i)*ival(j),(j,0,i))

Care to open an issue for this in our issue tracker
(https://code.google.com/p/sympy/issues/list)?

Aaron Meurer

>
> Thanks!
>
> --
> Shriramana Sharma
>
> --
> You received this message because you are subscribed to the Google Groups 
> "sympy" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to 
> [email protected].
> For more options, visit this group at 
> http://groups.google.com/group/sympy?hl=en.
>

-- 
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sympy?hl=en.

Reply via email to