Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes Complex Numbers in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Complex Numbers in Java" Watch "Complex Numbers in Java" New topic
Author

Complex Numbers in Java

Derrick Powell
Greenhorn

Joined: May 18, 2011
Posts: 1
Hello Everyone,

I am new to programming in Java. Been a Matlab programmer for the past 8-9 years. I have a question (This might look like a very dumb question to many of you Java programmers)?

Let say Z(n) is an array of complex number defined as (3+4*i)/(9-(7*i)*n); where for simplicity n=0,1,23,.....9;

T(n) is an array of complex number defined as (2.25-(9*i)*n)/(8+11.34*i); where n=0,1,23,.....9;


1. Now how do i store this complex numbers into an array using Java?

I prefer using a "for" loop
for (int n=0, n<10,n++){
Z[n]=(3+4*i)/(9-(7*i)*n);}

But i know Java don't recognize "i" which is sqrt(-1). How to go about creating a Z array then?


2. Next, how do i find the product of Z*T; lets say Z is saved as "nx1" vector (matrix) and T is saved as "1xn" vector (matrix)? I want the result to be "nxn" matrix.


I have looked at Michael Flanagan's Java library on Complex (Michael Thomas Flanagan's Java Scientific and Numerical Library), but still can't figure out. Any suggestions would be greatly appreciated.

Thanks!
Derrick
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41096
    
  44
Maybe try the Apache Commons Math library. The intro to its complex number classes looks pretty easy.


Ping & DNS - my free Android networking tools app
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37962
    
  22
Welcome to the Ranch
You can of course create your own ComplexNumber class.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7529
    
  18

Ashok Felix wrote:You can use this sample to create your own class...

Actually, there's a major flaw in that class: It says it's "immutable", but neither the class itself, nor its methods are final.

Nice to know that even Princeton grads can make basic mistakes.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41096
    
  44
Winston Gutkowski wrote:Actually, there's a major flaw in that class: It says it's "immutable", but neither the class itself, nor its methods are final.

But the only variables -re and im- are final, so after they get something assigned to them in the constructor their values can't be changed any more. So any given object of the class is immutable.

The bigger question is: why would I use this class when what should be considered a standard library (Apache Commons Math) has one built in?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7529
    
  18

Ulf Dittmer wrote:But the only variables -re and im- are final, so after they get something assigned to them in the constructor their values can't be changed any more. So any given object of the class is immutable.

Right, but since nothing is final, I can create a subtype of Complex that completely rewrites the underlying logic, even making it mutable if I want.

Read what Josh Bloch has to say about BigInteger and BigDecimal (which he wrote).

Winston
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37962
    
  22
Unfortunately I felt obliged to move them to a hidden “deleted” forum. The link you quoted points to a “forbidden” website and it simply says
Forbidden

You don't have permission to access /java/97data/Complex.java.html on this server.
Apache/2.2.15 (Red Hat) Server at introcs.cs.princeton.edu Port 80
I therefore believe you may be breaching copyright by posting that class, and I am afraid I felt I have no option but to delete it. Sorry.
You can repost it if you can show it is permissible in a public forum.
Paweł Baczyński
Bartender

Joined: Apr 18, 2013
Posts: 838
    
  14

Winston Gutkowski wrote:Read what Josh Bloch has to say about BigInteger and BigDecimal (which he wrote).

You mean Effective Java?


Formely Pawel Pawlowicz
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18511
    
  40

Winston Gutkowski wrote:
Ulf Dittmer wrote:But the only variables -re and im- are final, so after they get something assigned to them in the constructor their values can't be changed any more. So any given object of the class is immutable.

Right, but since nothing is final, I can create a subtype of Complex that completely rewrites the underlying logic, even making it mutable if I want.

Read what Josh Bloch has to say about BigInteger and BigDecimal (which he wrote).


There seems to be more than one definition of immutable classes -- and quite frankly, I think either is okay. It can be a weak definition, such as the developers never provided mutators. With this definition, it is talking more about the service provided. On the other end, it's is related to security, or at least, as some sort of protection against unintentional mistakes. With this, the class is defined in such a way as to prevent forced mutation. It is probably in a sealed jar file, installed locally, etc. etc. And of course, there are probably lots of variations in between these two ends.

