Skip to content

Instantly share code, notes, and snippets.

@encadyma
Created February 26, 2019 04:54
Show Gist options
  • Save encadyma/55a9dd77982c051d60213d19cf898ac3 to your computer and use it in GitHub Desktop.
Save encadyma/55a9dd77982c051d60213d19cf898ac3 to your computer and use it in GitHub Desktop.
EchelonTransformer - an attempt to reduce any matrix
import java.lang.*;
public class EchelonTransformer {
private static int[][] matrix = new int[1000][1000];
private static int pivotX = 0; // Pivot row
private static int pivotY = 0; // Pivot column
private static final int dimX = 4;
private static final int dimY = 5;
public static int[][] reduceToREF(int[][] t_matrix) {
for (pivotY = 0; pivotY < dimX; pivotY++) {
// Step 1: Begin with the leftmost non-zero column.
int[] colArr = getColumnArrayByIndex(t_matrix, pivotY);
if (isZeroArray(colArr)) { continue; }
// Step 2: Find a non-zero pivot, re-arranging if necessary.
// Trick: get the lowest via Math.abs();
int lowestIndex = pivotY;
for (int i = pivotY; i < colArr.length; i++) {
if (colArr[i] != 0) {
// Check if it needs to be swapped.
// Choose an element that has the lowest value
// so that the kFactor does not become a problem.
// We want to avoid ugly fractions as much as possible.
if (colArr[lowestIndex] == 0) { lowestIndex = i; }
if (Math.abs(colArr[i]) < Math.abs(colArr[lowestIndex])) {
lowestIndex = i;
}
}
}
int[] tempFirstRow = t_matrix[pivotY];
t_matrix[pivotY] = t_matrix[lowestIndex];
t_matrix[lowestIndex] = tempFirstRow;
colArr = getColumnArrayByIndex(t_matrix, pivotY);
// Step 3: Use row replacement operations to create zeros in all positions below the pivot.
for (int i = pivotY + 1; i < colArr.length; i++) {
if (colArr[i] != 0 && i != pivotY) {
// High assumption: kFactor results in an integer.
float kFactor = 0 - (getGCD(colArr[i], colArr[pivotY]) / colArr[pivotY]);
float mFactor = 0 - (getGCD(colArr[i], colArr[pivotY]) / colArr[i]);
t_matrix[i] = addTwoArrays(
multipleArrayByFactor(t_matrix[pivotY], kFactor),
multipleArrayByFactor(t_matrix[i], mFactor));
}
}
System.out.println("\nSTEP " + pivotY + ":\n");
print2DArray(t_matrix);
// break;
}
return t_matrix;
}
// Using the Euclidean algorithm to determine GCD
private static int getGCD(int i1, int i2) {
if (i2 == 0) { return i1; }
return getGCD(i2, i1 % i2);
}
public static int[] multipleArrayByFactor(int[] arr, float factor) {
int[] result = new int[arr.length];
for (int i = 0; i < arr.length; i++) {
result[i] = (int)(arr[i] * factor);
}
return result;
}
public static int[] addTwoArrays(int[] arr1, int[] arr2) {
int[] result = new int[arr1.length];
for (int i = 0; i < arr1.length; i++) {
result[i] = arr1[i] + arr2[i];
}
return result;
}
public static boolean isZeroArray(int[] checkedArray) {
for (int i: checkedArray) {
if (i != 0) { return false; }
}
return true;
}
public static int[] getColumnArrayByIndex(int[][] t_matrix, int index) {
int[] returnedArray = new int[dimY];
for (int i = 0; i < dimX; i++) {
returnedArray[i] = t_matrix[i][index];
}
return returnedArray;
}
public static void print2DArray(int[][] printedArray) {
System.out.println();
for (int[] i: printedArray) {
for (int j: i) {
System.out.print(j + "\t");
}
System.out.print("\n");
}
}
public static void main(String args[]) {
System.out.println("Transformer ON.");
// Each entry in the matrix represents
// a row in the 2D array
matrix = new int[][]{
{0, 3, 6, 4, 9},
{1, 2, 1, 3, 1},
{2, 3, 0, 3, 1},
{1, 4, 5, -9, -7}
};
/*matrix = new int[][]{
{0, 3, -6, 6, 4, -5},
{3, -7, 8, -5, 8, 9},
{3, -9, 12, -9, 6, 15}
};*/
System.out.println("\nBEFORE:\n");
print2DArray(matrix);
reduceToREF(matrix);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment