Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Returning Complex Numbers?  RSS feed

 
Jeong Ryu
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi CR,

I am working on a program where it calculates the AC circuit and the phasor circuit. The problem that I am stuck on is on returning complex numbers.
In the code below, I am trying to divide complex number V by the total sum of three complex numbers, which are each assigned and calculated for each array (private Complex impedance[] = new Complex[2]).

I have created my own Complex class, but I don't think that there is a problem with my complex class. I think that the syntax to return complex number is wrong. I would also post my Complex class code here, but it was pretty large. I can post it if it is needed.

The error I get is that "The constructor Complex (Complex) is undefined.

Could someone please help me?


 
Rob Spoor
Sheriff
Posts: 21090
85
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
V.divide(X) returns a Complex. You haven't written a constructor that takes a single Complex. Therefore, you need to fix that:
a) Add the constructor.
b) Just return V.divide(X) directly, without creating a new Complex for it.
 
Jeong Ryu
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:V.divide(X) returns a Complex. You haven't written a constructor that takes a single Complex. Therefore, you need to fix that:
a) Add the constructor.
b) Just return V.divide(X) directly, without creating a new Complex for it.


Sorry, could you elaborate please?
a) How would you add the constructor? And if I don't have a constructor that takes a single Complex, is it possible to change the calculation to return a double Complex or whatever it may be so that it does not return an error?


b) I tried to return it without making a new Complex (return (V.divide(X));), and I get these errors: (I also had to initialize my Complex total = null;)



---

This is my code in line 47:


Line 6~11:
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeong Ryu wrote:a) How would you add the constructor?


That is a really generic question. If you don't know how to create a constructors, perhaps you should go back a few chapters in your book. You really should get that part understood before you create and use objects.

Jeong Ryu wrote:And if I don't have a constructor that takes a single Complex, is it possible to change the calculation to return a double Complex or whatever it may be so that it does not return an error?


It is *your* class. Whether it is possible will depend on whether you allow it, or not allow it.... however, it is probably a good idea to do it the correct way, then to hope that you have a bug in your code that you can work around -- to get to compile without error.

Henry
 
Jeong Ryu
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Jeong Ryu wrote:a) How would you add the constructor?


That is a really generic question. If you don't know how to create a constructors, perhaps you should go back a few chapters in your book. You really should get that part understood before you create and use objects.

Jeong Ryu wrote:And if I don't have a constructor that takes a single Complex, is it possible to change the calculation to return a double Complex or whatever it may be so that it does not return an error?


It is *your* class. Whether it is possible will depend on whether you allow it, or not allow it.... however, it is probably a good idea to do it the correct way, then to hope that you have a bug in your code that you can work around -- to get to compile without error.

Henry


Thank you!
Unfortunately, I am in school so I can't access my textbook until later tonight. But, from what I understand, we just need to create two constructors : One that takes in four double values and sets to four instances, and another one without any arguments.

 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your ArrayIndexOutOfBoundsException is fairly straightforward:



You create an array containing two elements, then try to access three of them.
 
Jeong Ryu
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:Your ArrayIndexOutOfBoundsException is fairly straightforward:



You create an array containing two elements, then try to access three of them.


I see. Thank you!
I had it at 3 originally, but I switched to 2 because I thought that [2] would create array[0]. array[1], and array[2].

After fixing the array issue, I am coming across more errors... sigh, haha...


I am trying to return a complex number without creating a new Complex, as Rob Spoor had previously suggested. However, I couldn't do that unless I initalized total to 'null'. But, it seems that that is giving me this error.
Here is my revised code that gives this error (line 71):

[Edit - fixed line length to prevent horizontal scrolling]


And for line 14:
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, total = null initialises the reference. And if you hadn't initialised it, you'd be getting a compiler error. The compiler's smart enough to work that much out. But it initialises it to null. That is, it's referencing no object. You get a NullPointerException whenever you try to call a method on a null reference. So here:
Which object are you calling add() on the first time round the loop? You can think of calling a method as sending a message to an object, but here you're sending a message to nothing.

This approach to adding up the impedences should work, but you need to initialise total to an actual object. You need a zero complex number. That's not the same as null, that's an instance of Complex with its real and imaginary parts set to zero.

By the way, you're heading for another ArrayIndexOutOfBounds in that for loop. The maximum index of the impedance array is impedance.length - 1 (because arrays are zero-indexed).
 
