ComputerScienceExpert

(11)

$18/per page/

About ComputerScienceExpert

Levels Tought:
Elementary,Middle School,High School,College,University,PHD

Expertise:
Applied Sciences,Calculus See all
Applied Sciences,Calculus,Chemistry,Computer Science,Environmental science,Information Systems,Science Hide all
Teaching Since: Apr 2017
Last Sign in: 10 Weeks Ago, 5 Days Ago
Questions Answered: 4870
Tutorials Posted: 4863

Education

  • MBA IT, Mater in Science and Technology
    Devry
    Jul-1996 - Jul-2000

Experience

  • Professor
    Devry University
    Mar-2010 - Oct-2016

Category > Programming Posted 08 May 2017 My Price 11.00

Write your code in the file MatrixOps.java

Need help in this:

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 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*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 B may be "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 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) 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 MatrixOps:public static double[][] multiply(double[][] A, double[][] B):Multiply matrices A and B. 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. 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;}}

Answers

(11)
Status NEW Posted 08 May 2017 06:05 AM My Price 11.00

-----------

Not Rated(0)