Skip to content

Commit

Permalink
Added a parser to solve an equation of matrices
Browse files Browse the repository at this point in the history
-Implemented a node based recursive parser (modified from CogPar
#GitHub)
-Added small methods in the Edit class
-Nodes are extensive and scalable
-Currently supports 7 tokens
  • Loading branch information
Paras DPain committed Oct 16, 2015
1 parent a57b0c1 commit c50ee2f
Show file tree
Hide file tree
Showing 37 changed files with 1,475 additions and 143 deletions.
Binary file added Grammar_plans.docx
Binary file not shown.
Binary file modified bin/engine/Tester.class
Binary file not shown.
Binary file modified bin/framework/Edit.class
Binary file not shown.
Binary file modified bin/framework/Matrix.class
Binary file not shown.
Binary file modified bin/framework/Operator.class
Binary file not shown.
Binary file added bin/nodes/AdditionExpressionNode.class
Binary file not shown.
Binary file added bin/nodes/ExpressionNode.class
Binary file not shown.
Binary file added bin/nodes/ExpressionNodeVisitor.class
Binary file not shown.
Binary file added bin/nodes/MatrixExpressionNode.class
Binary file not shown.
Binary file added bin/nodes/MultiplicationExpressionNode.class
Binary file not shown.
Binary file added bin/nodes/SequenceExpressionNode$Term.class
Binary file not shown.
Binary file added bin/nodes/SequenceExpressionNode.class
Binary file not shown.
Binary file added bin/parser/EvaluationException.class
Binary file not shown.
Binary file added bin/parser/Parser.class
Binary file not shown.
Binary file added bin/parser/ParserException.class
Binary file not shown.
Binary file added bin/parser/SetVariable.class
Binary file not shown.
Binary file added bin/parser/Token.class
Binary file not shown.
Binary file added bin/parser/Tokenizer$TokenInfo.class
Binary file not shown.
Binary file added bin/parser/Tokenizer.class
Binary file not shown.
14 changes: 10 additions & 4 deletions src/engine/Tester.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,20 @@
public class Tester {

public static void main(String[] args) {


displayMatrix(Operator.Solve("A*B+C-D"));

/*
Scanner scanner = new Scanner (System.in);
Matrix m = new Matrix(8);
Matrix m = inputMatrix(scanner);
displayMatrix(m);
displayMatrix(test_Submatrix(m));
Matrix n = inputMatrix(scanner);
displayMatrix(n);
test_Operations(m, n);
scanner.close();

*/
}

private static Matrix test_Submatrix(Matrix m) {
Expand Down
237 changes: 127 additions & 110 deletions src/framework/Edit.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,122 +7,139 @@

public class Edit {

private static Matrix error = new Matrix(0,0);

// Multiplies the matrix with a scalar value
public static Matrix ScalarProduct(Matrix m, double s) {
// Pass -1 to change the matrix's sign
if(m.getValidity()) {

int row = m.getRowSize();
int column = m.getColumnSize();
double[][] grid = m.getGrid();
double[][] newGrid = new double[row][column];

// Simple cyclic increment
// Cycle through the row for each column
for(int ro = 0; ro < row; ro++) {
for (int col = 0; col < column; col++) {
newGrid[ro][col] = s * grid[ro][col];
}// --Column loop end
}// --Row loop end

Matrix result = new Matrix(newGrid);
return result;
}
return error;
}

// Swap rows with columns and update the element list
public static Matrix Transpose(Matrix m) {
if(m.getValidity()) {

int row = m.getRowSize();
int column = m.getColumnSize();
double[][] grid = m.getGrid();
double[][] newGrid = new double[column][row];

// Simple cyclic increment
// Cycle through the row for each column
for(int ro = 0; ro < row; ro++) {
for (int col = 0; col < column; col++) {
newGrid[col][ro] = grid[ro][col];
}// --Column loop end
}// --Row loop end

Matrix result = new Matrix(newGrid);
return result;
}
return error;
}

// Removes/Retains any selected number of rows and columns from the Matrix
public static Matrix Submatrix(Matrix m, List<Integer> listOfRows, List<Integer> listOfColumns, boolean removeThese) {
/*Pseudo
* If data is to be removed
private static final Matrix error = new Matrix(0, 0);

// Performs positive whole number power on a square Matrix
public static Matrix ToPower(Matrix m, int e){
// e must be over 1, else implies n/d forms
if(e >= 1 && m.IsSquare()){
for(int i = 0; i < e; i++){
m = Operator.Multiply(m, m);
}
} else{
return error;
}
return m;
}

// Multiplies the matrix with a scalar value
public static Matrix ScalarProduct(Matrix m, double s) {
// Pass -1 to change the matrix's sign
if (m.getValidity()) {

int row = m.getRowSize();
int column = m.getColumnSize();
double[][] grid = m.getGrid();
double[][] newGrid = new double[row][column];

// Simple cyclic increment
// Cycle through the row for each column
for (int ro = 0; ro < row; ro++) {
for (int col = 0; col < column; col++) {
newGrid[ro][col] = s * grid[ro][col];
}// --Column loop end
}// --Row loop end

return new Matrix(newGrid);
}
return error;
}

// Swap rows with columns and update the element list
public static Matrix Transpose(Matrix m) {
if (m.getValidity()) {

int row = m.getRowSize();
int column = m.getColumnSize();
double[][] grid = m.getGrid();
double[][] newGrid = new double[column][row];

// Simple cyclic increment
// Cycle through the row for each column
for (int ro = 0; ro < row; ro++) {
for (int col = 0; col < column; col++) {
newGrid[col][ro] = grid[ro][col];
}// --Column loop end
}// --Row loop end

return new Matrix(newGrid);
}
return error;
}

// Removes/Retains any selected number of rows and columns from the Matrix
public static Matrix Submatrix(Matrix m, List<Integer> listOfRows, List<Integer> listOfColumns, boolean removeThese) {
/*Pseudo
* If data is to be removed
* for all values in the grid
* if(i is NOT EQUAL to any value in the listofRows && same for j)
* if(i is NOT EQUAL to any value in the listofRows && same for j)
* listOf<double> buffer.add(grid[i][j])
* else nothing;
* else data is to be preserved
* for all values in the grid
* if(i is EQUAL to any value in the listofRows || same for j)
* if(i is EQUAL to any value in the listofRows || same for j)
* listOf<double> buffer.add(grid[i][j])
* else nothing;
*/

// Removed row/columns will be the ones above the sent values, arrays start from 0
// OutOfBound values in the row/column list will return garbled Matrix due to reduced size and zero removal
if (m.getValidity() && m.getRowSize() > listOfRows.size() && m.getColumnSize() > listOfColumns.size()) {
List<Double> buffer = new ArrayList<Double>();

int row = m.getRowSize();
int column = m.getColumnSize();
double[][] grid = m.getGrid();
int newR = row-listOfRows.size();
int newC = column-listOfColumns.size();
double[][] newGrid = new double[newR][newC];

// Data filter
if(removeThese) {
// Simple cyclic increment
// Cycle through the row for each column
for(int ro = 0; ro < row; ro++) {
for (int col = 0; col < column; col++) {
if(!listOfRows.contains(ro) && !listOfColumns.contains(col)) {
buffer.add(grid[ro][col]);
}
}// --Column loop end
}// --Row loop end

} else {
// Simple cyclic increment
// Cycle through the row for each column
for(int ro = 0; ro < row; ro++) {
for (int col = 0; col < column; col++) {
if(listOfRows.contains(ro) || listOfColumns.contains(col)) {
buffer.add(grid[ro][col]);
}
}// --Column loop end
}// --Row loop end
}

// Grid reconstruction
int index = 0;

// Simple cyclic increment
// Cycle through the row for each column
for(int ro = 0; ro < newR; ro++) {
for (int col = 0; col < newC; col++) {
newGrid[ro][col] = buffer.get(index);
index++;
}// --Column loop end
}// --Row loop end

Matrix s = new Matrix(newGrid);
return s;
}
return error;
}


// Removed row/columns will be the ones above the sent values, arrays start from 0
// OutOfBound values in the row/column list will return garbled Matrix due to reduced size and zero removal
if (m.getValidity() && m.getRowSize() > listOfRows.size() && m.getColumnSize() > listOfColumns.size()) {
List<Double> buffer = new ArrayList<>();

int row = m.getRowSize();
int column = m.getColumnSize();
double[][] grid = m.getGrid();
int newR, newC;

if (removeThese) {
newR = row - listOfRows.size();
newC = column - listOfColumns.size();
} else {
newR = listOfRows.size();
newC = listOfColumns.size();
}
double[][] newGrid = new double[newR][newC];

// Data filter
if (removeThese) {
// Simple cyclic increment
// Cycle through the row for each column
for (int ro = 0; ro < row; ro++) {
for (int col = 0; col < column; col++) {
if (!listOfRows.contains(ro) && !listOfColumns.contains(col)) {
buffer.add(grid[ro][col]);
}
}// --Column loop end
}// --Row loop end

} else {
// Simple cyclic increment
// Cycle through the row for each column
for (int ro = 0; ro < row; ro++) {
for (int col = 0; col < column; col++) {
if (listOfRows.contains(ro) || listOfColumns.contains(col)) {
buffer.add(grid[ro][col]);
}
}// --Column loop end
}// --Row loop end
}

// Grid reconstruction
int index = 0;

// Simple cyclic increment
// Cycle through the row for each column
for (int ro = 0; ro < newR; ro++) {
for (int col = 0; col < newC; col++) {
newGrid[ro][col] = buffer.get(index);
index++;
}// --Column loop end
}// --Row loop end

return new Matrix(newGrid);
}
return error;
}
}

Loading

0 comments on commit c50ee2f

Please sign in to comment.