Why Can't Sympy Integrate 1/\sqrt{x^2+x} - python

I tried this:
import sympy as sy
from sympy.abc import x
sy.integrate(1/sy.sqrt(x**2+x),x)
But sympy simply returned a repeat of the integration instead of giving the result of it which SageMath would do.
This should be easy to integrate.

I'm not sure what's happening. If you knew x to be real and positive, you could try:
import sympy as sy
x = sy.symbols('x', real=True, positive=True)
sol = sy.integrate(1/sy.sqrt(x**2+x), x)
print(sol)
print(sol.doit())
For some reason, this only gives a solution if you add doit().
Output:
Integral(1/(sqrt(x)*sqrt(x + 1)), x)
2*acosh(sqrt(x + 1))

Related

Evaluating the solution of sympy dsolve numerically

I would like to evaluate the solution of a differential equation against some x_test
array
from sympy import *
init_printing()
from __future__ import division
from sympy import *
x, y, z, t = symbols('x y z t')
# Constants
C, R, u_rest = symbols('C R u_rest')
f, g, h = symbols('f g h', cls=Function)
solution = dsolve(C*Derivative(f(x), x) + (1/R)*(f(x) - u_rest ),f(x))
x_text = np.array(range(0,100))
but I fail
# 1. attempt with evalf(Not working)
solution.args[1].evalf(subs={x: 3.14})
# 2. attempt with lambdify(Not working)
lambdify(x,solution.args[1])(3.14)
What is the right way to do it?
If you look at your solution.args[1] value you will see that it is an expression of several variables, including x. It will not evaluate to a number until you supply values for all variables. Your first attempt doesn't fail, but you don't explain why it is not giving you what you hoped for:
>>> solution.args[1].evalf(subs={x: 3.14})
u_rest*(1.0 - exp((C1 - 3.14/R)/C))

non-commutative generators when using Sympy

%reset -f
from sympy import *
import sympy as sym
from sympy.solvers import solve
b1=sym.Array([[Rational(-1/2),sqrt(3)/2,0],[-sqrt(3)/2,Rational(-1/2),0],[0,0,1]])
Sigma=[]
List=[]
x, y, z = symbols('\sigma_{11} \sigma_{12} \sigma_{13}',commutative=False)
k, m, n = symbols('\sigma_{21} \sigma_{22} \sigma_{23}',commutative=False)
f, g, h = symbols('\sigma_{31} \sigma_{32} \sigma_{33}',commutative=False)
a1 = sym.Matrix([[x, y, z], [k,m,n], [f, g,h]])
for i in range(0,3):
for j in range(0,3):
args = []
lists=[]
for k in range(0,3):
for l in range(0,3):
x= symbols(('\sigma_{%d%d}')%(k+1,l+1),commutative=False)
M=sym.Array([x])
Sigmatotal_tmp=tensorproduct(b1[i][k],b1[j][l],M)
args.append(Sigmatotal_tmp[0])
Sigma.append(Add(*args))
When trying to execute solve(Sigma) I get the error
non-commutative generators: (\sigma_{11}, \sigma_{12},
\sigma_{13}, \sigma_{21}, \sigma_{22}, \sigma_{23}, \sigma_{31},
\sigma_{32}, \sigma_{33})
I tried several ways, they all give either error or wrong answer. Any help?
In your question here you say that you don't want x + y = y + x. This is not what SymPy means when it says something is commutative or not. If x and y are not commutative then x*y != y*x. I am not aware of anything in SymPy that will deal with the property that you are dealing with.
The solution to your equations (consistent with your hand solution indicated in comments) is the answer obtained in the original question.

Python: Integrate product of exponential and gaussian function

I have a function y which is a product of y1 = exp(-x) and exp(-x^2):
y1 = exp(-x)
y2 = exp(-x**2)
y = y1*y2 = exp(-x)*exp(-x**2) = exp(-x **2-x)
Integrating the functions y1 or y2 works fine using sympy:
>>> import sympy as sy
>>> import numpy as np
>>> x = sy.Symbol('x')
>>> y1 = sy.exp(-x)
>>> sy.integrate(y1, (x, 0, np.inf))
1
and
>>> y2 = sy.exp(-x**2)
>>> sy.integrate(y2, (x, 0, np.inf))
0.5*sqrt(pi)
However, whenever I'm trying to integrate the product y1*y2 = y, the integral is not accepted:
>>> y = y1*y2
>>> sy.integrate(y, (x, 0, np.inf))
Integral(exp(-x)*exp(-x**2), (x, 0, np.inf))
Perhaps I'm blind and missing something obvious.
If sympy cannot evaluate it symbolically, then it just returns an Integral object. You need to either use a more powerful symbolic calculator like maxima or Wolfram|Alpha (link to solution)), or else resort to numerical integration. Here are a few options for numerically integrating it:
import sympy as sy
import numpy as np
import scipy as sp
import scipy.integrate
x = sy.Symbol('x')
y1 = sy.exp(-x)
y2 = sy.exp(-x**2)
y = y1*y2
# Pure scipy without sympy
print(sp.integrate.quad(lambda x: np.exp(-x) * np.exp(-(x**2)), 0, np.inf))
# Mix of scipy and sympy
print(sp.integrate.quad(lambda x_arg: y.subs(x, x_arg).evalf(), 0, np.inf))
# Without using scipy
print(sy.mpmath.quad(sy.lambdify([x], y), [0, np.inf]))
I believe the Integral is being accepted and it is giving you a valid sympy result. I used this site and it indicates (I am not enough of a mathematician to verify it) the answer has the error function (erf) in it:
There is no exact analytical answer, so it is giving you it's internal representation. I used Mathematica years ago and it would do something similar, but it provided a polynomial approximation to the solution. BTW, the answer is approximately 0.5456413607650471.
I used http://www.codecogs.com/latex/eqneditor.php to generate the equation image.

Rewrite equation as polynomial

from sympy import *
K, T, s = symbols('K T s')
G = K/(1+s*T)
Eq1 =Eq(G+1,0)
I want to rewrite equation Eq1 with sympy as polynomial: 1+K+T*s==0
How would I do this?
I spent some hours of searching and trying simplifications methods but could not find a elegant, short solution.
The actual problem in SymPy:
from IPython.display import display
import sympy as sp
sp.init_printing(use_unicode=True,use_latex=True,euler=True)
Kf,Td0s,Ke,Te,Tv,Kv,s= sp.symbols("K_f,T_d0^',K_e,T_e,T_v,K_v,s")
Ga= Kf/(1+s*Tv)
Gb= Ke/(1+s*Te)
Gc= Kf/(1+s*Td0s)
G0=Ga*Gb*Gc
G1=sp.Eq(G0+1,0)
display(G1)
How to tell Sympy to rewrite equation G1 as polynomial in shape s^3*(...)+s^2*(...)+s*(...)+(...)=... ?
The actual problem from textbook: http://i.imgur.com/J1MYo9H.png
How it should look like: http://i.imgur.com/RqEDo7H.png
The two equations are equivalent.
Here's what you can do.
import sympy as sp
Kf,Td0s,Ke,Te,Tv,Kv,s= sp.symbols("K_f,T_d0^',K_e,T_e,T_v,K_v,s")
Ga= Kf/(1+s*Tv)
Gb= Ke/(1+s*Te)
Gc= Kf/(1+s*Td0s)
G0=Ga*Gb*Gc
Throw away the denominator
eq = (G0 + 1).as_numer_denom()[0]
Expand the equation and collect terms with powers of s.
eq = eq.expand().collect(s)
Final Equation
Eq(eq, 0)
Eq(K_e*K_f**2 + T_d0^'*T_e*T_v*s**3 + s**2*(T_d0^'*T_e + T_d0^'*T_v + T_e*T_v) + s*(T_d0^' + T_e + T_v) + 1, 0)

Expected square matrix?

Kindly help in finding out the error.
import sympy as sp
from scipy.linalg import solve
x, y, z,w = sp.symbols('x,y,z,w')
alpha,beta, gamma, delta = sp.symbols('alpha,beta,gamma,delta')
f1 = alpha*y*z - beta*w
f2= gamma*z - delta*x
f3=-alpha*y*z + beta*w + x
f4= -alpha*y*z + beta*w - gamma*z + delta*x
K=solve(f1,f2,f3,f4)
print K
The error showing is
Expected square matrix
scipy.linalg.solve does not solve SymPy equations. Use a SymPy solver. See, for example, http://docs.sympy.org/dev/modules/solvers/solvers.html

Categories