Guest User

Untitled

a guest
Apr 26th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.19 KB | None | 0 0
  1. /**
  2.  * Simple class providing various operations on integer matrices
  3.  */
  4. public class Matrix {
  5.     /**
  6.      * Mirrors a 2-dimensional array along the X-axis and returns the sum of
  7.      * its cell's values.
  8.      *
  9.      * @param array In: The array to processed
  10.      *              Out: The mirrored araray
  11.      *
  12.      * @return The sum of the arrays' cells as long as it is binary (i.e.
  13.      *         contains nothing but 1's and 0's), otherwise -1
  14.      */
  15.     public static int mirrorArray(int[][] array) {
  16.         // The sum of all 1's contained in the array
  17.         int sum = 0;
  18.         // Use a clone as source
  19.         int[][] original = array.clone();
  20.         for (int i=0; i<original.length; ++i)
  21.             // Allow incomplete and non-rectangular originals
  22.             if (original[i] != null) {
  23.                 array[i] = new int[original[i].length];
  24.                 // Process each line
  25.                 for (int j=0; j<array[i].length; ++j) {
  26.                     // Reverse the line
  27.                     array[i][array[i].length-1-j] = original[i][j];
  28.                     // Sum the 1's or set the sum to -1 if the original contains
  29.                     // values outside [0,1]
  30.                     switch (original[i][j]) {
  31.                         case 0: break;
  32.                         case 1: if (sum != -1) ++sum; break;
  33.                         default: sum = -1;
  34.                     }
  35.                 }
  36.             }
  37.         return sum;
  38.     }  
  39.    
  40.    
  41.     /**
  42.      * Sums the diagonals in the lower left or the upper right triangle,
  43.      * whichever is filled.
  44.      *
  45.      * @param input A quadratic matrix to operate on.
  46.      *              Passing an incomplete or non-quadratic array will result in
  47.      *              undefined behaviour.
  48.      *
  49.      * @return An array containing the sums of each diagonal
  50.      */
  51.     public static int[] getDiagonalSums(int[][] input) {
  52.         // The number of diagonals equals the matrix' side length
  53.         int[] sums = new int[input.length];
  54.         // Work around a 1x1 matrix
  55.         if (input.length == 1) sums[0] = input[0][0];
  56.         else {
  57.             // If [1][0] != 0, sum the lower left triangle, else the
  58.             // upper right one
  59.             if (input[1][0] != 0) {
  60.                 // line: the current starting line / processed diagonal
  61.                 for (int line=0; line<input.length; ++line)
  62.                     // depth: the horizontal and vertical offset for each
  63.                     // iteration step
  64.                     for (int depth=0; depth<input.length-line; ++depth)
  65.                         sums[line] += input[line+depth][depth];
  66.             }
  67.             else {
  68.                 // See above. Iterate over columns rather than lines
  69.                 for (int column=0; column<input[0].length; ++column)
  70.                     for (int depth=0; depth<input[0].length-column; ++depth)
  71.                         sums[column] += input[depth][column+depth];
  72.             }
  73.         }
  74.         return sums;        
  75.     }
  76.    
  77.     public static void printMatrix(int[][] array)
  78.     {
  79.         // Print a martrix as  "1 2 3\n4 5 6\n7 8 9\n"
  80.         for (int i=0; i<array.length; ++i) {
  81.             for (int j=0; j<array[i].length; ++j)
  82.                 System.out.print(array[i][j] + " ");
  83.             System.out.println();
  84.         }
  85.     }
  86.    
  87.     public static void printArray(int[] array)
  88.     {
  89.         // Print an array as "1,2,3,4,..,n\n"
  90.         for (int i=0; i<array.length; ++i)
  91.             System.out.print(array[i] + (i<array.length-1 ? ", " : "\n"));
  92.     }
  93.    
  94.    
  95.     public static void main(String[] args) {
  96.         // Test the whole class
  97.         System.out.println("Test mirrorArray():\n===================");
  98.         System.out.println("binary1:");
  99.         int[][] binary1 = { { 1, 0, 1, 1 },
  100.                             { 0, 0, 1, 1 },
  101.                             { 1, 0, 0, 0 } };
  102.         printMatrix(binary1);
  103.         System.out.println("\n  | " + mirrorArray(binary1) + "\n  v\n");
  104.         printMatrix(binary1);
  105.        
  106.         System.out.println("\n\nbinary2:");
  107.         int[][] binary2 = { { 1, 3, 1, 1 },
  108.                             { 0, 0, 1, 0 },
  109.                             { 1, 0, 0, 2 } };
  110.         printMatrix(binary2);
  111.         System.out.println("\n  | " + mirrorArray(binary2) + "\n  v\n");
  112.         printMatrix(binary2);
  113.        
  114.         System.out.println(
  115.             "\n\nTest getDiagonalSums():\n=======================\n");
  116.         System.out.println("triangle1:");
  117.         int[][] triangle1 = { { 2, 0, 0, 0 },
  118.                               { 7, 8, 0, 0 },
  119.                               { 6, 5, 6, 0 },
  120.                               { 3, 1, 7, 5 } };
  121.         printMatrix(triangle1);
  122.         System.out.print(" = ");
  123.         printArray(getDiagonalSums(triangle1));
  124.        
  125.         System.out.println("\ntriangle2:");
  126.         int[][] triangle2 = { { 2, 7, 6, 3 },
  127.                               { 0, 8, 5, 1 },
  128.                               { 0, 0, 6, 7 },
  129.                               { 0, 0, 0, 5 } };
  130.         printMatrix(triangle2);
  131.         System.out.print(" = ");
  132.         printArray(getDiagonalSums(triangle2));
  133.         System.out.println();
  134.     }
  135. }
Add Comment
Please, Sign In to add comment