Java 点集合之间的快速插值
我用Java建立了一个太阳系模型。为了确定行星的位置,它做了大量的计算,给出了一个非常精确的值。然而,我经常对大致的位置感到满意,如果这能让它走得更快的话。因为我在模拟中使用它,速度很重要,因为行星的位置将被要求数百万次 目前,我试图缓存行星在其整个轨道上的位置,然后反复使用这些坐标。如果请求两个值之间的位置,我将执行线性插值。这就是我存储值的方式:Java 点集合之间的快速插值,java,performance,Java,Performance,我用Java建立了一个太阳系模型。为了确定行星的位置,它做了大量的计算,给出了一个非常精确的值。然而,我经常对大致的位置感到满意,如果这能让它走得更快的话。因为我在模拟中使用它,速度很重要,因为行星的位置将被要求数百万次 目前,我试图缓存行星在其整个轨道上的位置,然后反复使用这些坐标。如果请求两个值之间的位置,我将执行线性插值。这就是我存储值的方式: for(int t=0; t<tp; t++) { listCoordinates[t]=super.coordinates(ti+
for(int t=0; t<tp; t++) {
listCoordinates[t]=super.coordinates(ti+t);
}
interpolator = new PlanetOrbit(listCoordinates,tp);
您可以将
FastMath
视为Math
,但速度更快。但是,与每次计算精确值相比,此代码并没有多大的速度提升。你对如何加快速度有什么想法吗?我可以看到一些问题,我可以看到的主要问题如下
public double[] getInterpolatedCoordinates(double julian){ //julian calendar? This variable name needs to be something else, like day, or time, or whatever it actually means
int startIndex=(int)julian;
int endIndex=(startIndex+1>=coordinates.length?1:startIndex+1); //wrap around
double nonIntegerPortion=julian-startIndex;
double[] start = coordinates[startIndex];
double[] end = coordinates[endIndex];
double[] returnPosition= new double[3];
for(int i=0;i< start.length;i++){
returnPosition[i]=start[i]*(1-nonIntegerPortion)+end[i]*nonIntegerPortion;
}
return returnPosition;
}
似乎实际上改变了变量planetrobit#坐标
中的值。由于这种方法应该只进行插值,我预计每次运行轨道时,轨道实际上都会略微损坏(因为它是一种线性插值,轨道实际上会向其中心降级)李>坐标
- 同样的事情你做了几次,最明显的是
在代码中分别出现了3次T-FastMath。floor(T)
- 这不是效率或准确性的问题,但变量和方法名称非常不透明,请使用实词作为变量名称
public double[] getInterpolatedCoordinates(double julian){ //julian calendar? This variable name needs to be something else, like day, or time, or whatever it actually means
int startIndex=(int)julian;
int endIndex=(startIndex+1>=coordinates.length?1:startIndex+1); //wrap around
double nonIntegerPortion=julian-startIndex;
double[] start = coordinates[startIndex];
double[] end = coordinates[endIndex];
double[] returnPosition= new double[3];
for(int i=0;i< start.length;i++){
returnPosition[i]=start[i]*(1-nonIntegerPortion)+end[i]*nonIntegerPortion;
}
return returnPosition;
}
我的理解是否正确,方法
坐标
实际上改变了字段坐标
中的值?@richardingle在第一段代码坐标
中为每个t
返回一个三个值的数组,这些数组存储在列表坐标
中<代码>列表坐标提供给存储它的PlanetRobit
。从那时起,它从未改变过,但我用这个列表获得了这个职位PlanetOrbit。坐标是我将在模拟过程中调用以确定行星位置的方法。好的,我强烈建议不要调用所有坐标。我指的是行星坐标法。在该方法中f=坐标[楼层]代码>然后double[]retval=f
则retval
被修改为however;我能看到的一个优化是,你可以三次落地谢谢,这个答案很好,非常有用。我喜欢store
的技巧,我以前从未见过。
public double[] getInterpolatedCoordinates(double julian, double[] store){
int startIndex=(int)julian;
int endIndex=(startIndex+1>=coordinates.length?1:startIndex+1); //wrap around
double nonIntegerPortion=julian-startIndex;
double[] start = coordinates[startIndex];
double[] end = coordinates[endIndex];
double[] returnPosition= store;
for(int i=0;i< start.length;i++){
returnPosition[i]=start[i]*(1-nonIntegerPortion)+end[i]*nonIntegerPortion;
}
return returnPosition; //store is returned
}