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])
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}