• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Complex Numbers

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I'm studying Dietel and Dietel 'Java How to Program', going through the exercises.

I have hit a question with no background or explanation of the prerequisite concepts needed to complete it.

I need to create a Class called 'Complex' that creates 'Complex Numbers' using the square root of minus one.

The Class should be simple to create.

The user inputs two numbers (doubles?).

The computer calculates

numbera + numberb * sqrt(-1)

However Java responds with NaN to java.Math.sqrt(-1)

What do I do?

And incidentally, what is a Complex Number? What is it used for?

 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You won't actually take the square root of -1. Instead, the ComplexNumber consists of two parts, one the real component, and the other the imaginary component.

A complex number is of the form a + bi where i represents the square root of -1.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Complex numbers are a mathematical concept, which is used in various sciences. See the explanation on Wikipedia.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nor do you ever actually calculate numbera + numberb * sqrt(-1), because there is nothign TO calculate. Complex numbers are kind of like coordinates - there are two parts written together that tell you what you need to know - the 'real' part (numbera) and the 'imaginary' part (numberb). the imaginary part is witten with the literal charater 'i' after it:

7 + 2i
-34.7 + 17.9837i

even 'normal' numbers are really complex numbers where the numberb part is 0. So, 42 is really 42 + 0i
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
They're extensively used in engineering -- electrical engineers, for example spend a lot of time in complex space because it simplifies the math considerably (over dealing with frequency space).
 
Rancher
Posts: 1044
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When they found out the formula to solve the equation of third grade (cubic function) with real coefficients, it turned out that it was worth and even necessary to include (non-real) complex numbers in the computation even in the case when all roots are real.
Wikipedia: Cubic function

The polynomial x^2 + 1 clearly has no real roots - therefore the need for "i" arises. However polynomials with complex coefficients have complex roots.
Fundamental theorem of algebra

 
Ivan Jozsef Balazs
Rancher
Posts: 1044
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A famous German language novel by Austrian novelist Musil's novel "The Confusions of Young Törless" is a Bildungsroman,
that is, a story of a young disoriented man searching for moral values in society and their meaning for him.

The hero, Young törless, experiences a lof of confusions in the military boarding school.
A small part is about complex numbers along the lines of my poor transliteration:
"How come we can not take the square root of -1, but we stiil do it while introducing the complex numbers?"
He even asks for a private audience at the maths professor in order to ask this question, who is not able to provide a satisfying answer.

Now if the school is not able to provide guidance in this (relatively simple) question, how could it provide guidance and orientation
in even harder matters including moral values, violence among the comrades, or how to cope with the arising sexuality?
No way!

At the end he escapes the school: he is dismissed and sent home. The reader gets relieved along with him.

Wikipedia resources:

The Confusions of Young Törless

Or if you prefer it in the original language ;-)
Die Verwirrungen des Zöglings Törleß
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interesting stuff!

But: complex numbers are at the very hart of quantum mechanics. So, without complex numbers we would not have
computers, so this forum would also not exist, nor OP's problem.

Strange, if you come to think of it...

Coming back to OP's problem: what was the intention of your class?
Define addition and multiplication for these complex numbers, like in


?

Greetz,
Piet
 
Ivan Jozsef Balazs
Rancher
Posts: 1044
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Complex numbers and functions are very useful in the theory of the prime numbers (analytic number theory) as well.

Riemann zeta function
 
Ivan Jozsef Balazs
Rancher
Posts: 1044
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Apache Commons Complex Numbers
 
S Connor
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It might help future searchers to know that there are some superb videos available on this topic on Youtube.
You can find them by searching 'Complex Numbers' and 'Imaginary numbers'.

Here is the oringinal problem and my solution, please tell me if I have the right end of the stick or not:

"Create a Class called complex for performing arithmetic with complex numbers. Complex numbers have the form:

realPart + imaginaryPart * i

where i is: sqrt(-1)

Write a program to test your Class. Use floating point variables to represent the private data of the class. Provide a constructor that enables an object of this class to be intialized when it is declared. Provide public methods that perform the following operations:
a) Add two Complex numbers
b) Subtract two Complex bummbers
c) Print Complex numbers in the form (a, b), where a is the real part and b is the imaginary part.





If i can never be calculated, in what respect is it useful? I was under the impression that having an undefined variable in an equation defied the point of using mathematics?
Does the head engineer pass a set of blueprints to the construction team of a building site with undefined variables on the measurements?

 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you mean by "it can never be calculated"? If you think complex numbers can't be added/subtracted/multiplied/etc, then you need to go back and study some more.

"5 + 2i" IS a complex number - that's as simple as they get. "7 - 8i" is also a complex number. That is the EXACT value of both. If I added them, I would bet "12 - 6i". There is no undefined variable.

I would say you need some revision to your design. I would have a ComplexNumber class. It should contain two variables - one to hold the real part, and one to hold the imaginary part. Then, define the operations. In your driver class (which would do things like get the input, create the ComplexNumber objects), you would call the method...something like

ComplexNumber first = new ComplexNumber(5,2);
ComplexNumber second = new ComplexNumber(7,-8);
ComplexNumber sum = first.plus(second);
 
Ivan Jozsef Balazs
Rancher
Posts: 1044
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Provide a constructor that enables an object of this class to be intialized when it is declared. Provide public methods that perform the following operations:
a) Add two Complex numbers
b) Subtract two Complex bummbers
c) Print Complex numbers in the form (a, b), where a is the real part and b is the imaginary part.




I'd rather do this along these lines:


 
S Connor
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm simply questioning what the use of the concept sqrt(-1) is, if I am unable to calculate the square root of minus one?

Thank you for taking the time to respond though.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

S Connor wrote:I'm simply questioning what the use of the concept sqrt(-1) is


That's what nost of this discussion was about -- how imaginary numbers are used.

if I am unable to calculate the square root of minus one?


As already pointed out, you can't. It doesn;t exist. That's why it's called imaginary. Complex numbers were invented to express values with an imaginary component, because real numbers can't express such concepts.

 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would argue with Bear. I think it DOES exist, and it is called i.

sqrt(-1) == i

(i is not a variable here, any more than the symbol for pi is a variable in the expression 2 + 3pi.)

so once you can do that, you can do this:

sqrt(-4) == sqrt(4 * -1) == sqrt(4) * sqrt(-1) == 2 * i == 2i

or this

sqrt(-50) = sqrt(50 * -1) == sqrt(50) * sqrt(-1) = sqrt(2 * 25) * sqrt(-1) == sqrt(2) * 5 * i == 5(sqrt(2))i
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd say it depends by what you mean by "exists".

Modern mathematics is built upon a set of basic assumptions, called "axioms". We assert that they are true, and build everything else on top of that. In one approach, the whole of the real numbers are generated by stating that 0 and 1 exist, with a few other rules that allow you to combine them in various ways.

So when we get to complex numbers, we can simply state "there is a quantity i such that i^2 = -1" as an additional axiom. Then we see where that takes us - and as mentioned above, it turns out that it's incredibly useful in lots of applications.

So in that sense, as far as a mathematician is concerned it exists just as much as the number 1 does. But it's true that it might not map onto the real world in quite as intuitive a way.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote:I'd say it depends by what you mean by "exists".

Modern mathematics is built upon a set of basic assumptions, called "axioms". We assert that they are true, and build everything else on top of that. In one approach, the whole of the real numbers are generated by stating that 0 and 1 exist, with a few other rules that allow you to combine them in various ways.

So when we get to complex numbers, we can simply state "there is a quantity i such that i^2 = -1" as an additional axiom. Then we see where that takes us - and as mentioned above, it turns out that it's incredibly useful in lots of applications.

So in that sense, as far as a mathematician is concerned it exists just as much as the number 1 does. But it's true that it might not map onto the real world in quite as intuitive a way.




From an scientific / engineering point of view, it does map to the real world -- albeit, I would agree that it is not that intuitive. There are many scientific proofs that start off with real numbers, and end up with real numbers, which prove or calculate real world events... however, the path that they go through (meaning the interim numbers) may be complex. If complex numbers was merely only a representation, meaning it represents an incalculable result, then I would argue that the formulas will break down.

Henry
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

S Connor wrote:
The computer calculates

numbera + numberb * sqrt(-1)

However Java responds with NaN to java.Math.sqrt(-1)

What do I do?

And incidentally, what is a Complex Number? What is it used for?



Java floating point returns NaN for the square root of negative one because that is how it is defined. The IEEE floating point standard doesn't support complex numbers -- and merely just reports them as "not a number". This doesn't mean that complex numbers don't exist. It just means that you can't use the float or double primitive variables to represent them. If you need complex numbers, you will need to implement it yourself -- as mentioned in other posts of this topic.

Henry
 
Ivan Jozsef Balazs
Rancher
Posts: 1044
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you argue within the natural numbers, you can not subtract 1 from 0. But you can introduce negative integers and then it will be possible.
The point is whether the negative integers can be introduced with reasonable operations in a consistent way: they can.

But hey, reasoning among the integers we can not divide 1 by 2 or 2 by 3: introducing the fractions we can.
The point is whether the so called rational numbers can be introduced with reasonable operations in a consistent way: they can.

But hey, reasoning among the rational numbers we can not take the square root of 2 or 3.
The point is whether the so called real numbers can be introduced with reasonable operations in a consistent way: they can.

(Do you recognize the pattern?)

But hey, reasoning among the real numbers we can not take the square root of -1.
The point is whether the so called complex numbers can be introduced with reasonable operations in a consistent way: they can.
 
S Connor
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are there any examples of how i is used that can be visualized?

 
Ivan Jozsef Balazs
Rancher
Posts: 1044
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Are there any examples of how i is used that can be visualized?



Real numbers are usually represented visually as the points of the so called "complex plane":

Complex number @ Wikipedia
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ivan Jozsef Balazs wrote:
But hey, reasoning among the real numbers we can not take the square root of -1.
The point is whether the so called complex numbers can be introduced with reasonable operations in a consistent way: they can.



And I really like how consistent it is -- for example, the square root of a complex number is just another complex number. No need to introduce yet another type of imaginary numbers.

Henry
 
Ivan Jozsef Balazs
Rancher
Posts: 1044
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want, you can extend complex numbers as quaternions (quaternia?) where however production (multiplication) is noncommutative
Quaternion

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic