Tim Brown
Tim Brown

Reputation: 23

Thread Array Java Matrix

I have a code of matrix operations add, subtract, multiply. The code generates two matrices with random elements within the dimension the user states. My questions is that I have to create a thread for every element of the output Matrix. I tried using an array to store the threads, but it returns an error for "mul" which is the variable I used for the method that implements run. There are two different classes with the Thread array in the class of the main method of course and the next class contains all my Matrix operation algorithms and also contains the class "MatrixThreads" which implements Runnable for the run method. If anyone can take a look and see if they can help me, I would appreciate it tremendously.

The class 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");
    }
}

The Matrix class

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;
    }
}

Upvotes: 0

Views: 446

Answers (1)

GhostCat
GhostCat

Reputation: 140427

First of all, your question is very unclear; but I think I can give enough of an answer to get you going. And maybe you can clarify; we see further from there. So ...

Unclear: what do you mean by "one thread per output" matrix. There is simply no point in doing that. Probably you want to run different operations on several matrixes in parallel; and you should use threads for that.

Now, to help you: your over-simple model makes your whole design so overly complex that solving your problem looks much more complicated than it ought to be.

What I mean by that: you are mixing up responsibilities: you see, a Matrix should be about the Matrix data; and maybe about the mathematical operations that one can apply to a Matrix. Thus: leave the whole "thread business" out of that. Write code that allows you to create two Matrices; to then run operations such as add(), multiple(), etc. on them.

And then, when that all works; the code that instantiates those Matrix objects, that code could use multiple threads to do things like A+B, A*C, A-D in parallel.

Upvotes: 1

Related Questions