Examples of how to write functions and integrate them in integrals.py
using sympy
To obtain the Latex
for the function or result you can execute
print(smp.latex(function))
Where function can be passed as the result of the integration or the function itself.
This can help view the result better if the pprint()
function is too complex
-
$x^n$ =x ** n
-
$\log_{y}(x)$ =smp.log(x, y)
-
$e^x$ =smp.exp(x)
-
$\sqrt{x}$ =smp.sqrt(x)
-
$\frac{a}{b}$ =smp.Rational(a,b)
-
$\cos(x)$ =smp.cos(x)
-
$\sin(x)$ =smp.sin(x)
-
$\tan(x)$ =smp.tan(x)
-
$x^n$ does not requiresmp
beforehand as it is base python notation -
smp.Rational(a,b) is used for the number to be seen as a fraction instead of a floating point number
-
The rest of the trigonometric functions can be obtained by using:
smp.asin(x)
smp.acos(x)
smp.atan(x)
smp.acot(x)
smp.sinh(x)
smp.cosh(x)
smp.tanh(x)
smp.coth(x)
smp.asinh(x)
smp.acosh(x)
smp.atanh(x)
smp.acoth(x)
Calculate the integral of
# Define x as a real variable for the function
x = smp.symbols('x', real = True)
# Create the function x^2
function = x ** 2
# Send to integrate the function in terms of x
result = smp.integrate(function, x)
Result:
Calculate the integral of
# Define x as a real variable for the function
x = smp.symbols('x', real = True)
# Create the function
function = x / 2
# Send to integrate the function in terms of x
result = smp.integrate(function, x)
Result:
Calculate the integral of
# Define x as a real variable for the function
x = smp.symbols('x', real = True)
# Create the function
function = 1 / (smp.sqrt(x) * (x + 1))
# Send to integrate the function in terms of x
result = smp.integrate(function, x)
Result: