[英]Working with type double floating-point arrays in Java
As a relatively new Java programmer, I anticipate working with type double
floating-point arrays quite a bit. 作为一个相对较新的Java程序员,我期望使用类型double
浮点数组有很多工作。 Say I have four double
fp arrays declared as: 说我有四个声明为的double
fp数组:
double[] x = new double[i_max+1];
double[] v = new double[i_max+1];
double[] a = new double[i_max+1];
double[] time = new double[i_max+1];
Now, I would like to populate these while passing them in and out of a method. 现在,我想在将它们传入和传出方法时填充它们。 My understanding is that I need to define a class that houses these arrays: 我的理解是,我需要定义一个包含这些数组的类:
public class kinematics {
private double[] x;
private double[] v;
private double[] a;
private double[] time;
}
The call of my method looks like this: 我的方法的调用如下所示:
VERLET(i, x, v, a, time, const0, gamma, A_o, dt);
Here's the beginning of my method: 这是我方法的开始:
public static double VERLET(int i, double[] x, double[] v, double[] a, double[] time, double const0, double gamma, double A_o, double dt) {
And finally my return statement: 最后是我的退货声明:
return kinematics(x[i], v[i], a[i], time[i]);
Of which the compilar complains: 其中同胞抱怨:
driven.java:193: error: cannot find symbol
return kinematics(x[i], v[i], a[i], time[i]);
^
symbol: method kinematics(double,double,double,double)
location: class driven
The way I interpret this is that my method does not know my class exists. 我的解释方式是我的方法不知道我的类存在。 But I've declared it as public
, so I'm not exactly sure what this means. 但是我已将其声明为public
,所以我不确定这意味着什么。 Can anyone see what I'm doing wrong? 谁能看到我在做什么错?
Aside from this, it seems like I'm declaring double
quite a bit over and over again. 除此之外,似乎我一次又一次地声明了double
。 I'm sure this is due to inexperience. 我敢肯定这是由于经验不足。 Is there a more elegant way of working with type double
fp arrays than I have done? 有没有比我做过的更优雅的使用类型double
fp数组的方法? Thanks in advance. 提前致谢。
You need to return a new instance of kinematics. 您需要返回一个新的运动学实例。 ie 即
return new kinematics(x[i], v[i], a[i], time[i]);
At the moment you are trying to call a method called kinematics, not create a new object of that type. 目前,您正在尝试调用一种称为运动学的方法,而不是创建该类型的新对象。
To follow Java style guidelines you should start class names with a capital (ie Kinematics
) 要遵循Java样式准则,您应该以大写字母开头类名(即Kinematics
)
Also you will need to create a constructor for the Kinematics
class that takes the four parameters and assigns them. 同样,您将需要为Kinematics
类创建一个构造函数,该构造函数接受四个参数并为其分配参数。 Most IDEs will generate that automatically for you if you ask them to. 如果您要求,大多数IDE会自动为您生成该代码。
Seems to me that your Kinematics
Object will hold single double values for each parameter instead of arrays. 在我看来,您的Kinematics
对象将为每个参数而不是数组保留单个double值。 (Inferring this from what you are actually trying to return from your VERLET
function) (从您实际尝试从VERLET
函数返回的内容中VERLET
出这VERLET
)
Here is a template to get you started 这是一个入门的模板
public class Kinematics {
private double x;
private double v;
private double a;
private double time;
public Kinematics(double x, double v, double a, double time) {
this.x = x;
this.v = v;
this.a = a;
this.time = time;
}
public static Kinematics VERLET(int i, double[] x, double[] v, double[] a,double[] time, double const0, double gamma, double A_o, double dt) {
//do whatever functionality it has to be done
//assuming i is defined and is the ith element of the arrays
return new Kinematics(x[i], v[i], a[i], time[i]);
}
}
And this will a simple main
entry point 这将是一个简单的main
切入点
public static void main(String[] args) {
//Assuming i_max is already defined
double[] x = new double[i_max+1];
double[] v = new double[i_max+1];
double[] a = new double[i_max+1];
double[] time = new double[i_max+1];
//Assuming i, const0, gamma, A_o and dt are already defined
Kinematics kinOne = Kinematics.VERLET(i, x, v, a, time, const0, gamma, A_o, dt);
}
Working version of the code with Fortran comments: 具有Fortran注释的代码的工作版本:
import java.lang.Math.*;
import java.io.*;
import java.text.*;
//MODULE shared
//USE, INTRINSIC :: ISO_FORTRAN_ENV,dp=>REAL64!modern DOUBLE PRECISION
//INTEGER :: i
//INTEGER, PARAMETER :: i_max=5000
//REAL(dp) :: x_read,v_read,const0,gamma,A_o,dt
//REAL(dp), DIMENSION(:), ALLOCATABLE :: x,v,a,E,dE,time
//REAL(dp), PARAMETER :: m=1.0
//END MODULE shared
//PROGRAM nonlinear
public class driven {
public static void main(String args[]) {
//USE shared
//IMPLICIT NONE
//EXTERNAL VERLET
//ALLOCATE(x(i_max + 1))
//ALLOCATE(v(i_max + 1))
//ALLOCATE(a(i_max + 1))
//ALLOCATE(E(i_max + 1))
//ALLOCATE(dE(i_max + 1))
//ALLOCATE(time(i_max + 1))
int i;
final int i_max=5000;
double x_read,v_read,const0,const0_read,gamma,gamma_read,A_o,A_o_read,dt,dt_read;
final double m=1.0;
double[] x = new double[i_max+1];
double[] v = new double[i_max+1];
double[] a = new double[i_max+1];
double[] time = new double[i_max+1];
double[] f_osc = new double[i_max+1];
//PRINT *, ' '
//PRINT *, 'Initial position of the mass? [m]'
//PRINT *, ' '
//READ *, x_read
System.out.print("Initial position of the mass? [m] ");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
x_read = 0.0;
try {
x_read = Double.parseDouble(br.readLine());
} catch (IOException ioe) {
System.out.println(ioe);
System.exit(1);
}
//x(1) = x_read
x[0] = x_read;
//PRINT *, ' '
//PRINT *, 'Initial velocity of the mass? [m/sec]'
//PRINT *, ' '
//READ *, v_read
System.out.print("Initial velocity of the mass? [m/sec] ");
v_read = 0.0;
try {
v_read = Double.parseDouble(br.readLine());
} catch (IOException ioe) {
System.out.println(ioe);
System.exit(1);
}
//v(1) = v_read
v[0] = v_read;
//PRINT *, ' '
//PRINT *, 'Value of k/m? [1/sec^2]'
//PRINT *, ' '
//READ *, const0
System.out.print("Value of k/m? [1/sec^2] ");
const0_read = 0.0;
try {
const0_read = Double.parseDouble(br.readLine());
} catch (IOException ioe) {
System.out.println(ioe);
System.exit(1);
}
const0 = const0_read;
//PRINT *, ' '
//PRINT *, 'Value of the damping coefficient? [kg/sec]'
//PRINT *, ' '
//READ *, gamma
System.out.print("Value of the damping coefficient? [kg/sec] ");
gamma_read = 0.0;
try {
gamma_read = Double.parseDouble(br.readLine());
} catch (IOException ioe) {
System.out.println(ioe);
System.exit(1);
}
gamma = gamma_read;
//PRINT *, ' '
//PRINT *, 'Amplitude of the external force? [N]'
//PRINT *, ' '
//READ *, A_o
System.out.print("Amplitude of the external force? [N] ");
A_o_read = 0.0;
try {
A_o_read = Double.parseDouble(br.readLine());
} catch (IOException ioe) {
System.out.println(ioe);
System.exit(1);
}
A_o = A_o_read;
//PRINT *, ' '
//PRINT *, 'Time-step of the system? [sec]'
//PRINT *, ' '
//READ *, dt
//PRINT *, ' '
System.out.print("Time-step of the system? [sec] ");
dt_read = 0.0;
try {
dt_read = Double.parseDouble(br.readLine());
} catch (IOException ioe) {
System.out.println(ioe);
System.exit(1);
}
dt = dt_read;
//time(1) = 0.0
time[0] = 0.0;
//i = 1
i = 0;
//DO
do {
//IF(i > i_max) EXIT
//CALL VERLET
driven damped = new driven();
damped.VERLET(i, x, v, a, time, f_osc, const0, gamma, A_o, dt);
//i = i + 1
i = i + 1;
//END DO
} while (i < i_max);
//OPEN(7, file='xt.dat', status='unknown')
//WRITE(7,'(2f16.6)') (time(i),x(i),i=1,i_max)
//CLOSE(7)
//DEALLOCATE(x)
try {
PrintWriter writer = new PrintWriter("xt.txt", "UTF-8");
for (i = 0; i < i_max; i = i + 1) {
writer.printf("%1$16.6f%2$16.6f%n", time[i], x[i]);
}
writer.close();
} catch (IOException ioe) {
System.out.println(ioe);
}
//DEALLOCATE(v)
//DEALLOCATE(a)
//DEALLOCATE(E)
//DEALLOCATE(dE)
//DEALLOCATE(time)
}
//SUBROUTINE VERLET()
double VERLET(int i, double[] x, double[] v, double[] a, double[] time, double[] f_osc, double const0, double gamma, double A_o, double dt) {
//USE shared
//IMPLICIT NONE
//REAL(dp) :: x_temp,v_temp,t_temp,f_osc
//EXTERNAL FORCE,ENERGY
double x_temp,v_temp,t_temp;
final double m=1.0;
//x_temp = x(i)
x_temp = x[i];
//v_temp = v(i)
v_temp = v[i];
//t_temp = time(i)
t_temp = time[i];
//CALL FORCE(x_temp,v_temp,t_temp,f_osc)
FORCE(i, x_temp, v_temp, t_temp, const0, gamma, A_o, f_osc);
//x_temp = x(i)
//v_temp = v(i)
//CALL ENERGY!don't think I'm actually using this; no INTENT(OUT)
//a(i) = f_osc/m
a[i] = f_osc[i] / m;
//x(i + 1) = x(i) + v(i)*dt + 0.5*a(i)*dt*dt
x[i + 1] = x[i] + v[i] * dt + 0.5 * a[i] * dt * dt;
//x_temp = x(i + 1)
x_temp = x[i + 1];
//v_temp = v(i)
v_temp = v[i];
//time(i + 1) = time(i) + dt
time[i + 1] = time[i] + dt;
//t_temp = time(i + 1)
t_temp = time[i + 1];
//CALL FORCE(x_temp,v_temp,t_temp,f_osc)
FORCE(i, x_temp, v_temp, t_temp, const0, gamma, A_o, f_osc);
//a(i + 1) = f_osc/m
a[i + 1] = f_osc[i] / m;
//v(i + 1) = v(i) + 0.5*(a(i + 1) + a(i))*dt
v[i + 1] = v[i] + 0.5 * (a[i + 1] + a[i]) * dt;
//x_temp = x(i + 1)
//v_temp = v(i + 1)
//CALL ENERGY!don't think I'm actually using this; no INTENT(OUT)
//return x[i], v[i], a[i], time[i];
return x[i];
//END
}
//SUBROUTINE FORCE(xs,vs,ts,f_oscs)
double FORCE(int i, double x_temp, double v_temp, double t_temp, double const0, double gamma, double A_o, double[] f_osc) {
//USE shared
//IMPLICIT NONE
//REAL(dp), INTENT(IN) :: xs,vs,ts
//REAL(dp), INTENT(OUT) :: f_oscs
//REAL(dp) :: f_t
double f_t;
//f_t = A_o*DCOS(2.0*ts)
f_t = A_o * Math.cos(2.0 * t_temp);
//f_oscs = -const0*xs - gamma*vs + f_t
f_osc[i] = -const0 * x_temp - gamma * v_temp + f_t;
return f_osc[i];
//END
}
//SUBROUTINE ENERGY()
//USE shared
//IMPLICIT NONE
//!REAL(dp), INTENT(OUT) ::
//E(i) = 0.5*(v(i)**2 + const0*x(i)**2)
//E(1) = 0.5*(v(1)**2 + const0*x(1)**2)
//dE(i) = E(i) - E(1)
//END
//END PROGRAM nonlinear
}
The examples that helped me achieve this can be found here and here . 可以在这里和这里找到帮助我实现这一目标的示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.