it works!
This commit is contained in:
parent
f1c589e653
commit
a820852f22
@ -16,16 +16,15 @@ import java.util.ArrayList;
|
|||||||
*/
|
*/
|
||||||
@Named(value = "matrixBeanA")
|
@Named(value = "matrixBeanA")
|
||||||
@ViewScoped
|
@ViewScoped
|
||||||
public class MatrixBeanA implements Serializable
|
public class MatrixBeanA implements Serializable {
|
||||||
{
|
|
||||||
private int columns = 2;
|
private int columns = 2;
|
||||||
private int rows = 2;
|
private int rows = 2;
|
||||||
ArrayList<ArrayList<String>> matrix = new ArrayList<ArrayList<String>>();
|
ArrayList<ArrayList<String>> matrix = new ArrayList<ArrayList<String>>();
|
||||||
|
|
||||||
public MatrixBeanA()
|
public MatrixBeanA() {
|
||||||
{
|
|
||||||
matrix = new ArrayList<ArrayList<String>>();
|
matrix = new ArrayList<ArrayList<String>>();
|
||||||
BigInteger counter = BigInteger.ZERO;
|
BigInteger counter = BigInteger.ZERO;
|
||||||
for (int i = 0; i < columns; ++i) {
|
for (int i = 0; i < columns; ++i) {
|
||||||
ArrayList<String> row = new ArrayList<>();
|
ArrayList<String> row = new ArrayList<>();
|
||||||
for (int j = 0; j < rows; ++j) {
|
for (int j = 0; j < rows; ++j) {
|
||||||
@ -37,59 +36,58 @@ public class MatrixBeanA implements Serializable
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getColumns()
|
public int getColumns() {
|
||||||
{
|
|
||||||
return columns;
|
return columns;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setColumns(int columns)
|
public void setColumns(int columns) {
|
||||||
{
|
|
||||||
this.columns = columns;
|
this.columns = columns;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getRows()
|
public int getRows() {
|
||||||
{
|
|
||||||
return rows;
|
return rows;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setRows(int rows)
|
public void setRows(int rows) {
|
||||||
{
|
|
||||||
this.rows = rows;
|
this.rows = rows;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void listenForKeyUp(int i, int j, String input)
|
public void listenForKeyUp(int i, int j, String input) {
|
||||||
{
|
|
||||||
System.out.println("i=" + i);
|
System.out.println("i=" + i);
|
||||||
System.out.println("j=" + j);
|
System.out.println("j=" + j);
|
||||||
System.out.println("input=" + input);
|
System.out.println("input=" + input);
|
||||||
matrix.get(i).set(j, input);
|
matrix.get(i).set(j, input);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public ArrayList<ArrayList<String>> getMatrix()
|
public ArrayList<ArrayList<String>> getMatrix() {
|
||||||
{
|
|
||||||
return matrix;
|
return matrix;
|
||||||
}
|
}
|
||||||
public void changeRowsColumns() {
|
|
||||||
System.out.println("Columns:" + columns);
|
public void changeRowsColumns() {
|
||||||
System.out.println("Rows:" + rows);
|
System.out.println("Columns:" + columns);
|
||||||
ArrayList<ArrayList<String>> resizedMatrix = new ArrayList<>();
|
System.out.println("Rows:" + rows);
|
||||||
for (int i = 0; i < rows; ++i) {
|
ArrayList<ArrayList<String>> resizedMatrix = new ArrayList<>();
|
||||||
|
|
||||||
|
// Create a new resized matrix with the updated number of rows and columns
|
||||||
|
for (int i = 0; i < rows; ++i) {
|
||||||
ArrayList<String> row = new ArrayList<>();
|
ArrayList<String> row = new ArrayList<>();
|
||||||
for (int j = 0; j < columns; ++j) {
|
for (int j = 0; j < columns; ++j) {
|
||||||
row.add("1");
|
row.add("1");
|
||||||
}
|
}
|
||||||
resizedMatrix.add(row);
|
resizedMatrix.add(row);
|
||||||
}
|
}
|
||||||
// add the data from the old matrix to the new one
|
|
||||||
for (int i = 0; i < columns; ++i) {
|
// Copy data from the old matrix to the new resized matrix
|
||||||
for (int j = 0; j < rows; ++j) {
|
for (int i = 0; i < rows; ++i) {
|
||||||
if (i < matrix.size() && j < matrix.get(i).size()) {
|
for (int j = 0; j < columns; ++j) {
|
||||||
resizedMatrix.get(i).set(j, matrix.get(i).get(j));
|
if (i < matrix.size() && j < matrix.get(i).size()) {
|
||||||
}
|
resizedMatrix.get(i).set(j, matrix.get(i).get(j));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// overwrite the old matrix
|
}
|
||||||
matrix = resizedMatrix;
|
|
||||||
|
// Overwrite the old matrix with the resized matrix
|
||||||
|
matrix = resizedMatrix;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -13,12 +13,11 @@ import java.util.ArrayList;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @author A. V. Markou
|
* @author A. V. Markou
|
||||||
*/
|
*/
|
||||||
@Named(value = "matrixOperations")
|
@Named(value = "matrixOperations")
|
||||||
@ViewScoped
|
@ViewScoped
|
||||||
public class MatrixOperations implements Serializable
|
public class MatrixOperations implements Serializable {
|
||||||
{
|
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
MatrixBeanA matrixA;
|
MatrixBeanA matrixA;
|
||||||
@ -27,81 +26,91 @@ public class MatrixOperations implements Serializable
|
|||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
MatrixBeanC matrixC;
|
MatrixBeanC matrixC;
|
||||||
|
|
||||||
edu.slcc.asdv.bl.Matrices matrixManipulator = new edu.slcc.asdv.bl.Matrices();
|
edu.slcc.asdv.bl.Matrices matrixManipulator = new edu.slcc.asdv.bl.Matrices();
|
||||||
|
|
||||||
public String add() {
|
public String add() {
|
||||||
|
|
||||||
ArrayList<ArrayList<String>> a = matrixA.getMatrix();
|
ArrayList<ArrayList<String>> a = matrixA.getMatrix();
|
||||||
ArrayList<ArrayList<String>> b = matrixB.getMatrix();
|
ArrayList<ArrayList<String>> b = matrixB.getMatrix();
|
||||||
ArrayList<ArrayList<String>> c = new ArrayList<ArrayList<String>>();
|
ArrayList<ArrayList<String>> c = new ArrayList<ArrayList<String>>();
|
||||||
|
|
||||||
for ( int i=0; i < a.size(); ++i)
|
for (int i = 0; i < a.size(); ++i) {
|
||||||
{
|
|
||||||
ArrayList<String> row = new ArrayList<>();
|
ArrayList<String> row = new ArrayList<>();
|
||||||
for ( int j=0; j < a.size(); ++j)
|
for (int j = 0; j < a.size(); ++j) {
|
||||||
{
|
Integer result = Integer.parseInt(a.get(i).get(j))
|
||||||
Integer result = Integer.parseInt ( a.get(i).get(j))
|
+ Integer.parseInt(b.get(i).get(j));
|
||||||
+
|
|
||||||
Integer.parseInt ( b.get(i).get(j));
|
row.add(result.toString());
|
||||||
|
}
|
||||||
row.add( result.toString() );
|
c.add(row);
|
||||||
}
|
System.out.println(row);
|
||||||
c.add(row);
|
|
||||||
System.out.println(row);
|
|
||||||
}
|
}
|
||||||
matrixC.setMatrix( c );
|
matrixC.setMatrix(c);
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
public String multiply() {
|
public String multiply() {
|
||||||
ArrayList<ArrayList<BigInteger>> a = convertToBigInteger(matrixA.getMatrix());
|
ArrayList<ArrayList<BigInteger>> a = convertToBigInteger(matrixA.getMatrix());
|
||||||
ArrayList<ArrayList<BigInteger>> b = convertToBigInteger(matrixA.getMatrix());
|
ArrayList<ArrayList<BigInteger>> b = convertToBigInteger(matrixA.getMatrix());
|
||||||
ArrayList<ArrayList<String>> c = new ArrayList<ArrayList<String>>();
|
ArrayList<ArrayList<String>> c = new ArrayList<ArrayList<String>>();
|
||||||
c = convertToString(matrixManipulator.multiplyParallel(a, b));
|
|
||||||
printArray(c);
|
for (int i = 0; i < a.size(); i++) {
|
||||||
matrixC.setMatrix( c );
|
ArrayList<String> rowResult = new ArrayList<>();
|
||||||
return "";}
|
for (int j = 0; j < b.get(i).size(); j++) {
|
||||||
|
BigInteger sum = BigInteger.ZERO;
|
||||||
public String subtract() {
|
for (int k = 0; k < a.get(0).size(); k++) {
|
||||||
|
sum = sum.add(a.get(i).get(k).multiply(b.get(k).get(j)));
|
||||||
ArrayList<ArrayList<String>> a = matrixA.getMatrix();
|
}
|
||||||
ArrayList<ArrayList<String>> b = matrixB.getMatrix();
|
rowResult.add(sum.toString());
|
||||||
ArrayList<ArrayList<String>> c = new ArrayList<ArrayList<String>>();
|
}
|
||||||
|
c.add(rowResult);
|
||||||
for ( int i=0; i < a.size(); ++i)
|
|
||||||
{
|
|
||||||
ArrayList<String> row = new ArrayList<>();
|
|
||||||
for ( int j=0; j < a.size(); ++j)
|
|
||||||
{
|
|
||||||
Integer result = Integer.parseInt ( a.get(i).get(j))
|
|
||||||
-
|
|
||||||
Integer.parseInt ( b.get(i).get(j));
|
|
||||||
|
|
||||||
row.add( result.toString() );
|
|
||||||
}
|
|
||||||
c.add(row);
|
|
||||||
System.out.println(row);
|
|
||||||
}
|
}
|
||||||
matrixC.setMatrix( c );
|
|
||||||
return "";
|
printArray(c);
|
||||||
|
matrixC.setMatrix(c);
|
||||||
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String subtract() {
|
||||||
public MatrixBeanC getMatrixC(){return this.matrixC;}
|
|
||||||
|
|
||||||
public MatrixBeanA getMatrixA(){return matrixA;}
|
ArrayList<ArrayList<String>> a = matrixA.getMatrix();
|
||||||
|
ArrayList<ArrayList<String>> b = matrixB.getMatrix();
|
||||||
|
ArrayList<ArrayList<String>> c = new ArrayList<ArrayList<String>>();
|
||||||
|
|
||||||
|
for (int i = 0; i < a.size(); ++i) {
|
||||||
|
ArrayList<String> row = new ArrayList<>();
|
||||||
|
for (int j = 0; j < a.size(); ++j) {
|
||||||
|
Integer result = Integer.parseInt(a.get(i).get(j))
|
||||||
|
- Integer.parseInt(b.get(i).get(j));
|
||||||
|
|
||||||
|
row.add(result.toString());
|
||||||
|
}
|
||||||
|
c.add(row);
|
||||||
|
System.out.println(row);
|
||||||
|
}
|
||||||
|
matrixC.setMatrix(c);
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
public MatrixBeanC getMatrixC() {
|
||||||
|
return this.matrixC;
|
||||||
|
}
|
||||||
|
|
||||||
|
public MatrixBeanA getMatrixA() {
|
||||||
|
return matrixA;
|
||||||
|
}
|
||||||
|
|
||||||
|
public MatrixBeanB getMatrixB() {
|
||||||
|
return matrixB;
|
||||||
|
}
|
||||||
|
|
||||||
public MatrixBeanB getMatrixB(){return matrixB;}
|
|
||||||
|
|
||||||
/*
|
|
||||||
|
|
||||||
public ArrayList<ArrayList<BigInteger>> convertToBigInteger(ArrayList<ArrayList<String>> matrix) {
|
public ArrayList<ArrayList<BigInteger>> convertToBigInteger(ArrayList<ArrayList<String>> matrix) {
|
||||||
ArrayList<ArrayList<BigInteger>> bigInt2DArray = new ArrayList<>();
|
ArrayList<ArrayList<BigInteger>> bigInt2DArray = new ArrayList<>();
|
||||||
|
|
||||||
for (int i = 0; i < matrix.size(); i++) {
|
for (int i = 0; i < matrix.size(); i++) {
|
||||||
ArrayList<BigInteger> columnArray = new ArrayList<BigInteger>();
|
ArrayList<BigInteger> columnArray = new ArrayList<BigInteger>();
|
||||||
for (int j = 0; j < matrix.get(i).size(); j++) {
|
for (int j = 0; j < matrix.get(i).size(); j++) {
|
||||||
String cellValue = matrix.get(i).get(j);
|
String cellValue = matrix.get(i).get(j);
|
||||||
columnArray.add(new BigInteger(cellValue));
|
columnArray.add(new BigInteger(cellValue));
|
||||||
}
|
}
|
||||||
@ -110,7 +119,7 @@ public class MatrixOperations implements Serializable
|
|||||||
|
|
||||||
return bigInt2DArray;
|
return bigInt2DArray;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ArrayList<ArrayList<String>> convertToString(ArrayList<ArrayList<BigInteger>> matrix) {
|
public ArrayList<ArrayList<String>> convertToString(ArrayList<ArrayList<BigInteger>> matrix) {
|
||||||
int numRows = matrix.size();
|
int numRows = matrix.size();
|
||||||
int numCols = matrix.get(0).size();
|
int numCols = matrix.get(0).size();
|
||||||
@ -125,7 +134,7 @@ public class MatrixOperations implements Serializable
|
|||||||
}
|
}
|
||||||
return string2DArray;
|
return string2DArray;
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param matrix
|
* @param matrix
|
||||||
@ -143,7 +152,7 @@ public class MatrixOperations implements Serializable
|
|||||||
System.out.println(output);
|
System.out.println(output);
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String printSingleArray(ArrayList<String> matrix) {
|
public String printSingleArray(ArrayList<String> matrix) {
|
||||||
String output = "\t{";
|
String output = "\t{";
|
||||||
for (int i = 0; i < matrix.size(); ++i) {
|
for (int i = 0; i < matrix.size(); ++i) {
|
||||||
@ -155,6 +164,5 @@ public class MatrixOperations implements Serializable
|
|||||||
output += "}";
|
output += "}";
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -75,7 +75,24 @@ public class MenuBar implements Serializable
|
|||||||
PrimeFaces.current().ajax().update(idsC);
|
PrimeFaces.current().ajax().update(idsC);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean areMatricesTheSameSize() {
|
||||||
|
int matrixAColumns = matrixOperations.getMatrixA().getColumns();
|
||||||
|
int matrixBColumns = matrixOperations.getMatrixB().getColumns();
|
||||||
|
int matrixCColumns = matrixOperations.getMatrixC().getColumns();
|
||||||
|
|
||||||
|
int matrixARows = matrixOperations.getMatrixA().getRows();
|
||||||
|
int matrixBRows = matrixOperations.getMatrixB().getRows();
|
||||||
|
int matrixCRows = matrixOperations.getMatrixC().getRows();
|
||||||
|
|
||||||
|
boolean matrixAEqual = (matrixAColumns == matrixARows);
|
||||||
|
boolean matrixBEqual = (matrixBColumns == matrixBRows);
|
||||||
|
boolean matrixCEqual = (matrixCColumns == matrixCRows);
|
||||||
|
|
||||||
|
if (matrixAEqual && matrixBEqual && matrixCEqual) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -17,7 +17,7 @@
|
|||||||
<p:submenu id ="submenu_matrices"
|
<p:submenu id ="submenu_matrices"
|
||||||
label="Matrix" icon="pi pi-table">
|
label="Matrix" icon="pi pi-table">
|
||||||
<p:menuitem title="This does addition"
|
<p:menuitem title="This does addition"
|
||||||
id ="menuitem_add" value="Add" disabled="false"
|
id ="menuitem_add" value="Add" disabled="#{!menuBar.areMatricesTheSameSize()}"
|
||||||
action="#{menuBar.add()}"
|
action="#{menuBar.add()}"
|
||||||
icon="pi pi-plus" update="id_message "/>
|
icon="pi pi-plus" update="id_message "/>
|
||||||
|
|
||||||
@ -28,7 +28,7 @@
|
|||||||
update="id_message"/>
|
update="id_message"/>
|
||||||
|
|
||||||
<p:menuitem title="Subtraction" id ="menuitem_subtract" value="Subtract"
|
<p:menuitem title="Subtraction" id ="menuitem_subtract" value="Subtract"
|
||||||
action="#{menuBar.subtract}" disabled="false"
|
action="#{menuBar.subtract}" disabled="#{!menuBar.areMatricesTheSameSize()}"
|
||||||
icon="pi pi-minus" update="id_message"/>
|
icon="pi pi-minus" update="id_message"/>
|
||||||
</p:submenu>
|
</p:submenu>
|
||||||
</p:menubar>
|
</p:menubar>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user