Advertisement
Nattack

Untitled

Feb 28th, 2017
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.95 KB | None | 0 0
  1.  
  2. public class PE8_6_100281657 {
  3.    
  4.     public static double[][] multiplyMatrix(double[][] lhs, double[][] rhs) {
  5.        
  6.         // two matrices can only be multiplied if lhs cols = rhs rows
  7.         if (lhs[0].length == rhs.length) {
  8.             // resulting product array will be lhs rows * rhs cols.
  9.             final int rows = rhs[0].length;
  10.             final int height = lhs.length;
  11.             double[][] result = new double[height][rows];
  12.            
  13.             for (int i = 0; i < height; i++)
  14.                 for (int j = 0; j < rows; j++)
  15.                     for (int k = 0; k < lhs[0].length; k++)
  16.                             result[i][j] += (lhs[i][k] * rhs[k][j]);
  17.             return result;
  18.         }
  19.         else return new double[lhs.length][lhs[0].length]; // else return a dummy array, but we should throw an exception.
  20.     }
  21.    
  22.     public static double[][] multiplyMatrix(double[][] matrix, double scalar) {
  23.         return matrix;
  24.     }
  25.    
  26.    
  27.     public static double[][] addMatrix(double[][] lhs, double[][] rhs) {
  28.        
  29.         // if the dimensions are equal, add them together.
  30.         if ((lhs[0].length == rhs[0].length) && (lhs.length == rhs.length)) {
  31.             double[][] result = new double[lhs.length][lhs[0].length];
  32.             for( int i = 0; i < lhs.length; i++)
  33.                 for( int j = 0; j < lhs[0].length; j++)
  34.                     result[i][j] = lhs[i][j] + rhs[i][j];
  35.             return result;
  36.                    
  37.         }
  38.         // else return a dummy array.
  39.         // in the future, we should throw an exception.
  40.         else return new double[lhs.length][lhs[0].length];
  41.     }
  42.    
  43.     public static double[][] getMatrix( int height, int width ) {
  44.         java.util.Scanner input = new java.util.Scanner( System.in );
  45.         String buffer;
  46.         java.util.Scanner getBufferedInput;
  47.         double[][] result = new double[height][width];
  48.         for (int i = 0; i < width; i++) {
  49.             System.out.print("Enter row " + (i + 1) + " of the array, " + width + " number" + (width !=1 ? "s" : "") +" separated by spaces: ");
  50.             buffer = input.nextLine();
  51.             getBufferedInput = new java.util.Scanner(buffer);
  52.             for (int j = 0; j < width; j++)
  53.                 if (getBufferedInput.hasNextDouble())
  54.                     result[i][j] = getBufferedInput.nextDouble();
  55.                 else
  56.                     result[i][j] = 0; // fill with zeroes if no valid input
  57.         }
  58.         input.close();
  59.         return result;
  60.                
  61.     }
  62.    
  63.     public static String matrixToString( double[][] matrix ) {
  64.         StringBuilder result = new StringBuilder();
  65.         for (int i = 0; i < matrix.length; i++) {
  66.             for (int j = 0; j < matrix[0].length; j++ )
  67.                 result.append(String.format("%6.2f", matrix[i][j]));
  68.             result.append("\n");
  69.         }
  70.         return result.toString();
  71.     }
  72.    
  73.     public static void printAddMatrix ( double[][] lhs, double[][] rhs) {
  74.         // these three scanners will simplify putting together this string,
  75.         // although the method may be a bit inefficient.
  76.         StringBuilder matricesAdded = new StringBuilder();
  77.         java.util.Scanner lhsStr = new java.util.Scanner( matrixToString(lhs) );
  78.         java.util.Scanner rhsStr = new java.util.Scanner( matrixToString(rhs) );
  79.         java.util.Scanner addStr = new java.util.Scanner( matrixToString(addMatrix(lhs, rhs)) );
  80.        
  81.         // where do we put the +/= sign
  82.         final int midPoint = lhs.length / 2;
  83.        
  84.         // build the equation
  85.         for (int i = 0; i < lhs.length; i++) {
  86.             matricesAdded.append(lhsStr.nextLine() + (i == midPoint ? "   +  " : "      "));
  87.             matricesAdded.append(rhsStr.nextLine() + (i == midPoint ? "   =  " : "      "));
  88.             matricesAdded.append(addStr.nextLine() + "\n");
  89.         }
  90.         lhsStr.close();
  91.         rhsStr.close();
  92.         addStr.close();
  93.        
  94.         System.out.println(matricesAdded.toString());
  95.     }
  96.    
  97.     public static void printMulMatrix( double[][] lhs, double[][] rhs) {
  98.         // because multiplying matrices often has a few more steps, and because the matrices can be of different sizes,
  99.         // this method is a lot more complicated than building the adding equation.
  100.        
  101.         // these three scanners will simplify putting together this string,
  102.         // although the method may be a bit inefficient.
  103.         StringBuilder matricesAdded = new StringBuilder();
  104.         double[][] mul = multiplyMatrix(lhs, rhs);
  105.        
  106.         java.util.Scanner lhsStr = new java.util.Scanner( matrixToString(lhs) );
  107.         java.util.Scanner rhsStr = new java.util.Scanner( matrixToString(rhs) );
  108.         java.util.Scanner mulStr = new java.util.Scanner( matrixToString(mul) );
  109.        
  110.         // find the biggest array
  111.         final int longestArray = lhs.length > rhs[0].length ? lhs.length : rhs.length;
  112.        
  113.         // where do we put the +/= sign
  114.         final int midPoint = longestArray / 2;
  115.        
  116.         // build  empty string buffer
  117.         StringBuilder lhsEmpty = new StringBuilder();
  118.         StringBuilder rhsEmpty = new StringBuilder();
  119.         StringBuilder mulEmpty = new StringBuilder();
  120.         for (int i = 0; i < lhs[0].length; i++)
  121.             lhsEmpty.append("      ");
  122.         for (int i = 0; i < rhs[0].length; i++)
  123.             rhsEmpty.append("      ");
  124.         for (int i = 0; i < rhs[0].length; i++)
  125.             mulEmpty.append("      ");
  126.        
  127.         // build the equation
  128.         for (int i = 0; i < longestArray; i++) {
  129.             if (lhsStr.hasNextLine()) matricesAdded.append(lhsStr.nextLine() + (i == midPoint ? "   *  " : "      "));
  130.             else matricesAdded.append(lhsEmpty + (i == midPoint ? "   *  " : "      "));
  131.             if (rhsStr.hasNextLine()) matricesAdded.append(rhsStr.nextLine() + (i == midPoint ? "   =  " : "      "));
  132.             else matricesAdded.append(rhsEmpty + (i == midPoint ? "   =   " : "      "));
  133.             if (mulStr.hasNextLine()) matricesAdded.append(mulStr.nextLine() + "\n");
  134.             else matricesAdded.append(mulEmpty + "\n");
  135.         }
  136.         lhsStr.close();
  137.         rhsStr.close();
  138.         mulStr.close();
  139.        
  140.         System.out.println(matricesAdded.toString());
  141.     }
  142.  
  143.  
  144.     public static void main(String[] args) {
  145.         // Add square matrices
  146.        
  147.        
  148.         // should be able to change these, I made the array printer extendible. Any dimensions should work.
  149.         final int arrayH = 3;
  150.         final int arrayW = 3;
  151.        
  152.        
  153.         // get first matrix
  154.         System.out.println("First matrix ");
  155.         double[][] left = getMatrix(arrayH, arrayW);
  156.        
  157.        
  158.         // get second matrix
  159.         System.out.println("\nSecond matrix ");
  160.         double[][] right = getMatrix(arrayH, arrayW);
  161.        
  162.         // display matrix
  163.         System.out.println("\nThe multiplied matrix is:");
  164.         printMulMatrix(left, right);
  165.     }
  166.  
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement