• Post Reply Bookmark Topic Watch Topic
  • New Topic

Complex numbers  RSS feed

 
Andrzej Borucki
Ranch Hand
Posts: 90
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java has not structures, I must allocate whole object to one complex number?
Java has complex numbers handling, classes for complex?
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not built into the JRE class libraries, but the Apache Commons Math library has an implementation of complex numbers (as do many other libraries).
 
Andrzej Borucki
Ranch Hand
Posts: 90
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:Not built into the JRE class libraries, but the Apache Commons Math library has an implementation of complex numbers (as do many other libraries).

How to use Apache Commons? I must download and install jar?
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a link to Commons Math.
Here is javadoc of Complex class.
Here you have an explanation on how to use classes from jars.

An example code:Using Windows:
To compile: javac -cp ".;commons-math3-3.3.jar" Temp.java
To run: java -cp ".;commons-math3-3.3.jar" Temp

An output:
(3.0, 0.0)
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's big fun writing your own complex number library. Should that fail, you can always
go for some external library.
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:It's big fun writing your own complex number library. Should that fail, you can always
go for some external library.

Yeah, writing Complex class at Object Programming class was a must
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and practising immutability at the same time. It is a pity though that you cannot implement
a method like '+' (that is: with the name '+').
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:and practising immutability at the same time. It is a pity though that you cannot implement
a method like '+' (that is: with the name '+').


Too bad Java does not support operator overloading. I learnt to live without it
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andrzej Borucki wrote:Java has not structures, I must allocate whole object to one complex number?

You have a problem with that? Java is an Object-Oriented language, so you need objects to do practically anything. Even a String is an object in Java.

Java has complex numbers handling, classes for complex?

I think the others have dealt with this, but I'd just add:

While you're absolutely right to ask whether the SDK already has a class to do something you want (in this case provide a complex number), you shouldn't be afraid of writing your own. As Pawel said: writing your own Complex class is actually kind of a fun project.

And just for future reference, I suggest you ALWAYS have a copy of the API docs open on your desktop when you're writing code (and even when you're not ) because a quick search of the frame on the left-hand side will often answer "does Java have...?" questions. In this case, it would have told you that there is no native Complex class; in which case it's perfectly reasonable to ask us.

Good luck.

Winston
 
Andrzej Borucki
Ranch Hand
Posts: 90
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paweł Baczyński wrote:Here is a link to Commons Math.

File commons-math3 is quite big, while it is only part of Apache Commons. It library to solving cubic function and quartic equation, while quadratic is trivial?
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
File commons-math3 is quite big

Does that matter? If for some reason it does (which reason?), I would imagine you can extract the subset of the source code that deals with complex number classes and use it as a basis for your own code/library (respecting the Apache Commons license, of course).

It library to solving cubic function and quartic equation, while quadratic is trivial?

I'm not sure what you're asking; are you also interested in those areas, besides complex numbers?
 
Andrzej Borucki
Ranch Hand
Posts: 90
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:I'm not sure what you're asking; are you also interested in those areas, besides complex numbers?

I want write polynomial factorization. Coefficient can be complex; Complex numbers works fine.
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The equations for solving quadratic, cubic, and quartic polynomials are well-known, so if you can't find a library which implements those, then it shouldn't be too hard to write your own. Given, as you say, an implementation of complex numbers.

I expect you know that there aren't any general algebraic methods for solving polynomials of order five or higher.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andrzej Borucki wrote:I want write polynomial factorization. Coefficient can be complex; Complex numbers works fine.

While I applaud your enterprise, I wonder if you might not be trying to recreate Mathematica in Java's image.

Java is a highly versatile language, but it doesn't really contain the "grammar" that (from what I understand) more targeted languages contain for complex mathematical or scientific constructs, so I suspect that you're going to end up doing a lot of "building from the ground up".

Don't let that stop you; it's just an observation.

Winston
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!