gpt4 book ai didi

java - 实现雅可比算法来实现拉普拉斯方程

转载 作者:行者123 更新时间:2023-12-01 16:04:17 24 4
gpt4 key购买 nike

该算法遍历 2D NxN 数组,使每个元素为其周围 4 个相邻元素(左、右、上、下)的平均值。

NxN 数组最初全为 0,并被全为 1 的边距包围,如下所示下面的例子。 1永远不变,0一点点增加。

             1 1 1 1 1 1 1 1
1 0 0 0 0 0 0 1
1 0 0 0 0 0 0 1
1 0 0 0 0 0 0 1
1 0 0 0 0 0 0 1
1 0 0 0 0 0 0 1
1 0 0 0 0 0 0 1
1 1 1 1 1 1 1 1

我已经实现了以下代码,并且遇到了数组索引越界异常。请纠正我。

my code :
public class Main {
static int NO_OF_THREADS =8;

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Jacobi jacobi = new Jacobi(NO_OF_THREADS);
jacobi.initialize();
jacobi.create_threads();
}

}//end of Main class

public class Jacobi {
int ROWS=1000,COLS=1000;
private int i;
private int upper=100;//prevents buffer overflow
private int lower=99;//prevents buffer overflow
private int j;
private double[][] jacobi=new double[ROWS][COLS];
private int NO_OF_THREADS;


public Jacobi(int k)
{
NO_OF_THREADS=k;
}

public void initialize() {
for(i=1;i<=upper;i++)
{
for(j=1;j<=upper;j++)
{

if((i==1)||(i==upper)||(j==1)||(j==upper)){
jacobi[i][j]=1.0;
}

else
jacobi[i][j]=0.0;


}

}

}
public double[][] getJacobi()
{
return jacobi;
}

public void create_threads()
{
theThread[] threads=new theThread[NO_OF_THREADS];
for(int k=1;k<=NO_OF_THREADS;k++)
{
threads[k]=new theThread();
threads[k].start();
}
}
//Inner class of Jacobi

class theThread extends Thread {

@Override
public void run()
{
for(int q=2;q<=lower;q++)
{

System.out.println("The ID of this thread is: "+getName());
for(int j=2;j<=lower;j++)
{
synchronized(Jacobi.this)
{

jacobi[q][j]=(jacobi[q-1][j]+jacobi[q+1][j]+jacobi[q] [j-1]+jacobi[q][j+1])/4;
}//end of synchronized
}//end of inner for loop


}//end of for loop
}
}//end of theThread class
}//end of jacobi class

最佳答案

在行

int ROWS,COLS=1000;

我认为您想将其更改为

int ROWS=1000, COLS=1000;

否则 ROWS 设置不正确...

关于java - 实现雅可比算法来实现拉普拉斯方程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2965402/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com