Skip to content

Commit 813d346

Browse files
author
Paras DPain
committed
Initial Commit
Backend (with the testing class)
1 parent 077e1d2 commit 813d346

File tree

9 files changed

+380
-0
lines changed

9 files changed

+380
-0
lines changed

.classpath

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
5+
<classpathentry kind="output" path="bin"/>
6+
</classpath>

.project

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>ProjectMatrix</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>

.settings/org.eclipse.jdt.core.prefs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
4+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5+
org.eclipse.jdt.core.compiler.compliance=1.8
6+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
8+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11+
org.eclipse.jdt.core.compiler.source=1.8

bin/engine/Tester.class

3.6 KB
Binary file not shown.

bin/framework/Matrix.class

2.16 KB
Binary file not shown.

bin/framework/Operator.class

2.38 KB
Binary file not shown.

src/engine/Tester.java

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package engine;
2+
3+
import java.util.Scanner;
4+
5+
import framework.Matrix;
6+
import framework.Operator;
7+
8+
public class Tester {
9+
10+
public static void main(String[] args) {
11+
12+
Scanner scanner = new Scanner (System.in);
13+
Matrix m;
14+
Matrix n;
15+
16+
m = inputMatrix(scanner);
17+
n = inputMatrix(scanner);
18+
19+
displayMatrix(m);
20+
displayMatrix(n);
21+
22+
test_Operations(m, n);
23+
24+
scanner.close();
25+
26+
}
27+
28+
private static void test_Operations(Matrix m, Matrix n) {
29+
Operator test = new Operator();
30+
31+
System.out.println("SUM");
32+
displayMatrix(test.Plus(m,n));
33+
34+
System.out.println("DIFFERENCE");
35+
displayMatrix(test.Minus(m,n));
36+
37+
System.out.println("PRODUCT");
38+
displayMatrix(test.Multiply(m,n));
39+
40+
System.out.println("QUOTIENT");
41+
displayMatrix(test.Divide(m,n));
42+
}
43+
44+
@SuppressWarnings("unused")
45+
private static void test_Transpose(Matrix m) {
46+
System.out.println("And it's transpose is: ");
47+
m.Transpose();
48+
}
49+
50+
@SuppressWarnings("unused")
51+
private static void test_ChangeSize(Scanner scanner, Matrix m) {
52+
// Change grid size
53+
System.out.println("Enter the new row size: ");
54+
int newRow = Integer.parseInt(scanner.nextLine());
55+
System.out.println("Enter the new column size: ");
56+
int newColumn = Integer.parseInt(scanner.nextLine());
57+
58+
m.ChangeSize(newRow, newColumn);
59+
}
60+
61+
// Input and Initialize matrix instance
62+
private static Matrix inputMatrix(Scanner scanner) {
63+
Matrix m;
64+
// Initialize matrix object
65+
System.out.println("Enter the number of rows: ");
66+
int row = Integer.parseInt(scanner.nextLine());
67+
System.out.println("Enter the number of columns: ");
68+
int column = Integer.parseInt(scanner.nextLine());
69+
70+
m = new Matrix(row, column);
71+
72+
// Initialize matrix elements
73+
// User enters each row as a string until the last column is reached
74+
// String is split into array and plugged in the matrix after parsing
75+
for (int ro=0; ro<row; ro++) {
76+
System.out.println("Enter " + column + " values for this column, separated by commas: ");
77+
String input = scanner.nextLine();
78+
79+
String[] thisRow = null;
80+
try {
81+
thisRow = input.split(",", column);
82+
} catch(IndexOutOfBoundsException e) {
83+
System.out.println(e.getMessage());
84+
}
85+
86+
for (int col=0; col<column; col++) {
87+
double value = Double.parseDouble(thisRow[col]);
88+
m.AddElement(ro, col, value);
89+
}// --Row loop end
90+
}// --Column loop end
91+
return m;
92+
}
93+
94+
// Display object instance
95+
private static void displayMatrix(Matrix m) {
96+
double[][] myMatrix;
97+
int row = m.getRow();
98+
int column = m.getColumn();
99+
100+
myMatrix = m.getGrid();
101+
System.out.println("Your matrix is: ");
102+
for (int i=0; i<row; i++) {
103+
for (int j=0; j<column; j++) {
104+
System.out.print(String.valueOf(myMatrix[i][j]) + " ");
105+
}// --Column loop end
106+
107+
System.out.println();
108+
}// --Row loop end
109+
}
110+
111+
}

