It works but the output is broken :)

master
Caleb Fontenot 2024-03-21 21:50:22 +07:00
parent 118d95a69d
commit f6d0920781
5 changed files with 183 additions and 11 deletions

@ -35,6 +35,7 @@ public class MatrixOperations implements Serializable
ArrayList<ArrayList<BigInteger>> b = convertToBigInteger(matrixA.getMatrix());
ArrayList<ArrayList<String>> c = new ArrayList<ArrayList<String>>();
c = convertToString(matrixManipulator.addParallel(a, b));
printArray(c);
matrixC.setMatrixC( c );
return "";
}
@ -44,6 +45,16 @@ public class MatrixOperations implements Serializable
ArrayList<ArrayList<BigInteger>> b = convertToBigInteger(matrixA.getMatrix());
ArrayList<ArrayList<String>> c = new ArrayList<ArrayList<String>>();
c = convertToString(matrixManipulator.multiplyParallel(a, b));
printArray(c);
matrixC.setMatrixC( c );
return "";}
public String subtract() {
ArrayList<ArrayList<BigInteger>> a = convertToBigInteger(matrixA.getMatrix());
ArrayList<ArrayList<BigInteger>> b = convertToBigInteger(matrixA.getMatrix());
ArrayList<ArrayList<String>> c = new ArrayList<ArrayList<String>>();
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<ArrayList<String>> 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<String> 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;
}
}

@ -31,7 +31,7 @@ public class MenuBar implements Serializable
matrixOperations.add();///adition of matrices
System.out.println(matrixOperations.getMatrixC());
List<String> idsC = new ArrayList();
List<String> 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<String> idsC = new ArrayList();
List<String> 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<String> 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);
}

@ -33,10 +33,16 @@ public class Matrices implements Matrix
return result;
}
@Override
public ArrayList<ArrayList<BigInteger>> subtractParallel(ArrayList<ArrayList<BigInteger>> A, ArrayList<ArrayList<BigInteger>> B)
{
RecursiveTask<ArrayList<ArrayList<BigInteger>>> rt
= new Matrices.MatricesSubtraction(0, A.size() - 1, A, B);
ForkJoinPool pool = new ForkJoinPool();
ArrayList<ArrayList<BigInteger>> result = pool.invoke(rt);
return result;
}
static class MatricesAddition extends RecursiveTask<ArrayList<ArrayList<BigInteger>>>
{
@ -131,6 +137,18 @@ public class Matrices implements Matrix
}
return resultsOfMultiplications;
}
public static ArrayList<BigInteger> subtractLists(ArrayList<BigInteger> list1, ArrayList<BigInteger> list2)
{
ArrayList<BigInteger> resultsOfSubtraction = new ArrayList<BigInteger>();
for (int bi = 0; bi < list1.size();
++bi)
{
resultsOfSubtraction.add(list1.get(bi).subtract(list2.get(bi)));
}
return resultsOfSubtraction;
}
public static ArrayList<ArrayList<BigInteger>> columnMajorOrderReversal(ArrayList<ArrayList<BigInteger>> b)
{
@ -170,6 +188,14 @@ public class Matrices implements Matrix
return bd;
}
public static BigInteger subtract(ArrayList<BigInteger> 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<ArrayList<ArrayList<BigInteger>>>
{
@ -261,5 +287,98 @@ public class Matrices implements Matrix
}
}
static class MatricesSubtraction extends RecursiveTask<ArrayList<ArrayList<BigInteger>>>
{
ArrayList<ArrayList<BigInteger>> A;
ArrayList<ArrayList<BigInteger>> B;
ArrayList<ArrayList<BigInteger>> AxB;
final int HOW_MANY_ROWS_IN_PARALLEL = 3;//threshold
int startIndex;
int endIndex;
public MatricesSubtraction(int startIndex, int endIndex,
ArrayList<ArrayList<BigInteger>> A,
ArrayList<ArrayList<BigInteger>> 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<ArrayList<BigInteger>>();
}
/**
* matrix
* 1, 2, 3
* 4, 5, 6
*
* will be transformed to
* 1, 4
* 2, 5
* 3, 6
*
* @param list
* @return
*/
@Override
protected ArrayList<ArrayList<BigInteger>> 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<ArrayList<BigInteger>> resultC = new ArrayList<ArrayList<BigInteger>>();
ArrayList<ArrayList<BigInteger>> bTransformed = columnMajorOrderReversal(B);
for (int biA = this.startIndex;
biA <= this.endIndex;
++biA)
{
ArrayList<BigInteger> rowA = A.get(biA);
ArrayList<BigInteger> rowAxB = new ArrayList<BigInteger>();
ArrayList<BigInteger> rowCalculation = new ArrayList<BigInteger>();
for (int biB = 0;
biB < bTransformed.size();
++biB)
{
ArrayList<BigInteger> rowB = bTransformed.get(biB);
ArrayList<BigInteger> 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<ArrayList<ArrayList<BigInteger>>> firstHalf
= new MatricesMultiplication(this.startIndex, mid, A, B);
RecursiveTask<ArrayList<ArrayList<BigInteger>>> 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;
}
}
}
}

@ -15,5 +15,7 @@ public interface Matrix
{
ArrayList<ArrayList<BigInteger>> addParallel(ArrayList<ArrayList<BigInteger>> A, ArrayList<ArrayList<BigInteger>> B);
ArrayList<ArrayList<BigInteger>> multiplyParallel(ArrayList<ArrayList<BigInteger>> A, ArrayList<ArrayList<BigInteger>> B);
ArrayList<ArrayList<BigInteger>> subtractParallel(ArrayList<ArrayList<BigInteger>> A, ArrayList<ArrayList<BigInteger>> B);
}