Question

In: Computer Science

How to write a method that performs matrix multiplication with three rectangle arrays along with their...

How to write a method that performs matrix multiplication with three rectangle arrays along with their dimensions as parameters using Java programming?

Solutions

Expert Solution

import java.util.*;
public class Main
{
static int[][] matrixMultiplication(int [][]a,int [][]b,int [][]c,int r1,int c1,int r2,int c2,int r3,int c3)
{
int [][]product1=new int[r1][c2];// product1 matrix with dimensions r1 X c2
int i,j,k;
if(r2!=c1)//if first matrix column and second matrix row doesn't match
{
System.out.println("First matrix and second matrix cannot be multiplied");
return null;
}
for (i=0;i<r1;i++) {
for (j=0;j<c2;j++) {
for (k=0;k<c1;k++) {
product1[i][j]+=a[i][k]*b[k][j];
}
}
}
int [][]product2=new int[r1][c3];//final matrix with dimensions r1 X c3
if(c2!=r3)
{
System.out.println("product1 matrix and third matrix cannot be multiplied");
return null;
}
for (i=0;i<r1;i++) {
for (j=0;j<c3;j++) {
for (k=0;k<c2;k++) {
product2[i][j]+=product1[i][k]*c[k][j];
}
}
}
return product2;

}
   public static void main(String[] args) {
       Scanner s=new Scanner(System.in);
       int r1,r2,r3,c1,c2,c3,i,j;
       r1=s.nextInt();
       c1=s.nextInt();
       r2=s.nextInt();
       c2=s.nextInt();
       r3=s.nextInt();
       c3=s.nextInt();
       int [][]a=new int[r1][c1];
       int [][]b=new int[r2][c2];
       int [][]c=new int[r3][c3];
       int [][]res=new int[r1][c3];
       System.out.println("enter numbers into matrix1");
       for(i=0;i<r1;i++)
       {
       for(j=0;j<c1;j++)
       {
       a[i][j]=s.nextInt();
       }
       }
       System.out.println("enter numbers into matrix2");
       for(i=0;i<r2;i++)
       {
       for(j=0;j<c2;j++)
       {
       b[i][j]=s.nextInt();
       }
       }
       System.out.println("enter numbers into matrix3");
       for(i=0;i<r3;i++)
       {
       for(j=0;j<c3;j++)
       {
       c[i][j]=s.nextInt();
       }
       }
       res=matrixMultiplication(a,b,c,r1,c1,r2,c2,r3,c3);
       for(i=0;i<r1;i++)
       {
       for(j=0;j<c3;j++)
       {
       System.out.print(res[i][j]+" ");
       }
       System.out.println();
       }
   }
}


Related Solutions

Matrix multiplication with arrays In this question you will verify that the numpy.ndarray matrix multiplication operator,...
Matrix multiplication with arrays In this question you will verify that the numpy.ndarray matrix multiplication operator, @, does a proper matrix multiplcation. To do this, first write a function, mat_mult(a1, a2) that creates a new 2d array of zeros, and using nested for loops, fill in the elements of the matrix multiplication, and return this new array. Then, write a second function, matrix_diff(b, c) that takes two arrays as arguments then generates and returns the following ratio: sqrt((∑(??,?−??,?)^2)/(∑(??,?+??,?)^2)) This function...
build a program which performs matrix multiplication on square matrices. use UNIX "time" to capture the...
build a program which performs matrix multiplication on square matrices. use UNIX "time" to capture the time it takes to run the program with different data sizes. Languages: Python Task: Create matrix multiplication Input: Size of square matrix.   Size should be    250, 500, 1000, 1500, 2000 Internals: Explicitly or implicitly allocate sufficient memory to hold three NxN floating point Matrices, using a random number generator -- populate two of the Matrices, Multiply the two matrices, putting the result into the...
1. Write an algorithm to calculate the Matrix multiplication (or write with pseudo code) 2. Write...
1. Write an algorithm to calculate the Matrix multiplication (or write with pseudo code) 2. Write an algorithm to calculate the recursive Matrix multiplication (or write with pseudo code) 3. Find the time complexity of your pseudo code and analyze the differences
Need to write a code using c# Strassen’s Algorithm for matrix multiplication.
Need to write a code using c# Strassen’s Algorithm for matrix multiplication.
To understand the value of counting loops: Write a java program that implements matrix multiplication using...
To understand the value of counting loops: Write a java program that implements matrix multiplication using counting loop constructs. Then write the same program using only logical loops—for example, while loops. Write 2 classes for each program Sample Result is shown below: Enter the number of rows of matrix A: 2 Enter the number of columns of matrix A: 3 Enter the number of columns of matrix B: 3 Enter the number of columns of matrix B: 4 Enter matrix...
To understand the value of counting loops: Write a java program that implements matrix multiplication using...
To understand the value of counting loops: Write a java program that implements matrix multiplication using counting loop constructs. Then write the same program using only logical loops—for example, while loops. Sample Result is shown below: Enter the number of rows of matrix A: 2 Enter the number of columns of matrix A: 3 Enter the number of columns of matrix B: 3 Enter the number of columns of matrix B: 4 Enter matrix A; 1 2 3 4 5...
Write a C++ arithmetic calculator program that performs four arithmetic operations namely addition, subtraction, multiplication and...
Write a C++ arithmetic calculator program that performs four arithmetic operations namely addition, subtraction, multiplication and division. This program prompts user to enter data in the following order: First data is the number, second data is an operator and the third data is the number. You are required to: Write a function for addition. Write a function for subtraction. Write a function for multiplication. Write a function for division. Write a main program which calls four functions based on the...
Use Java to: 1. Write a method with a Rectangle and a Point as parameters. Without...
Use Java to: 1. Write a method with a Rectangle and a Point as parameters. Without using methods from the Rectangle class, return true if the point is inside the rectangle and false otherwise. 2. Write a second method with the same functionality as Exercise 1. However, use a method from the Rectangle class this time. 3. Normally, the == operator cannot be used to compare two strings for equality. There are 2 main exceptions we talked about. The first...
Write a function such that given a number N, display the N*N multiplication matrix from 1...
Write a function such that given a number N, display the N*N multiplication matrix from 1 to N. Then, write a C++ program such that, it prints the multiplication matrices of numbers 1,4,7, and 10 using a loop concept. Check the sample output below, to see how the program should work. Make sure to have your output exactly the same as the below output.
Exercise 1. Rectangle, Circle and Square Write three Python classes named Rectangle constructed by a length...
Exercise 1. Rectangle, Circle and Square Write three Python classes named Rectangle constructed by a length and width, a Circle constructed by a radius and a Square constructed by a side length. Both classes should have the methods that compute: - The area - The diagonal - The perimeter Use as much abstraction as you can. At the end of the file, use those classes to calculate the perimeter of a circle with radius the half of the diagonal of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT