Levels Tought:
Elementary,Middle School,High School,College,University,PHD
Teaching Since: | Apr 2017 |
Last Sign in: | 10 Weeks Ago, 5 Days Ago |
Questions Answered: | 4870 |
Tutorials Posted: | 4863 |
MBA IT, Mater in Science and Technology
Devry
Jul-1996 - Jul-2000
Professor
Devry University
Mar-2010 - Oct-2016
Need help in this:
Write your code in the file MatrixOps.java. .
Â
Consider the following definitions from matrix algebra:
42 9 20 2 28 79 19 -3 1 37 55 64
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 117More explanations of matrix multiplication may be found here or here.
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. 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.
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-5.0 -6.0-7.0
import java.io.BufferedReader;import java.io.FileReader;import java.io.IOException;public class MatrixDriver {private static BufferedReader br;public static void main(String[] args) throws IOException{System.out.println("Enter name of file containing first matrix:");double[][] matrix1 = readMatrixFromFile("D:/m1.txt");//reading thefile thru readMatrixFile methodSystem.out.println("Enter name of file containing second matrix:");double[][] matrix2 = readMatrixFromFile("D:/m2.txt");//reading thefile thru readMatrixFile methoddouble[][] product = MatrixOps.multiply(matrix1, matrix2);//gettingproduct from MatrixOps class containing multiply methodif (product != null){System.out.println("product matrix:");printMatrix(product);}else{System.out.println(" bad input");}}public static double[][] readMatrixFromFile(String filename) throwsIOException{br = null;String line;int numrows, numcols, colsOnLine;double[][] matrix;String[] lineparts;numrows = 0;numcols = -1;while ((line = br.readLine()) != null){numrows++;colsOnLine = line.trim().split("\\s+").length;if (numcols == -1){numcols = colsOnLine;}if (colsOnLine != numcols){System.err.println("Badly formatted matrix file: " +filename);return null;}}br.close();matrix = new double[numrows][numcols];br = new BufferedReader(new FileReader(filename));for (int row = 0 ; row < numrows ; row++)
{lineparts = br.readLine().trim().split("\\s+");for (int col = 0 ; col < numcols ; col++){matrix[row][col] = Double.parseDouble(lineparts[col]);}}br.close();return matrix;}public static void printMatrix(double[][] matrix){for (int row = 0 ; row < matrix.length ; row++){for (int col = 0 ; col < matrix[0].length ; col++){System.out.print(matrix[row][col] + "\t");}System.out.println();}}}class MatrixOps{public static double[][] multiply(double[][] matrix1, double[][]matrix2){int rowA = matrix1.length;int rowB = matrix2.length;int columnA = matrix1[0].length;int columnB = matrix2[0].length;double[][] matrix3 = new double[matrix1.length][matrix2[0].length];if ( columnA != rowB ) {System.out.println("Matrix does not match cannot be multiplied");return null;}for(int i = 0; i < rowA; i++){for(int j = 0; j < columnB; j++){for(int k = 0; k < columnA; k++){matrix3[i][j] += matrix1[i][k] * matrix2[k][j];}}}return matrix3;}}