python matrix multiplication
>>> a = np.array([[ 5, 1 ,3],
[ 1, 1 ,1],
[ 1, 2 ,1]])
>>> b = np.array([1, 2, 3])
>>> print a.dot(b)
array([16, 6, 8])
python matrix multiplication
>>> a = np.array([[ 5, 1 ,3],
[ 1, 1 ,1],
[ 1, 2 ,1]])
>>> b = np.array([1, 2, 3])
>>> print a.dot(b)
array([16, 6, 8])
matrix multiplication
import java.util.Arrays;
import java.util.Scanner;
public class Matrix {
private static int counter =0;
private static Scanner scanner = new Scanner(System.in);
private int rawForArr1 , columnforarr1Andrawsforarr2, columnForArr2 =0; //
public Matrix(){
}
public Matrix(int rawForArr1, int columnforarr1Andrawsforarr2, int columnForArr2) {
this.rawForArr1 = rawForArr1;
this.columnforarr1Andrawsforarr2 = columnforarr1Andrawsforarr2;
this.columnForArr2 = columnForArr2;
}
public void setRawsAndColumn(){
setRawsAndColumns();
}
private void setRawsAndColumns(){
System.out.println("Enter numbers of raws for the (LEFT ARRAY)");
int r1=scanner.nextInt();
this.rawForArr1 =r1;
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");
int colArr1RawArr2 = scanner.nextInt();
this.columnforarr1Andrawsforarr2 =colArr1RawArr2;
System.out.println("Enter numbers of columns for the (RIGHT ARRAY)");
int c2=scanner.nextInt();
this.columnForArr2 = c2;
double [][] firstArr = new double[r1][colArr1RawArr2];
double [][] secondArr = new double[colArr1RawArr2][c2];
enterElements(firstArr);
enterElements(secondArr);
double[][] multiplied =multiplieMatrix(firstArr,secondArr);
printValue(multiplied);
}
// Print the Matrix as a string of Array
public static void printValue(double[][] arr)
{
System.out.println(Arrays.deepToString(arr));
}
public static double [][] multiplieMatrix(double [][] leftArr,double[][] rightArr){
int m = leftArr.length;
int n = rightArr[0].length;
double [][] sumOfMultiplie = new double[m][n];
int lARawIndex =0,rACIndex=0,iterateOnLArrColAndRArrRaw=0;
double sum = 0 ;
for(lARawIndex =0 ; lARawIndex < leftArr.length ; ++lARawIndex)
{
for(rACIndex = 0 ; rACIndex < rightArr[0].length ; ++rACIndex)
{
for(iterateOnLArrColAndRArrRaw = 0 ; iterateOnLArrColAndRArrRaw < rightArr.length ; ++iterateOnLArrColAndRArrRaw)
{
sum+= (leftArr[lARawIndex][iterateOnLArrColAndRArrRaw]) * (rightArr[iterateOnLArrColAndRArrRaw][rACIndex]);
}
sumOfMultiplie[lARawIndex][rACIndex]=sum ;
sum=0;
}
}
return sumOfMultiplie ;
}
/* enter elements of the array*/
public static double[][] enterElements(double[][] arr)
{
counter++;
if(counter%2==0){
System.out.println("\t***(Enter the elements for the RIGHT array)***\n");
}
System.out.println("\t ***(Enter the elements for the LEFT array)***\n");
for(int i = 0 ; i < arr.length ; ++i) // numbers of raws
{
for(int j = 0 ; j<arr[0].length ; ++j) // numbers of columns
{
System.out.println("Enter element in raw #"+(i+1)+" column #"+(j+1));
arr[i][j] = scanner.nextDouble();
}
}
return arr ;
}
//
// public static double[][] recursiveMultiplicationMatrix(double [][] multilplies ){
// double[][] nextMatrix =initilizeMatrix();
// enterElements(nextMatrix);
// int m = nextMatrix.length;
// int n = multilplies[0].length;
// double [][] sumOfMultiplie = new double[m][n];
// int lARawIndex =0,rACIndex=0,iterateOnLArrColAndRArrRaw=0;
// int sum = 0 ;
// for(lARawIndex =0 ; lARawIndex < nextMatrix.length ; ++lARawIndex)
// {
// for(rACIndex = 0 ; rACIndex < multilplies[0].length ; ++rACIndex)
// {
// for(iterateOnLArrColAndRArrRaw = 0 ; iterateOnLArrColAndRArrRaw < multilplies.length ; ++iterateOnLArrColAndRArrRaw)
// {
// sum+= (nextMatrix[lARawIndex][iterateOnLArrColAndRArrRaw]) * (multilplies[iterateOnLArrColAndRArrRaw][rACIndex]);
// }
// sumOfMultiplie[lARawIndex][rACIndex]=sum ;
// sum=0;
// }
// }
//
// String answer = answer();
// if (answer.equals("yes")) {
// return recursiveMultiplicationMatrix(sumOfMultiplie);
//
// }
// return sumOfMultiplie ;
//
// }
//
// public static String answer(){
// System.out.println("Do you want to continue to multiple? "+'\n'+"enter yes to continue or any other key to print the 2d matrix");
// String answer ;
// answer = scanner.nextLine();
// return answer;
// }
}
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us