View Full Version : java: some mathematical problems
NewAtJava
08-01-2005, 07:25 PM
i've been trying so long and i could not solve any of the problems (below) that i've been having for some time. if any of you could help out with any of the problems, that'd be absolutely AMAZING.
1. http://www.sci.usq.edu.au/staff/leighb/graph/Top.html
that's the website from which i got my plotting codes/classes for a plot that supposedly takes a set of data from a different calculating program (example 3 code is the one where you can load data). i'm looking at the graph code example3 and it's not obvious to me how to call up the data for the graph. is it where it says "DATA"? if the data file is called output.txt would i just substitute "output.txt" for "DATA"?
2. how would i make it so that my outout.txt file from a calculating program is composed of two columns (so basically a set of data, x coordinates and y coordinates)?
3. in the following equation, i want P to be varying b/t two given constants Pmax and Pmin. how would i do that?
public double I0(double Vb, double P, double Pbath) {
return ((Pbath-P)/Vb);
}
that's it for now... wow i'm hopelessly lost... ahh.
thanks so much for everything.
Aradon
08-01-2005, 08:02 PM
well let's look at the source code for some answers
1)
This is the source code
String data = getParameter("DATA");
...
data1 = dynamic.loadDataSet(new URL(getDocumentBase(),data), graph);
This is a snippit of the html page where the applet loads:
<param name=title
value="Spectrum of a giant elliptical Galaxy in the Virgo cluster">
<param name=data
value="elliptical.data">
As you can see, the String data is really "elliptical.data". So if you replace this or if you place it directly in the loadDataSet part then yes, you will load your data.
2 You are kind of ambigious on this fact. Is the dataset something other then two columns? What is the dataset after it comes out of the calculating program?
Maybe you should consider just writing a quick program to open the file and format it to another file the way you want it to be formated.
3
If you are calling the function IO several times then you could do this by initiating a boolean variable. So for example
boolean minmax = true;
double Pmax = 99.99;
double Pmin = 1.00;
double Vb = some_double;
double Pbath = somedouble;
while(true)
{
if(minmax)
IO(Vb, Pmax, Pbath);
else
IO(Vb, Pmin, Pbath);
}
Now if you wanted it to vary WITHIN the function then you would have to create a static boolean variable in that class and do about the same thing, changing P.
Hope this help (hope I understood the questions).
NewAtJava
08-01-2005, 08:32 PM
thanks so much for those tips.
1. "As you can see, the String data is really "elliptical.data". So if you replace this or if you place it directly in the loadDataSet part then yes, you will load your data."
data1 = dynamic.loadDataSet(new URL(getDocumentBase(),data), graph);
i'm not quite sure how i can replace that above with my .txt datafile (it's ok if the data's in .txt format instead of .data right?)...
2. "Is the dataset something other then two columns? What is the dataset after it comes out of the calculating program?"
well basically the calculating program calculates some values based on a value that is also changing. it ties into my third question really; i'm varying the value P from Pmin to Pmax and then i am calculating bunch of I0 values which are based on the P variable. so i'd like the output.txt file to have simply two columns, first column all the P values and the second column R0/Rn's, and R0 values are dependent upon those varying I0 values. right now the following is my code for that but it seems like it won't display P and R0/Rn side by side...
FileOutputStream out;
PrintStream p;
out = new FileOutputStream("output.txt");
p = new PrintStream( out );
p.println("P R0/Rn");
p.println(P + "\r\n" + R0(Vb,I0(Vb,P,Pbath(G,beta,T0,Tb),Pmax,Pmin))/Rn + "\r\n");
p.close();
3. the below is what i wrote after your suggestion:
public double I0(double Vb, double P, double Pbath, double Pmax, double Pmin) {
boolean minmax = true;
while(true)
{
if(minmax)
return ((Pbath-Pmax)/Vb);
else
return ((Pbath-Pmin)/Vb);
}
}
public double R0(double Vb, double I0){
return (Vb/I0);
}
it compiles fine but it won't give me a set of values... it only gives me one pair of values when i want a bunch of pairs. what am i doing wrong?
thank you so much.
Aradon
08-01-2005, 09:07 PM
i'm not quite sure how i can replace that above with my .txt datafile (it's ok if the data's in .txt format instead of .data right?)...
Opps! Yes, you can use .txt. As far as the program is concerned it's just going to read it line by line regardless..so it may end up looking something like this:
data1 = dynamic.loadDataSet(new URL(getDocumentBase(),"data.txt"), graph);
3. the below is what i wrote after your suggestion:
opps again. It seems I did not write solid code to you the first time through. Since the boolean variable is not changing you will always hit true, here is the first change. Bolded is where the problem of one set occurs.
public double I0(double Vb, double P, double Pbath, double Pmax, double Pmin)
{
boolean minmax = true;
while(true)
{
if(minmax)
{
minmax = false;
return ((Pbath-Pmax)/Vb);
}
else
{
minmax = true;
return ((Pbath-Pmin)/Vb);
}
}
}
public double R0(double Vb, double I0){
return (Vb/I0);
}
The return statments break out of the method completely, taking you out of the loop. Which is okay if you just want to run the method once and then switch it. However if you want to do it this way you'll need to make the boolean variable static so that it's scope is not of just the method but of the entire program. (yay static's). (btw I'm assuming that you are calling the method as many times as you need to and return that one data pair).
So here is the real solution (as I currently see it ^_^ )
private static boolean minmax = true;
public double I0(double Vb, double P, double Pbath, double Pmax, double Pmin)
{
if(minmax)
{
minmax = false;
return ((Pbath-Pmax)/Vb);
}
else
{
minmax = true;
return ((Pbath-Pmin)/Vb);
}
}
public double R0(double Vb, double I0){
return (Vb/I0);
}
Let me look at the file reader a bit longer before I comment on that. (personally I use filewriter to make my life slightly eaiser).
Aradon
08-01-2005, 09:18 PM
Okay on sixth read I got what you were doing..
FileOutputStream out;
PrintStream p;
out = new FileOutputStream("output.txt");
p = new PrintStream( out );
p.println("P R0/Rn");
p.println(P + "\r\n" + R0(Vb,I0(Vb,P,Pbath(G,beta,T0,Tb),Pmax,Pmin))/Rn + "\r\n");
p.close();
which called the IO thing we've been bugging with this entire time. Well with the P+"\r\n" the P value and the RO/Rn value will be on two different lines
P
RO/Rn
I think what you really want is P+"\t"+other stuff so...
FileOutputStream out;
PrintStream p;
out = new FileOutputStream("output.txt");
p = new PrintStream( out );
p.println("P R0/Rn");
p.println(P + "\t" + R0(Vb,I0(Vb,P,Pbath(G,beta,T0,Tb),Pmax,Pmin))/Rn + "\r\n");
p.close();
This code here will take the on P value it has at the time and the other values it has at the time and print it as such
P RO/Rn
So at this point we will have one of the points read, formated and printed out onto the file (as long as I didn't make anymore silly mistakes! hehe )
NewAtJava
08-01-2005, 09:40 PM
sorry i wasn't so clear, but it definitely worked. thank you :)
#3 is still a bit of a problem though. i'm getting two columns and all that, but there's only one set of values even if i have that varying boolean code in... basically it gives me the resulting value only when P is Pmax. I want to produce, say, 100 data points b/t Pmax and Pmin (which are 4 and 1.171, respectively). how would i do this?
you've been so much help.
[[edit1]]
i figured it out why it'll only give me one set of data... i've previously declared P as a fixed constant. my bad. now that i got rid of it, i'm faced with a situation where i have not declared P anything anywhere, so in this line:
p.println(P + "\t" + R0(Vb,I0(Vb,Pbath(G,beta,T0,Tb),Pmax,Pmin))/Rn + "\r\n");
where P currently stands, i don't know what to substitute for P, since in the boolean code there's no mention of P either:
private static boolean minmax = true;
public double I0(double Vb, double Pbath, double Pmax, double Pmin)
{
if(minmax) private static boolean minmax = true;
public double I0(double Vb, double Pbath, double Pmax, double Pmin)
{
if(minmax)
{
minmax = false;
return ((Pbath-Pmax)/Vb);
}
else
{
minmax = true;
return ((Pbath-Pmin)/Vb);
}
}
{
minmax = false;
return ((Pbath-Pmax)/Vb);
}
else
{
minmax = true;
return ((Pbath-Pmin)/Vb);
}
}
(i know previously we had double P with other doubles like Vb up there somwhere but i thought it would be unnecessary since we mention nothing of the sort in the actual code itself so i got rid of it.)
now i'm faced with that problem... what can i do?
[[edit2]]
oh and also: in terms of the graphing code, after getting those two columns P and R0/Rn (saved as output.txt) i tried calling it up in the graphing code to graph, P column hopefully being the x values and R0/Rn as y values. i even got rid of the very first line where it's not a set of data... is it because it's in two columns? the elliptical.data that i looked at was also in two columns so i don't think it'd be any problem... i'm a bit confused.
Aradon
08-01-2005, 10:23 PM
Yes, on number three you will only get 1 value returned.
Maybe this?
FileOutputStream out;
PrintStream p;
out = new FileOutputStream("output.txt");
p = new PrintStream( out );
p.println(P + "\t" + R0(Vb,I0(Vb,P,Pbath(G,beta,T0,Tb),Pmax,Pmin))/Rn + "\n");
p.println(P + "\t" + R0(Vb,I0(Vb,P,Pbath(G,beta,T0,Tb),Pmax,Pmin))/Rn + "\n");
p.close();
I've changed the fact that it's doubled and the \r is removed and there is only a \n.
I'm thinking the \r may have thrown it off as far as graphing is concerned which is why I removed it.
To be perfectly honest I wouldn't do the writing in this manor. I would use File Writer and BufferedWriter. That's more of a personal preference though and it would go something like this:
BufferedWriter out = new BufferedWriter(new FileWriter("output.txt", true));
out.write(P + "\t" + R0(Vb,I0(Vb,P,Pbath(G,beta,T0,Tb),Pmax,Pmin))/Rn + "\n")
out.write(P + "\t" + R0(Vb,I0(Vb,P,Pbath(G,beta,T0,Tb),Pmax,Pmin))/Rn + "\n")
out.close();
But I've heard people not liking that for various reasons (apparently notepad doesn't like the \n character in windows but wordpad loves it).
Hope this helps some more ^_^
NewAtJava
08-01-2005, 10:37 PM
ah the BufferedWriter works too. but either way, it still doesn't solve the problem of plugging in values between Pmax and Pmin for P etc that i listed when editing the response above... not only do i want to plug in Pmax (4.0) and Pmin(1.171) for P, i'd also like the values between them, i.e. 3.0. maybe increments of 0.1, let's say. how would i do that?
Aradon
08-01-2005, 11:06 PM
incrementing and decrementing is easy with a for loop.
for(double i = Pmin; i < Pmax; i+.01)
{
// Do something with the increment of i
}
the variable i will then be incremented by .01 until it reaches Pmax. You can use this then to generate different points in your dataset.
The boolean code doesn't include P because there is no need to use P in order to generate the RO/Rn equation. P is only used in that one instance that you want to create the point set in the file.
I guess I'm kind of confused on the mathmatical part of this (four years of calculus seems to have failed me). You have your Pmin which is the mininmum P value (obviously) and your Max is your Max value. So My new assumption is that P is a function or a variable? If it's a function you can use the incrementation to just plug it back into function P. If it's a variable then I'm unsure what you mean completely.
Sorry I'm a bit confused. ^_^
NewAtJava
08-02-2005, 04:00 PM
private static boolean minmax = true;
public double I0(double Vb, double Pbath, double Pmax, double Pmin)
{
if(minmax)
{
minmax = false;
for(double i = Pmin; i < Pmax; i+.01) {
return ((Pbath-i)/Vb);
}
}
else
{
minmax = true;
for(double i = Pmin; i < Pmax; i+.01) {
return ((Pbath-i)/Vb);
}
}
}
that's what i tried to do and it returned me two errors saying that i+.01 wasn't a statement...
my P was temporary, if you could say that, and it's just a variable. like x. where i plug in values b/t Pmax and Pmin. basically the equation involves a variable that should be changed from Pmin to Pmax in the increments of say 0.1.
thank you so much!!
Aradon
08-02-2005, 06:15 PM
As far as the for loop goes it seems I'm thinking a different language..
Do a while loop instead
double i = Pmin;
while(i < Pmax)
{
// Let i do it's job
i += .01; // <--interval
}
I think you want to put this in the calling method, not the method itself. So where you call IO to print.
FileOutputStream out;
PrintStream p;
out = new FileOutputStream("output.txt");
p = new PrintStream( out );
double i = Pmin;
while(i < Pmax)
{
p.println(P + "\t" + R0(Vb,I0(Vb,Pbath(G,beta,T0,Tb),i))/Rn + "\r\n");
i += .01;
}
p.close();
And change your IO code to
public double I0(double Vb, double Pbath, double i)
{
if(minmax)
{
minmax = false;
return ((Pbath-i)/Vb);
}
else
{
minmax = true;
return ((Pbath-i)/Vb);
}
}
I think :D
NewAtJava
08-02-2005, 07:46 PM
that works well, except now the output is skipping a line instead of line after line... basically instead of this:
1 2
3 4
5 6
it comes out as:
1 2
3 4
5 6
for some reason... hmm.
i have a different bit of a question... i want to work with some complex numbers for one of the equations written below:
public double sI(double omega, double I0, double R0, double betaI, double tau_plus, double tau_minus, double tauI) {
return ((-1./(I0*R0))*(1./(2+betaI))*((1-tau_plus/tauI)/omega*tau_plus*i)*((1-tau_minus/tauI)/omega*tau_minus*i));
}
and the i in there is i in complex numbers, square root of -1. and since java doesn't have a built-in thing for complex numbers, i imported a code found online here:
http://www.cs.princeton.edu/introcs/97data/Complex.java.html
i saved the code as Complex.java under a folder called classes.
i found that i first had to put this line on the top of the Complex.java code to import to my thing:
package classes;
then i put the import classes.Complex; line on top of my calculating program. then i changed the equation code above to this:
public Complex sI(double omega, double I0, double R0, double betaI, double tau_plus, double tau_minus, double tauI) { // power-to-current responsivity
return ((-1./(I0*R0))*(1./(2+betaI))*((1-tau_plus/tauI)/Complex(1.,omega*tau_plus))*((1-tau_minus/tauI)/Complex(1.,omega*tau_minus)));
}
cuz i thought that's how i should be using the Complex class, but it didn't work. what am i doing wrong?
thank you for everything.
Aradon
08-02-2005, 08:16 PM
that works well, except now the output is skipping a line instead of line after line... basically instead of this:
1 2
3 4
5 6
it comes out as:
1 2
3 4
5 6
for some reason... hmm.
Yeah cause I copy and pasted incorrectly:
FileOutputStream out;
PrintStream p;
out = new FileOutputStream("output.txt");
p = new PrintStream( out );
double i = Pmin;
while(i < Pmax)
{
p.println(P + "\t" + R0(Vb,I0(Vb,Pbath(G,beta,T0,Tb),i))/Rn + "\n");
i += .01;
}
p.close();
is the fix (deleted the \r)
public Complex sI(double omega, double I0, double R0, double betaI, double tau_plus, double tau_minus, double tauI) { // power-to-current responsivity
return ((-1./(I0*R0))*(1./(2+betaI))*((1-tau_plus/tauI)/Complex(1.,omega*tau_plus))*((1-tau_minus/tauI)/Complex(1.,omega*tau_minus)));
}
What a statement. I'm not sure what you're doing here to be honest. It looks like you are creating from the constructor complex. The problem is you are doing math function on an Object (which without a default isn't entirly possible). It looks like if you're going to do any multiplication/addition/division with a complex Number you'll need to make everything complient with it (which looks like a huge pain).
But if you're going to create a complex object you must remember to use the new operator. My suggestion is to put it into a variable for easy early use, (so
Complex num1 = new Complex(1.,omega*tau_plus);
Complex num2 = new Complex(1.,omega*tau_minus);
Then go ahead through. You may have to consider doing it so that it's like,
num1.divide(something)
Right about now you're getting into that theoretical math which I don't know that much about. But if you give me the errors it's giving you, I may still be able to help a bit ^_^
NewAtJava
08-02-2005, 08:34 PM
if i were to do this:
Complex num1 = new Complex(1.,omega*tau_plus);
Complex num2 = new Complex(1.,omega*tau_minus);
would that be a separate thing from other public double equations? cuz when i put it separately it's saying that it doesn't recognize omega and such, "cannot resolve symbol" and stuff... where do i define them as doubles?
[[edit]]
i don't think the Complex.java code that i showed you has any dividing operation... unless i'm blind. which is possible. but i found a more extensive Complex code:
http://www.netlib.org/java/
(the first item)
but i can't figure out how to use this one...
Aradon
08-02-2005, 09:10 PM
if i were to do this:
Complex num1 = new Complex(1.,omega*tau_plus);
Complex num2 = new Complex(1.,omega*tau_minus);
would that be a separate thing from other public double equations? cuz when i put it separately it's saying that it doesn't recognize omega and such, "cannot resolve symbol" and stuff... where do i define them as doubles?
You would place these two inside the method. Not outside of it.
public Complex times(Complex b) {
Complex a = this;
double real = a.re * b.re - a.im * b.im;
double imag = a.re * b.im + a.im * b.re;
Complex prod = new Complex(real, imag);
return prod;
}
Can't you just modify this to make it divide (replace the * with / )? Just curious on that effect. I'll have to read up on the code you just posted for the complex numbers, my computer is having trouble opening it (cause it's made of evil :D )
NewAtJava
08-02-2005, 09:17 PM
ok so i did that:
// return a new object whose value is (this / b)
public Complex divide(Complex b) {
Complex a = this;
double real = a.re / b.re - a.im / b.im;
double imag = a.re / b.im + a.im / b.re;
Complex div = new Complex(real, imag);
return div;
}
and i'm assuming that could be used like:
Complex c = b.divide(a);
[[edit]]
public Complex sI(double omega, double I0, double R0, double betaI, double tau_plus, double tau_minus, double tauI) {
Complex a = new Complex(1.,omega*tau_plus);
Complex b = new Complex(1.,omega*tau_minus);
Complex c = a.divide(1-tau_plus/tauI);
Complex d = b.divide(1-tau_minus/tauI);
Complex e = c.times(d);
Complex f = e.times(1./(2+betaI));
Complex g = f.times(-1./(I0*R0));
return (g);
}
tried that... and gave me two errors:
TES2.java:134: divide(classes.Complex) in classes.Complex cannot be applied to (double)
Complex c = a.divide(1-tau_plus/tauI);
^
TES2.java:135: divide(classes.Complex) in classes.Complex cannot be applied to (double)
Complex d = b.divide(1-tau_minus/tauI);
^
except from my new divide code i don't know why it cannot be applied to a double number...
Aradon
08-02-2005, 11:36 PM
because there is no constructor that has one double, only two doubles.
I'm not sure how you would define a real number with a complex number..that would be one of those math things :D
NewAtJava
08-03-2005, 04:51 PM
i decided that the shorter code was too limited, so i think i'll stick with the second code (a lot longer one, the one that's zipped).
i tried to write my equation while looking at the TestComplex.java (it's in the zipped thing, and it shows how things can be calculated and gives examples etc) and wrote this:
public Complex sI(double omega, double I0, double R0, double betaI, double tau_plus, double tau_minus, double tauI) {
return ((((1-tau_plus/tauI)+0.0i).div(0.0+omega*tau_plusi)).mul(((1-tau_minus/tauI)+0.0i).div(0.0+omega*tau_minusi))*(1./(2+betaI))*(-1./(I0*R0)));
}
but there are two errors:
TES2.java:132: ')' expected
return ((((1-tau_plus/tauI)+0.0i).div(0.0+omega*tau_plusi)).mul(((1-tau_minus/tauI)+0.0i).div(0.0+omega*tau_minusi))*(1./(2+betaI))*(-1./(I0*R0)));
^
TES2.java:132: ')' expected
return ((((1-tau_plus/tauI)+0.0i).div(0.0+omega*tau_plusi)).mul(((1-tau_minus/tauI)+0.0i).div(0.0+omega*tau_minusi))*(1./(2+betaI))*(-1./(I0*R0)));
^
2 errors
and i have no idea what they mean since i know the parenthesis are all correct... help?
[[edit]]
oh also, unrelated to complex numbers problem:
i want this one calculating program to produce two .txt files with different data in them. so i tried:
FileOutputStream out;
PrintStream p;
out = new FileOutputStream("R0RnP.txt");
p = new PrintStream(out);
double P = Pmin;
while(P<Pmax){
p.println(P + "\t" + R0(Vb,I0(Vb,Pbath(G,beta,T0,Tb),P))/Rn + "\n"); // CHECK UNITS!
P+=.01;
}
p.close();
FileOutputStream out1;
PrintStream p1;
out1 = new FileOutputStream("sIP.txt");
p1 = new PrintStream(out1);
while(P<Pmax){
p1.println(P + "\t" + R0(Vb,I0(Vb,Pbath(G,beta,T0,Tb),P))/Rn + "\n"); // CHECK UNITS!
P+=.01;
}
p1.close();
yes, the equations are same, but that's cuz i just wanted to see how it worked; and it did produce two separate files, R0RnP.txt and sIP.txt, but then sIP.txt is empty... what am i doing wrong?
Aradon
08-03-2005, 07:52 PM
return ((((1-tau_plus/tauI)+0.0i).div(0.0+omega*tau_plusi)).mul(((1-tau_minus/tauI)+0.0i).div(0.0+omega*tau_minusi))*(1./(2+betaI))*(-1./(I0*R0)));
Man that's a complicated statement. We'll save that till the end..
(btw to procude these code segments just type [*code] [/*code] without the * and place the code in between.
FileOutputStream out;
PrintStream p;
out = new FileOutputStream("R0RnP.txt");
p = new PrintStream(out);
double P = Pmin;
while(P<Pmax){
p.println(P + "\t" + R0(Vb,I0(Vb,Pbath(G,beta,T0,Tb),P))/Rn + "\n"); // CHECK UNITS!
P+=.01;
}
p.close();
FileOutputStream out1;
PrintStream p1;
out1 = new FileOutputStream("sIP.txt");
p1 = new PrintStream(out1);
while(P<Pmax){
p1.println(P + "\t" + R0(Vb,I0(Vb,Pbath(G,beta,T0,Tb),P))/Rn + "\n"); // CHECK UNITS!
P+=.01;
}
p1.close();
The first file writes everything you want it to because P is less then Pmax. In the first whlie loop you keep adding to P over and over till P is equalto or greater then Pmax. You then come down to the second while loop. P is still equalto or greater then Pmax, so it never does anything.
So, just add P = Pmin; after the p1 = new PrintStream(out1);
FileOutputStream out;
PrintStream p;
out = new FileOutputStream("R0RnP.txt");
p = new PrintStream(out);
double P = Pmin;
while(P<Pmax)
{
p.println(P + "\t" + R0(Vb,I0(Vb,Pbath(G,beta,T0,Tb),P))/Rn + "\n"); // CHECK UNITS!
P+=.01;
}
p.close();
FileOutputStream out1;
PrintStream p1;
out1 = new FileOutputStream("sIP.txt");
p1 = new PrintStream(out1);
P = Pmin;
while(P<Pmax)
{
p1.println(P + "\t" + R0(Vb,I0(Vb,Pbath(G,beta,T0,Tb),P))/Rn + "\n"); // CHECK UNITS!
P+=.01;
}
p1.close();
That should fix that.
Now to the statement. The error messages are stating that you are missing a ) where there should be (duh) so let's go through it.
12343232123432
return ((((1-tau_plus/tauI)+0.0i).div(0.0+omega*tau_plusi)).mul(((1-tau_minus/tauI)+0.0i).div(0.0+omega*tau_minusi))*(1./(2+betaI))*(-1./(I0*R0)));
The number I've placed is the number of paretheses open at different times of the script. So it looks like your parentheses are fine (yay). The 2 at the end means you still have 2 parentheses open. The script seems to think that both of these need to be closed for some reason. My question is what are you dividing? Are you trying to multiply and then divide or are you multiplying something that needs to be divided? Also, is i a variable? does tau_plusi mean tau_plus * i or just as it looks? 0.0i is a variable? Are you sure?
Can you post what the div method does here? My program I use for the zips has decided to flunk out on me.
[[Edit]]
Okay, another point. I think what you need to do is somehow make everything a complex number. You can actually do this with the complex class given from http://www.netlib.org/java/ as they have a constructor for a complex number with a real number representation. If you read through complex.html and look at the API there it may be able to help you a little bit instead of looking at the code.
NewAtJava
08-03-2005, 08:12 PM
thanks!
well i counted all the parenthesis and i didn't find any open parenthesis left at the end... they were all closed, and they were all fine... it's still giving me the same errors though. i'll paste the code again:
public Complex sI(double omega, double I0, double R0, double betaI, double tau_plus, double tau_minus, double tauI) {
return ( ((((1-tau_plus/tauI)+(0.0)i).div(0.0+(omega*tau_plus)i)).mul(((1-tau_minus/tauI)+(0.0)i).div(0.0+(omega*tau_minus)i))) * ((1./(2+betaI))*(-1./(I0*R0))) );
}
i's aren't variables. i is supposed to be square root of -1, as defined by complex numbers.
the one off of netlib is the one that i'm trying to work with. here's the section abt dividing:
div
public Complex div(Complex z)
To perform z1 / z2, you write z1.div(z2) .
(a + i*b) / (c + i*d) = ( (a*c) + (b*d) + i*((b*c) - (a*d)) ) / norm(c + i*d)
Take care not to divide by zero!
Note:
Complex arithmetic in Java never causes exceptions. You have to deliberately check for overflow, division by zero, and so on, for yourself.
Domain Restrictions:
z1/z2 is undefined if z2 = 0
See Also:
scale(double)
and multiplying:
mul
public Complex mul(Complex z)
To perform z1 * z2, you write z1.mul(z2) .
(a + i*b) * (c + i*d) = ( (a*c) - (b*d) + i*((a*d) + (b*c)) )
See Also:
scale(double)
so i thought i was using it correctly in the thing but i guess not...
here's the code, TestComplex.java... basically the program for testing all aspects of Complex.class. i took out irrelevant stuff like cosines and sines and such to make the code a bit shorter...
package ORG.netlib.math.complex;
import ORG.netlib.math.complex.Complex;
public class
TestComplex {
static final String MODULE = TestComplex.class.getName();
static final String VERSION = "1.0.1";
static final String DATE = "Fri 23-Mar-2001 9:13 pm";
static final String AUTHOR = "sandy@almide.demon.co.uk";
static final String REMARK = "Tester for Complex class";
public static void
main (String[] args) {
// debug = false; // !!!
final String nanStr = "NaN";
final String negInfStr = "-Infinity";
final String posInfStr = "Infinity";
final String traceFlagStr = "-trace";
boolean setDebug = false;
int maxArg = 6;
String method = "unknown";
double[] doubleArgs = {0.0, 0.0, 0.0, 0.0, 0.5, 999, 999}; //!!! epsilon woz ere
boolean noArgs = false;
boolean inputError = false;
double scalar = 100.0;
double tolerance;
String complexClassName;
Complex z1 = null;
Complex z2;
if (args.length == 0) {
inputError = true;
noArgs = true;
} else {
method = args[0];
for (int i = 1; ((i < args.length)) ; i++) {
try {
if (args[i].equalsIgnoreCase(traceFlagStr)) {
setDebug = true;
} else {
if (i >= maxArg) break;
String argsI = args[i];
if (argsI.equalsIgnoreCase(nanStr)) {
doubleArgs[i-1] = Double.NaN;
} else if (argsI.equalsIgnoreCase(posInfStr)) {
doubleArgs[i-1] = Double.POSITIVE_INFINITY;
} else if (argsI.equalsIgnoreCase(negInfStr)) {
doubleArgs[i-1] = Double.NEGATIVE_INFINITY;
} else {
doubleArgs[i-1] = Double.valueOf(args[i]).doubleValue();
}//endif
}//endif
} catch (NumberFormatException e) {
inputError = true;
}//endtry
}//endfor
}//endif
z1 = Complex.cart(doubleArgs[0], doubleArgs[1]);
z2 = Complex.cart(doubleArgs[2], doubleArgs[3]);
tolerance = doubleArgs[4];
complexClassName = z1.getClass().getName();
System.out.println();
if (!noArgs) {
System.out.println("z1 == " + z1);
System.out.println("z2 == " + z2);
System.out.println("tolerance == " + tolerance);
// System.out.println("trace == " + debug); // !!!
System.out.println();
if (method.equals("real")) {
System.out.println(complexClassName + "." + method + "(" + z1.re() + ") == " + Complex.real(z1.re()));
} else if (method.equals("cart")) {
System.out.println(complexClassName + "." + method + z1 + " == " + Complex.cart(z1.re(), z1.im()));
} else if (method.equals("polar")) {
System.out.println(complexClassName + "." + method + z1 + " == " + Complex.polar(z1.re(), z1.im()));
} else if (method.equals("pow")) {
System.out.println(complexClassName + "." + method + "(" + z1.re() + ", " + z2 + ") == " + Complex.pow(z1.re(), z2));
System.out.println(complexClassName + "." + method + "(" + z1 + ", " + z2.re() + ") == " + Complex.pow(z1, z2.re()));
System.out.println(complexClassName + "." + method + "(" + z1 + ", " + z2 + ") == " + Complex.pow(z1, z2));
} else if (method.equals("isInfinite")) {
System.out.println(z1 + "." + method + "() == " + z1.isInfinite());
System.out.println(z1 + ".isNaN() == " + z1.isNaN());
} else if (method.equals("isNaN")) {
System.out.println(z1 + "." + method + "() == " + z1.isNaN());
System.out.println(z1 + ".isInfinite() == " + z1.isInfinite());
} else if (method.equals("equals")) {
System.out.println(z1 + "." + method + "(" + z2 + ", " + tolerance + ") == " + z1.equals(z2, tolerance));
} else if (method.equals("re")) {
System.out.println(z1 + "." + method + "() == " + z1.re());
} else if (method.equals("im")) {
System.out.println(z1 + "." + method + "() == " + z1.im());
} else if (method.equals("norm")) {
System.out.println(z1 + "." + method + "() == " + z1.norm());
} else if (method.equals("neg")) {
System.out.println(z1 + "." + method + "() == " + z1.neg());
} else if (method.equals("add")) {
System.out.println(z1 + "." + method + z2 + " == " + z1.add(z2));
} else if (method.equals("sub")) {
System.out.println(z1 + "." + method + z2 + " == " + z1.sub(z2));
} else if (method.equals("mul")) {
System.out.println(z1 + "." + method + z2 + " == " + z1.mul(z2));
} else if (method.equals("div")) {
System.out.println(z1 + "." + method + z2 + " == " + z1.div(z2));
} else if (method.equals("sqrt")) {
System.out.println(z1 + "." + method + "() == " + z1.sqrt());
} else if (method.equals("exp")) {
System.out.println(z1 + "." + method + "() == " + z1.exp());
} else if (method.equals("toString")) {
System.out.println(z1 + "." + method + "() == " + z1.toString());
} else {
System.out.println("Does the method '" + method + "' have the correct spelling?");
System.out.println();
System.out.println("(Run TestComplex with no args to get a list of method-names.)");
inputError = true;
}//endif
}//endif
if (inputError) {
if (noArgs) {
System.out.println("Tester...");
System.out.println(" Module : " + MODULE);
System.out.println(" Version: " + VERSION);
System.out.println(" Date : " + DATE);
System.out.println(" Author : " + AUTHOR);
System.out.println(" Remark : " + REMARK);
System.out.println("Testing...");
System.out.println(" Module : " + complexClassName);
System.out.println(" Version: " + Complex.VERSION);
System.out.println(" Date : " + Complex.DATE);
System.out.println(" Author : " + Complex.AUTHOR);
System.out.println(" Remark : " + Complex.REMARK);
System.out.println();
manInfo();
}//endif
System.out.println();
System.out.print("usage: ");
System.out.println("java " + MODULE + " method-name [a] [bi] [c] [di]");
}//endif
}//end main(String[])
static void
manInfo () {
System.out.println("The following is a list of Complex methods:");
System.out.println();
System.out.println(" method-name arguments -- remark");
System.out.println(" -------------------------------------------------------------------");
System.out.println(" add sub mul div a b c d");
System.out.println();
System.out.println(" arg a b -- angle (in radians)");
System.out.println(" abs a b -- length, or magnitude");
System.out.println(" norm a b -- square of magnitude");
System.out.println(" scale a b scalar -- scale complex by a real number");
System.out.println(" neg a b -- scale by minus 1");
System.out.println(" conj a b -- complex conjugate");
System.out.println();
System.out.println(" sqrt a b -- square root");
System.out.println(" exp a b -- raise e to a complex power");
System.out.println(" log a b -- natural logarithm");
System.out.println(" pow a b c d -- raise (a+bi) to the power (c+di)");
System.out.println();
System.out.println(" sin cos tan a b --");
System.out.println(" cosec sec cot a b --");
System.out.println(" sinh cosh tanh a b -- trigonometry");
System.out.println(" asin acos atan a b --");
System.out.println(" asinh acosh atanh a b --");
System.out.println();
System.out.println(" polar r theta -- convert polar to cartesian");
System.out.println();
System.out.println(" -- More method-names ...");
System.out.println(" cart a b");
System.out.println(" clone a b");
System.out.println(" equals a b c d tolerance");
System.out.println(" isInfinite isNaN a b");
System.out.println(" re im a b");
System.out.println(" real x");
System.out.println(" toString a b");
}//end manInfo()
}//end TestComplex
i don't know whether or not the parenthesis errors are caused by my misusage of the Complex.class...
[[edit]]
here's the part abt multiplying and dividing from the actual code Complex.java:
/**
* To perform z1 * z2, you write <tt>z1.mul(z2)</tt> .
*
* <p>
* <pre>
* (a + <i><b>i</b></i>*b) * (c + <i><b>i</b></i>*d) = ( (a*c) - (b*d) + <i><b>i</b></i>*((a*d) + (b*c)) )
* </pre>
* <p>
* @see Complex#scale(double)
**/
public Complex
mul (Complex z) {
return cart( (re*z.re) - (im*z.im), (re*z.im) + (im*z.re) );
// return cart( (re*z.re) - (im*z.im), (re + im)*(z.re + z.im) - re*z.re - im*z.im);
}//end mul(Complex)
/**
* To perform z1 / z2, you write <tt>z1.div(z2)</tt> .
*
* <p>
* <pre>
* (a + <i><b>i</b></i>*b) / (c + <i><b>i</b></i>*d) = ( (a*c) + (b*d) + <i><b>i</b></i>*((b*c) - (a*d)) ) / norm(c + <i><b>i</b></i>*d)
* </pre>
* <p>
* <i><b>Take care not to divide by zero!</b></i>
* <p>
* <i><b>Note:</b><ul> <tt>Complex</tt> arithmetic in Java never causes
* exceptions. You have to deliberately check for overflow, division by
* zero, and so on, <u>for yourself</u>.
* </ul></i>
* <p>
* <i><b>Domain Restrictions:</b><ul> z1/z2 is undefined if z2 = 0
* </ul></i>
* <p>
* @see Complex#scale(double)
**/
public Complex
div (Complex z) {
Complex result = new Complex(this);
div(result, z.re, z.im);
return result;
}//end div(Complex)
static private void
div (Complex z, double x, double y) {
// Adapted from
// "Numerical Recipes in Fortran 77: The Art of Scientific Computing"
// (ISBN 0-521-43064-X)
double zRe, zIm;
double scalar;
if (Math.abs(x) >= Math.abs(y)) {
scalar = 1.0 / ( x + y*(y/x) );
zRe = scalar * (z.re + z.im*(y/x));
zIm = scalar * (z.im - z.re*(y/x));
} else {
scalar = 1.0 / ( x*(x/y) + y );
zRe = scalar * (z.re*(x/y) + z.im);
zIm = scalar * (z.im*(x/y) - z.re);
}//endif
z.re = zRe;
z.im = zIm;
}//end div(Complex,double,double)
Aradon
08-04-2005, 02:55 PM
public Complex sI(double omega, double I0, double R0, double betaI, double tau_plus, double tau_minus, double tauI) {
return (((((1-tau_plus/tauI)+(0.0)i).div(0.0+(omega*tau_plus)i)).mul(((1-tau_minus/tauI)+(0.0)i).div(0.0+(omega*tau_minus)i))) * ((1./(2+betaI))*(-1./(I0*R0))) );
}
i's aren't variables. i is supposed to be square root of -1, as defined by complex numbers.
Okay, well I'm going to assume that i is a variable in the complex class and go on. The reason for this error is that it doesn't see a complex divided by a complex.
Instead it probably see's you as trying to take a number and using it in a "complex" method. Or so my guess goes.
You might want to consider breaking this long statement apart so that you can see step by step what is going on. It's eaiser to debug it as such, even though yours just looks neat :>
NewAtJava
08-04-2005, 04:39 PM
well ok, this is what i did to break it down:
public Complex div1(double tau_plus, double tauI, double omega){
return( ((1-tau_plus/tauI)+(0.0)i).div(0.0+(omega*tau_plus)i) );
}
public Complex div2(double tau_minus, double tauI, double omega){
return( ((1-tau_minus/tauI)+(0.0)i).div(0.0+(omega*tau_minus)i) );
}
public Complex sI(double div1, double I0, double R0, double betaI, double div2) {
return ( (div1.mul(div2)) * ((1./(2+betaI))*(-1./(I0*R0))) );
}
it still gives me that parenthesis error for those two statements.
the first two division statements: i don't understand why it won't work that way, i'm still thinking that i misinterpreted the Complex code usage but i keep on looking at the code and i can't figure it out... because in TestComplex, when you compile & execute it, it works for a similar case. i'll paste what i see when i execute it with some numbers:
10:29:58 java/# java ORG.netlib.math.complex.TestComplex div 3 0 0 2
z1 == (3.0 + 0.0i)
z2 == (0.0 + 2.0i)
tolerance == 0.510:29:58 java/# java ORG.netlib.math.complex.TestComplex div 3 0 0 2
z1 == (3.0 + 0.0i)
z2 == (0.0 + 2.0i)
tolerance == 0.5
(3.0 + 0.0i).div(0.0 + 2.0i) == (0.0 - 1.5i)
(3.0 + 0.0i).div(0.0 + 2.0i) == (0.0 - 1.5i)
so essentially it shouldn't have any problems... i'm lost. i pasted the TestComplex code in the post above.
ps: i isn't a variable; it's a constant that has the value of square root of -1. it's a constant defined by this complex class.
NewAtJava
08-04-2005, 09:40 PM
ok here's the progress i've made so far:
public Complex div1(double tau_plus, double tauI, double omega){
Complex z1 = Complex.cart(1-tau_plus/tauI, 0.0);
Complex z2 = Complex.cart(0.0, omega*tau_plus);
return(z1.div(z2));
}
public Complex div2(double tau_minus, double tauI, double omega){
Complex z3 = Complex.cart(1-tau_minus/tauI,0.0);
Complex z4 = Complex.cart(0.0, omega*tau_minus);
return(z3.div(z4));
}
public Complex m(Complex div1, Complex div2){
return (div1.mul(div2));
}
public double m1(Complex m){
return(m.re());
}
public double sI(double I0, double R0, double betaI, double m1) {
return ( m1 * ((1./(2+betaI))*(-1./(I0*R0))) );
}
and it compiles w/o a problem. the problem is the resulting data file (i'm doing the same thing w/ R0/Rn case; P values first column, these new sI values second column). The sI values are all NaN, but i know for a fact that the values aren't imaginary; they're real numbers (because i's cancel out in the process, becoming a real number) and they shouldn't give any NaNs. any thoughts?
just in case, here's the printing code:
FileOutputStream out1;
PrintStream p1;
out1 = new FileOutputStream("sIP.txt");
p1 = new PrintStream(out1);
P = Pmin;
while(P<Pmax){
p1.println(P + "\t" + sI(I0(Vb,Pbath(G,beta,T0,Tb),P), R0(Vb,I0(Vb,Pbath(G,beta,T0,Tb),P)), betaI, m1(m(div1(tau_plus(tau(C,G), tauel(L, RL, R0(Vb, I0(Vb, Pbath(G,beta,T0,Tb), P)), betaI), tauI(tau(C,G), LI(PJ0(Pbath(G,beta,T0,Tb), Pmin), alphaI, G, T0)), R0(Vb, I0(Vb, Pbath(G,beta,T0,Tb), P)), L, LI(PJ0(Pbath(G,beta,T0,Tb), Pmin), alphaI, G, T0), betaI), tauI(tau(C,G), LI(PJ0(Pbath(G,beta,T0,Tb), Pmin), alphaI, G, T0)), omega(f)), div2(tau_minus(tau(C,G), tauel(L, RL, R0(Vb, I0(Vb, Pbath(G,beta,T0,Tb), P)), betaI), tauI(tau(C,G), LI(PJ0(Pbath(G,beta,T0,Tb), Pmin), alphaI, G, T0)), R0(Vb, I0(Vb, Pbath(G,beta,T0,Tb), P)), L, LI(PJ0(Pbath(G,beta,T0,Tb), Pmin), alphaI, G, T0), betaI), tauI(tau(C,G), LI(PJ0(Pbath(G,beta,T0,Tb), Pmin), alphaI, G, T0)), omega(f))))) + "\n"); // CHECK UNITS!
P+=.01;
}
p1.close();
Aradon
08-05-2005, 02:04 PM
If we assume that the logic of the complex stuff is valid then I can only assume that the math somewhere in that LONG print statement is invalid. Once again, maybe you should try splitting it up to simplify it.
This time though, put it into a variable so that you can just call a shorter print statement.
So do one for each function call starting from the inner most function and going out. Hopefully that will help isolate the problem.
NewAtJava
08-05-2005, 03:24 PM
well the fact that the program itself compiled fine suggests to me that the code themselves are ok really... it's just that when i execute it i get NaNs. and i rechecked the print and it all makes sense... but i still don't know why it's NaN, maybe i should check some values
NewAtJava
08-05-2005, 04:10 PM
oh well. i didn't quite get it to work, but no matter, the project is over.
thank you SO much for all your help. i couldn't have done it without you.
Aradon
08-06-2005, 11:54 PM
Sorry we couldn't get it done in time. It was no problem, let me know if you need any more help, you can pm me here or start another topic ^_^
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.