> 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
This may be more a result from the underlying C math library (also the exception you got below); I think the functions in that library are called when using functions from the math module, and the C library only works for non-complex numbers. You could have a look at the cmath module. However, you won't find a pow() function there. Instead, try using the built-in power operator: **, and use complex(): >>> complex(-27)**1/3. (-9+0j) >>> complex(-4)**(1/2.) (1.2246467991473532e-16+2j) Note the (classic) inaccuracy problem of floating points: the real part should simply be 0. (Also, create complex numbers with an imaginary part != 0 as: >>> complex(5, -2) (5-2j) ) Would that help you? Cheers, Evert > 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 > > > > > _______________________________________________ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor