Matrix Multiplication Write a method called multiply in the Matrix class that takes an instance of Matrix as a parameter and returns the result of matrix multiplication. Complete the definition of the class Matrix and method definition multiply shown here and in the starter code. public class Matrix { public Matrix multiply(Matrix other) { // fill in code here } } For purposes of this exercise, you should assume (without checking) that the matrices to be multiplied have the correct shapes, ie, that this.numCols() is equivalent to other.numRows(). To multiply matrices, the number of columns in the first matrix must equal the number of rows in the second matrix. In general, if we multiply an m x n matrix A (ie, having m rows and n columns) by a n x p matrix B (ie, having n rows and p columns), the product will have size m x p (ie, m rows and p columns). Here is a simple illustration of the computation for two 2 x 2 matrices:     Examples Example 1 If A and B were both instances of the Matrix class with the following data, respectively, 0.0 1.0 2.0 2.0 1.0 2.0 2.0 2.0 2.0 2.0 0.0 2.0 the product a.multiply(b) would be the Matrix containing the following data: 2.0 6.0 6.0 10.0   Example 2 If A and B were both instances of the Matrix class with the following data, respectively, 1.0 3.0 1.0 2.0 3.0 1.0 0.0 3.0 1.0 0.0 3.0 1.0 1.0 1.0 2.0 3.0 2.0 3.0 0.0 1.0 the product a.multiply(b) would be the Matrix containing the following data: 9.0 15.0 5.0 9.0 7.0 11.0 Matrix.java code is attached  import java.util.Random; public class Matrix { // Largest random value in Matrix will be MAX_VALUE-1 privatestaticfinalintMAX_VALUE = 3; privatedouble[][] matrix; // Constructor initializes an empty matrix publicMatrix(intnumRows, intnumCols) { matrix = newdouble[numRows][numCols]; } // Fill Matrix with random small integers for testing publicvoidfillMatrix() { Randomrand = newRandom(); for (intr = 0; r < numRows(); r++) { for (intc = 0; c < numCols(); c++) { matrix[r][c] = rand.nextInt(MAX_VALUE); } } } // Return number of rows in Matrix object publicintnumRows() { returnmatrix.length; } // Return number of columns in Matrix object publicintnumCols() { return (matrix.length == 0) ?0:matrix[0].length; } // Return String representation of Matrix object publicStringtoString() { Stringm = ""; for (intr = 0; r < numRows(); r++) { for (intc = 0; c < numCols(); c++) { m += matrix[r][c] + " "; } m += "\n"; } returnm; } // Get value at row r and column c publicdoublegetValue(intr, intc) { returnmatrix[r][c]; } // Set value at row r and column c publicvoidsetValue(intr, intc, doublevalue) { matrix[r][c] = value; } // Return product of this and other publicMatrixmultiply(Matrixother) { // TODO: complete method definition, replace this placeholder code Matrixproduct = newMatrix(0,0); returnproduct; } // main method to test Matrix class publicstaticvoidmain(String[] args) { Matrixa = newMatrix(2, 3); Matrixb = newMatrix(3, 2); a.fillMatrix(); b.fillMatrix(); Matrixproduct = a.multiply(b); System.out.println(a); System.out.println(b); System.out.println(product); } }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Matrix Multiplication

Write a method called multiply in the Matrix class that takes an instance of Matrix as a parameter and returns the result of matrix multiplication. Complete the definition of the class Matrix and method definition multiply shown here and in the starter code.

public class Matrix { public Matrix multiply(Matrix other) { // fill in code here } }

For purposes of this exercise, you should assume (without checking) that the matrices to be multiplied have the correct shapes, ie, that this.numCols() is equivalent to other.numRows().

To multiply matrices, the number of columns in the first matrix must equal the number of rows in the second matrix. In general, if we multiply an m x n matrix A (ie, having m rows and n columns) by a n x p matrix B (ie, having n rows and p columns), the product will have size m x p (ie, m rows and p columns).

Here is a simple illustration of the computation for two 2 x 2 matrices: 

  

Examples

Example 1

If A and B were both instances of the Matrix class with the following data, respectively,

0.0 1.0 2.0 2.0 1.0 2.0 2.0 2.0 2.0 2.0 0.0 2.0

the product a.multiply(b) would be the Matrix containing the following data:

2.0 6.0 6.0 10.0

 

Example 2

If A and B were both instances of the Matrix class with the following data, respectively,

1.0 3.0 1.0 2.0 3.0 1.0 0.0 3.0 1.0 0.0 3.0 1.0 1.0 1.0 2.0 3.0 2.0 3.0 0.0 1.0

the product a.multiply(b) would be the Matrix containing the following data:

9.0 15.0 5.0 9.0 7.0 11.0

Matrix.java code is attached 

import java.util.Random;

public class Matrix {
// Largest random value in Matrix will be MAX_VALUE-1
privatestaticfinalintMAX_VALUE = 3;

privatedouble[][] matrix;

// Constructor initializes an empty matrix
publicMatrix(intnumRows, intnumCols) {
matrix = newdouble[numRows][numCols];
}

// Fill Matrix with random small integers for testing
publicvoidfillMatrix() {
Randomrand = newRandom();
for (intr = 0; r < numRows(); r++) {
for (intc = 0; c < numCols(); c++) {
matrix[r][c] = rand.nextInt(MAX_VALUE);
}
}
}

// Return number of rows in Matrix object
publicintnumRows() {
returnmatrix.length;
}

// Return number of columns in Matrix object
publicintnumCols() {
return (matrix.length == 0) ?0:matrix[0].length;
}

// Return String representation of Matrix object
publicStringtoString() {
Stringm = "";
for (intr = 0; r < numRows(); r++) {
for (intc = 0; c < numCols(); c++) {
m += matrix[r][c] + " ";
}
m += "\n";
}
returnm;
}

// Get value at row r and column c
publicdoublegetValue(intr, intc) {
returnmatrix[r][c];
}

// Set value at row r and column c
publicvoidsetValue(intr, intc, doublevalue) {
matrix[r][c] = value;
}

// Return product of this and other
publicMatrixmultiply(Matrixother) {
// TODO: complete method definition, replace this placeholder code
Matrixproduct = newMatrix(0,0);
returnproduct;
}


// main method to test Matrix class
publicstaticvoidmain(String[] args) {
Matrixa = newMatrix(2, 3);
Matrixb = newMatrix(3, 2);
a.fillMatrix();
b.fillMatrix();
Matrixproduct = a.multiply(b);
System.out.println(a);
System.out.println(b);
System.out.println(product);
}

}
Expert Solution
steps

Step by step

Solved in 4 steps with 4 images

Blurred answer
Knowledge Booster
Class
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education