Announcing NR3 in Java (port of C++ version 3.04)
Bill Press
06-04-2012, 06:57 PM
Numerical Recipes user Huang Wen Hui has produced high-quality translations of the Numerical Recipes C++ code, version 3.04, into Java. With his permission, we are pleased to make this Java code available to all licensed users of Numerical Recipes Third Edition Machine-Readable Code. The Java code is now included in all purchased downloads (https://xnumrec.merchantquest.net/cart/Step1.php) of both the C++ and All Languages machine readable code products. Users who have previously purchased these products may contact us (http://www.nr.com/contactform.php)to obtain the Java code. (Please include your previous order number, or other identifying information.)
Note that the Java translations are not a supported product of Numerical Recipes Software, but are provided only as a convenience to our users. Also, because these are contributed by a user, but based on Numerical Recipes code, their use is governed by the license and disclaimer of warranty here (http://www.nr.com/aboutJava.html).
Please use this forum to post questions about using NR3 in Java. Even better, please answer other user's questions, or post tutorials here. Numerical Recipes in Java is, for now, strictly user-supported!
kutta
09-02-2012, 04:36 AM
Numerical Recipes user Huang Wen Hui has produced high-quality translations of the Numerical Recipes C++ code, version 3.04, into Java. With his permission, we are pleased to make this Java code available to all licensed users of Numerical Recipes Third Edition Machine-Readable Code. The Java code is now included in all purchased downloads (https://xnumrec.merchantquest.net/cart/Step1.php) of both the C++ and All Languages machine readable code products. Users who have previously purchased these products may contact us (http://www.nr.com/contactform.php)to obtain the Java code. (Please include your previous order number, or other identifying information.)
Note that the Java translations are not a supported product of Numerical Recipes Software, but are provided only as a convenience to our users. Also, because these are contributed by a user, but based on Numerical Recipes code, their use is governed by the license and disclaimer of warranty here (http://www.nr.com/aboutJava.html).
Please use this forum to post questions about using NR3 in Java. Even better, please answer other user's questions, or post tutorials here. Numerical Recipes in Java is, for now, strictly user-supported!
Hello NR Authors & colleagues
Please inform about the status of JavaVersion of already initiated one some years ago.
Thanks
As
C.R.Muthukumar(Kutta)
davekw7x
11-06-2012, 11:09 AM
...translations of the Numerical Recipes C++ code, version 3.04, into Java....
Please use this forum to post questions about using NR3 in Java. Even better, please answer other user's questions, or post tutorials here. Numerical Recipes in Java is, for now, strictly user-supported!
I downloaded, unzipped, did a cursory inspection, and wrote a couple of tests with Java_304.zip.
Tests were done on Centos Linux workstations, with the "standard" Java supplied in RedHat/Centos 5.8 distributions: OpenJDK version 1.6.0_22, as of this writing.
Discoveries so far:
An annoying "feature" of build.xml is that an "ant clean" erases the supplied text files INSTALL.TXT, LICENSE.TXT, and NAMES.txt. It's easy to fix it so that doesn't happen.
I just commented out a line in the <delete> section of the "clean" target:
<target name="clean">
<delete dir="build"/>
<delete dir="${junit.output.dir}"/>
<delete>
<fileset dir="." includes="*.ps"/>
<fileset dir="." includes="*.dat"/>
<!-- davekw7x: Don't delete txt files! -->
<!--<fileset dir="." includes="*.txt"/> -->
</delete>
</target>
In fact, I don't see the need to delete .ps or .dat files either, but I left the other stuff in place, since my goal is to get something useful (and a little less annoying) with minimal changes in the original.
A bug in build.xml causes "ant jar" to fail. It's easy to fix that also.
The problem is that with the "jar" target, it creates a directory named "build/jar" but then designates the destination file name being in directory "build/lib"
Here's the section that I changed:
<target depends="build" name="jar">
<!-- davekw7x make directory consistent with destfile -->
<!-- <mkdir dir="build/jar"/> -->
<mkdir dir="build/lib"/>
<jar destfile="build/lib/nr3.jar" basedir="build/classes">
</jar>
</target>
So far I haven't been able to get "ant AllTests" to work. I haven't used ant-junit before, so I may be doing something wrong.
Note that I have successfully completed a few tests with tutorial material from ant.apache.org, but nothing with the SuiteClasses stuff, so I'm really lost.
Anyhow...
I have attached a zipped up log file that shows what happened when I ran "ant -v AllTests 2>& 1 | tee ant_AllTests.log"
(This was after executing "ant jar" successfully.)
Bottom line:
[junit] Test com.nr.test.AllTests FAILED
Note that compilation of AllTests.java had successfully created AllTests.class.
I will show results of a test program or two in subsequent posts on a new thread.
Regards,
Dave
kutta
09-25-2013, 06:07 AM
Numerical Recipes user Huang Wen Hui has produced high-quality translations of the Numerical Recipes C++ code, version 3.04, into Java. With his permission, we are pleased to make this Java code available to all licensed users of Numerical Recipes Third Edition Machine-Readable Code. The Java code is now included in all purchased downloads (https://xnumrec.merchantquest.net/cart/Step1.php) of both the C++ and All Languages machine readable code products. Users who have previously purchased these products may contact us (http://www.nr.com/contactform.php)to obtain the Java code. (Please include your previous order number, or other identifying information.)
Note that the Java translations are not a supported product of Numerical Recipes Software, but are provided only as a convenience to our users. Also, because these are contributed by a user, but based on Numerical Recipes code, their use is governed by the license and disclaimer of warranty here (http://www.nr.com/aboutJava.html).
Please use this forum to post questions about using NR3 in Java. Even better, please answer other user's questions, or post tutorials here. Numerical Recipes in Java is, for now, strictly user-supported!
Hi
Please let me know if any of the users have made
a jar file for the whole lot of
the codes of C++ injava so as to use as it is for review or suggestion. If yes is it free of cost.
Please help?
Have a niceday!
Cheers
Kutta(C.R.Muthukumar)
kutta
11-09-2013, 06:18 AM
Hi
Please let me know if any of the users have made
a jar file for the whole lot of
the codes of C++ injava so as to use as it is for review or suggestion. If yes is it free of cost.
Please help?
Have a niceday!
Cheers
Kutta(C.R.Muthukumar)
Hello colleagues and authors
In continuation of the above i request one and all
atleast to correct my java code for the the folllowing program that is used in linear programming under operation research on maximisation The program is cmpiled and found errorfree,but does give no result
Thanks
As
Kutta(C.R.Muthukumar)
import java.io.*;
import java.text.*;
import java.awt.*;
import java.awt.event.ActionEvent.*;
import java.awt.AWTEvent;
import java.awt.event.WindowEvent;
import java.lang.Math.*;
import java.lang.Character;
import java.lang.Float;
import java.lang.Integer;
import java.lang.Object;
import java.io.*;
import java.util.*;
import javax.swing.*;
import java.lang.Boolean;
public class Linear2 extends Frame{
static final int M=15;
static final int N=30;
static double PENALTY =-900.00;
static final double EPS=0.00001;
static int j;
static int i;
static int kc;
static int kr;
static Frame lea=new Frame();
static char res_in_file;
static char yn;
static Linear2 lp;
static int mm;
static int k;
static int m;
static int n;
static float top[];
static float base[];
static float left[];
static char sign[];
static int basic[];
int rows,cols;
static float x[][];
static int kf;
float keyNo;
static float obj;
public static void readData() throws Exception{
String s1;
int i,j;
i=0;
j=0;
DecimalFormat df = new DecimalFormat("0.000000");
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
s1=br.readLine();
System.out.println("\n Read data from keyboard or(1) or file (2)" );
s1=br.readLine();
System.in.read();
if(kf == 1)
{
System.out.println("\n Store data in discfile yn" );
s1=br.readLine();
System.in.read();
if(yn == 'y' || yn == 'Y')
{
System.out.println("\n Enter file to store data:myFile1 " );
s1=br.readLine();
System.in.read();
lp.writeFile();
}
else
if(yn == 'y' || yn == 'Y')
{
System.out.println("\n Enter file name to read data myFile1: " );
s1=br.readLine();
System.in.read();
lp.readFile();
}
}
System.out.print("\n Do you want to maximise(1) or minimize(2) mm: (1 or 2):");
s1=br.readLine();
System.in.read();
System.out.print("\n Enter no of variables k: " );
s1=br.readLine();
System.in.read();
System.out.print("\n Enter no of Constraints m: ");
s1=br.readLine();
System.in.read();
System.out.print("\n Enter > for >= and < for <=" );
s1=br.readLine();
System.in.read();
System.out.print("\n Enter the ObjectiveFunctions: obj " );
s1=br.readLine();
System.in.read();
System.out.println("\n Enter whether output required infile y/n : " );
s1=br.readLine();
System.in.read();
System.out.println("\n Enter file name to write result::res_in_file: myFile2:" );
s1=br.readLine();
System.in.read();
System.out.println("\n Enter file name to write result() myFile3:" );
s1=br.readLine();
System.in.read();
if(res_in_file == 'y')
{
lp.resultFile();
for(j=0;j<k;j++)
{
if(mm==2)
top[j]=0-top[j];
System.out.print("Enter top[]:");
}
System.out.print("Enter the constraints: ");
System.out.print("Enter > for > and <: ");
{
for(j=0;j<k;j++)
System.out.print(x[i][j]);
System.out.print(sign[i]);
System.out.print(left[i]);
}
}
}
public static void addSlack()
{
int i;
for(i=0;i<m;i++)
{
if(sign[i] == '<'|| sign[i] == '>')
{
n++;
for(int i1=0;i1<m;i1++)
x[i1][n-1]=0;
if(sign[i] == '<')
{
x[i][n-1]=1;
top[n-1]=0;
basic[i]=n-1;
}
else
{
x[i][n-1]=-1;
top[n-1]=0;
}
}
}
}
public static void addArti()
{
int i;
for(i=0;i<n;i++)
if (sign[i]== '='||sign[i]=='>')
{
n++;
for(int i1=0;i1<m;i1++)
x[i][n-1]=0;
x[i][n-1]=-900;
basic[i]=n-1;
}
}
public static void findbase()
{
int i,j;
for(j=0;j<n;j++)
{
base[j]=0;
for(i=0;i<m;i++)
base[j] += x[i][j]*top[basic[i]];
base[j]=base[j]-top[j];
}
}
public static int keyRow(int kc)
{
int i;
float ratio;
float min =100000;
int l=1;
for(i=0;i<m;i++)
{
if(x[i][k] < EPS && left[i] > EPS)
continue;
else
ratio = left[i]/x[i][k];
if(ratio < -EPS)
continue;
else
if(left[i]/x[i][k] < min)
{
min = left[i]/x[i][k];
l=i;
};
}
if (l == -1)
{
System.out.print("\n UnboundedSolution");
}
return(i);
}
public static int keyColumn()
{
int j;
float min = 0;
int l = -1;
for(j=0;j<n;j++)
{
if(base[j] < min)
{
min = base[j];
l =j;
}
}
return(i);
}
public static void display()
{
int i,j;
for(j=0;j<n;j++)
System.out.print("\n top[j]" + " " );
for(i=0;i<m;i++)
{
System.out.print("\n top[basic[i].basic[i]]+1"+ "\nleft[i]"+" "+" ");
for(j=0;j<n;j++)
System.out.print("\n x[i][j] "+ " ");
for(j=0;j<n;j++)
System.out.print("\n basic[j]"+" ");
}
}
public static void modify(int kr,int kc) throws Exception
{
// final int M=15;
int i;
int j;
float keyNo;
float temp[] = new float[M];
for(i=0;i<m;i++)
temp[i] = x[i][kr];
basic[kr]=kc;
keyNo=x[kr][kc];
for(j=0;j<n;j++)
x[kr][j]=x[kr][j]/keyNo;
left[kr]=left[kr]/keyNo;
for(i=0;i<m;i++)
{
if(i== kr)
continue;
left[i]=left[i]-left[kr]*top[i];
}
for(i=0;i<m;i++)
{
if(i==kr)
continue;
else
for(j=0;j<n;j++)
x[i][j]=x[i][j]-temp[i]*x[kr][j];
}
//float obj=0;
for (i=0;i<m;i++)
obj=obj+top[basic[i]]*left[i];
return;
}
public void result()throws Exception
{
int i;
int feasible =1;
{
RandomAccessFile raf3 = new RandomAccessFile("myFile1.txt","rw");
raf3.seek(raf3.length());
for(i=0;i<m;i++)
if(top[basic[i]] <(PENALTY +0.01)&& left[i]> EPS)
{
if(true);
break;
}
if(false)
{
for(i=0;i<m;i++)
System.out.print("\n basic[i]+1,left[i]"+" " +" ");
System.out.print("\nOptialValue of Objective function " +obj);
if(res_in_file == 'y')
{
raf3.writeInt(i);
for(i=0;i<m;i++){
obj=basic[i]+1;
obj=left[i];
}
}
else{
System.out.print("Solution is unfeasible");
if(res_in_file== 'y')
raf3.writeUTF("Solution not feasible");
}
}
}
}
public static void readFile() throws Exception
{
int i,j;
try
{
RandomAccessFile raf2= new RandomAccessFile("myFile1.txt","rw");
raf2.getFilePointer();
raf2.length();
while(raf2.getFilePointer()<raf2.length())
{
int mm=raf2.readInt();
k=raf2.readInt();
m=raf2.readInt();
n=k;
for(j=0;j<k;j++)
{
top[j]= j;
if(mm==2)
top[j]=0-top[j];
}
for(i=0;i<m;i++)
{
for(j=0;j<k;j++)
top[j] =raf2.readInt();
do{
sign[i]=raf2.readChar();
left[i]=raf2.readInt();
}while(true);
}
}
}
catch(Exception e)
{
}
}
public static void writeFile()
{
int i,j;
try
{
RandomAccessFile raf1= new RandomAccessFile("myFile1.txt","rw");
raf1.seek(raf1.length());
while(raf1.getFilePointer()<raf1.length()){
raf1.writeInt(mm);
raf1.writeInt(k);
raf1.writeInt(m);
n=k;
if(mm==1){
for(j=0;j<k;j++)
top[j]=j;
for (j=0;j<top.length;j++)
{
System.out.write(j);
}
}
else
for(i=0;i<m;i++)
for (j=0;j<k;j++)
x[i][j]=i;
for(i=0;i<x.length;i++){
System.out.write(i);
}
for(i=0;i<sign.length;i++)
{
System.out.write(i);
}
left[i]=i;
for(i=0;i<left.length;i++)
{
System.out.write(i);
}
}
raf1.close();
}
catch(Exception e)
{}
}
public static void resultFile()
{
int i,j;
try
{
RandomAccessFile raf2 = new RandomAccessFile("myFile1.txt","rw");
raf2.seek(raf2.length());
while(raf2.getFilePointer()<raf2.length())
for(j=0;j<n;j++)
top[j]=j;
for(j=0;j<top.length;j++){
System.out.write(j);
}
for(j=0;j<n;j++)
for(i=0;i<m;i++)
{
top[basic[i]] = i+1;
for(i=0;i<top.length;i++)
for(i=0;i<basic.length;i++)
{
System.out.write(i);
}
for(j=0;j<n;j++)
x[i][j]=i;
for(i=0;i<x.length;i++)
for(j=0;j<x.length;j++)
{
System.out.write(i);
}
for(j=0;j<n;j++)
//for(j=0;j<n;j++)
obj=j;
{
System.out.write(j);
}
for(j=0;j<n;j++)
base[j]=j;
{
System.out.write(j);
}
}
}
catch(Exception e)
{};
}
public static void main(String args[]) throws Exception
{
Linear2 lp = new Linear2();
try
{
lp.readData();
lp.addSlack();
lp.addArti();
lp.findbase();
lp.display();
if(res_in_file =='y')
lp.resultFile();
i=1;
for(;;)
{
if (kc == -1) break;
System.out.println("\n Liner2 Iteration :" + i);
{
System.out.println("\n *********************\n");
kc= lp.keyColumn();
System.out.println("kc" + kc);
keyRow(kc);
System.out.println("kr" + kr);
lp.modify( kc , kr );
lp.findbase();
lp.display();
if(res_in_file =='y')
lp.resultFile();
i++;
lp.result();
}
}
}catch(Exception e)
{
}
return;
}
}
kutta
12-14-2013, 04:50 AM
I downloaded, unzipped, did a cursory inspection, and wrote a couple of tests with Java_304.zip.
Tests were done on Centos Linux workstations, with the "standard" Java supplied in RedHat/Centos 5.8 distributions: OpenJDK version 1.6.0_22, as of this writing.
Discoveries so far:
An annoying "feature" of build.xml is that an "ant clean" erases the supplied text files INSTALL.TXT, LICENSE.TXT, and NAMES.txt. It's easy to fix it so that doesn't happen.
I just commented out a line in the <delete> section of the "clean" target:
<target name="clean">
<delete dir="build"/>
<delete dir="${junit.output.dir}"/>
<delete>
<fileset dir="." includes="*.ps"/>
<fileset dir="." includes="*.dat"/>
<!-- davekw7x: Don't delete txt files! -->
<!--<fileset dir="." includes="*.txt"/> -->
</delete>
</target>
In fact, I don't see the need to delete .ps or .dat files either, but I left the other stuff in place, since my goal is to get something useful (and a little less annoying) with minimal changes in the original.
A bug in build.xml causes "ant jar" to fail. It's easy to fix that also.
The problem is that with the "jar" target, it creates a directory named "build/jar" but then designates the destination file name being in directory "build/lib"
Here's the section that I changed:
<target depends="build" name="jar">
<!-- davekw7x make directory consistent with destfile -->
<!-- <mkdir dir="build/jar"/> -->
<mkdir dir="build/lib"/>
<jar destfile="build/lib/nr3.jar" basedir="build/classes">
</jar>
</target>
So far I haven't been able to get "ant AllTests" to work. I haven't used ant-junit before, so I may be doing something wrong.
Note that I have successfully completed a few tests with tutorial material from ant.apache.org, but nothing with the SuiteClasses stuff, so I'm really lost.
Anyhow...
I have attached a zipped up log file that shows what happened when I ran "ant -v AllTests 2>& 1 | tee ant_AllTests.log"
(This was after executing "ant jar" successfully.)
Bottom line:
[junit] Test com.nr.test.AllTests FAILED
Note that compilation of AllTests.java had successfully created AllTests.class.
I will show results of a test program or two in subsequent posts on a new thread.
Regards,
Dave
Hi
Well done but not at the same time favouring me
since i need J2SDK1_4_0-FORTE-4 to open your ant logfile and hence I request you to do the needfull.Alternatively you can try my javacoded progrm given below to make it executable.(in the same section)
I wolud be otherwise wishing you a great new new year but for your help
Thanks
As
C.R.Muthukumar(Kutta)
kutta
12-31-2013, 03:53 AM
Hi
Well done but not at the same time favouring me
since i need J2SDK1_4_0-FORTE-4 to open your ant logfile and hence I request you to do the needfull.Alternatively you can try my javacoded progrm given below to make it executable.(in the same section)
I wolud be otherwise wishing you a great new new year but for your help
Thanks
As
C.R.Muthukumar(Kutta)
Hi
I take this oppurtunity to wish every one(includes Authors as well) a very prosperous new year
for the help rendered mathematically via NR Forum
As
Kutta(C.R.Muthukumar)