[Tutor] Math: integers to a fractional power (Matthew Denaburg)

C or L Smith smiles at worksmail.net
Tue Nov 16 07:26:09 CET 2010


If you are interested in a symbolic solution or numerical solution without missing any of the possible roots (real or imaginary) you could check out sympy, a CAS that offers a quartic solution. It uses appropriate simplifications besides a general solution to the quartic. The code for this is in the polys/polyroots.py file; that code makes some calls to other routines in sympy so it's not totally self contained.

It can be downloaded from sympy.org. It can be sandboxed at live.sympy.org (but that has an error in the quartic solution right now).  It's a pretty quick install.

Here it is in use:

>>> from sympy import solve, Rational, var, nsimplify
>>> var('x') # create a variable
x
>>> solve(x**4 + 3*x**3 - 2*x + x/3 + .7) # note the 0.7 float
...
... error message ending with
...
CoercionFailed: can't convert 2.1 of type RR to ZZ

So either use nsimplify to replace floats with rationals
>>> ans = solve(nsimplify(x**4 + 3*x**3 - 2*x + x/3 + .7, rational=1))
>>> ans[0].n()
-2.74495954970930

Or enter floats as Rationals from the start
>>> ans = solve(x**4 + 3*x**3 - 2*x + x/3 + Rational(7, 10))
>>> ans[0].n()
-2.74495954970930

You can use a symbol for the number that will take on a float
>>> var('a')
a
>>> ans = solve(x**4 + 3*x**3 - 2*x + x/3 + a, x) # now indicate you want to solve for x

Substitute 'a' with your desired float:
>>> [w.subs(a, .7).n() for w in ans] # .n() means "give a numerical answer"
[0.423047811447149 - 0.229393202547502*I, -2.74495954970930, 0.423047811447149 + 0.229393202547502*I, -1.10113607318500]

The answer is there at index 1.

Best regards,
 Chris


More information about the Tutor mailing list