Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class PE8_6_100281657 {
- public static double[][] multiplyMatrix(double[][] lhs, double[][] rhs) {
- // two matrices can only be multiplied if lhs cols = rhs rows
- if (lhs[0].length == rhs.length) {
- // resulting product array will be lhs rows * rhs cols.
- final int rows = rhs[0].length;
- final int height = lhs.length;
- double[][] result = new double[height][rows];
- for (int i = 0; i < height; i++)
- for (int j = 0; j < rows; j++)
- for (int k = 0; k < lhs[0].length; k++)
- result[i][j] += (lhs[i][k] * rhs[k][j]);
- return result;
- }
- else return new double[lhs.length][lhs[0].length]; // else return a dummy array, but we should throw an exception.
- }
- public static double[][] multiplyMatrix(double[][] matrix, double scalar) {
- return matrix;
- }
- public static double[][] addMatrix(double[][] lhs, double[][] rhs) {
- // if the dimensions are equal, add them together.
- if ((lhs[0].length == rhs[0].length) && (lhs.length == rhs.length)) {
- double[][] result = new double[lhs.length][lhs[0].length];
- for( int i = 0; i < lhs.length; i++)
- for( int j = 0; j < lhs[0].length; j++)
- result[i][j] = lhs[i][j] + rhs[i][j];
- return result;
- }
- // else return a dummy array.
- // in the future, we should throw an exception.
- else return new double[lhs.length][lhs[0].length];
- }
- public static double[][] getMatrix( int height, int width ) {
- java.util.Scanner input = new java.util.Scanner( System.in );
- String buffer;
- java.util.Scanner getBufferedInput;
- double[][] result = new double[height][width];
- for (int i = 0; i < width; i++) {
- System.out.print("Enter row " + (i + 1) + " of the array, " + width + " number" + (width !=1 ? "s" : "") +" separated by spaces: ");
- buffer = input.nextLine();
- getBufferedInput = new java.util.Scanner(buffer);
- for (int j = 0; j < width; j++)
- if (getBufferedInput.hasNextDouble())
- result[i][j] = getBufferedInput.nextDouble();
- else
- result[i][j] = 0; // fill with zeroes if no valid input
- }
- input.close();
- return result;
- }
- public static String matrixToString( double[][] matrix ) {
- StringBuilder result = new StringBuilder();
- for (int i = 0; i < matrix.length; i++) {
- for (int j = 0; j < matrix[0].length; j++ )
- result.append(String.format("%6.2f", matrix[i][j]));
- result.append("\n");
- }
- return result.toString();
- }
- public static void printAddMatrix ( double[][] lhs, double[][] rhs) {
- // these three scanners will simplify putting together this string,
- // although the method may be a bit inefficient.
- StringBuilder matricesAdded = new StringBuilder();
- java.util.Scanner lhsStr = new java.util.Scanner( matrixToString(lhs) );
- java.util.Scanner rhsStr = new java.util.Scanner( matrixToString(rhs) );
- java.util.Scanner addStr = new java.util.Scanner( matrixToString(addMatrix(lhs, rhs)) );
- // where do we put the +/= sign
- final int midPoint = lhs.length / 2;
- // build the equation
- for (int i = 0; i < lhs.length; i++) {
- matricesAdded.append(lhsStr.nextLine() + (i == midPoint ? " + " : " "));
- matricesAdded.append(rhsStr.nextLine() + (i == midPoint ? " = " : " "));
- matricesAdded.append(addStr.nextLine() + "\n");
- }
- lhsStr.close();
- rhsStr.close();
- addStr.close();
- System.out.println(matricesAdded.toString());
- }
- public static void printMulMatrix( double[][] lhs, double[][] rhs) {
- // because multiplying matrices often has a few more steps, and because the matrices can be of different sizes,
- // this method is a lot more complicated than building the adding equation.
- // these three scanners will simplify putting together this string,
- // although the method may be a bit inefficient.
- StringBuilder matricesAdded = new StringBuilder();
- double[][] mul = multiplyMatrix(lhs, rhs);
- java.util.Scanner lhsStr = new java.util.Scanner( matrixToString(lhs) );
- java.util.Scanner rhsStr = new java.util.Scanner( matrixToString(rhs) );
- java.util.Scanner mulStr = new java.util.Scanner( matrixToString(mul) );
- // find the biggest array
- final int longestArray = lhs.length > rhs[0].length ? lhs.length : rhs.length;
- // where do we put the +/= sign
- final int midPoint = longestArray / 2;
- // build empty string buffer
- StringBuilder lhsEmpty = new StringBuilder();
- StringBuilder rhsEmpty = new StringBuilder();
- StringBuilder mulEmpty = new StringBuilder();
- for (int i = 0; i < lhs[0].length; i++)
- lhsEmpty.append(" ");
- for (int i = 0; i < rhs[0].length; i++)
- rhsEmpty.append(" ");
- for (int i = 0; i < rhs[0].length; i++)
- mulEmpty.append(" ");
- // build the equation
- for (int i = 0; i < longestArray; i++) {
- if (lhsStr.hasNextLine()) matricesAdded.append(lhsStr.nextLine() + (i == midPoint ? " * " : " "));
- else matricesAdded.append(lhsEmpty + (i == midPoint ? " * " : " "));
- if (rhsStr.hasNextLine()) matricesAdded.append(rhsStr.nextLine() + (i == midPoint ? " = " : " "));
- else matricesAdded.append(rhsEmpty + (i == midPoint ? " = " : " "));
- if (mulStr.hasNextLine()) matricesAdded.append(mulStr.nextLine() + "\n");
- else matricesAdded.append(mulEmpty + "\n");
- }
- lhsStr.close();
- rhsStr.close();
- mulStr.close();
- System.out.println(matricesAdded.toString());
- }
- public static void main(String[] args) {
- // Add square matrices
- // should be able to change these, I made the array printer extendible. Any dimensions should work.
- final int arrayH = 3;
- final int arrayW = 3;
- // get first matrix
- System.out.println("First matrix ");
- double[][] left = getMatrix(arrayH, arrayW);
- // get second matrix
- System.out.println("\nSecond matrix ");
- double[][] right = getMatrix(arrayH, arrayW);
- // display matrix
- System.out.println("\nThe multiplied matrix is:");
- printMulMatrix(left, right);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement