Sympy step by step solution of integrals
Asked Answered
M

2

6

In doc of sympy http://docs.sympy.org/latest/modules/integrals/integrals.html we can read:

The manualintegrate module has functions that return the steps used (see the module docstring for more information).

but calling help(sympy.integrals.manualintegrate) we get:

Help on function manualintegrate in module sympy.integrals.manualintegrate:

manualintegrate(f, var)
manualintegrate(f, var)

Compute indefinite integral of a single variable using an algorithm that
resembles what a student would do by hand.

Unlike ``integrate``, var can only be a single symbol.

Examples
========

>>> from sympy import sin, cos, tan, exp, log, integrate
>>> from sympy.integrals.manualintegrate import manualintegrate
>>> from sympy.abc import x
>>> manualintegrate(1 / x, x)
log(x)
>>> integrate(1/x)
log(x)
>>> manualintegrate(log(x), x)
x*log(x) - x
>>> integrate(log(x))
x*log(x) - x
>>> manualintegrate(exp(x) / (1 + exp(2 * x)), x)
atan(exp(x))
>>> integrate(exp(x) / (1 + exp(2 * x)))
RootSum(4*_z**2 + 1, Lambda(_i, _i*log(2*_i + exp(x))))
>>> manualintegrate(cos(x)**4 * sin(x), x)
-cos(x)**5/5
>>> integrate(cos(x)**4 * sin(x), x)
-cos(x)**5/5
>>> manualintegrate(cos(x)**4 * sin(x)**3, x)
cos(x)**7/7 - cos(x)**5/5
>>> integrate(cos(x)**4 * sin(x)**3, x)
cos(x)**7/7 - cos(x)**5/5
>>> manualintegrate(tan(x), x)
-log(cos(x))
>>> integrate(tan(x), x)
-log(sin(x)**2 - 1)/2

See Also
========

sympy.integrals.integrals.integrate
sympy.integrals.integrals.Integral.doit
sympy.integrals.integrals.Integral

I don't see step by step solution.

Malayopolynesian answered 26/2, 2018 at 22:10 Comment(1)
Unfortunately, due to the way Python imports work, when a function and a submodule of the same name exist, the function takes precedence, so sympy.integrals.manualintegrate is the function, not the module. If you run from sympy.integrals import manualintegrate it will give you the module.Istle
S
7

You are looking at the docstring of the function manualintegrate, not of the module manualintegrate. The module is here and it says

This module also provides functionality to get the steps used to evaluate a particular integral, in the integral_steps function. This will return nested namedtuples representing the integration rules used.

The integral_steps function is documented thus:

Returns the steps needed to compute an integral. This function attempts to mirror what a student would do by hand as closely as possible. SymPy Gamma uses this to provide a step-by-step explanation of an integral. The code it uses to format the results of this function can be found at https://github.com/sympy/sympy_gamma/blob/master/app/logic/intsteps.py.

Unless you are using SymPy Gamma, the output of integral_steps will be hard to read. Example:

from sympy.integrals.manualintegrate import integral_steps
integral_steps(x*sin(3*x), x)

returns

PartsRule(u=x, dv=sin(3*x), v_step=URule(u_var=_u, u_func=3*x, constant=1/3, substep=ConstantTimesRule(constant=1/3, other=sin(_u), substep=TrigRule(func='sin', arg=_u, context=sin(_u), symbol=_u), context=sin(_u), symbol=_u), context=sin(3*x), symbol=x), second_step=ConstantTimesRule(constant=-1/3, other=cos(3*x), substep=URule(u_var=_u, u_func=3*x, constant=1/3, substep=ConstantTimesRule(constant=1/3, other=cos(_u), substep=TrigRule(func='cos', arg=_u, context=cos(_u), symbol=_u), context=cos(_u), symbol=_u), context=cos(3*x), symbol=x), context=-cos(3*x)/3, symbol=x), context=x*sin(3*x), symbol=x)

It's much more readable on SymPy Gamma site.

Sparry answered 26/2, 2018 at 22:26 Comment(5)
Where can I find a function to convert that output? I would like to use it in Jupyter.Malayopolynesian
The link is in the integral_steps docstring quoted above. It outputs HTML, so you would need some modifications to make it work in a notebook.Sparry
I tried in Jupyter but I have: ModuleNotFoundError: No module named 'stepprinter'Malayopolynesian
Because that module is in sympy gamma which is not a part of sympy, so you don't have it installed. You'll have to find a way to install sympy gamma code in your Python environment.Sparry
Yes, I tried to download all logic folder but nothing. Maybe I am not doing it in right way.Malayopolynesian
T
0

Following the tip from user6655984, I was able to adjust the Sympy Gama formatting code to output to the console in LaTex format. I'm still working on getting every rule to translate correctly, but here is what I have for now: sympy gama

Hope this helps!

Theomachy answered 6/7, 2023 at 18:49 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Barbee

© 2022 - 2024 — McMap. All rights reserved.