Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 132,638 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,073 people online right now. Registration is fast and FREE... Join Now!




Complex Numbers Program

 
Reply to this topicStart new topic

Complex Numbers Program, Using complex numbers

Mykich
post 12 Oct, 2008 - 11:57 PM
Post #1


New D.I.C Head

*
Joined: 12 Oct, 2008
Posts: 2

I'm working on an assignment right now where we have to use and manipulate objects of complex numbers. I'm a bit stuck on several methods though. The description given for what these methods should accomplish is as follows:

"These routines convert a complex number into a value of the appropriate type"
CODE

public int intValue();
public long longValue();
public float floatValue();
public double doubleValue();


This may be more of a math question than a programming question... but I'm unsure of what to do to convert a complex number into a value. It seems my high school algebra has escaped me here and all of my searching hasn't helped. I think it may have something to do with multiplying the complex number by its conjugate though? Any help and examples are very appreciated.
User is offlineProfile CardPM

Go to the top of the page

bbq
post 13 Oct, 2008 - 01:01 AM
Post #2


D.I.C Head

Group Icon
Joined: 15 May, 2008
Posts: 185



Thanked 15 times

Dream Kudos: 50
My Contributions


I am a little rusty of my maths as well as i have not done it for quite some time... however just a question doesn't a complex number consist of an algebraic representative 'i' ? as in

-1 = i^2 ?

If so how can u represent complex numbers in this way confused.gif sorry just a bit rusty and will be interested if someone proficient in maths posts a rough idea of how to deal with this... as i really cannot remember doing it :|
User is offlineProfile CardPM

Go to the top of the page

Gloin
post 13 Oct, 2008 - 02:14 AM
Post #3


On MeD.i.Cation

Group Icon
Joined: 4 Aug, 2008
Posts: 717



Thanked 46 times
My Contributions


You need two variables to represent a complex number.

One that represents the real part and
one that represents the imaginary part.

Like the numbers 2 + 3i and 3 - 4i :

Re[0] = 2, Im[0] = 3
Re[1] = 3, Im[1] = -4

When you operate on them with for example multiplication

Re[2] = Re[0]*Re[1] - Im[0]*Im[1]
Im[2] = Re[0]*Im[1] + Re[1]*Im[0]

It's the same whether you work with integers of doubles or whatever but you'll have to implement methods (overload) for each type I guess as the assignment.

User is offlineProfile CardPM

Go to the top of the page

Gloin
post 13 Oct, 2008 - 02:23 AM
Post #4


On MeD.i.Cation

Group Icon
Joined: 4 Aug, 2008
Posts: 717



Thanked 46 times
My Contributions


QUOTE(Mykich @ 13 Oct, 2008 - 12:57 AM) *

I think it may have something to do with multiplying the complex number by its conjugate though?


This only happens when you do division. When multiplying with the conjugate, you loose the imaginary part (of the denominator) which is very handy.

2 + 3i / 3 - 4i = (2 + 3i * 3 + 4i) / (3 - 4i * 3 + 4i) = (2 + 3i * 3 + 4i) / (9 +16) = (-6 + 17i) / 25


Addition and subtraction is trivial.. wink2.gif

This post has been edited by Gloin: 13 Oct, 2008 - 03:12 AM
User is offlineProfile CardPM

Go to the top of the page

Gloin
post 13 Oct, 2008 - 02:42 AM
Post #5


On MeD.i.Cation

Group Icon
Joined: 4 Aug, 2008
Posts: 717



Thanked 46 times
My Contributions


If anything is unclear or if I misunderstood your question, just let me know.
User is offlineProfile CardPM

Go to the top of the page

Mykich
post 13 Oct, 2008 - 01:02 PM
Post #6


New D.I.C Head

*
Joined: 12 Oct, 2008
Posts: 2

Thanks for your help Gloin. I did have to write methods to add, subtract,multiply, and divide complex numbers, but as you suggested all of that was pretty easy. The only reason I'm really struggling with this part is the directions (I feel) are a bit unclear. What I've come up with though is this:

CODE

// These routines convert a complex number into
// a value of the appropriate type; they all throw
// ComplexNumberException if the complex number
// cannot be converted into a real number

public int intValue() {
    double value = 0.0;

    try {
        value = Math.pow(nums[0],2) + Math.pow(nums[1],2);
    } catch (ComplexNumberException e1) {
        System.err.println("The complex number cannot be converted into a real number.");
    } finally {
    }

    return (int)value;
}
public long longValue() {
    double value = 0.0;

    try {
        value = Math.pow(nums[0],2) + Math.pow(nums[1],2);
    } catch (ComplexNumberException e1) {
        System.err.println("The complex number cannot be converted into a real number.");
    } finally {
    }

    return (long)value;
}
public float floatValue() {
    double value = 0.0;

    try {
        value = Math.pow(nums[0],2) + Math.pow(nums[1],2);
    } catch (ComplexNumberException e1) {
        System.err.println("The complex number cannot be converted into a real number.");
    } finally {
    }

    return (float)value;
}
public double doubleValue() {
    double value = 0.0;

    try {
        value = Math.pow(nums[0],2) + Math.pow(nums[1],2);
    } catch (ComplexNumberException e1) {
        System.err.println("The complex number cannot be converted into a real number.");
    } finally {
    }

    return value;
}


If anyone interprets the directions differently or sees an error please let me know. I really appreciate your help.
User is offlineProfile CardPM

Go to the top of the page

Gloin
post 13 Oct, 2008 - 01:13 PM
Post #7


On MeD.i.Cation

Group Icon
Joined: 4 Aug, 2008
Posts: 717



Thanked 46 times
My Contributions


If it ain't broken, don't fix it.

What I'm trying to say is, it's probably good.
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/23/08 04:25AM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month