Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Python (http://www.programmingforums.org/forum43.html)
-   -   Quadratic Formula in Python (http://www.programmingforums.org/showthread.php?t=14303)

acwbrat Oct 30th, 2007 10:43 PM

Quadratic Formula in Python
 
I have written a python code for quadratic formula :) . However, I need both real and complex numbers for my results. I have done research and noticed two differences: real numbers uses import math and complex numbers uses import cmath

Here's my code. I would like to know what I have done wrong. I have even put my comments on there:
:

# quadraticcomplex.py
# A program that computes the complex roots of a quadratic equation.
# Illustrates use of the cmath library.
# The presence of complex numbers also requires mathematical functions that can perform operations on them.
# The imaginary component of a complex number is denoted by a suffix of ``J'' or ``j'';

import cmath  # Makes the cmath library available.

def main(): # beginning of the code
    print "Hello! This program finds the complex solutions to a quadratic"
    print

    a, b, c = input("Please enter the coefficients (a, b, c): ")

    discRoot = cmath.sqrt(b * b - 4 * a * c) # formula solving discriminant
    root1 = (-b + discRoot) / (2 * a) # formula solving root1
    root2 = (-b - discRoot) / (2 * a) # formula solving root2

    print
    print "The solutions are:", root1, root2 # display the results

main() # end of the code


Sane Oct 30th, 2007 10:49 PM

Re: Quadratic Formula in Python
 
Don't use cmath. Use math.

Sorry about that. Edit:

Just force it to be positive before you square root it, and then indicate that it's an imaginary number, calculating the real and imaginary portion how you normally would by hand.

:

    disc = b**2 - 4*a*c # discriminant
    if disc < 0:
        disc = -disc    # force it to be positive
    else:
        print "This quadratic does not have imaginary roots"
        return

    discRoot = math.sqrt(disc)

    root1r = (-b) / (2 * a)      # the real portion of root1
    root1i = discRoot / (2 * a)  # the imaginary portion of root1
   
    root2r = root1r      # the real portions are the same for each root
    root2i = -root1i    # the only difference is it's negative


So it's basically done how someone solving it with a pencil would do it. That's usually an ideal way of thinking about how to program a solution.

By the way, wrap your code in [code][/code] tags next time.

acwbrat Oct 30th, 2007 10:52 PM

Re: Quadratic Formula in Python
 
Is this the same for real numbers too?

Sane Oct 30th, 2007 11:00 PM

Re: Quadratic Formula in Python
 
[Delete]

Sane Oct 30th, 2007 11:03 PM

Re: Quadratic Formula in Python
 
Sorry about that. I forgot that imaginary numbers are calculated in pieces when you use the quadratic formula. It's been too long.

My first post is now edited.

acwbrat Oct 30th, 2007 11:12 PM

Re: Quadratic Formula in Python
 
Oh Thank you! Yes I am new to programming in python. I thought that the code was straight. But when I have noticed on import math, it would calculate the real numbers of the quadratic formula. I also thought that cmath would calculate complex numbers for quadratic formula. I thought in python, j represented complex numbers.

acwbrat Oct 30th, 2007 11:15 PM

Re: Quadratic Formula in Python
 
So is this the correct format for the code?

:

def main(): # beginning of the code
print "Hello! This program finds the complex solutions to a quadratic"
print

a, b, c = input("Please enter the coefficients (a, b, c): ")

disc = b**2 - 4*a*c # discriminant
    if disc < 0:
        disc = -disc    # force it to be positive
    else:
        print "This quadratic does not have imaginary roots"
        return

    discRoot = math.sqrt(disc)

    root1r = (-b) / (2 * a)      # the real portion of root1
    root1i = discRoot / (2 * a)  # the imaginary portion of root1
   
    root2r = root1r      # the real portions are the same for each root
    root2i = -root1i    # the only difference is it's negative

main()


Sane Oct 30th, 2007 11:19 PM

Re: Quadratic Formula in Python
 
Yes, that should work, if you fix your indenting. And print out the output in a way the user can understand.

If you want it to be able to handle both real and imaginary roots, you'll basically need to split the program up into two parts. If the discriminant is less than 0, you use the end part of the code I gave you. Otherwise, you make it use the end part of your original code.

If you understand enough about your code, and the code in my edited post, it should be enough to piece together a complete solution.

And you could use the cmath library for complex numbers, but it's intended for doing calculations on known complex numbers. Such as taking the cosine of an imaginary number. It's not good for what you're trying to do here.

acwbrat Oct 30th, 2007 11:24 PM

Re: Quadratic Formula in Python
 
I am going to try it out and then let you know if it works. Hopefully it will! Let me go check! Thanks Sane. But let me go check really quick.

acwbrat Oct 30th, 2007 11:32 PM

Re: Quadratic Formula in Python
 
I tried running the program and it doesn't display my results at all. Also, when I tried performing calculations, the program would not run.


All times are GMT -5. The time now is 3:36 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC