gpt4 book ai didi

java - 线程数组 Java 矩阵

转载 作者:行者123 更新时间:2023-12-01 09:06:34 27 4
gpt4 key购买 nike

我有一个矩阵运算加、减、乘的代码。该代码生成两个矩阵,其中包含用户指定维度内的随机元素。我的问题是我必须为输出矩阵的每个元素创建一个线程。我尝试使用数组来存储线程,但它返回“mul”错误,这是我用于实现 run 的方法的变量。当然,主方法的类中有两个带有 Thread 数组的不同类,下一个类包含我所有的 Matrix 运算算法,还包含为 run 方法实现 Runnable 的类“MatrixThreads”。如果有人可以看一下并看看是否可以帮助我,我将非常感激。

MatrixOperations 类

public class MatrixOperations{
public static void main(String args[]) throws InterruptedException{
int row1,col1,row2,col2;

Scanner sc = new Scanner(System.in);

System.out.print("\n\n Input Matrix 1 dimensions (ROWS space COLUMNS):");
row1= sc.nextInt();
col1 = sc.nextInt();

System.out.print("\n\n Input Matrix 2 dimensions (ROWS space COlUMNS):");
row2= sc.nextInt();
col2 = sc.nextInt();

int operation;

System.out.print("\n\n Select the operation to executed: 1. Add 2. Subtract 3. Multiply \n > ");
operation = sc.nextInt();

Matrix result;
Matrix m1 = new Matrix(row1, col1);
Matrix m2 = new Matrix(row2, col2);
int m3 = col1*row2;


Thread myThreads[]= new Thread[m3];
for(int i=0; i<m3;i++){
myThreads[i] = new Thread(new MatrixOperations(mul));//here is the error
myThreads[i].start();
}
for (int i=0; i<m3;i++){
myThreads[i].join();

}
switch(operation){
case 1:
result = m1.add(m2);
System.out.println("\n\n First Matrix: \n " + m1.getPrintableMatrix());
System.out.println("\n\n Second Matrix: \n " + m2.getPrintableMatrix());
System.out.println("\n\n Resultant Matrix: \n " + result.getPrintableMatrix());

break;

case 2:
result = m1.subtract(m2);

System.out.println("\n\n First Matrix: \n " + m1.getPrintableMatrix());
System.out.println("\n\n Second Matrix: \n " + m2.getPrintableMatrix());
System.out.println("\n\n Resultant Matrix: \n " + result.getPrintableMatrix());


break;

case 3:

result = m1.dotProduct(m2);

System.out.println("\n\n First Matrix: \n " + m1.getPrintableMatrix());
System.out.println("\n\n Second Matrix: \n " + m2.getPrintableMatrix());
System.out.println("\n\n Resultant Matrix: \n " + result.getPrintableMatrix());

break;

default: System.out.println("\nInvalid operation......\n");break;
}
System.out.print("\n\n");
}
}

矩阵类

import java.util.Scanner;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Matrix {
private int row,column;
private double [][] matrixElements;

class ThreadMatrix implements Runnable{
private Matrix mul;

public ThreadMatrix(Matrix mul){
this.mul = mul;
}

@Override
public void run() {
mul.add(mul);
}
}

public Matrix (int rows, int columns){
this.row= rows;
this.column = columns;
matrixElements = new double[row][column];
populatematrix(-100,100);
}


public Matrix(double[][] matrixArray){
this.row = matrixArray.length;
this.column = (matrixArray[0]).length;
matrixElements = new double [row][column];
for (int i=0; i<row;i++){
for (int j=0; j<column;j++){
matrixElements[i][j] = matrixArray[i][j];
}
}
}

private void populatematrix(int min, int max){
Random randnum = new Random();
Random rand = new Random();

for (int i=0; i<row; i++){
for (int j= 0;j<row;j++){
matrixElements[i][j] = rand.nextInt((max - min) + 1) + min;
}
}
}
public Matrix add(Matrix otherMatrix){
double[][] resultMatrixArray = new double[this.row][this.column];
for (int i=0; i<row; i++){
for (int j=0; j<column; j++){
resultMatrixArray[i][j] = this.matrixElements[i][j] + otherMatrix.matrixElements[i][j];

}

}
return new Matrix(resultMatrixArray);
}

public Matrix subtract(Matrix otherMatrix){
double[][] resultMatrixArray = new double[row][column];

for (int i=0; i<row; i++){
for (int j=0; j<column; j++){
resultMatrixArray[i][j] = this.matrixElements[i][j] - otherMatrix.matrixElements[i][j];
}
}
return new Matrix(resultMatrixArray);

}

public Matrix dotProduct(Matrix otherMatrix){

double[][] resultMatrixArray = new double [row][column];

double sum = 0;

if (this.column !=otherMatrix.row)
System.out.println("\n\n Matrices Multiplication is not possible...Invalid Dimensions...\n\n");
else {
for (int c=0; c<this.row;c++){
for (int d = 0; d<otherMatrix.column;d++){
for (int k = 0; k<otherMatrix.row; k++){
sum = sum+((this.matrixElements[c][k])*(otherMatrix.matrixElements[k][d]));
}
resultMatrixArray[c][d]=sum;
sum = 0;
}
}
}
return new Matrix(resultMatrixArray);
}

public String getPrintableMatrix(){
String result ="";

for (double[] roww: matrixElements){
for (double j:roww){
result +=""+j + "";
}
result +="\n";
}
return result;
}
}

最佳答案

首先,你的问题非常不清楚;但我想我可以给出足够的答案来让你继续下去。也许你可以澄清一下;我们从那里看得更远。所以...

不清楚:“每个输出一个线程”矩阵是什么意思。这样做根本没有意义。也许您想在多个矩阵上并行运行不同操作;您应该为此使用线程。

现在,为了帮助您:您的模型过于简单使您的整个设计变得过于复杂,以至于解决您的问题看起来比应有的复杂得多。

我的意思是:你混淆了职责:你看,矩阵应该是关于矩阵数据的;也许还有关于可以应用于矩阵的数学运算。因此:将整个“线程业务”排除在外。编写允许您创建两个矩阵的代码;然后对其运行 add()、multiple() 等操作。

然后,当一切顺利时; 实例化这些 Matrix 对象的代码,该代码可以使用多个线程并行执行 A+B、A*C、A-D 等操作。

关于java - 线程数组 Java 矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41221935/

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