[Tutor] Math: integers to a fractional power

Matthew Denaburg scifiman4 at verizon.net
Mon Nov 15 21:35:27 CET 2010


Hi,

	OK, so I have a question for you math people: I am trying to solve a quartic equation (Ax^4 + Bx^3 + Cx^2 + Dx + E) using Ferrari's Method, which I found on Wikipedia at this location.

	I'm using Python 3.1.2 (on Mac OS X 10.6, in case that matters).

	First, since I don't know Python very well, I was wondering if there is an easier way to do this, without having to install any modules. (I can't figure out how to put anything on my PYTHONPATH).

	If not there is not an easier way without installing a module, could someone recommend a module AND give me a link to detailed instructions for how to install it? 

	I would PERFER to continue using Ferrari's Method, but am not opposed to trying something else.

	My only problem when using Ferrari's Method was this error:

Traceback (most recent call last):
  File "Problem309.py", line 135, in <module>
    print(main())
  File "Problem309.py", line 127, in main
    math.pow(L1, 2)-math.pow(L2, 2))
  File "Problem309.py", line 73, in sQuartic
    W = math.pow(alpha + 2 * y, 1/2)
TypeError: can't convert complex to float

	Now, your first thought is probably that I have a negative number in the square root, or that I should be using math.sqrt. According to this method, I need to use cube roots as well, and I wanted to keep my code consistent. I also have checked, and the only variable that could be ending up as a complex number is the variable y. So, here is the code that assigns y:

u = math.pow(Q, 2)/4 + math.pow(P, 3)/27
if u < 0:
    return None
R = -(Q / 2) + math.pow(u, 1/2)
U = R ** (1/3)
y = -(5/6) * alpha + U
if U == 0:
    y = y - Q ** (1/3)
elif U != 0:
    y = y - P/(3*U)
W = math.pow(alpha + 2 * y, (1/2))

	The problem I am having is that, as far as I can tell,  is that U is turning into a complex number, which is impossible! You can take the cube root of any number, positive or negative!

	So I tried in the Python Interpreter, and got this:

>>> -27**(1/3)
-3.0
>>> math.pow(-27, 1/3)
Traceback (most recent call last):
  File "<pyshell#23>", line 1, in <module>
    math.pow(-27, 1/3)
ValueError: math domain error
>>>

	Is there something going on here that I am unaware of?

	Please let me know!! Thanks so much!

---Matthew Denaburg




-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20101115/00823d20/attachment-0001.html>


More information about the Tutor mailing list