Reputation: 21663
I have an expression involving fractional exponents that I want to make into a polynomial recognisable to sympy for solution. I could, if necessary, write the exponents using Rational
but can't make that work.
What can I do?
>>> from sympy import *
>>> var('d x')
(d, x)
>>> (0.125567*(d + 0.04) - d**2.25*(2.51327*d + 6.72929)).subs(d,x**4)
0.125567*x**4 - (2.51327*x**4 + 6.72929)*(x**4)**2.25 + 0.00502268
Upvotes: 2
Views: 827
Reputation:
SymPy does not combine exponents unless it knows it is safe to do so. For complex numbers it's only safe with integer exponents. Since we don't know if x is real or complex, the exponents are not combined.
Even for real x, (x**4)**(9/4)
is not the same as x**9
(consider negative x). If x
is declared real, using x = Symbol('x', real=True)
, then (x**4)**Rational(9, 4)
correctly returns x**8*Abs(x)
instead of x**9
.
If x
is declared positive, x = Symbol('x', positive=True)
, then (x**4)**Rational(9, 4)
returns x**9
.
It is not advisable to use floating point representation of rational numbers in SymPy, especially as exponents. This is why I replaced 2.25 by Rational(9, 4)
above. With 2.25, the result is Abs(x)**9.0
when x is real, and x**9.0
if x is declared positive. The decimal dot indicates these are floating point numbers; so subsequent manipulations will have floating-point results instead of symbolic ones. For example (with x declared positive):
>>> solve((x**4)**Rational(9, 4) - 2)
[2**(1/9)]
>>> solve((x**4)**2.25 - 2)
[1.08005973889231]
Upvotes: 5