invalid syntax in python while trying to plot
Question: After my blunder with the infinity factorial sum XD I redid the code, but I
keep getting syntax error :\

    
    
    from scitools.std import *
    from math import factorial, cos, e
    from scipy import *
    import numpy as np
    
    
    def f1(t):
        return 0.5*(1 + sum( (a**(2*n)*cos(2*sqrt(1 + n)*t))/(e**a**2*factorial(n)) for n in range(0,100)))
    
    a=4
    t = linspace(0, 35, 1000)
    y1 = f1(t)
    
    plot(t, y1)
    
    xlabel(r'$\tau$')
    ylabel(r'P($\tau$)')
    legend(r'P($\tau$)')
    axis([0.0, 35.0, 0.0, 1.0])
    grid(True)
    show()
    

It says that there's an error in my program: invalid syntax and the a is red
:\

What's wrong now? :(

EDIT:

I've added another ) at the end of the sum, but now I keep getting huge error:

    
    
    Traceback (most recent call last):
      File "D:\faxstuff\3.godina\kvantna\vježbe\qm2\v8\plot.py", line 12, in <module>
        y1 = f1(t)
      File "D:\faxstuff\3.godina\kvantna\vježbe\qm2\v8\plot.py", line 8, in f1
        return 0.5*(1 + sum( (a**(2*n)*cos(2*sqrt(1 + n)*t))/(e**a**2*factorial(n)) for n in range(0,100)))
      File "C:\Python26\lib\site-packages\numpy\core\fromnumeric.py", line 1415, in sum
        res = _sum_(a)
      File "D:\faxstuff\3.godina\kvantna\vježbe\qm2\v8\plot.py", line 8, in <genexpr>
        return 0.5*(1 + sum( (a**(2*n)*cos(2*sqrt(1 + n)*t))/(e**a**2*factorial(n)) for n in range(0,100)))
    TypeError: unsupported operand type(s) for /: 'numpy.ndarray' and 'numpy.float64'
    

Should I make so that the sum expression gives back an array from which I can
plot or?


Answer: The parentheses in `return 0.5*(1 + sum(...` are not balanced.

As a matter of style, I would recommend avoiding `from <module> import
*`-style imports. In your specific example you end up with a single-letter
variable name (`e`) imported into the global namespace, which you then proceed
to use. This could lead to confusion and, worse, hard-to-diagnose bugs.

