From ddcfa526f7c46a36a7963a4af2ea899d7b4eae6d Mon Sep 17 00:00:00 2001 From: 0nandon Date: Fri, 17 Mar 2023 02:34:14 +0800 Subject: [PATCH 1/6] implement matrix calculator --- src/main/java/seedu/badMaths/Command.java | 5 ++ .../seedu/badMaths/matrix/Calculator.java | 21 +++++ .../java/seedu/badMaths/matrix/Execute.java | 89 +++++++++++++++++++ .../java/seedu/badMaths/matrix/Tensor2D.java | 10 +-- src/main/java/seedu/badMaths/matrix/Ui.java | 23 +++++ 5 files changed, 143 insertions(+), 5 deletions(-) create mode 100644 src/main/java/seedu/badMaths/matrix/Calculator.java create mode 100644 src/main/java/seedu/badMaths/matrix/Execute.java create mode 100644 src/main/java/seedu/badMaths/matrix/Ui.java 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/Calculator.java b/src/main/java/seedu/badMaths/matrix/Calculator.java new file mode 100644 index 0000000000..7be642f242 --- /dev/null +++ b/src/main/java/seedu/badMaths/matrix/Calculator.java @@ -0,0 +1,21 @@ +package seedu.badMaths.matrix; + +import java.util.HashMap; +import java.util.Map; + +public class Calculator { + public void run(){ + 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..44813e34ea --- /dev/null +++ b/src/main/java/seedu/badMaths/matrix/Execute.java @@ -0,0 +1,89 @@ +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; + } + + 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 t1 = executeTranspose(operator[0]); + Tensor2D t2 = executeTranspose(operator[1]); + + return c.mul(t1, t2); + } + + public Tensor2D executeTranspose(String command){ + String operator; + if(command.contains(".T")){ + operator = command.replace(".T", ""); + return cache.get(operator).t(); + }else{ + return cache.get(command); + } + } + + public Tensor2D parseMatrix(String command) { + int[][] tensor; + int rowNum, colNum; + + command = command.replace("[", ""); + command = command.replace("]", ""); + + String[] rows; + String[] column; + + rows = command.split(";"); + rowNum = rows.length; + colNum = rows[0].split(",").length; + + tensor = new int[rowNum][colNum]; + for(int i=0; i> "); + return in.nextLine(); + } +} From 50cc41078c107933840ecc3fe28808764358f1c6 Mon Sep 17 00:00:00 2001 From: 0nandon Date: Fri, 17 Mar 2023 08:06:54 +0800 Subject: [PATCH 2/6] add assertions and logging --- .../java/seedu/badMaths/matrix/Calculator.java | 9 +++++++-- src/main/java/seedu/badMaths/matrix/Execute.java | 16 +++++++++++++++- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/main/java/seedu/badMaths/matrix/Calculator.java b/src/main/java/seedu/badMaths/matrix/Calculator.java index 7be642f242..f3dc4232af 100644 --- a/src/main/java/seedu/badMaths/matrix/Calculator.java +++ b/src/main/java/seedu/badMaths/matrix/Calculator.java @@ -1,10 +1,15 @@ package seedu.badMaths.matrix; -import java.util.HashMap; -import java.util.Map; +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; diff --git a/src/main/java/seedu/badMaths/matrix/Execute.java b/src/main/java/seedu/badMaths/matrix/Execute.java index 44813e34ea..f55d9e0ae9 100644 --- a/src/main/java/seedu/badMaths/matrix/Execute.java +++ b/src/main/java/seedu/badMaths/matrix/Execute.java @@ -23,6 +23,8 @@ public void parse(String command){ assign = command; } + assert !assign.isEmpty(); + System.out.println(assign); System.out.println(cache.get(assign)); } @@ -38,6 +40,8 @@ public Tensor2D execute(String command) { result = executeTranspose(command); } + assert result != null; + return result; } @@ -46,18 +50,26 @@ public Tensor2D executeMul(String command){ String[] operator = command.split("\\*"); + Tensor2D result; Tensor2D t1 = executeTranspose(operator[0]); Tensor2D t2 = executeTranspose(operator[1]); - return c.mul(t1, t2); + result = c.mul(t1, t2); + assert result != null; + + 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); } } @@ -76,6 +88,8 @@ public Tensor2D parseMatrix(String command) { 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 Date: Fri, 17 Mar 2023 08:12:33 +0800 Subject: [PATCH 3/6] Add '{}' to if statement --- src/main/java/seedu/badMaths/matrix/Calculator.java | 3 ++- .../badMaths/matrix/exception/ShapeMismatchException.java | 4 ++++ 2 files changed, 6 insertions(+), 1 deletion(-) create mode 100644 src/main/java/seedu/badMaths/matrix/exception/ShapeMismatchException.java diff --git a/src/main/java/seedu/badMaths/matrix/Calculator.java b/src/main/java/seedu/badMaths/matrix/Calculator.java index f3dc4232af..79ae78f334 100644 --- a/src/main/java/seedu/badMaths/matrix/Calculator.java +++ b/src/main/java/seedu/badMaths/matrix/Calculator.java @@ -17,8 +17,9 @@ public void run(){ ui.printBeginning(); while (true) { command = ui.readCommand(); - if(command.equals("exit")) + if(command.equals("exit")) { break; + } e.parse(command); } ui.printEnding(); 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..5506ae8170 --- /dev/null +++ b/src/main/java/seedu/badMaths/matrix/exception/ShapeMismatchException.java @@ -0,0 +1,4 @@ +package seedu.badMaths.matrix.exception; + +public class ShapeMismatchException { +} From b4a4f20bf9389d1a92fca347dde9334e7221e23c Mon Sep 17 00:00:00 2001 From: 0nandon Date: Fri, 17 Mar 2023 08:16:35 +0800 Subject: [PATCH 4/6] Fix declaration issue --- src/main/java/seedu/badMaths/matrix/Execute.java | 3 ++- .../badMaths/matrix/exception/ExceptionChecker.java | 9 +++++++++ .../matrix/exception/ShapeMismatchException.java | 2 +- 3 files changed, 12 insertions(+), 2 deletions(-) create mode 100644 src/main/java/seedu/badMaths/matrix/exception/ExceptionChecker.java diff --git a/src/main/java/seedu/badMaths/matrix/Execute.java b/src/main/java/seedu/badMaths/matrix/Execute.java index f55d9e0ae9..09747fb7a1 100644 --- a/src/main/java/seedu/badMaths/matrix/Execute.java +++ b/src/main/java/seedu/badMaths/matrix/Execute.java @@ -76,7 +76,8 @@ public Tensor2D executeTranspose(String command){ public Tensor2D parseMatrix(String command) { int[][] tensor; - int rowNum, colNum; + int rowNum; + int colNum; command = command.replace("[", ""); command = command.replace("]", ""); 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..f8da31880c --- /dev/null +++ b/src/main/java/seedu/badMaths/matrix/exception/ExceptionChecker.java @@ -0,0 +1,9 @@ +package seedu.badMaths.matrix.exception; + +import seedu.badMaths.matrix.Tensor2D; + +public class ExceptionChecker { + public void checkShapeMismatch(Tensor2D t1, Tensor2D t2) throws ShapeMismatchException{ + + } +} diff --git a/src/main/java/seedu/badMaths/matrix/exception/ShapeMismatchException.java b/src/main/java/seedu/badMaths/matrix/exception/ShapeMismatchException.java index 5506ae8170..6111945511 100644 --- a/src/main/java/seedu/badMaths/matrix/exception/ShapeMismatchException.java +++ b/src/main/java/seedu/badMaths/matrix/exception/ShapeMismatchException.java @@ -1,4 +1,4 @@ package seedu.badMaths.matrix.exception; -public class ShapeMismatchException { +public class ShapeMismatchException extends Exception { } From a65f4a72f740221be1b09ab939acf16ccaf77bb7 Mon Sep 17 00:00:00 2001 From: 0nandon Date: Fri, 17 Mar 2023 08:36:18 +0800 Subject: [PATCH 5/6] Add exception --- .../java/seedu/badMaths/matrix/Calculate.java | 17 +++++++++++++++++ .../java/seedu/badMaths/matrix/Execute.java | 12 +++++++----- src/main/java/seedu/badMaths/matrix/Ui.java | 10 ++++++++++ .../matrix/exception/ExceptionChecker.java | 4 +++- 4 files changed, 37 insertions(+), 6 deletions(-) diff --git a/src/main/java/seedu/badMaths/matrix/Calculate.java b/src/main/java/seedu/badMaths/matrix/Calculate.java index 0819189afc..c36f359d9c 100644 --- a/src/main/java/seedu/badMaths/matrix/Calculate.java +++ b/src/main/java/seedu/badMaths/matrix/Calculate.java @@ -1,11 +1,28 @@ package seedu.badMaths.matrix; +import seedu.badMaths.matrix.exception.ExceptionChecker; +import seedu.badMaths.matrix.exception.ShapeMismatchException; + +import java.util.logging.Level; +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/Execute.java b/src/main/java/seedu/badMaths/matrix/Execute.java index 09747fb7a1..7a27988d6a 100644 --- a/src/main/java/seedu/badMaths/matrix/Execute.java +++ b/src/main/java/seedu/badMaths/matrix/Execute.java @@ -1,5 +1,8 @@ package seedu.badMaths.matrix; +import seedu.badMaths.matrix.exception.ExceptionChecker; +import seedu.badMaths.matrix.exception.ShapeMismatchException; + import java.util.HashMap; import java.util.Map; @@ -25,8 +28,10 @@ public void parse(String command){ assert !assign.isEmpty(); - System.out.println(assign); - System.out.println(cache.get(assign)); + if(cache.get(assign) != null) { + System.out.println(assign); + System.out.println(cache.get(assign)); + } } public Tensor2D execute(String command) { @@ -40,8 +45,6 @@ public Tensor2D execute(String command) { result = executeTranspose(command); } - assert result != null; - return result; } @@ -55,7 +58,6 @@ public Tensor2D executeMul(String command){ Tensor2D t2 = executeTranspose(operator[1]); result = c.mul(t1, t2); - assert result != null; return result; } diff --git a/src/main/java/seedu/badMaths/matrix/Ui.java b/src/main/java/seedu/badMaths/matrix/Ui.java index 34707d97dd..9a07808542 100644 --- a/src/main/java/seedu/badMaths/matrix/Ui.java +++ b/src/main/java/seedu/badMaths/matrix/Ui.java @@ -1,8 +1,12 @@ 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!"); @@ -14,6 +18,12 @@ public void printEnding(){ 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); diff --git a/src/main/java/seedu/badMaths/matrix/exception/ExceptionChecker.java b/src/main/java/seedu/badMaths/matrix/exception/ExceptionChecker.java index f8da31880c..32e64490e0 100644 --- a/src/main/java/seedu/badMaths/matrix/exception/ExceptionChecker.java +++ b/src/main/java/seedu/badMaths/matrix/exception/ExceptionChecker.java @@ -4,6 +4,8 @@ public class ExceptionChecker { public void checkShapeMismatch(Tensor2D t1, Tensor2D t2) throws ShapeMismatchException{ - + if(t1.column() != t2.row()){ + throw new ShapeMismatchException(); + } } } From 94960e4f60bc283d56c10d6d2c89b7007c1e5151 Mon Sep 17 00:00:00 2001 From: 0nandon Date: Fri, 17 Mar 2023 08:37:47 +0800 Subject: [PATCH 6/6] Remove unused import --- src/main/java/seedu/badMaths/matrix/Calculate.java | 1 - src/main/java/seedu/badMaths/matrix/Execute.java | 3 --- 2 files changed, 4 deletions(-) diff --git a/src/main/java/seedu/badMaths/matrix/Calculate.java b/src/main/java/seedu/badMaths/matrix/Calculate.java index c36f359d9c..3ff19a0b6a 100644 --- a/src/main/java/seedu/badMaths/matrix/Calculate.java +++ b/src/main/java/seedu/badMaths/matrix/Calculate.java @@ -3,7 +3,6 @@ import seedu.badMaths.matrix.exception.ExceptionChecker; import seedu.badMaths.matrix.exception.ShapeMismatchException; -import java.util.logging.Level; import java.util.logging.Logger; public class Calculate { diff --git a/src/main/java/seedu/badMaths/matrix/Execute.java b/src/main/java/seedu/badMaths/matrix/Execute.java index 7a27988d6a..a43c30f4cb 100644 --- a/src/main/java/seedu/badMaths/matrix/Execute.java +++ b/src/main/java/seedu/badMaths/matrix/Execute.java @@ -1,8 +1,5 @@ package seedu.badMaths.matrix; -import seedu.badMaths.matrix.exception.ExceptionChecker; -import seedu.badMaths.matrix.exception.ShapeMismatchException; - import java.util.HashMap; import java.util.Map;