src/framework/Matrix.java

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package framework;
2+
3+
// Add error checks (use false returns in boolean functions)
4+
public class Matrix {
5+
6+
public Matrix(int r, int c) {
7+
// Constructor
8+
if(r > 0 && c > 0) {
9+
setRow(r);
10+
setColumn(c);
11+
double[][] matrix = new double[row][column];
12+
setGrid(matrix);
13+
isValid = true;
14+
} else {
15+
setRow(0);
16+
setColumn(0);
17+
double[][] matrix = new double[row][column];
18+
setGrid(matrix);
19+
isValid = false;
20+
}
21+
22+
}
23+
24+
// Error check
25+
public boolean isValid;
26+
27+
// Public property Row
28+
private int row;
29+
public int getRow() {
30+
return row;
31+
}
32+
public void setRow(int value) {
33+
row = value;
34+
}
35+
36+
// Public property Column
37+
private int column;
38+
public int getColumn() {
39+
return column;
40+
}
41+
public void setColumn(int value) {
42+
column = value;
43+
}
44+
45+
// Public property Grid
46+
private double[][] grid;
47+
public double[][] getGrid() {
48+
return grid;
49+
}
50+
public void setGrid(double[][] value) {
51+
grid = value;
52+
53+
}
54+
55+
public boolean AddElement(int posX, int posY, double value) {
56+
// Add new element to the grid at (x,y)
57+
58+
double[][] matrix = getGrid();
59+
matrix[posX][posY] = value;
60+
setGrid(matrix);
61+
return true;
62+
}
63+
64+
public boolean RemoveElement(int x, int y) {
65+
// Removes element at (x,y) in the grid
66+
67+
double[][] matrix = getGrid();
68+
matrix[x][y] = 0;
69+
setGrid(matrix);
70+
return true;
71+
}
72+
73+
public boolean ChangeSize(int newR, int newC) {
74+
// Changes row, column and grid size
75+
int oldCol = getColumn();
76+
int oldRow = getRow();
77+
double[][] oldMatrix = getGrid();
78+
double[][] newMatrix = new double[newR][newC];
79+
80+
// Simple cyclic increment
81+
// Cycle through the row for each column
82+
for(int ro = 0; ro < oldRow; ro++) {
83+
for (int col = 0; col < oldCol; col++) {
84+
// To catch changes when r or c or alternatively great n lower
85+
try {
86+
newMatrix[ro][col] = oldMatrix[ro][col];
87+
} catch(IndexOutOfBoundsException e) {
88+
continue;
89+
}
90+
}// --Column loop end
91+
}// --Row loop end
92+
setRow(newR);
93+
setColumn(newC);
94+
setGrid(newMatrix);
95+
return true;
96+
}
97+
98+
public boolean Transpose() {
99+
// Swaps rows with columns and updates the elements list
100+
double[][] Matrix = getGrid();
101+
double[][] newMatrix = new double[column][row];
102+
103+
// Simple cyclic increment
104+
// Cycle through the row for each column
105+
for(int ro = 0; ro < row; ro++) {
106+
for (int col = 0; col < column; col++) {
107+
newMatrix[col][ro] = Matrix[ro][col];
108+
}// --Column loop end
109+
}// --Row loop end
110+
setGrid(newMatrix);
111+
return true;
112+
}
113+
}

