Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reflect tp review for the matrix part #240

Merged
merged 5 commits into from
Apr 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/DeveloperGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -428,8 +428,9 @@ Below is an example usage scenario and how the `Matrix` feature behaves at each

When user enters an input:
```
Matrix. [1,2;3,4] .* [5,6;7,8]
Matrix [1,2;3,4] .* [5,6;7,8]
```

#### Calculator class

step 1. `run()` function in `Calculator.class` firstly get `toDo` as the argument which is `[1,2;3,4] .* [5,6;7,8]` in the above example.
Expand Down
82 changes: 79 additions & 3 deletions docs/UserGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ This is the vertical shift: 0.0
### 2) Matrix Calculation: `Matrix`
This feature accepts matrix equation and outputs calculation result.

Format: `Matrix. [Matrix] [operator] [Matrix]`
Format: `Matrix [Matrix] [operator] [Matrix]`

* `[Matrix]` is the 2 dimensional matrix with integer elements. When you declare the matrix, you should follow the matrix format below:
```
Expand All @@ -129,10 +129,86 @@ Format: `Matrix. [Matrix] [operator] [Matrix]`
* `+` : matrix addition
* `-` : matrix subtraction

Example input:
#### Examples for the matrix multiplication

Input:
```
Matrix [1,2;3,4] .* [4,5;6,7]
```

Expected output:
```
Result.
1. shape : 2 x 2
2. value :
0) 16 19
1) 36 43
```

#### Examples for the matrix multiplication with transpose

Input:
```
Matrix [1,2;3,4] .* [4,5;6,7].T
```
Matrix. [1,2;3,4] .* [4,5;6,7]

output:
```
Result.
1. shape : 2 x 2
2. value :
0) 14 20
1) 32 46
```

#### Examples for the matrix element wise product

Input:
```
Matrix [1,2;3,4] * [4,5;6,7]
```

output:
```
Result.
1. shape : 2 x 2
2. value :
0) 4 10
1) 18 28
```

#### Examples for the matrix addition

Input:
```
Matrix [1,2;3,4] + [4,5;6,7]
```

output:
```
Result.
1. shape : 2 x 2
2. value :
0) 5 7
1) 9 11
```

#### Examples for the matrix subtraction

Input:
```
Matrix [1,2;3,4] - [4,5;6,7]
```

output:
```
Result.
1. shape : 2 x 2
2. value :
0) -3 -3
1) -3 -3
```

### 3) Store Notes: `Store`
* Adds a new item to the Notes list.
* Format: `Store <item description>`
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/seedu/badmaths/CommandHistory.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ public void displayHistory() {
for (String command : historyCommand) {
System.out.println(command);
}
System.out.println();

}
}
34 changes: 17 additions & 17 deletions src/main/java/seedu/badmaths/matrix/Calculate.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class Calculate {
ExceptionPrinter ep = new ExceptionPrinter();

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

Expand All @@ -22,25 +22,25 @@ public Tensor2D mul(Tensor2D t1, Tensor2D t2){
Tensor2D t2T = t2.t();
int[][] output = new int[t1Shape.row][t2Shape.column];

for(int i=0; i<t1Shape.row; i++){
for(int j=0; j<t2Shape.column; j++){
for(int i = 0; i < t1Shape.row; i++) {
for(int j = 0; j < t2Shape.column; j++) {
output[i][j] = 0;

for(int k=0; k<t1Shape.column; k++){
for(int k = 0; k < t1Shape.column; k++) {
output[i][j] += t1.get(i, k) * t2T.get(j, k);
}
}
}

return new Tensor2D(output);
}catch (ShapeMismatchException e){
} catch (ShapeMismatchException e) {
ep.printShapeMismatchExceptionLog();
return null;
}
}

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

Expand All @@ -49,21 +49,21 @@ public Tensor2D dot(Tensor2D t1, Tensor2D t2){

int[][] output = new int[t1Shape.row][t1Shape.column];

for(int i=0; i<t1Shape.row; i++){
for(int j=0; j<t1Shape.column; j++){
for(int i = 0; i < t1Shape.row; i++) {
for(int j = 0; j < t1Shape.column; j++) {
output[i][j] = t1.get(i, j) * t2.get(i, j);
}
}

return new Tensor2D(output);
}catch (ShapeMismatchException e){
} catch (ShapeMismatchException e) {
ep.printShapeMismatchExceptionLog();
return null;
}
}

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

Expand All @@ -72,20 +72,20 @@ public Tensor2D add(Tensor2D t1, Tensor2D t2){

int[][] output = new int[t1Shape.row][t1Shape.column];

for(int i=0; i<t1Shape.row; i++){
for(int j=0; j<t1Shape.column; j++){
for(int i = 0; i < t1Shape.row; i++) {
for(int j = 0; j < t1Shape.column; j++) {
output[i][j] = t1.get(i, j) + t2.get(i, j);
}
}

return new Tensor2D(output);
}catch (ShapeMismatchException e){
} catch (ShapeMismatchException e) {
ep.printShapeMismatchExceptionLog();
return null;
}
}

public Tensor2D sub(Tensor2D t1, Tensor2D t2){
public Tensor2D sub(Tensor2D t1, Tensor2D t2) {
Shape t1Shape = t1.shape();
Shape t2Shape = t2.shape();

Expand All @@ -94,14 +94,14 @@ public Tensor2D sub(Tensor2D t1, Tensor2D t2){

int[][] output = new int[t1Shape.row][t1Shape.column];

for(int i=0; i<t1Shape.row; i++){
for(int j=0; j<t1Shape.column; j++){
for(int i = 0; i < t1Shape.row; i++) {
for(int j = 0; j < t1Shape.column; j++) {
output[i][j] = t1.get(i, j) - t2.get(i, j);
}
}

return new Tensor2D(output);
}catch (ShapeMismatchException e){
} catch (ShapeMismatchException e) {
ep.printShapeMismatchExceptionLog();
return null;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/seedu/badmaths/matrix/Calculator.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
public class Calculator {
Logger logger = Logger.getLogger("matrix");

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

Ui ui = new Ui();
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/seedu/badmaths/matrix/Execute.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package seedu.badmaths.matrix;

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

String[] operator = command.split("\\.\\*");
Expand All @@ -16,7 +16,7 @@ public Tensor2D executeMul(String command){
return result;
}

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

String[] operator = command.split("\\*");
Expand All @@ -31,7 +31,7 @@ public Tensor2D executeDot(String command){
return result;
}

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

String[] operator = command.split("\\+");
Expand All @@ -46,7 +46,7 @@ public Tensor2D executeAdd(String command){
return result;
}

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

String[] operator = command.split("-");
Expand All @@ -61,12 +61,12 @@ public Tensor2D executeSub(String command){
return result;
}

public Tensor2D executeTranspose(String command){
public Tensor2D executeTranspose(String command) {
String operator;
if(command.contains(".T")){
if(command.contains(".T")) {
operator = command.replace(".T", "");
return Parser.parseMatrix(operator).t();
}else{
} else {
return Parser.parseMatrix(command);
}
}
Expand Down
36 changes: 18 additions & 18 deletions src/main/java/seedu/badmaths/matrix/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ public Tensor2D parse(String command) {
try {
check.checkUnknownOperator(command);

if(command.contains("+")){
type = CalType.ADD;
}else if(command.contains("-")){
type = CalType.SUB;
}else if(command.contains(".*")){
type = CalType.MUL;
}else if(command.contains("*")){
type = CalType.DOT;
}else{
if(command.contains("+")) {
type = CalType.ADDITION;
} else if(command.contains("-")) {
type = CalType.SUBTRACTION;
} else if(command.contains(".*")) {
type = CalType.MULTIPLICATION;
} else if(command.contains("*")) {
type = CalType.ELEMENT_WISE_DOT_PRODUCT;
} else {
type = CalType.UNKNOWN;
}

Expand All @@ -36,25 +36,25 @@ public Tensor2D parse(String command) {
Calculate c = new Calculate();
Execute e = new Execute();

switch(type){
case ADD:
switch(type) {
case ADDITION:
result = e.executeAdd(command);
break;
case SUB:
case SUBTRACTION:
result = e.executeSub(command);
break;
case MUL:
case MULTIPLICATION:
result = e.executeMul(command);
break;
case DOT:
case ELEMENT_WISE_DOT_PRODUCT:
result = e.executeDot(command);
break;
default:
break;
}

return result;
}catch (UnknownOperatorException e){
} catch (UnknownOperatorException e) {
ep.printUnknownOperatorExceptionLog();
return null;
}
Expand All @@ -78,8 +78,8 @@ public static Tensor2D parseMatrix(String command) {
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++){
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]);
}
Expand All @@ -89,6 +89,6 @@ public static Tensor2D parseMatrix(String command) {
}

enum CalType {
ADD, SUB, MUL, DOT, UNKNOWN
ADDITION, SUBTRACTION, MULTIPLICATION, ELEMENT_WISE_DOT_PRODUCT, UNKNOWN
}
}
2 changes: 1 addition & 1 deletion src/main/java/seedu/badmaths/matrix/Shape.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public class Shape {
protected int row;
protected int column;

public Shape(int row, int column){
public Shape(int row, int column) {
this.row = row;
this.column = column;
}
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/seedu/badmaths/matrix/Tensor2D.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public Tensor2D t(){

int[][] transposeTensor = new int[column][row];

for(int i=0; i<column; i++){
for(int j=0; j<row; j++){
for(int i = 0; i < column; i++) {
for(int j = 0; j < row; j++) {
transposeTensor[i][j] = tensor[j][i];
}
}
Expand All @@ -53,9 +53,9 @@ public String toString() {
str.append(" 1. shape : ").append(row).append(" x ").append(column).append("\n");
str.append(" 2. value : \n");

for(int i=0; i<row; i++){
for(int i = 0; i < row; i++) {
str.append(" ").append(i).append(") ");
for(int j=0; j<column; j++){
for(int j = 0; j < column; j++){
str.append(tensor[i][j]).append(" ");
}
str.append("\n");
Expand Down
Loading