-
Installing SymPy:
pip install sympy
-
Importing SymPy:
import sympy as sp
-
Defining Symbols:
x, y, z = sp.symbols('x y z')
-
Defining Functions:
f = x**2 + y**2 g = sp.sin(x) * sp.exp(y)
-
Algebraic Manipulations:
-
Expand:
expanded = sp.expand((x + y)**3) print(f"Expanded: {expanded}")
-
Factor:
factored = sp.factor(x**3 - x**2 + x - 1) print(f"Factored: {factored}")
-
Simplify:
simplified = sp.simplify((x**2 - y**2) / (x - y)) print(f"Simplified: {simplified}")
-
-
Calculus Operations:
-
Derivative:
derivative = sp.diff(f, x) print(f"Derivative of {f} with respect to x: {derivative}")
-
Integral:
integral = sp.integrate(f, x) print(f"Integral of {f} with respect to x: {integral}")
-
Definite Integral:
definite_integral = sp.integrate(f, (x, 0, 1)) print(f"Definite integral of {f} from 0 to 1: {definite_integral}")
-
Limit:
limit = sp.limit(sp.sin(x) / x, x, 0) print(f"Limit of sin(x)/x as x approaches 0: {limit}")
-
-
Solving Equations:
equation = sp.Eq(x**2 + y, 1) solutions = sp.solve(equation, x) print(f"Solutions to the equation {equation}: {solutions}")
-
Matrix Operations:
matrix = sp.Matrix([[1, 2], [3, 4]]) determinant = matrix.det() inverse = matrix.inv() print(f"Matrix: {matrix}") print(f"Determinant: {determinant}") print(f"Inverse: {inverse}")
-
Series Expansion:
series = sp.series(sp.sin(x), x, 0, 10) print(f"Series expansion of sin(x) around x=0: {series}")
-
Substitution:
substituted = f.subs(x, 1) print(f"Substituting x=1 in {f}: {substituted}")
Combining some of the above operations into a coherent example:
import sympy as sp
# Define the variable
x = sp.symbols('x')
# Define the function to integrate
f = x**2
# Perform the indefinite integration
integral = sp.integrate(f, x)
# Simplify the integral
integral_simplified = sp.simplify(integral)
# Display the result
print(f"The integral of {f} with respect to x is {integral_simplified}")
# Perform the definite integration from 0 to 1
definite_integral = sp.integrate(f, (x, 0, 1))
# Display the definite integral result
print(f"The definite integral of {f} from 0 to 1 is {definite_integral}")
# Evaluate the definite integral numerically
definite_integral_evaluated = definite_integral.evalf()
# Display the numerical evaluation
print(f"The numerical evaluation of the definite integral is {definite_integral_evaluated}")The integral of x**2 with respect to x is x**3/3
The definite integral of x**2 from 0 to 1 is 1/3
The numerical evaluation of the definite integral is 0.333333333333333