How to Do Calculus with Python ? (original) (raw)
**Calculus is a branch of mathematics focused on limits, functions, derivatives, integrals, and infinite series. We will use **SymPy library to do calculus with python. SymPy is a Python library for symbolic mathematics. It aims to become a full-featured computer algebra system (CAS) while keeping the code as simple as possible in order to be comprehensible and easily extensible. SymPy is written entirely in Python.
**Installation:
pip install sympy
If we want to write any sympy expression, first we have to declare its symbolic variables. To do this, we can use the following two functions :
- **sympy.Symbol(): It is used to declare a single variable by passing the variable as a string into its parameter.
- **sympy.symbols(): It is used to declare multivariable by passing the variables as a string into its parameter. All the variables must be separated by a space forming a string.
Differentiation
We can differentiate any sympy expression by using **diff(func, var) method. The parameter func denotes the sympy expression to be differentiated and _var denotes the variable with respect to which we have to differentiate.
**Example 1:
Python `
Importing library
import sympy as sym
Declaring variables
x, y, z = sym.symbols('x y z')
expression of which we have to find derivative
exp = x3 * y + y3 + z
Differentiating exp with respect to x
derivative1_x = sym.diff(exp, x) print('derivative w.r.t x: ', derivative1_x)
Differentiating exp with respect to y
derivative1_y = sym.diff(exp, y) print('derivative w.r.t y: ', derivative1_y)
`
**Output:
derivative w.r.t x: 3x**2y
derivative w.r.t y: x3 + 3*y2
We can also find higher derivatives using the **diff(func, var, n) method. Here, the parameter _n denotes the nth derivative to be found.
**Example 2:
Python `
Finding second derivative
of exp with respect to x
derivative2_x = sym.diff(exp, x, 2) print('second derivative w.r.t. x: ', derivative2_x)
Finding second derivative
of exp with respect to y
derivative2_y = sym.diff(exp, y, 2) print('second derivative w.r.t. y: ', derivative2_y)
`
**Output:
second derivative w.r.t. x: 6xy
second derivative w.r.t. y: 6*y
Integration
You can do indefinite and definite integration of transcendental elementary and special functions via **integrate() function.
Syntax for indefinite integration: sympy.integrate(func, var)
Syntax for definite integration: sympy.integrate(func, (var, lower_limit, upper_limit))
The parameter _func denotes the sympy expression to be differentiated, _var denotes the variable with respect to which we have to differentiate, _lower_limit denotes to the lower limit of the definite integration and _upper_limit denotes the upper limit of the definite integration.
**Note: ∞ in SymPy is oo.
**Example 1:
Python `
Indefinite integration of cos(x) w.r.t. dx
integral1 = sym.integrate(sym.cos(x), x) print('indefinite integral of cos(x): ', integral1)
definite integration of cos(x) w.r.t. dx between -1 to 1
integral2 = sym.integrate(sym.cos(x), (x, -1, 1)) print('definite integral of cos(x) between -1 to 1: ', integral2)
definite integration of exp(-x) w.r.t. dx between 0 to ∞
integral3 = sym.integrate(sym.exp(-x), (x, 0, sym.oo)) print('definite integral of exp(-x) between 0 to ∞: ', integral3)
`
**Output:
indefinite integral of cos(x): sin(x)
definite integral of cos(x) between -1 to 1: 2*sin(1)
definite integral of exp(-x) between 0 to ∞: 1
Limits
You can calculate limit of a function by using limit(function, variable, point). So, if you want to compute the limit of f(x) as x->0, you would issue limit(f, x, 0).
**Example:
Python `
Calculating limit of f(x) = x as x->∞
limit1 = sym.limit(x, x, sym.oo) print(limit1)
Calculating limit of f(x) = 1/x as x->∞
limit2 = sym.limit(1/x, x, sym.oo) print(limit2)
Calculating limit of f(x) = sin(x)/x as x->0
limit3 = sym.limit(sym.sin(x)/x, x, 0) print(limit3)
`
**Output:
oo
0
1
Series Expansion
We can also compute Taylor series expansions of functions around a point. To compute the expansion of f(x) around the point x=x0 terms of order xn, use **sympy.series(f, x, x 0 , n). x0 and n can be omitted, in which case the defaults x0=0 and n=6 will be used.
**Example:
Python `
assign series
series1 = sym.series(sym.cos(x), x) print(series1)
assign series
series2 = sym.series(1/sym.cos(x), x, 0, 4) print(series2)
`
**Output:
1 - x2/2 + x4/24 + O(x6)
1 + x2/2 + O(x**4)
The O(x4) or O(x6) term at the end means that all x terms with a power greater than or equal to x4 or x6 are omitted.