forked from nus-cs2113-AY2223S2/tp
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #55 from 0nandon/tP-ver1
Implementing matrix calculator, and add assertions, logging.
- Loading branch information
Showing
8 changed files
with
204 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/seedu/badMaths/matrix/exception/ExceptionChecker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
src/main/java/seedu/badMaths/matrix/exception/ShapeMismatchException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package seedu.badMaths.matrix.exception; | ||
|
||
public class ShapeMismatchException extends Exception { | ||
} |