Matrix Ops Write your code in the file MatrixOps.java. Consider the following de
ID: 3705096 • Letter: M
Question
Matrix Ops Write your code in the file MatrixOps.java. Consider the following definitions from matrix algebra. A vector is a one-dimensional set of numbers, such as [42 920 The dot product of two equal-length vectors A and B is computed by multiplying the first entry of A by the first entry of B, the second entry of A by the second entry of B, etc., and then summing these products. For example, the dot product of (42 9 20] and [2 28 79) is 42 '29 28+20*79 -1916 A matrix is a two-dimensional set of numbers. For example, here is a matrix with 4 rows and 3 columns (each row or column can be treated asa vector): 42 9 20 2 28 79 19 -3 1 37 55 64 Two matrices A and B may be "multiplied" into a "product matrix in the following manner: The number in row iand column j of the product matrix is the dot product of row i of matrix A and column jof matrix B. Since you must have equal-length vectors in order to compute a dot product, it follows that the number of columns in matrix A must be the same as the number of rows in matrix B. The product matrix will have as many rows as matrix A, and as many columns as matrix B. Example. 1 2 7 8 9 (1+7 + 2+10) (1*8 + 2+11) (1+9 + 2*12) 3 4 *10 11 12(3*7 + 4*10) (3*84*11) (3*9 + 4*12) 27 30 33 61 6875 95 106 117 (5*7 + 6*10) (5*8 6*11) (5*9+6 12) More explanations of matrix multiplication may be found here or here. Complete the following method of MatrixOps: public static double[] multiply (doublet1I1 A, doublet1 B): Multiply matrices A and B. Return the product matrix. This method must work for matrices of any size fie, with any number of rows and/or columns). Treat "rows" as the first dimension and "columns" as the second dimension. Return nul if the matrices cannot be multiplied. Use MatrixDriver.java to test your method. MatrixDriver will ask you for the names of files containing matrices to multiply. Sample files m1.txt and m2.txt are provided for your use. Ifyour method works for this pair of matrices, that does NOT mean it will always work. You MUST create your own text files containing matrices, formatted like these samples, but with different numbers of rows and columns, in order to test your code Example: java MatrixDriver Enter name of file containing first matrix: m1.txt Enter name of file containing second matrix: m2.txt product matrix: 0.0 -6.0 7.0 You can find the requisite files hereExplanation / Answer
Step1: copy the below code into a file MatrixOps.java
public class MatrixOps {
public static double[][] multiply(double[][] A, double[][] B)
{
int r1 = A.length;
int c1 = A[0].length;
int r2 = B.length;
int c2 = B[0].length;
if(c1 != r2) //no. of cols int first matrix should be same as no. of rows in second , for multiplication
{
System.out.printf("Matrix1: %dx%d and Matrix2: %dx%d. Can't perform multiplication ", r1,c1, r2, c2);
return null;
}
double[][] C = new double[r1][c2];
for(int i = 0; i < r1; i++)
{
for(int j = 0; j < c2; j++)
{
for(int k = 0; k < c1; k++)
C[i][j] = C[i][j] + A[i][k] * B[k][j];
}
}
return C;
}
}
Step2: Download the MatrixDriver.java and m1.txt and m2.txt files as specified in the question. They are provided by instructor
MAKE SURE TO PLACE .txt files DIRECTLY IN THE PROJECT FOLDER AND NOT IN src FOLDER( in eclipse). Otherwise you will get file not found error.
Step 3: Execute the MatrixDriver.java file
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you