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
⎝⌡ ⎠ ⌡
Related
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]
I'm not sure if the following is possible. I tried looking at the docs, but see a lot of physics stuff and not quite what I want.
Is it possible to be like matrixcalculus.org, and specify say a general matrix Q of shape (m,n), x, a vector of shape n, and do this kind of calculation. Rather than limiting it to a set number of dimensions and specifying each element individually
You can use MatrixSymbol:
In [5]: n = Symbol('n')
In [6]: Q = MatrixSymbol('Q', n, n)
In [7]: x = MatrixSymbol('x', n, 1)
In [8]: f = x.T # Q # x
In [9]: f
Out[9]:
T
x ⋅Q⋅x
In [10]: diff(f, x)
Out[10]:
T
Q⋅x + Q ⋅x
If you substitute a concrete value for n then as_explicit can give you the expanded result:
In [11]: diff(f, x).subs(n, 2).as_explicit()
Out[11]:
⎡2⋅Q₀₀⋅x₀₀ + Q₀₁⋅x₁₀ + Q₁₀⋅x₁₀⎤
⎢ ⎥
⎣Q₀₁⋅x₀₀ + Q₁₀⋅x₀₀ + 2⋅Q₁₁⋅x₁₀⎦
https://docs.sympy.org/latest/modules/matrices/expressions.html
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)
I want to solve the following equation for x with SymPy:
(Note that the equation can be simplified as mentioned in the comments, I copied it verbatim from an example in a legal document.)
According to my understanding, this translates to the following SymPy expression:
from sympy import Sum, solve
from sympy.abc import k, x
solve(350 - 18500 + Sum(182.94 * (1/(1+x)**(k/12)), (k, 1, 120)), x)
However, when I run this, the result is empty:
[]
What am I doing wrong?
solve probably shouldn't give [] but you will get better results from nsolve for this expression using a guess for x near 0:
>>> from sympy.abc import k, x
>>> from sympy import nsolve
eq = 350 - 18500 + Sum(182.94 * (1/(1+x)**(k/12)), (k, 1, 120))
>>> nsolve(eq, 0)
0.0397546543274819
>>> eq.subs(x,_).round(2)
0
I'm trying to setup sympy to calculate derivatives. When I test it with simple equation, I'm finding the same answer (equality is true between sympy calculation and my own calculation). However when I try with more complicated ones, when it doesnt work (I checked answers with wolfram alpha too).
Here is my code:
from __future__ import division
from sympy import simplify, cos, sin, expand
from sympy import *
x, y, z, t = symbols('x y z t')
k, m, n = symbols('k m n', integer=True)
f, g, h = symbols('f g h', cls=Function)
equation = (x**3*y-x*y**3)/(x**2+y**2)
equation2 = (x**4*y+4*x**2*y**3-y**5)/((x**2+y**2)**2)
pprint(equation)
print ""
pprint(equation2)
print diff(equation,x) == equation2
This is a common "gotcha" in Sympy. For creating symbolic equalities, you should use sympy.Eq and not = or == (see the tutorial). For your example,
Eq(equation.diff(x), equation2).simplify()
True
Note, as above, that you may have to call simplify() in order to see wheather the Eq object corresponds to True or False