diff --git a/src/main/java/seedu/badMaths/Command.java b/src/main/java/seedu/badMaths/Command.java index d39ab967d0..fb556e24d7 100644 --- a/src/main/java/seedu/badMaths/Command.java +++ b/src/main/java/seedu/badMaths/Command.java @@ -10,6 +10,7 @@ import seedu.badMaths.trigograph.TrigoGraph; import seedu.badMaths.ui.Ui; +import seedu.badMaths.matrix.Calculator; public class Command { @@ -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": @@ -58,6 +60,9 @@ public void executeCommand() { case "Help": HelpManual.readHelpManual(); break; + case "Matrix": + calculator.run(); + break; default: break; } diff --git a/src/main/java/seedu/badMaths/matrix/Calculate.java b/src/main/java/seedu/badMaths/matrix/Calculate.java index 0819189afc..3ff19a0b6a 100644 --- a/src/main/java/seedu/badMaths/matrix/Calculate.java +++ b/src/main/java/seedu/badMaths/matrix/Calculate.java @@ -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]; diff --git a/src/main/java/seedu/badMaths/matrix/Calculator.java b/src/main/java/seedu/badMaths/matrix/Calculator.java new file mode 100644 index 0000000000..79ae78f334 --- /dev/null +++ b/src/main/java/seedu/badMaths/matrix/Calculator.java @@ -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(); + } +} diff --git a/src/main/java/seedu/badMaths/matrix/Execute.java b/src/main/java/seedu/badMaths/matrix/Execute.java new file mode 100644 index 0000000000..a43c30f4cb --- /dev/null +++ b/src/main/java/seedu/badMaths/matrix/Execute.java @@ -0,0 +1,103 @@ +package seedu.badMaths.matrix; + +import java.util.HashMap; +import java.util.Map; + +public class Execute { + Map 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> "); + return in.nextLine(); + } +} diff --git a/src/main/java/seedu/badMaths/matrix/exception/ExceptionChecker.java b/src/main/java/seedu/badMaths/matrix/exception/ExceptionChecker.java new file mode 100644 index 0000000000..32e64490e0 --- /dev/null +++ b/src/main/java/seedu/badMaths/matrix/exception/ExceptionChecker.java @@ -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(); + } + } +} diff --git a/src/main/java/seedu/badMaths/matrix/exception/ShapeMismatchException.java b/src/main/java/seedu/badMaths/matrix/exception/ShapeMismatchException.java new file mode 100644 index 0000000000..6111945511 --- /dev/null +++ b/src/main/java/seedu/badMaths/matrix/exception/ShapeMismatchException.java @@ -0,0 +1,4 @@ +package seedu.badMaths.matrix.exception; + +public class ShapeMismatchException extends Exception { +}