Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Simple class providing various operations on integer matrices
- */
- public class Matrix {
- /**
- * Mirrors a 2-dimensional array along the X-axis and returns the sum of
- * its cell's values.
- *
- * @param array In: The array to processed
- * Out: The mirrored araray
- *
- * @return The sum of the arrays' cells as long as it is binary (i.e.
- * contains nothing but 1's and 0's), otherwise -1
- */
- public static int mirrorArray(int[][] array) {
- // The sum of all 1's contained in the array
- int sum = 0;
- // Use a clone as source
- int[][] original = array.clone();
- for (int i=0; i<original.length; ++i)
- // Allow incomplete and non-rectangular originals
- if (original[i] != null) {
- array[i] = new int[original[i].length];
- // Process each line
- for (int j=0; j<array[i].length; ++j) {
- // Reverse the line
- array[i][array[i].length-1-j] = original[i][j];
- // Sum the 1's or set the sum to -1 if the original contains
- // values outside [0,1]
- switch (original[i][j]) {
- case 0: break;
- case 1: if (sum != -1) ++sum; break;
- default: sum = -1;
- }
- }
- }
- return sum;
- }
- /**
- * Sums the diagonals in the lower left or the upper right triangle,
- * whichever is filled.
- *
- * @param input A quadratic matrix to operate on.
- * Passing an incomplete or non-quadratic array will result in
- * undefined behaviour.
- *
- * @return An array containing the sums of each diagonal
- */
- public static int[] getDiagonalSums(int[][] input) {
- // The number of diagonals equals the matrix' side length
- int[] sums = new int[input.length];
- // Work around a 1x1 matrix
- if (input.length == 1) sums[0] = input[0][0];
- else {
- // If [1][0] != 0, sum the lower left triangle, else the
- // upper right one
- if (input[1][0] != 0) {
- // line: the current starting line / processed diagonal
- for (int line=0; line<input.length; ++line)
- // depth: the horizontal and vertical offset for each
- // iteration step
- for (int depth=0; depth<input.length-line; ++depth)
- sums[line] += input[line+depth][depth];
- }
- else {
- // See above. Iterate over columns rather than lines
- for (int column=0; column<input[0].length; ++column)
- for (int depth=0; depth<input[0].length-column; ++depth)
- sums[column] += input[depth][column+depth];
- }
- }
- return sums;
- }
- public static void printMatrix(int[][] array)
- {
- // Print a martrix as "1 2 3\n4 5 6\n7 8 9\n"
- for (int i=0; i<array.length; ++i) {
- for (int j=0; j<array[i].length; ++j)
- System.out.print(array[i][j] + " ");
- System.out.println();
- }
- }
- public static void printArray(int[] array)
- {
- // Print an array as "1,2,3,4,..,n\n"
- for (int i=0; i<array.length; ++i)
- System.out.print(array[i] + (i<array.length-1 ? ", " : "\n"));
- }
- public static void main(String[] args) {
- // Test the whole class
- System.out.println("Test mirrorArray():\n===================");
- System.out.println("binary1:");
- int[][] binary1 = { { 1, 0, 1, 1 },
- { 0, 0, 1, 1 },
- { 1, 0, 0, 0 } };
- printMatrix(binary1);
- System.out.println("\n | " + mirrorArray(binary1) + "\n v\n");
- printMatrix(binary1);
- System.out.println("\n\nbinary2:");
- int[][] binary2 = { { 1, 3, 1, 1 },
- { 0, 0, 1, 0 },
- { 1, 0, 0, 2 } };
- printMatrix(binary2);
- System.out.println("\n | " + mirrorArray(binary2) + "\n v\n");
- printMatrix(binary2);
- System.out.println(
- "\n\nTest getDiagonalSums():\n=======================\n");
- System.out.println("triangle1:");
- int[][] triangle1 = { { 2, 0, 0, 0 },
- { 7, 8, 0, 0 },
- { 6, 5, 6, 0 },
- { 3, 1, 7, 5 } };
- printMatrix(triangle1);
- System.out.print(" = ");
- printArray(getDiagonalSums(triangle1));
- System.out.println("\ntriangle2:");
- int[][] triangle2 = { { 2, 7, 6, 3 },
- { 0, 8, 5, 1 },
- { 0, 0, 6, 7 },
- { 0, 0, 0, 5 } };
- printMatrix(triangle2);
- System.out.print(" = ");
- printArray(getDiagonalSums(triangle2));
- System.out.println();
- }
- }
Add Comment
Please, Sign In to add comment