Write your code in the file MatrixOpsjava Consider the follo

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 9 20]. The dot product of two equal-length vectors A and 3 is computed by multiplying the first entry of A by the first entry of 3, the second entry of A by the second entry of 3, etc., and then summing these products. For example, the dot product of [42 9 20] and [2 28 79] is 42*2 + 9*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 as a vector): 42 9 20 2 28 79 19 -3 1 37 55 64 Two matrices A and 3 maybe \"multiplied\" into a \"product matrix\" in the following manner: The number in row i and column j of the product matrix is the dot product of row i of matrix A and column j of 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 3. The product matrix will have as many rows as matrix A, and as many columns as matrix 3. Example: 1 2 7 8 9 (1*7 + 2 * 10) (1*8 + 2*11) (1*9 + 2*12) 27 30 33 3 4 * 10 11 12 = (3 * 7 + 4 * 10) (3*8 + 4*11) (3*9 + 4*12) = 61 68 75 5 6 (5*7 + 6 * 10) (5*8 + 6* 11) (5*9 + 6*12) 95 106 117 More explanations of matrix multiplication may be found here or here. Complete the following method of Matrix Ops: public static double]][] multiply(double[][] A, double]][] B). Multiply matrices A and 3. Return the product matrix. This method must work for matrices of any size (i.e., with any number of rows and/or columns;. Treat \"rows\" as the first dimension and \"columns \" as the second dimension. Return null if the matrices cannot be multiplied. Use MatrixDriver.java to test your method. Matrix Driver will ask you for the names of files containing matrices to multiply. Sample files m1.txt and m2.txt are provided for your use. If your 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.

Solution

public class MatrixOps {

/**
*
* @param A
* @param B
* @return
*/
public static double[][] multiply(double[][] A, double[][] B) {
int rowsInA = A.length;
int rowsInB = B.length;
int columnsInA = A[0].length; // same as rows in B
int columnsInB = B[0].length;
double[][] C = new double[rowsInA][columnsInB];
if (columnsInA == rowsInB) {
for (int i = 0; i < rowsInA; i++) {
for (int j = 0; j < columnsInB; j++) {
for (int k = 0; k < columnsInA; k++) {
C[i][j] += A[i][k] * B[k][j];
}
}
}
return C;
} else {
return null;
}

}
}

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;

public class MatrixDriver {

/**
*
* @param fileName
* @return
* @throws IOException
*/
public static double[][] makeMatrix(String fileName) throws IOException {
int nRow = 0;
int nColumn = 0;
BufferedReader br = null;

try {

String sCurrentLine;

br = new BufferedReader(new FileReader(fileName));
nColumn = br.readLine().split(\" \").length;
if (nColumn !=0){
nRow=nRow+1;
}
// System.out.println(Arrays.toString( br.readLine().split(\" \")));
while ((sCurrentLine = br.readLine()) != null) {
// System.out.println(sCurrentLine);
nRow=nRow+1;
}

} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null) {
br.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}

System.out.println(nRow+\" \"+nColumn);
double[][] matrix = new double[nRow][nColumn];

Scanner sc = new Scanner(new File(fileName));
for (int i = 0; i < nRow; ++i) {
for (int j = 0; j < nColumn; ++j) {
if (sc.hasNextInt()) {
matrix[i][j] = sc.nextInt();
}
}
}
sc.close();

return matrix;
}

public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
String fileName1;
String fileName2;
System.out.println(\"Enter File Name for Matrix A:\");
fileName1 = sc.nextLine();
System.out.println(\"Enter File Name for Matrix B:\");
fileName2 = sc.nextLine();
System.out.println(fileName1 + fileName2);
double[][] A = makeMatrix(fileName1);
double[][] B = makeMatrix(fileName2);
double[][] C = MatrixOps.multiply(A, B);
for(double[] x:C){
for(double y:x){
System.out.print(y+\" \");
}
System.out.println(\"\");
}

sc.close();
}
}

 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
 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
 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

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site