0

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;
    }
}
6
  • sorry it's my first time using this Commented Dec 19, 2016 at 11:52
  • thanks for fixing the format Filotto Commented Dec 19, 2016 at 12:19
  • mul doesn't exists in that context Commented Dec 19, 2016 at 12:28
  • so then how do I pass the thread array? sorry if it's a dumb question but this is my first time programming in parallel Commented Dec 19, 2016 at 12:31
  • Well this is a base from programming in general, make the variables you're using actually exsist (not null/undeclared). Btw I don't understand your code enough to give you some useful help, read @GhostCat answer and maye rebuild some code ;) Commented Dec 19, 2016 at 12:43

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Alright thank you for taking the time to respond, let me clarify. The project I was assigned is asking me to "make it so that every operation uses as many thread as there are elements in the output matrix." I understand that it's basically overkill, but it's what is required. So from my understanding if a user inputs a matrix with the dimensions 2x2 and another 2x3 matrix and adds them or multiplies them the resulting matrix will have 6 elements therefore it must have 6 threads. Each thread performs the operation for each element of the other two matrix not the resulting one
I don't know if that cleared it up, I hope I did, I can't seem to figure this out

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.