diff --git a/Semester 3/Assignments/templatesMatricesSample/src/main/java/edu/slcc/asdv/beans/MatrixOperations.java b/Semester 3/Assignments/templatesMatricesSample/src/main/java/edu/slcc/asdv/beans/MatrixOperations.java index 659348f..43351fd 100644 --- a/Semester 3/Assignments/templatesMatricesSample/src/main/java/edu/slcc/asdv/beans/MatrixOperations.java +++ b/Semester 3/Assignments/templatesMatricesSample/src/main/java/edu/slcc/asdv/beans/MatrixOperations.java @@ -35,6 +35,7 @@ public class MatrixOperations implements Serializable ArrayList> b = convertToBigInteger(matrixA.getMatrix()); ArrayList> c = new ArrayList>(); c = convertToString(matrixManipulator.addParallel(a, b)); + printArray(c); matrixC.setMatrixC( c ); return ""; } @@ -44,6 +45,16 @@ public class MatrixOperations implements Serializable ArrayList> b = convertToBigInteger(matrixA.getMatrix()); ArrayList> c = new ArrayList>(); c = convertToString(matrixManipulator.multiplyParallel(a, b)); + printArray(c); + matrixC.setMatrixC( c ); + return "";} + + public String subtract() { + ArrayList> a = convertToBigInteger(matrixA.getMatrix()); + ArrayList> b = convertToBigInteger(matrixA.getMatrix()); + ArrayList> c = new ArrayList>(); + c = convertToString(matrixManipulator.subtractParallel(a, b)); + printArray(c); matrixC.setMatrixC( c ); return "";} @@ -83,5 +94,36 @@ public class MatrixOperations implements Serializable } return string2DArray; } + + /** + * + * @param matrix + * @return + */ + public String printArray(ArrayList> matrix) { + String output = "{\n"; + for (int i = 0; i < matrix.size(); ++i) { + output += printSingleArray(matrix.get(i)); + if ((matrix.size() - 1) > i) { + output += ",\n"; + } + } + output += "\n}"; + System.out.println(output); + return output; + } + + public String printSingleArray(ArrayList matrix) { + String output = "\t{"; + for (int i = 0; i < matrix.size(); ++i) { + output += matrix.get(i); + if ((matrix.size() - 1) > i) { + output += ", "; + } + } + output += "}"; + return output; + } + } diff --git a/Semester 3/Assignments/templatesMatricesSample/src/main/java/edu/slcc/asdv/beans/MenuBar.java b/Semester 3/Assignments/templatesMatricesSample/src/main/java/edu/slcc/asdv/beans/MenuBar.java index d9fb89e..8d98aee 100644 --- a/Semester 3/Assignments/templatesMatricesSample/src/main/java/edu/slcc/asdv/beans/MenuBar.java +++ b/Semester 3/Assignments/templatesMatricesSample/src/main/java/edu/slcc/asdv/beans/MenuBar.java @@ -31,7 +31,7 @@ public class MenuBar implements Serializable matrixOperations.add();///adition of matrices System.out.println(matrixOperations.getMatrixC()); - List idsC = new ArrayList(); + List idsC = new ArrayList<>(); idsC.add("formC"); idsC.add("formC:datatableC"); idsC.add("formC:datatableC:columnsC"); @@ -46,10 +46,10 @@ public class MenuBar implements Serializable System.out.println("menu multiply was called"); System.out.println("menu bar multiply()"); - matrixOperations.multiply();///adition of matrices + matrixOperations.multiply();///multiplication of matrices System.out.println(matrixOperations.getMatrixC()); - List idsC = new ArrayList(); + List idsC = new ArrayList<>(); idsC.add("formC"); idsC.add("formC:datatableC"); idsC.add("formC:datatableC:columnsC"); @@ -60,10 +60,19 @@ public class MenuBar implements Serializable public void subtract() { - message( - FacesMessage.SEVERITY_INFO, - "Not implemented.", "To be implemented." - ); + System.out.println("menu subtract was called"); + + System.out.println("menu bar subtract()"); + matrixOperations.subtract();///subtraction of matrices + System.out.println(matrixOperations.getMatrixC()); + + List idsC = new ArrayList<>(); + idsC.add("formC"); + idsC.add("formC:datatableC"); + idsC.add("formC:datatableC:columnsC"); + idsC.add("formC:datatableC:columnsC:inputTextC"); + //idsC.add("form-menu");//:menuBar:submenu_matrices:menuitem_add"); + PrimeFaces.current().ajax().update(idsC); } diff --git a/Semester 3/Assignments/templatesMatricesSample/src/main/java/edu/slcc/asdv/bl/Matrices.java b/Semester 3/Assignments/templatesMatricesSample/src/main/java/edu/slcc/asdv/bl/Matrices.java index cdc428a..ab749c3 100644 --- a/Semester 3/Assignments/templatesMatricesSample/src/main/java/edu/slcc/asdv/bl/Matrices.java +++ b/Semester 3/Assignments/templatesMatricesSample/src/main/java/edu/slcc/asdv/bl/Matrices.java @@ -33,10 +33,16 @@ public class Matrices implements Matrix return result; } - - - - + @Override + public ArrayList> subtractParallel(ArrayList> A, ArrayList> B) + { + RecursiveTask>> rt + = new Matrices.MatricesSubtraction(0, A.size() - 1, A, B); + ForkJoinPool pool = new ForkJoinPool(); + ArrayList> result = pool.invoke(rt); + return result; + } + static class MatricesAddition extends RecursiveTask>> { @@ -131,6 +137,18 @@ public class Matrices implements Matrix } return resultsOfMultiplications; } + + public static ArrayList subtractLists(ArrayList list1, ArrayList list2) + { + ArrayList resultsOfSubtraction = new ArrayList(); + for (int bi = 0; bi < list1.size(); + ++bi) + { + resultsOfSubtraction.add(list1.get(bi).subtract(list2.get(bi))); + } + return resultsOfSubtraction; + } + public static ArrayList> columnMajorOrderReversal(ArrayList> b) { @@ -170,6 +188,14 @@ public class Matrices implements Matrix return bd; } + public static BigInteger subtract(ArrayList list) { + BigInteger bd = BigInteger.ZERO; + for (int bi = 0; bi < list.size(); bi++) { + bd = bd.subtract(list.get(bi)); + } + return bd; + } + static class MatricesMultiplication extends RecursiveTask>> { @@ -261,5 +287,98 @@ public class Matrices implements Matrix } } + + static class MatricesSubtraction extends RecursiveTask>> + { + + ArrayList> A; + ArrayList> B; + ArrayList> AxB; + final int HOW_MANY_ROWS_IN_PARALLEL = 3;//threshold + int startIndex; + int endIndex; + + public MatricesSubtraction(int startIndex, int endIndex, + ArrayList> A, + ArrayList> B) + { + this.startIndex = startIndex;//start at this row of the matrix + this.endIndex = endIndex;//end at this row of the matrix + this.A = A; + this.B = B; + AxB = new ArrayList>(); + + } + + /** + * matrix + * 1, 2, 3 + * 4, 5, 6 + * + * will be transformed to + * 1, 4 + * 2, 5 + * 3, 6 + * + * @param list + * @return + */ + @Override + protected ArrayList> compute() + { + //>>This is the addition of matrices in the IF. + //That is, HOW_MANY_ROWS_IN_PARALLEL from matrix A and HOW_MANY_ROWS_IN_PARALLEL from matrix B + if (this.endIndex - this.startIndex < HOW_MANY_ROWS_IN_PARALLEL) + { + ArrayList> resultC = new ArrayList>(); + ArrayList> bTransformed = columnMajorOrderReversal(B); + + for (int biA = this.startIndex; + biA <= this.endIndex; + ++biA) + { + ArrayList rowA = A.get(biA); + ArrayList rowAxB = new ArrayList(); + ArrayList rowCalculation = new ArrayList(); + for (int biB = 0; + biB < bTransformed.size(); + ++biB) + { + ArrayList rowB = bTransformed.get(biB); + + ArrayList productsOfRow = subtractLists(rowA, rowB); + BigInteger sumOfDifference = add(productsOfRow); + rowCalculation.add(sumOfDifference); + + } + resultC.add(rowCalculation); + + } + return resultC; + } + else + { //>> keep on FORKING the matrix until the + //side of the matric is equal or less to HOW_MANY_ROWS_IN_PARALLEL + + int mid = (this.startIndex + this.endIndex) / 2; + RecursiveTask>> firstHalf + = new MatricesMultiplication(this.startIndex, mid, A, B); + + RecursiveTask>> secondHalf + = new MatricesMultiplication(mid + 1, this.endIndex, A, B); + + firstHalf.fork();//this line will invoke method compute + secondHalf.fork();///this line will invoke method compute + + //>> join what the FORKs returned from the IFs + AxB.addAll(firstHalf.join()); + AxB.addAll(secondHalf.join()); + + return AxB; + } + } + + } + } diff --git a/Semester 3/Assignments/templatesMatricesSample/src/main/java/edu/slcc/asdv/bl/Matrix.java b/Semester 3/Assignments/templatesMatricesSample/src/main/java/edu/slcc/asdv/bl/Matrix.java index fd06101..951e67e 100644 --- a/Semester 3/Assignments/templatesMatricesSample/src/main/java/edu/slcc/asdv/bl/Matrix.java +++ b/Semester 3/Assignments/templatesMatricesSample/src/main/java/edu/slcc/asdv/bl/Matrix.java @@ -15,5 +15,7 @@ public interface Matrix { ArrayList> addParallel(ArrayList> A, ArrayList> B); ArrayList> multiplyParallel(ArrayList> A, ArrayList> B); + ArrayList> subtractParallel(ArrayList> A, ArrayList> B); + } diff --git a/Semester 3/ZIPs/TermProject1_CalebFontenot.zip b/Semester 3/ZIPs/TermProject1_CalebFontenot.zip new file mode 100644 index 0000000..5527979 Binary files /dev/null and b/Semester 3/ZIPs/TermProject1_CalebFontenot.zip differ