Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 30th, 2007, 9:43 PM   #1
acwbrat
Newbie
 
Join Date: Oct 2007
Posts: 17
Rep Power: 0 acwbrat is on a distinguished road
Lightbulb 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

Last edited by DaWei; Oct 30th, 2007 at 10:26 PM. Reason: Added code tags. Read the rules.
acwbrat is offline   Reply With Quote
Old Oct 30th, 2007, 9:49 PM   #2
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,835
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
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.

Last edited by Sane; Oct 30th, 2007 at 10:09 PM.
Sane is offline   Reply With Quote
Old Oct 30th, 2007, 9:52 PM   #3
acwbrat
Newbie
 
Join Date: Oct 2007
Posts: 17
Rep Power: 0 acwbrat is on a distinguished road
Re: Quadratic Formula in Python

Is this the same for real numbers too?
acwbrat is offline   Reply With Quote
Old Oct 30th, 2007, 10:00 PM   #4
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,835
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
Re: Quadratic Formula in Python

[Delete]
Sane is offline   Reply With Quote
Old Oct 30th, 2007, 10:03 PM   #5
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,835
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
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.

Last edited by Sane; Oct 30th, 2007 at 10:13 PM.
Sane is offline   Reply With Quote
Old Oct 30th, 2007, 10:12 PM   #6
acwbrat
Newbie
 
Join Date: Oct 2007
Posts: 17
Rep Power: 0 acwbrat is on a distinguished road
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 is offline   Reply With Quote
Old Oct 30th, 2007, 10:15 PM   #7
acwbrat
Newbie
 
Join Date: Oct 2007
Posts: 17
Rep Power: 0 acwbrat is on a distinguished road
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()
acwbrat is offline   Reply With Quote
Old Oct 30th, 2007, 10:19 PM   #8
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,835
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
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.
Sane is offline   Reply With Quote
Old Oct 30th, 2007, 10:24 PM   #9
acwbrat
Newbie
 
Join Date: Oct 2007
Posts: 17
Rep Power: 0 acwbrat is on a distinguished road
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 is offline   Reply With Quote
Old Oct 30th, 2007, 10:32 PM   #10
acwbrat
Newbie
 
Join Date: Oct 2007
Posts: 17
Rep Power: 0 acwbrat is on a distinguished road
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.
acwbrat is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[tutorial] Python for programming beginners coldDeath Python 30 Dec 14th, 2005 11:35 AM
If you want to learn Python or improve your Python skills coldDeath Python 2 Nov 23rd, 2005 12:27 AM
Convert Python script to C++ code clanotheduck Python 17 Sep 25th, 2005 8:55 AM
Advanced Python Tricks Arevos Python 19 Sep 24th, 2005 7:39 AM
Python - A Programmers Introduction coldDeath Python 17 Aug 19th, 2005 12:41 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:42 AM.

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