Skip to content

Commit

Permalink
Merge pull request #55 from 0nandon/tP-ver1
Browse files Browse the repository at this point in the history
Implementing matrix calculator, and add assertions, logging.
  • Loading branch information
WilsonLee2000 authored Mar 17, 2023
2 parents 2bd2c72 + 94960e4 commit a8a0d82
Show file tree
Hide file tree
Showing 8 changed files with 204 additions and 5 deletions.
5 changes: 5 additions & 0 deletions src/main/java/seedu/badMaths/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import seedu.badMaths.trigograph.TrigoGraph;
import seedu.badMaths.ui.Ui;
import seedu.badMaths.matrix.Calculator;

public class Command {

Expand Down Expand Up @@ -40,6 +41,7 @@ public void setToDo(String toDo){

public void executeCommand() {
TrigoGraph trigoGraph = new TrigoGraph(toDo);
Calculator calculator = new Calculator();

switch (command) {
case "Bye":
Expand All @@ -58,6 +60,9 @@ public void executeCommand() {
case "Help":
HelpManual.readHelpManual();
break;
case "Matrix":
calculator.run();
break;
default:
break;
}
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/seedu/badMaths/matrix/Calculate.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
package seedu.badMaths.matrix;

import seedu.badMaths.matrix.exception.ExceptionChecker;
import seedu.badMaths.matrix.exception.ShapeMismatchException;

import java.util.logging.Logger;

public class Calculate {
ExceptionChecker check = new ExceptionChecker();
Logger logger = Logger.getLogger("Exception");
Ui ui = new Ui();

// TODO : Implement exception related to tensor's shape mismatch
public Tensor2D mul(Tensor2D t1, Tensor2D t2){
Shape t1Shape = t1.shape();
Shape t2Shape = t2.shape();

try{
check.checkShapeMismatch(t1, t2);
}catch (ShapeMismatchException e){
ui.printShapeMismatchExceptionLog();
return null;
}

Tensor2D t2T = t2.t();
int[][] output = new int[t1Shape.row][t2Shape.column];

Expand Down
27 changes: 27 additions & 0 deletions src/main/java/seedu/badMaths/matrix/Calculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package seedu.badMaths.matrix;

import java.util.logging.Level;
import java.util.logging.Logger;

public class Calculator {

Logger logger = Logger.getLogger("matrix");

public void run(){
logger.log(Level.INFO, "Start to open calculator.\n");

Ui ui = new Ui();
Execute e = new Execute();
String command;

ui.printBeginning();
while (true) {
command = ui.readCommand();
if(command.equals("exit")) {
break;
}
e.parse(command);
}
ui.printEnding();
}
}
103 changes: 103 additions & 0 deletions src/main/java/seedu/badMaths/matrix/Execute.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package seedu.badMaths.matrix;

import java.util.HashMap;
import java.util.Map;

public class Execute {
Map<String, Tensor2D> cache = new HashMap<>(500);

public void parse(String command){
int assignIndex;
String assign = "";
String calculation = "";

command = command.replace(" ", "");

if(command.contains("=")) {
assignIndex = command.indexOf("=");
assign = command.substring(0, assignIndex);
calculation = command.substring(assignIndex + 1);

cache.put(assign, execute(calculation));
}else if(cache.containsKey(command)){
assign = command;
}

assert !assign.isEmpty();

if(cache.get(assign) != null) {
System.out.println(assign);
System.out.println(cache.get(assign));
}
}

public Tensor2D execute(String command) {
Tensor2D result;

if(command.contains("*")){
result = executeMul(command);
}else if(command.contains("[") && command.contains("]")){
result = parseMatrix(command);
}else{
result = executeTranspose(command);
}

return result;
}

public Tensor2D executeMul(String command){
Calculate c = new Calculate();

String[] operator = command.split("\\*");

Tensor2D result;
Tensor2D t1 = executeTranspose(operator[0]);
Tensor2D t2 = executeTranspose(operator[1]);

result = c.mul(t1, t2);

return result;
}

public Tensor2D executeTranspose(String command){
String operator;
if(command.contains(".T")){
operator = command.replace(".T", "");

assert cache.get(operator) != null;
return cache.get(operator).t();
}else{

assert cache.get(command) != null;
return cache.get(command);
}
}

public Tensor2D parseMatrix(String command) {
int[][] tensor;
int rowNum;
int colNum;

command = command.replace("[", "");
command = command.replace("]", "");

String[] rows;
String[] column;

rows = command.split(";");
rowNum = rows.length;
colNum = rows[0].split(",").length;

assert rowNum == 1 || colNum == rows[1].split(",").length;

tensor = new int[rowNum][colNum];
for(int i=0; i<rowNum; i++){
for(int j=0; j<colNum; j++){
column = rows[i].split(",");
tensor[i][j] = Integer.parseInt(column[j]);
}
}

return new Tensor2D(tensor);
}
}
10 changes: 5 additions & 5 deletions src/main/java/seedu/badMaths/matrix/Tensor2D.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

public class Tensor2D extends Tensor {
protected int[][] tensor;
Shape shape;
protected Shape shape;

public Tensor2D(int[][] tensor){
this.tensor = tensor;
Expand Down Expand Up @@ -49,12 +49,12 @@ public String toString() {
int row = shape.row;
int column = shape.column;

StringBuilder str = new StringBuilder("[Printing 2D tensor configuration]\n");
str.append("1. shape : ").append(row).append(" x ").append(column).append("\n");
str.append("2. value : \n");
StringBuilder str = new StringBuilder("");
str.append("\t1. shape : ").append(row).append(" x ").append(column).append("\n");
str.append("\t2. value : \n");

for(int i=0; i<row; i++){
str.append("\t").append(i).append(") ");
str.append("\t\t").append(i).append(") ");
for(int j=0; j<column; j++){
str.append(tensor[i][j]).append(" ");
}
Expand Down
33 changes: 33 additions & 0 deletions src/main/java/seedu/badMaths/matrix/Ui.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package seedu.badMaths.matrix;

import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Ui {
Logger logger = Logger.getLogger("matrix");

public void printBeginning(){
System.out.println("========= MATRIX CALCULATOR =========");
System.out.println("You enter into the matrix calculator!");
System.out.println("You can type 'info' to learn how to use this calculator.\n");
}
public void printEnding(){
System.out.println("Exit from the calculator!");
}
public void printInfo(){
System.out.println("");
}
public void printShapeMismatchExceptionLog(){
String logMessage = "";
logMessage += "Shape mismatch between t1 and t2 occur : cannot execute matrix multiplication.\n";
logMessage += "Click ENTER to resume your calculation.";
logger.log(Level.WARNING, logMessage);
}

public static String readCommand(){
Scanner in = new Scanner(System.in);
System.out.print(">> ");
return in.nextLine();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package seedu.badMaths.matrix.exception;

import seedu.badMaths.matrix.Tensor2D;

public class ExceptionChecker {
public void checkShapeMismatch(Tensor2D t1, Tensor2D t2) throws ShapeMismatchException{
if(t1.column() != t2.row()){
throw new ShapeMismatchException();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package seedu.badMaths.matrix.exception;

public class ShapeMismatchException extends Exception {
}

0 comments on commit a8a0d82

Please sign in to comment.