【发布时间】:2017-05-04 11:11:16
【问题描述】:
我有一个矩阵运算加、减、乘的代码。该代码生成两个矩阵,其中包含用户声明的维度内的随机元素。我的问题是我必须为输出矩阵的每个元素创建一个线程。我尝试使用数组来存储线程,但它为“mul”返回错误,这是我用于实现运行的方法的变量。在 main 方法的类中当然有两个不同的类和 Thread 数组,下一个类包含我所有的矩阵运算算法,还包含为 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;
}
}
【问题讨论】:
-
抱歉这是我第一次使用这个
-
感谢您修复 Filotto 格式
-
mul 在该上下文中不存在
-
那么我该如何传递线程数组呢?对不起,如果这是一个愚蠢的问题,但这是我第一次并行编程
-
嗯,这是一般编程的基础,让您使用的变量实际存在(不是空/未声明)。顺便说一句,我不太了解您的代码,无法为您提供一些有用的帮助,请阅读@GhostCat 答案并可能重建一些代码;)
标签: java arrays multithreading object matrix-multiplication