src/framework/Operator.java

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package framework;
2+
3+
// index: 1-Plus; 2-Minus; 3-Multiply; 4-Divide;
4+
public class Operator {
5+
6+
// private Matrix result;
7+
// private Matrix error = null;
8+
9+
public Matrix Plus(Matrix leftMatrix, Matrix rightMatrix) {
10+
// local data for easier use
11+
int lRow = leftMatrix.getRow();
12+
int lColumn = leftMatrix.getColumn();
13+
int rRow = leftMatrix.getRow();
14+
int rColumn = leftMatrix.getColumn();
15+
16+
// Addition/Subtraction can only be performed on similar matrices
17+
if(lRow == rRow && lColumn == rColumn) {
18+
Matrix result = new Matrix(lRow, lColumn);
19+
20+
// Matrix grid extraction for the operation
21+
double[][] newGrid = new double[lRow][lColumn];
22+
double[][] leftGrid = leftMatrix.getGrid();
23+
double[][] rightGrid = rightMatrix.getGrid();
24+
25+
// Simple cyclic increment
26+
for(int ro = 0; ro < lRow; ro++) {
27+
for (int col = 0; col < lColumn; col++) {
28+
newGrid[ro][col] = leftGrid[ro][col] + rightGrid[ro][col];
29+
}// --Column loop end
30+
}// --Row loop end
31+
32+
result.setGrid(newGrid);
33+
return result;
34+
}
35+
return null;
36+
}
37+
38+
public Matrix Minus(Matrix leftMatrix, Matrix rightMatrix) {
39+
// local data for easier use
40+
int lRow = leftMatrix.getRow();
41+
int lColumn = leftMatrix.getColumn();
42+
int rRow = leftMatrix.getRow();
43+
int rColumn = leftMatrix.getColumn();
44+
45+
// Addition/Subtraction can only be performed on similar matrices
46+
if(lRow == rRow && lColumn == rColumn) {
47+
Matrix result = new Matrix(lRow, lColumn);
48+
49+
// Matrix grid extraction for the operation
50+
double[][] newGrid = new double[lRow][lColumn];
51+
double[][] leftGrid = leftMatrix.getGrid();
52+
double[][] rightGrid = rightMatrix.getGrid();
53+
54+
// Simple cyclic increment
55+
for(int ro = 0; ro < lRow; ro++) {
56+
for (int col = 0; col < lColumn; col++) {
57+
newGrid[ro][col] = leftGrid[ro][col] - rightGrid[ro][col];
58+
}// --Column loop end
59+
}// --Row loop end
60+
61+
result.setGrid(newGrid);
62+
return result;
63+
}
64+
return null;
65+
}
66+
67+
public Matrix Multiply(Matrix leftMatrix, Matrix rightMatrix) {
68+
int lRow = leftMatrix.getRow();
69+
int lColumn = leftMatrix.getColumn();
70+
int rRow = leftMatrix.getRow();
71+
int rColumn = leftMatrix.getColumn();
72+
73+
if(lColumn == rRow) {
74+
Matrix result = new Matrix(lColumn, rRow);
75+
76+
// Matrix grid extraction for the operation
77+
double[][] newGrid = new double[lRow][rColumn];
78+
double[][] leftGrid = leftMatrix.getGrid();
79+
double[][] rightGrid = rightMatrix.getGrid();
80+
81+
// Extended cyclic increment
82+
for(int ro = 0; ro < lRow; ro++) {
83+
for (int col = 0; col < rColumn; col++) {
84+
for (int x = 0; x<lRow; x++) {
85+
newGrid[ro][col] += leftGrid[ro][x] * rightGrid[x][col];
86+
}// --X loop end
87+
}// --Column loop end
88+
}// --Row loop end
89+
90+
result.setGrid(newGrid);
91+
return result;
92+
}
93+
return null;
94+
}
95+
96+
public Matrix Divide(Matrix leftMatrix, Matrix rightMatrix) {
97+
// local data for easier use
98+
int lRow = leftMatrix.getRow();
99+
int lColumn = leftMatrix.getColumn();
100+
int rRow = leftMatrix.getRow();
101+
int rColumn = leftMatrix.getColumn();
102+
103+
// Addition/Subtraction can only be performed on similar matrices
104+
if(lRow == rRow && lColumn == rColumn) {
105+
// Matrix grid extraction for the operation
106+
double[][] newGrid = new double[lRow][lColumn];
107+
double[][] rightGrid = rightMatrix.getGrid();
108+
109+
// Simple cyclic increment
110+
for(int ro = 0; ro < lRow; ro++) {
111+
for (int col = 0; col < lColumn; col++) {
112+
newGrid[ro][col] = 1/rightGrid[ro][col];
113+
}// --Column loop end
114+
}// --Row loop end
115+
116+
// Forwards operation after preparing the rightMatrix
117+
rightMatrix.setGrid(newGrid);
118+
return Multiply(leftMatrix, rightMatrix);
119+
}
120+
return null;
121+
}
122+
}

0 commit comments

Comments
 (0)