• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Complex number

 
william brown
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Do JAVA provide complex number operation class?
 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No.
 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But Google shows there are loads of third party Complex number classes. e.g. http://www.ee.ucl.ac.uk/~mflanaga/java/Complex.html
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving this to Java in General (Beginner) forum as it has nothing to do with GUI related stuff.
 
Rick O'Shay
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, a complex number class is not generally useful and never required, emphasis on generally and required.

When you have a specific problem domains that requires complex types you can use two variables. When that becomes too much of a burden, use a third-party class or create your own. There are certainly problems that would be a royal pain without a specific Complex class.

Apache Commons has a Complex class, for example:

http://jakarta.apache.org/commons/math/apidocs/org/apache/commons/math/complex/Complex.html
 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, a complex number class is not generally useful and never required, emphasis on generally and required.


As an engineer I would have to disagree with you here! Maybe in your applications complex is 'not generally useful' but in my applications it is very useful. In fact it is essential!

The data type double and float are 'never required' since one could use BigDecimal but the speed an notational convenience make them essential. I would like to see a primitive type 'complex double' that would allow the standard operators */+- to be used. This would make Java much more useful to engineers and scientists.


When you have a specific problem domains that requires complex types you can use two variables. When that becomes too much of a burden, use a third-party class or create your own. There are certainly problems that would be a royal pain without a specific Complex class.


Yes you can use two variables to represent each number. That is what I have to do at the moment. It stinks. Consider mutiplyong two complex number a and b. The code without complex is

double ar = 4.2;
double ai = 2.2;
double br = 1.7;
double bi = 0.5;

double cr = ar*br-ai*ci;
double ci = ar*bi+ai*br;

but the code with complex is

complex double a = (5.2, 2.2);
complex double b = (1,7, 0,5);
complex double c = a * b;

Which is easiest to read? If you do a large number of complex 'complex' calculations then the current approach is error prone and very difficult to read.

Though a complex class makes things a bit better, since one can't use operator overloading in Java one still has to define methods like Complex.add() and Complex.mul(). Also, the fact that you have to create a Java object to represent your complex numbers makes the overhead of using the Complex class too great except in the most simple of applications.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic