calculate an integral in python

10,718

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))
Share:
10,718
user3440244
Author by

user3440244

Updated on June 06, 2022

Comments

  • user3440244
    user3440244 almost 2 years

    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
    
  • Carsten
    Carsten about 10 years
    @user3440244 Your new question is different from your old one. If you want to ask another question, please open a new question using the "Ask Question" button above.