it works!
This commit is contained in:
parent
f1c589e653
commit
a820852f22
@ -16,16 +16,15 @@ import java.util.ArrayList;
|
||||
*/
|
||||
@Named(value = "matrixBeanA")
|
||||
@ViewScoped
|
||||
public class MatrixBeanA implements Serializable
|
||||
{
|
||||
public class MatrixBeanA implements Serializable {
|
||||
|
||||
private int columns = 2;
|
||||
private int rows = 2;
|
||||
ArrayList<ArrayList<String>> matrix = new ArrayList<ArrayList<String>>();
|
||||
|
||||
public MatrixBeanA()
|
||||
{
|
||||
public MatrixBeanA() {
|
||||
matrix = new ArrayList<ArrayList<String>>();
|
||||
BigInteger counter = BigInteger.ZERO;
|
||||
BigInteger counter = BigInteger.ZERO;
|
||||
for (int i = 0; i < columns; ++i) {
|
||||
ArrayList<String> row = new ArrayList<>();
|
||||
for (int j = 0; j < rows; ++j) {
|
||||
@ -37,28 +36,23 @@ public class MatrixBeanA implements Serializable
|
||||
}
|
||||
}
|
||||
|
||||
public int getColumns()
|
||||
{
|
||||
public int getColumns() {
|
||||
return columns;
|
||||
}
|
||||
|
||||
public void setColumns(int columns)
|
||||
{
|
||||
public void setColumns(int columns) {
|
||||
this.columns = columns;
|
||||
}
|
||||
|
||||
public int getRows()
|
||||
{
|
||||
public int getRows() {
|
||||
return rows;
|
||||
}
|
||||
|
||||
public void setRows(int rows)
|
||||
{
|
||||
public void setRows(int 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("j=" + j);
|
||||
System.out.println("input=" + input);
|
||||
@ -66,30 +60,34 @@ public class MatrixBeanA implements Serializable
|
||||
|
||||
}
|
||||
|
||||
public ArrayList<ArrayList<String>> getMatrix()
|
||||
{
|
||||
public ArrayList<ArrayList<String>> getMatrix() {
|
||||
return matrix;
|
||||
}
|
||||
public void changeRowsColumns() {
|
||||
System.out.println("Columns:" + columns);
|
||||
System.out.println("Rows:" + rows);
|
||||
ArrayList<ArrayList<String>> resizedMatrix = new ArrayList<>();
|
||||
for (int i = 0; i < rows; ++i) {
|
||||
|
||||
public void changeRowsColumns() {
|
||||
System.out.println("Columns:" + columns);
|
||||
System.out.println("Rows:" + rows);
|
||||
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<>();
|
||||
for (int j = 0; j < columns; ++j) {
|
||||
row.add("1");
|
||||
}
|
||||
resizedMatrix.add(row);
|
||||
}
|
||||
// add the data from the old matrix to the new one
|
||||
for (int i = 0; i < columns; ++i) {
|
||||
for (int j = 0; j < rows; ++j) {
|
||||
if (i < matrix.size() && j < matrix.get(i).size()) {
|
||||
resizedMatrix.get(i).set(j, matrix.get(i).get(j));
|
||||
}
|
||||
|
||||
// Copy data from the old matrix to the new resized matrix
|
||||
for (int i = 0; i < rows; ++i) {
|
||||
for (int j = 0; j < columns; ++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")
|
||||
@ViewScoped
|
||||
public class MatrixOperations implements Serializable
|
||||
{
|
||||
public class MatrixOperations implements Serializable {
|
||||
|
||||
@Inject
|
||||
MatrixBeanA matrixA;
|
||||
@ -32,76 +31,86 @@ public class MatrixOperations implements Serializable
|
||||
|
||||
public String add() {
|
||||
|
||||
ArrayList<ArrayList<String>> a = matrixA.getMatrix();
|
||||
ArrayList<ArrayList<String>> b = matrixB.getMatrix();
|
||||
ArrayList<ArrayList<String>> c = new ArrayList<ArrayList<String>>();
|
||||
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)
|
||||
{
|
||||
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));
|
||||
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);
|
||||
row.add(result.toString());
|
||||
}
|
||||
c.add(row);
|
||||
System.out.println(row);
|
||||
}
|
||||
matrixC.setMatrix( c );
|
||||
return "";
|
||||
matrixC.setMatrix(c);
|
||||
return "";
|
||||
}
|
||||
|
||||
public String multiply() {
|
||||
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.multiplyParallel(a, b));
|
||||
printArray(c);
|
||||
matrixC.setMatrix( c );
|
||||
return "";}
|
||||
ArrayList<ArrayList<BigInteger>> a = convertToBigInteger(matrixA.getMatrix());
|
||||
ArrayList<ArrayList<BigInteger>> b = convertToBigInteger(matrixA.getMatrix());
|
||||
ArrayList<ArrayList<String>> c = new ArrayList<ArrayList<String>>();
|
||||
|
||||
public String subtract() {
|
||||
|
||||
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);
|
||||
for (int i = 0; i < a.size(); i++) {
|
||||
ArrayList<String> rowResult = new ArrayList<>();
|
||||
for (int j = 0; j < b.get(i).size(); j++) {
|
||||
BigInteger sum = BigInteger.ZERO;
|
||||
for (int k = 0; k < a.get(0).size(); k++) {
|
||||
sum = sum.add(a.get(i).get(k).multiply(b.get(k).get(j)));
|
||||
}
|
||||
rowResult.add(sum.toString());
|
||||
}
|
||||
c.add(rowResult);
|
||||
}
|
||||
matrixC.setMatrix( c );
|
||||
return "";
|
||||
|
||||
printArray(c);
|
||||
matrixC.setMatrix(c);
|
||||
return "";
|
||||
}
|
||||
|
||||
public String subtract() {
|
||||
|
||||
public MatrixBeanC getMatrixC(){return this.matrixC;}
|
||||
ArrayList<ArrayList<String>> a = matrixA.getMatrix();
|
||||
ArrayList<ArrayList<String>> b = matrixB.getMatrix();
|
||||
ArrayList<ArrayList<String>> c = new ArrayList<ArrayList<String>>();
|
||||
|
||||
public MatrixBeanA getMatrixA(){return matrixA;}
|
||||
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));
|
||||
|
||||
public MatrixBeanB getMatrixB(){return matrixB;}
|
||||
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 ArrayList<ArrayList<BigInteger>> convertToBigInteger(ArrayList<ArrayList<String>> matrix) {
|
||||
ArrayList<ArrayList<BigInteger>> bigInt2DArray = new ArrayList<>();
|
||||
|
||||
for (int i = 0; i < matrix.size(); i++) {
|
||||
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);
|
||||
columnArray.add(new BigInteger(cellValue));
|
||||
}
|
||||
@ -125,7 +134,7 @@ public class MatrixOperations implements Serializable
|
||||
}
|
||||
return string2DArray;
|
||||
}
|
||||
*/
|
||||
|
||||
/**
|
||||
*
|
||||
* @param matrix
|
||||
@ -156,5 +165,4 @@ public class MatrixOperations implements Serializable
|
||||
return output;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -75,7 +75,24 @@ public class MenuBar implements Serializable
|
||||
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"
|
||||
label="Matrix" icon="pi pi-table">
|
||||
<p:menuitem title="This does addition"
|
||||
id ="menuitem_add" value="Add" disabled="false"
|
||||
id ="menuitem_add" value="Add" disabled="#{!menuBar.areMatricesTheSameSize()}"
|
||||
action="#{menuBar.add()}"
|
||||
icon="pi pi-plus" update="id_message "/>
|
||||
|
||||
@ -28,7 +28,7 @@
|
||||
update="id_message"/>
|
||||
|
||||
<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"/>
|
||||
</p:submenu>
|
||||
</p:menubar>
|
||||
|
Loading…
Reference in New Issue
Block a user