matrix multiplication

Solutions on MaxInterview for matrix multiplication by the best coders in the world

showing results for - "matrix multiplication"
Andrea
25 Mar 2018
1>>> a = np.array([[ 5, 1 ,3], 
2                  [ 1, 1 ,1], 
3                  [ 1, 2 ,1]])
4>>> b = np.array([1, 2, 3])
5>>> print a.dot(b)
6array([16, 6, 8])
Isabell
22 Aug 2019
1import java.util.Arrays;
2import java.util.Scanner;
3public class Matrix {
4
5    private static int counter =0;
6    private static Scanner scanner = new Scanner(System.in);
7    private int rawForArr1 , columnforarr1Andrawsforarr2, columnForArr2 =0; //
8
9    public Matrix(){
10    }
11
12
13    public Matrix(int rawForArr1, int columnforarr1Andrawsforarr2, int columnForArr2) {
14        this.rawForArr1 = rawForArr1;
15        this.columnforarr1Andrawsforarr2 = columnforarr1Andrawsforarr2;
16        this.columnForArr2 = columnForArr2;
17    }
18
19    public void setRawsAndColumn(){
20        setRawsAndColumns();
21    }
22
23    private void setRawsAndColumns(){
24        System.out.println("Enter numbers of raws for the (LEFT ARRAY)");
25        int r1=scanner.nextInt();
26        this.rawForArr1 =r1;
27        System.out.println("\nEnter numbers of columns for the (LEFT ARRAY).\nThis value automatically will be assigned for the raws in the right matrix ensuring 'Valid Multiplication'\n");
28        int colArr1RawArr2 = scanner.nextInt();
29        this.columnforarr1Andrawsforarr2 =colArr1RawArr2;
30        System.out.println("Enter numbers of columns for the (RIGHT ARRAY)");
31        int c2=scanner.nextInt();
32        this.columnForArr2 = c2;
33        double [][] firstArr = new double[r1][colArr1RawArr2];
34        double [][] secondArr = new double[colArr1RawArr2][c2];
35        enterElements(firstArr);
36        enterElements(secondArr);
37        double[][] multiplied =multiplieMatrix(firstArr,secondArr);
38        printValue(multiplied);
39    }
40
41
42    // Print the Matrix as a string of Array
43    public static void printValue(double[][] arr)
44    {
45                System.out.println(Arrays.deepToString(arr));
46    }
47
48    public static double [][] multiplieMatrix(double [][] leftArr,double[][] rightArr){
49        int m = leftArr.length;
50        int n = rightArr[0].length;
51        double [][] sumOfMultiplie = new double[m][n];
52        int lARawIndex =0,rACIndex=0,iterateOnLArrColAndRArrRaw=0;
53        double sum = 0 ;
54        for(lARawIndex =0 ; lARawIndex < leftArr.length ; ++lARawIndex)
55        {
56            for(rACIndex = 0 ; rACIndex < rightArr[0].length ; ++rACIndex)
57            {
58                for(iterateOnLArrColAndRArrRaw = 0 ; iterateOnLArrColAndRArrRaw < rightArr.length ; ++iterateOnLArrColAndRArrRaw)
59                {
60                    sum+= (leftArr[lARawIndex][iterateOnLArrColAndRArrRaw]) * (rightArr[iterateOnLArrColAndRArrRaw][rACIndex]);
61                }
62                sumOfMultiplie[lARawIndex][rACIndex]=sum ;
63                sum=0;
64            }
65        }
66        return sumOfMultiplie ;
67    }
68    /* enter elements of the array*/
69    public static double[][] enterElements(double[][] arr)
70    {
71        counter++;
72        if(counter%2==0){
73            System.out.println("\t***(Enter the elements for the RIGHT array)***\n");
74        }
75        System.out.println("\t ***(Enter the elements for the LEFT array)***\n");
76        for(int i = 0 ; i < arr.length ; ++i) // numbers of raws
77        {
78            for(int j = 0 ; j<arr[0].length ; ++j) // numbers of columns
79            {
80                System.out.println("Enter element in raw #"+(i+1)+" column #"+(j+1));
81                arr[i][j] = scanner.nextDouble();
82            }
83        }
84        return arr ;
85
86    }
87
88//
89//    public static  double[][] recursiveMultiplicationMatrix(double [][] multilplies ){
90//        double[][] nextMatrix =initilizeMatrix();
91//        enterElements(nextMatrix);
92//        int m = nextMatrix.length;
93//        int n = multilplies[0].length;
94//        double [][] sumOfMultiplie = new double[m][n];
95//        int lARawIndex =0,rACIndex=0,iterateOnLArrColAndRArrRaw=0;
96//        int sum = 0 ;
97//        for(lARawIndex =0 ; lARawIndex < nextMatrix.length ; ++lARawIndex)
98//        {
99//            for(rACIndex = 0 ; rACIndex < multilplies[0].length ; ++rACIndex)
100//            {
101//                for(iterateOnLArrColAndRArrRaw = 0 ; iterateOnLArrColAndRArrRaw < multilplies.length ; ++iterateOnLArrColAndRArrRaw)
102//                {
103//                    sum+= (nextMatrix[lARawIndex][iterateOnLArrColAndRArrRaw]) * (multilplies[iterateOnLArrColAndRArrRaw][rACIndex]);
104//                }
105//                sumOfMultiplie[lARawIndex][rACIndex]=sum ;
106//                sum=0;
107//            }
108//        }
109//
110//            String answer = answer();
111//            if (answer.equals("yes")) {
112//                return recursiveMultiplicationMatrix(sumOfMultiplie);
113//
114//            }
115//        return sumOfMultiplie ;
116//
117//    }
118//
119//    public static String answer(){
120//        System.out.println("Do you want to continue to multiple? "+'\n'+"enter yes to continue or any other key to print the 2d matrix");
121//        String answer ;
122//        answer = scanner.nextLine();
123//        return answer;
124//    }
125
126
127
128}
queries leading to this page
matrix multiplication python numpymatrix multiplication program in python matrix multiplicationmultiplication in matrixhow to multiply 2 matrices in python using numpymultiply to matrices in pythonpython multiply matrix how does matrix multiplication workmatrix multiplication examplenumpy multiply matrixmatrix multi as np array for pythonmultiply matrices in pythonmultiplication of matrix in pythonnumpy multiply two arraysnumpy array multiplicationmatrix multiplication codematrix multiplicationgnumpy multiply matrixes 2cmatrix multiplicationhow to matrix multiply in numpyuse of matrix multiplicationmatrix multiplication programhow to do matrix multiplication in numpypython program for matrix multiplicationhow to multiply to matrix in pythonmatrix multiplication onwrite a python program to perform matrix multiplication multiplication of matricesmatrix multiplication with numpynumpy matrix multiplication codemultiply matrix in pythonto write a python program to perform matrix multiplication numpy matrix vector multiplicationmatrix product in numpymatrix product numpypython matrix multipicationmatrix vector product numpystrassen 27s matrix multiplication pymatrix multiplication python 40matrix multiplication pythonwhat is matrix matrix multiplicationmultiply matrix pythonwrite a python program to perform matrix multiplicationmatriz multiplication in pythonmatrix multiplication program pythonhow to matrix multiplication in nupymatrix multiplcation numpyarray multiplication in mumpyhow to do matrix multiplication numpymatrix mulptilication numywhat is matrix multiplicationnumpy python matrix multiplicationhow to multiply 2 arrays in python numpymultiplication matrixmatrix multiplication in python numpymatrix multipication numpymatrix multiplication in pythonhow to multiply array with matrix in pythonmatrix matrix multiplicationdefinition of matrix multiplicationnumpy multiply n no of matrixnumber of multiplication in matrixmatrix matrix multiplicationhow to do matrix multiplicationthree matrix multiplication pythonmultiply matrices pythonhow to multiply matrix in pythonpython matrix multiplication librarynumpy matrix multiplicationprogram for matrix multiplication in pythonmultiplication of two matrices in pythonpython numpy matrix multiplicationmatrix multiplication numpypython matrix multiplicationmatrix multiplication using pythonwhat does matrix multiplication representuse numpy for matrix multiplicationmultiplication of matrix numpymultiplication of matrixmatrix multiplication of two numpy arrayspython matrix multiplication numpymatrices multiplicationmatrix multiplication tablematrix multiplicationhow to multiply matrices pythonmatrix multiplication in numpypythonmatrix multiplicationwrite a program in python to multiplication of matrices python matrix multiplication with numpymatrix multiplication explanationnumpy matrix multiplication pythonmatrix multiplication definitionmatrix multiplicationsmatrick multiplication pythonmatrix multiplication in 5c pythonmatrix multiplication