Sympy cannot compute integrate(sec(x+1)**2, x) - sympy

I find it strange that sympy cannot evaluate integrate(sec(x+1)**2, x) when it can evaluate integrate(sec(x)**2, x). I've restricted the domain of x just in case and I still can't evaluate the integral of `sec(x+1)**2.
x, y, z = symbols('x, y, z', real=True, positive=True)
Why does sympy struggle with this?

This is arguably a bug in SymPy but you can work around by rewriting sec:
>>> integrate(sec(x+1)**2, x)
Integral(sec(x + 1)**2, x)
>>> _.rewrite(cos) # sin or tan works too
Integral(cos(x + 1)**(-2), x)
>>> _.doit()
-2*tan(x/2 + 1/2)/(tan(x/2 + 1/2)**2 - 1)
>>> simplify(_)
tan(x + 1)

Related

Simplification in sympy

I am new to sympy, and I cannot understand why the result of the following piece of code does not results in f(x)=0
from sympy import *
f = Function('f')
x = Symbol('x')
simplify(Eq(f(x)+1,1))
When SymPy rewrites x + x as 2*x that is automatic rewriting. Not everything is automatic, however, as you have seen. If you want to know what value of f(x) makes that Equality true, you can solve for it:
>>> solve(Eq(f(x) + 1, 1), f(x))
[0]

get coefficient of a monomial in a sympy expression

I have a sympy expression like so:
exp_str = '3 * x**2*y + 4*a**2 * x*y + 9*b * x'
my_expr = sp.parsing.sympy_parser.parse_expr(exp_str)
and I want to get the coefficient of x*y, which should be 4*a**2.
Is there a function that I can pass my_expr to along with a list of variables I want my polynomial to be over? For example, I would need to pass this function x and y so that it knows x and y are variables and that a and b are coefficients.
If there is no such function, and recommendations on how to write code to do this would be appreciated. Thanks
There is a coeff method of sympy expressions:
In [28]: x, y, a, b = symbols('x, y, a, b')
In [29]: expr = 3 * x**2*y + 4*a**2 * x*y + 9*b * x
In [30]: expr.coeff(x*y)
Out[30]:
2
4⋅a
https://docs.sympy.org/latest/modules/core.html?highlight=coeff#sympy.core.expr.Expr.coeff
You might find it useful though to work with expressions as structured polynomials e.g.:
In [31]: p = Poly(expr, [x, y])
In [32]: p
Out[32]: Poly(3*x**2*y + 4*a**2*x*y + 9*b*x, x, y, domain='ZZ[a,b]')
In [33]: p.coeff_monomial(x**2 * y)
Out[33]: 3
In [34]: p.coeff_monomial(x * y)
Out[34]:
2
4⋅a
https://docs.sympy.org/latest/modules/polys/basics.html

How in sympy Disable unnecessary parenthesis?

Tell me please, How to forbid to open brackets? For example,
8 * (x + 1) It should be that way, not 8 * x + 8
Using evaluate = False doesn't help
The global evaluate flag will allow you to do this in the most natural manner:
>>> with evaluate(False):
... 8*(x+1)
...
8*(x + 1)
Otherwise, Mul(8, x + 1, evaluate=False) is a lower level way to do this. And conversion from a string (already in that form) is possible as
>>> S('8*(x+1)',evaluate=False)
8*(x + 1)
In general, SymPy will convert the expression to its internal format, which includes some minimal simplifications. For example, sqrt is represented internally as Pow(x,1/2). Also, some reordering of terms may happen.
In your specific case, you could try:
from sympy import factor
from sympy.abc import x, y
y = x + 1
g = 8 * y
g = factor(g)
print(g) # "8 * (x + 1)"
But, if for example you have g = y * y, SymPy will either represent it as a second power ((x + 1)**2), or expand it to x**2 + 2*x + 1.
PS: See also this answer by SymPy's maintainer for some possible workarounds. (It might complicate things later when you would like to evaluate or simplify this expression in other calculations.)
How about sympy.collect_const(sympy.S("8 * (x + 1)"), 8)?
In general you might be interested in some of these expression manipulations: https://docs.sympy.org/0.7.1/modules/simplify/simplify.html

Sympy step by step solution of integrals

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.
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.

How to group integrals in sympy?

Suppose an expression contains nested integrals, for example:
I'd like to "group" (not sure if this is the right word) the integrals in the front of the expression, if possible. The result in this case would be
I am working with equations that require me to change the order of integration, and I think this form would be more useful.
Is there an existing function in sympy that would do this?
I think this is what you want:
In [1]: from sympy import *
In [2]: from sympy.abc import x, y
In [3]: a, b = Function('a'), Function('b')
In [4]: Integral(a(x)*b(y), y, x)
Out[4]:
⌠ ⌠
⎮ ⎮ a(x)⋅b(y) dy dx
⌡ ⌡
Update: Because code in comment is awful
In [14]: Integral(a(x)*b(y), y, x).doit()
Out[14]:
⎛⌠ ⎞ ⌠
⎜⎮ a(x) dx⎟⋅⎮ b(y) dy
⎝⌡ ⎠ ⌡