Jeong Ryu
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:Yes, total = null initialises the reference. And if you hadn't initialised it, you'd be getting a compiler error. The compiler's smart enough to work that much out. But it initialises it to null. That is, it's referencing no object. You get a NullPointerException whenever you try to call a method on a null reference. So here:
Which object are you calling add() on the first time round the loop? You can think of calling a method as sending a message to an object, but here you're sending a message to nothing.

This approach to adding up the impedences should work, but you need to initialise total to an actual object. You need a zero complex number. That's not the same as null, that's an instance of Complex with its real and imaginary parts set to zero.

By the way, you're heading for another ArrayIndexOutOfBounds in that for loop. The maximum index of the impedance array is impedance.length - 1 (because arrays are zero-indexed).


That makes sense, thank you! I initialized to an object in the Complex class, and I also fixed the loop so that it does not go out of bound (I think). I am now able to run the program without any errors, but I get the values of (NaN, NaN). This probably means that I did not code correctly to separate the answers to (real, imaginary), and is probably being calculated as one number or something. I am not sure...

Here's revised code portion:

 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are probably getting NaN because you divide by zero someplace.
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Luke wrote:You are probably getting NaN because you divide by zero someplace.


Actually, a divide by zero doesn't necessarly result in NaN -- unless of course, you divide zero by zero. A positive number divided by zero results in Infinity.

Henry
 
Jeong Ryu
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, I've found the problem. It seems that I am not correctly assigning each arrays. It produces zeros, when it shouldn't - which is why I got (NaN,NaN).
What would be the correct way to assign complex numbers to arrays?

UPDATE: It seems that the values from the main method weren't passing right. I've fixed it, and now I am not getting any NaN or -Infinity values. But, it seems that the three Complex values are not adding right. After I fix this, I will post back here after class.

Thanks everyone!


P.S. How do you pass a fraction number (1/6)? Just writing 1/6 passes the value as 0.0.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeong Ryu wrote:P.S. How do you pass a fraction number (1/6)? Just writing 1/6 passes the value as 0.0.

If you write 1/6 it will use integer arithmetic, so it gets rounded down to zero before you convert it to a double. You can avoid that by forcing one or both into a double in the first place - e.g. 1.0/6.0.
 
Jeong Ryu
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:
Jeong Ryu wrote:P.S. How do you pass a fraction number (1/6)? Just writing 1/6 passes the value as 0.0.

If you write 1/6 it will use integer arithmetic, so it gets rounded down to zero before you convert it to a double. You can avoid that by forcing one or both into a double in the first place - e.g. 1.0/6.0.


Thank you!
 
Campbell Ritchie
Marshal
Posts: 55698
163
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Possibly more precise, but more difficult solution for ⅙: create a Fraction class. That is probably beyond your present requirements.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeong Ryu wrote:I am trying to return a complex number without creating a new Complex, as Rob Spoor had previously suggested...

It may be worth mentioning that doing that makes your class mutable, which has issues of its own (specifically Thread safety). In general, creating new objects is extremely fast, so I wouldn't worry too much about it at the moment; and making your Complex class immutable may allow the compiler and/or JVM to add some "efficiency magic" behind the scenes.
Doing so makes your class a bit more "functional" in style though, eg:but doing things this way generally makes your design very 'clean'.

Other "value" classes, such as BigInteger and BigDecimal use precisely this approach.

It also seems to me that this impedance calculation is quite specific, so you might want to think about an Impedance class (or method).

HIH

Winston
 
Jeong Ryu
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Jeong Ryu wrote:I am trying to return a complex number without creating a new Complex, as Rob Spoor had previously suggested...

It may be worth mentioning that doing that makes your class mutable, which has issues of its own (specifically Thread safety). In general, creating new objects is extremely fast, so I wouldn't worry too much about it at the moment; and making your Complex class immutable may allow the compiler and/or JVM to add some "efficiency magic" behind the scenes.
Doing so makes your class a bit more "functional" in style though, eg:but doing things this way generally makes your design very 'clean'.

Other "value" classes, such as BigInteger and BigDecimal use precisely this approach.

It also seems to me that this impedance calculation is quite specific, so you might want to think about an Impedance class (or method).

HIH

Winston


Thank you!
I tried changing my Complex class to match your code, but it broke a lot of my codes. So, I just left that class as it was.
For the impedance calculation, I just had to add them up and then do division. My program now correctly produces the answer without any errors. My program for this project had a lot of useless codes like the set/get methods. All I had to do was assign the arrays to the formula to get the complex number, add all three of them, and then do phasor voltage,v, (which was (3,0)) divided by the total of the sum.

Thanks everyone!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!