Considering that the Princeton class is provided for free as as source code, I think it is safe to assume that it is not targeting for the secure end of immutable ...

Henry



Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18511
    
  40

Ulf Dittmer wrote:
The bigger question is: why would I use this class when what should be considered a standard library (Apache Commons Math) has one built in?


Personal note. I am not a fan of bringing in a library just for one or two classes. I am especially not a fan of it, if bringing in a library has dependencies on other libraries (hence, requires even more libraries).


And this is many times more important if what you are building is a library itself. Clients, particularly those that pay for your libraries, really do not like such dependencies.

Henry
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4343
    
    8

Campbell Ritchie wrote:I therefore believe you may be breaching copyright by posting that class, and I am afraid I felt I have no option but to delete it.


If the removed class was the one linked to from http://algs4.cs.princeton.edu/code/ - they're released under GPLv3 (see Q + A section at the bottom of the page).

The link given seems an old one, though - it's now at http://algs4.cs.princeton.edu/99scientific/Complex.java.html



(I'm taking their Algorithms II course at the moment, which is why I knew)

Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7529
    
  18

Henry Wong wrote:With this definition, it is talking more about the service provided. On the other end, it's is related to security, or at least, as some sort of protection against unintentional mistakes.

True. I'm an unrepentant member of the Paranoid Programming Club though.

Considering that the Princeton class is provided for free as as source code, I think it is safe to assume that it is not targeting for the secure end of immutable ...

Actually, I'd have thought that would be all the more reason for making it bulletproof; but, like I say, PPC born and bred...

Winston
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41096
    
  44
Winston Gutkowski wrote:
Ulf Dittmer wrote:But the only variables -re and im- are final, so after they get something assigned to them in the constructor their values can't be changed any more. So any given object of the class is immutable.

Right, but since nothing is final, I can create a subtype of Complex that completely rewrites the underlying logic, even making it mutable if I want.

I think here we're getting into the territory Henry mentioned - differing interpretations of immutability. I think the most common one (which I was using) is object immutability - which this class has. Frankly, I have not come across a definition of immutability that concerns behavior (i.e., methods), just data, in the form of something like "if I have two objects that are equal, it is not possible to alter them so that they are no longer equal". That assumes an equals method as one would reasonably create it in Java - defined in terms of the encapsulated data. (Of course it's possible to write a class that creates objects that are sometimes equal to one another, and sometimes not, but I think that falls into the same category as "can I create a Java class that I can execute from the command line although it has no main method?").
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7529
    
  18

Ulf Dittmer wrote:I think here we're getting into the territory Henry mentioned - differing interpretations of immutability. I think the most common one (which I was using) is object immutability - which this class has. Frankly, I have not come across a definition of immutability that concerns behavior (i.e., methods), just data, in the form of something like "if I have two objects that are equal, it is not possible to alter them so that they are no longer equal".

I guess we'll have to agree to disagree then; and I suspect JB would agree with me. To me, the only place that a word like "immutable" makes any sense is on a concrete class; and if that class is extendable then its behaviour can be changed or added to to make it mutable while still satisfying the requirements of any already-written code that takes the superclass and assumes that it's immutable.

AFAIK, BigInteger and BigDecimal are the only classes in the SDK that claim immutability without being final and, for something like a Complex class, why would you want to extend it?

Fun argument though.

Winston
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37962
    
  22
Matthew Brown wrote: . . . they're released under GPLv3 (see Q + A section at the bottom of the page). . . .
In which case it is all right, and I shall restore the discussion to this forum. Here it is (I hope). and I shall re‑post the actual code.

Sorry for being so paranoid. If I had a link which had actually opened I could have seen the licence info, I might not have been so worried.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Complex Numbers in Java
 
Similar Threads
Searching a char[]
Sun Cirtification
Converting Letters to Corresponding Numbers
Help me with matrix...please!!!!!!!
Array of Arrays?