What means W(-k) from sympy solve result? - sympy

I was using sympy to find roots of the function x+k*exp(-x)
My Python code is the following :
from sympy import *
init_printing()
init_session()
k=symbols('k',real=True,nonzero=True)
f=Function('f')
f=x+k*E**(-x)
solve(f,x)
The result of the solve command is [W(-k)]
Can you explain to me the signification of this result?

Related

sympy function compose - bizzare results

I'm trying to compose two functions and I get a bizzare result
'''
#!/usr/bin/python
from sympy import *
init_printing(use_unicode=True)
x= symbols('x')
f = x/(x+1);
g = x/(x+2);
print(compose(f,g))
This shows : x/((x + 1)*(x + 2))
Should be x/(2x+2)
I don't get it. Does anyone has an idea?
Thanks
Despite being available in the top-level sympy namespace under the plain name compose, sympy.compose doesn't actually do general function composition.
sympy.compose is actually sympy.polys.polytools.compose. It's actually a function for polynomial composition. When you try to compose x/(x+1) and x/(x+2), it ends up interpreting these inputs as multivariate polynomials in 3 variables, x, 1/(x+1), and 1/(x+2), and the results are total nonsense.

Quadratic equation with SymPy

Very strange behaviour of solve command in SymPy, when I try to solve quadratic equation with parameter a:
from sympy import *
x, a = symbols("x a")
eq = Eq(0, (4 - 4*x + x**2)/(4*a**2))
print(solve(eq, x))
print(solve(simplify(eq), x))
Output:
[2 - sqrt(a**2 - 1)/a, 2 + sqrt(a**2 - 1)/a]
[2]
Just solve gives two (!) solutions, which depend on a. After symplifying it gives only solution x=2, which is correct. What happens? Command solveset works correctly, but I am interested in using solve command. My SymPy version is 1.9.
This is a bug in as_numer_denom which get an expression that starts as an Add but is essentially a Mul. It is being corrected with this PR.

No solution to "x^-d = e^100" (negative exponents)?

I am trying to automatically solve $x**-d - sympy.exp(100)$ with sympy.
import sympy
from sympy import solve, Symbol
x = Symbol('x')
d = Symbol('d', integer=True)
print(solve(x**-d - sympy.exp(100), x))
returns no solutions [], but the solution should be $x=e^{-100/d}$ (see e.g., wolframalpha)
What did I do wrong? Same result when removing integer=True.
However,
print(solve(x**-d - sympy.exp(100), x))
returns [exp(100/d)].
The solution appears when solve(check=False) is given.
see Solving Equations Involving Radicals of
http://www.cfm.brown.edu/people/dobrush/am33/SymPy/solvers.html

How do I display a full expression in sympy?

I am trying to use sympy in a Jupyter notebook to document and perform a series of mathematical cacluations in a reporducible way.
If I define the following:
from sympy import *
init_printing()
x, y, z = symbols("x y z")
x=y+z
x
then I can display the value of x (that is, y+z).
How do I display the full equation (x=y+z)?
Running Eq(x,y+z), even with evaluate=False) returns the expression with the value of x substituted (y+z=y+z).
I tried using Eq(S('x'),y+z), also Eq(S('x'),x) and sympy keep returning a boolean variable.
So I found a way to display it using the Ipython built-in functions:
from sympy import *
from IPython.display import display, Math
init_printing()
x, y, z = symbols("x y z")
x=y+z
display(Math('x = '+latex(x)))
I think that this is a more general solution to the problem.
Although you first declare x as a sympy.Symbol, once you perform the assignment x=y+z, x becomes an alias for y+z. Whenever you use x from that point after, x will be automatically translated by python as y+z.
If you insist on this workflow, you could use Eq(S('x'),y+z) to display the equation.
I know this isn't exactly the answer, but for those just looking for a neat print of the right-hand-side of a function f(x,y,z,...), you can just do f.subs(x,x) like so:
import sympy as sp
x,y,z=sp.symbols('x,y,z')
f=x+2*y+3*sp.exp(z)
f.subs(x,x)

how to print polynomial equation using numpy?

I want print a polynomial equation for the given data. I am using this line of code, however I am not sure this the correct way to get the polynomial equation for the given data
fit2 = numpy.polyfit(temp[0][0],temp[0][1],deg=2)
y1=numpy.poly1d(fit2)
Please advise
Sympy has some nice features do that simply:
from sympy import Symbol,expand
fit2 = numpy.polyfit(temp[0][0],temp[0][1],deg=2)
y1=numpy.poly1d(fit2)
x=Symbol('x')
print(expand(y1(x)))
for:
-2.6666666666668*x**3 + 41.0000000000018*x**2 - 195.333333333342*x + 323.000000000013