sympy solve unable to find roots - sympy

sympy does not find the two real roots of this rather straightforward equation:
import sympy
x = Symbol('x')
solve(-2*x**2*exp(1 - x**2) + exp(1 - x**2),x)
Returns "[oo]"
Is this a bug or is my command ill formed ?
Thanks!
Markus

It seems to work fine for me, so perhaps you just need to upgrade?
>>> from sympy import Symbol, solve, exp
>>> x = Symbol('x')
>>> solve(-2*x**2*exp(1 - x**2) + exp(1 - x**2),x)
[-sqrt(2)/2, sqrt(2)/2]
>>> sympy.__version__
'0.7.2-git'

Related

Sympy performance issue with solving exp related equation

I have an equation: f = (100*E**(-x*1600)-50), it takes a very long time to give a solution for x but in another online solver, it only takes a few ms. How can I do to boost the performance?
raw code:
f = (100*E**(-x*1600)-50)
solve(f,x)
Expected result:
If you just want the real solution (and not the many symbolic solutions) use nsolve. The bisect solvers is appropriated for this function with a steep gradient near the root. Defining lam = var('lam') gives
If you want a symbolic solution (but not the many imaginary ones) then solveset or the lower level _invert can be used:
>>> from sympy import solveset, var, Reals
>>> from sympy.solvers.solvers import _invert
>>> var('x')
x
>>> eq=(100*E**(-x*1600)-50)
>>> solveset(eq,x,Reals)
{log(2)/1600}
>>> from sympy.solvers.solvers import _invert
>>> _invert(eq,x)
(log(2)/1600, x)

Sympy cannot solve my equation and gets stuck

I have a simple equation, trying to solve for using symbolic, however the code gets stuck and I do not get an error for me to debug. How can I do this correctly?
from sympy import *
from sympy import init_printing
init_printing(use_latex = True)
import sympy as sp
from numpy import random
import numpy as np
import math
from decimal import *
timeS = symbols("t")
eq1 = Eq( (-4221.4125*exp(-2750.0*timeS)*sin(6514.4071*timeS) + 10000.0*exp(-2750.0*timeS)*cos(6514.4071*timeS)),8000)
T_off = solve(eq1, timeS )[0]
display("T_off = ",T_off)
Your equation looks like one that doesn't have an analytic solution so you should use nsolve instead. There is a root between -0.001 and 0 so the bisect method gives:
In [59]: nsolve(eq1, timeS, [-0.001, 0])
Out[59]: 3.46762014687136e-5
This is an ill-posed equation. Rescale by dividing both sides by 10**4 and replace timeS with Symbol(x)/10**4 and use nsolve
>>> eq2 = eq1.func(eq1.lhs/10**4, eq1.rhs/10**4).subs(timeS, symbols('x')/10**4)
>>> from sympy import nsolve
>>> nsolve(eq2,0)
0.346762014687135
So timeS ~= 0.35e-4. The sqrt function automatically scales sums so sqrt(eq1.rewrite(Add)) will reduce coefficients of the terms to something less than 1 but such simplification is not part of SymPy simplification of general epxressions at present.

Is there a complete example to write a mathematical expression in sympy to a Microsoft Word document?

this might be a silly question. But I am desperate. I am a math teacher and I try to generate Math tests. I tried Python for this and I get some things done. However, I am not a professional programmer, so I get lost with MathMl, prettyprint() and whatsoever.
Is there anybody who can supply me a complete example that I can execute? It may just contain one small silly equation, that does not matter. I just want to see how I can get it into a Word document. After that, I can use that as a basis. I work on a Mac.
I hope anyone can help me out. Thanks in advance!
Best regards, Johan
This works for me:
from sympy import *
from docx import Document
from lxml import etree
# create expression
x, y = symbols('x y')
expr1 = (x+y)**2
# create MathML structure
expr1xml = mathml(expr1, printer = 'presentation')
tree = etree.fromstring('<math xmlns="http://www.w3.org/1998/Math/MathML">'+expr1xml+'</math>')
# convert to MS Office structure
xslt = etree.parse('C:/MML2OMML.XSL')
transform = etree.XSLT(xslt)
new_dom = transform(tree)
# write to docx
document = Document()
p = document.add_paragraph()
p._element.append(new_dom.getroot())
document.save("simpleEq.docx")
How about the following. The capture captures whatever is printed. In this case I use pprint to print the expression that I want written to file. There are lots of options you can use with pprint (including wrapping which you might want to set to False). The quality of output will depend on the fonts you use. I don't do this at all so I don't have a lot of hints for that.
from pprint import pprint
from sympy.utilities.iterables import capture
from sympy.abc import x
from sympy import Integral
with open('out.doc','w',encoding='utf-8') as f:
f.write(capture(lambda:pprint(Integral(x**2, (x, 1, 3)))))
When I double click (in Windows) on the out.doc file, a word equation with the integral appears.
Here is the actual IPython session:
IPython console for SymPy 1.6.dev (Python 3.7.3-32-bit) (ground types: python)
These commands were executed:
>>> from __future__ import division
>>> 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)
>>> init_printing()
Documentation can be found at https://docs.sympy.org/dev
In [1]: pprint(Integral(x**2, (x, 1, 3)))
3
(
? 2
? x dx
)
1
In [2]: from pprint import pprint
...: from sympy.utilities.iterables import capture
...: from sympy.abc import x
...: from sympy import Integral
...: with open('out.doc','w',encoding='utf-8') as f:
...: f.write(capture(lambda:pprint(Integral(x**2, (x, 1, 3)))))
...:
{problems pasting the unicode here, but it shows up as an integral symbol in console}

Sympy derivative with a non-symbol

For a project i am working on i need the derivative of a function against wrt cos(theta) but when using Sympy v1.5.1 get an error message stating non-symbols cannot be used as a derivative. This was no problem up to Sympy v1.3 but later versions give this error.
>>> l=1
>>> theta = symbols('theta')
>>> eq=diff((cos(theta)**2-1)**l,cos(theta),l)
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/base/data/home/apps/s~sympy-live-
hrd/20200105t193609.423659059328302322/sympy/sympy/core/function.py", line 2446, in diff
return f.diff(*symbols, **kwargs)
File "/base/data/home/apps/s~sympy-live-
hrd/20200105t193609.423659059328302322/sympy/sympy/core/expr.py", line 3352, in diff
return Derivative(self, *symbols, **assumptions)
File "/base/data/home/apps/s~sympy-live-
hrd/20200105t193609.423659059328302322/sympy/sympy/core/function.py", line 1343, in __new__
__)))
ValueError:
Can't calculate derivative wrt cos(theta).
According to the Sympy documentation (https://docs.sympy.org/latest/modules/core.html#sympy.core.function.Derivative) i may be able to solve this using:
>>> from sympy.abc import t
>>> F = Function('F')
>>> U = f(t)
>>> V = U.diff(t)
>>> direct = F(t, U, V).diff(U)
Unfortunately i can't get this to work with this equation in Sympy v1.5.1.
Suggestions/help are much appreciated.
derivative of a function against wrt cos(theta)
Did this really work before in sympy? i.e. you were able to differentiate w.r.t cos(theta)? This should not work as differentiation is w.r.t to a symbol. For example Maple also gives error
diff( 1+cos(theta)^2,cos(theta))
Error, invalid input: diff received cos(theta), which is not valid for its 2nd argument
Strange that Mathematica does allow this. But I think this is not good behavior. May be that is why sympy no longer allows it.
But you can do this in sympy
from sympy import *
theta,x = symbols('theta x')
eq = (cos(theta)**2-1)**2
result = diff( eq.subs(cos(theta),x) ,x)
result.subs(x,cos(theta))
Which gives
4*(cos(theta)**2 - 1)*cos(theta)
In Mathematica (which allows this)
D[(Cos[theta]^2 - 1)^2, Cos[theta]]
gives
4 Cos[theta] (-1 + Cos[theta]^2)
Perhaps SymPy over-corrected. If the expression has a single generator matching the function of interest then the substitution-equivalent differentiation could take place. Cases which shouldn't (probably) be allowed are (x + 1).diff(cos(x)), sin(x).diff(cos(x)), etc... But (cos(x)**2 - 1).diff(cos(x)) should (probably) be ok. As #Nasser has indicated, a simple substitution/differentiation/backsubstitution will work.

Can sympy solve this ODE (y')**2 = y

Using Python 3.6.5 |Anaconda, Inc.| (default, Apr 29 2018, 16:14:56)
[GCC 7.2.0] on linux
Trying to see if sympy can solve $y'(x)^2 = y(x)$. The general solution can be easily solved by hand. But sympy gives [] for solution.
Did I type something wrong? Is there a trick to make sympy solve this?
>>> from sympy import *
>>> y = Function('y')
>>> x = symbols('x')
>>> dsolve( Eq( Derivative(y(x),x)**2 , y(x) ), y(x))
[]
Maple gives both the singular and general solution
ode:=( diff(y(x),x))^2=y(x);
dsolve( ode,y(x));
Mathematica gives the general solution only
ode=(y'[x])^2==y[x]
DSolve[ode,y[x],x]
Can sympy solve this ODE?
The methods implemented in SymPy focus on the ODEs in which the highest derivative of the unknown appears on its own, not inside of some function. Once the ODE is rewritten as y'(x) = (+ or -) sqrt(y(x)), SymPy solves it.
>>> dsolve(Eq(Derivative(y(x), x), sqrt(y(x))), y(x))
Eq(y(x), C1**2/4 + C1*x/2 + x**2/4)
>>> dsolve(Eq(Derivative(y(x), x), -sqrt(y(x))), y(x))
Eq(y(x), C1**2/4 - C1*x/2 + x**2/4)
This reduction can be done automatically with solve, starting with the original form of the equation:
rhs = solve(Eq(Derivative(y(x), x)**2, y(x)), Derivative(y(x), x))
[dsolve(Eq(Derivative(y(x), x), r), y(x)) for r in rhs]