calculate an integral in python

user3440244 picture user3440244 · Mar 21, 2014 · Viewed 9.6k times · Source

I need to calculate an integral in python.

I have imported sympy.

 g(a,z) = integral_from_z_to_inf of ( t^(a-1) * e^(-1))

in python:

 x,a,z = symbols('x a z')
 g = integrate(x**(a-1) * exp(-x), z, oo)

I got error:

 ValueError: Invalid limits given: (z, oo)

I called:

b,c,mean,variance  = S('b c mean variance'.split())

ff1 = b*g((1+c), lb / b)  // lb is a constant, b and c are unknown var that I need to solve. and mean and variance are constants. 

I got error:

TypeError: 'Add' object is not callable

Answer

cc7768 picture cc7768 · Mar 21, 2014

I am on Python 2.7, but your problems seems to be not reading the documentation closely enough. The docs say:

var can be:

  • a symbol – indefinite integration

  • a tuple (symbol, a) – indefinite integration with result

    given with a replacing symbol

  • a tuple (symbol, a, b) – definite integration

You want to perform the last one, so you need to use a tuple.

The command you are looking for is:

import sympy as sym


x, a, z = sym.symbols('x a z')
g = sym.integrate(x**(a-1) * sym.exp(-x), (x, z, sym.oo))