• 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 in Java

 
Greenhorn
Posts: 1
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 43081
77
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe try the Apache Commons Math library. The intro to its complex number classes looks pretty easy.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch
You can of course create your own ComplexNumber class.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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


You mean Effective Java?
 
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

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

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

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
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic