Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
Backend (with the testing class)
  • Loading branch information
Paras DPain committed Apr 7, 2015
1 parent 077e1d2 commit 813d346
Show file tree
Hide file tree
Showing 9 changed files with 380 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="output" path="bin"/>
</classpath>
17 changes: 17 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>ProjectMatrix</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
11 changes: 11 additions & 0 deletions .settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.8
Binary file added bin/engine/Tester.class
Binary file not shown.
Binary file added bin/framework/Matrix.class
Binary file not shown.
Binary file added bin/framework/Operator.class
Binary file not shown.
111 changes: 111 additions & 0 deletions src/engine/Tester.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package engine;

import java.util.Scanner;

import framework.Matrix;
import framework.Operator;

public class Tester {

public static void main(String[] args) {

Scanner scanner = new Scanner (System.in);
Matrix m;
Matrix n;

m = inputMatrix(scanner);
n = inputMatrix(scanner);

displayMatrix(m);
displayMatrix(n);

test_Operations(m, n);

scanner.close();

}

private static void test_Operations(Matrix m, Matrix n) {
Operator test = new Operator();

System.out.println("SUM");
displayMatrix(test.Plus(m,n));

System.out.println("DIFFERENCE");
displayMatrix(test.Minus(m,n));

System.out.println("PRODUCT");
displayMatrix(test.Multiply(m,n));

System.out.println("QUOTIENT");
displayMatrix(test.Divide(m,n));
}

@SuppressWarnings("unused")
private static void test_Transpose(Matrix m) {
System.out.println("And it's transpose is: ");
m.Transpose();
}

@SuppressWarnings("unused")
private static void test_ChangeSize(Scanner scanner, Matrix m) {
// Change grid size
System.out.println("Enter the new row size: ");
int newRow = Integer.parseInt(scanner.nextLine());
System.out.println("Enter the new column size: ");
int newColumn = Integer.parseInt(scanner.nextLine());

m.ChangeSize(newRow, newColumn);
}

// Input and Initialize matrix instance
private static Matrix inputMatrix(Scanner scanner) {
Matrix m;
// Initialize matrix object
System.out.println("Enter the number of rows: ");
int row = Integer.parseInt(scanner.nextLine());
System.out.println("Enter the number of columns: ");
int column = Integer.parseInt(scanner.nextLine());

m = new Matrix(row, column);

// Initialize matrix elements
// User enters each row as a string until the last column is reached
// String is split into array and plugged in the matrix after parsing
for (int ro=0; ro<row; ro++) {
System.out.println("Enter " + column + " values for this column, separated by commas: ");
String input = scanner.nextLine();

String[] thisRow = null;
try {
thisRow = input.split(",", column);
} catch(IndexOutOfBoundsException e) {
System.out.println(e.getMessage());
}

for (int col=0; col<column; col++) {
double value = Double.parseDouble(thisRow[col]);
m.AddElement(ro, col, value);
}// --Row loop end
}// --Column loop end
return m;
}

// Display object instance
private static void displayMatrix(Matrix m) {
double[][] myMatrix;
int row = m.getRow();
int column = m.getColumn();

myMatrix = m.getGrid();
System.out.println("Your matrix is: ");
for (int i=0; i<row; i++) {
for (int j=0; j<column; j++) {
System.out.print(String.valueOf(myMatrix[i][j]) + " ");
}// --Column loop end

System.out.println();
}// --Row loop end
}

}
113 changes: 113 additions & 0 deletions src/framework/Matrix.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package framework;

// Add error checks (use false returns in boolean functions)
public class Matrix {

public Matrix(int r, int c) {
// Constructor
if(r > 0 && c > 0) {
setRow(r);
setColumn(c);
double[][] matrix = new double[row][column];
setGrid(matrix);
isValid = true;
} else {
setRow(0);
setColumn(0);
double[][] matrix = new double[row][column];
setGrid(matrix);
isValid = false;
}

}

// Error check
public boolean isValid;

// Public property Row
private int row;
public int getRow() {
return row;
}
public void setRow(int value) {
row = value;
}

// Public property Column
private int column;
public int getColumn() {
return column;
}
public void setColumn(int value) {
column = value;
}

// Public property Grid
private double[][] grid;
public double[][] getGrid() {
return grid;
}
public void setGrid(double[][] value) {
grid = value;

}

public boolean AddElement(int posX, int posY, double value) {
// Add new element to the grid at (x,y)

double[][] matrix = getGrid();
matrix[posX][posY] = value;
setGrid(matrix);
return true;
}

public boolean RemoveElement(int x, int y) {
// Removes element at (x,y) in the grid

double[][] matrix = getGrid();
matrix[x][y] = 0;
setGrid(matrix);
return true;
}

public boolean ChangeSize(int newR, int newC) {
// Changes row, column and grid size
int oldCol = getColumn();
int oldRow = getRow();
double[][] oldMatrix = getGrid();
double[][] newMatrix = new double[newR][newC];

// Simple cyclic increment
// Cycle through the row for each column
for(int ro = 0; ro < oldRow; ro++) {
for (int col = 0; col < oldCol; col++) {
// To catch changes when r or c or alternatively great n lower
try {
newMatrix[ro][col] = oldMatrix[ro][col];
} catch(IndexOutOfBoundsException e) {
continue;
}
}// --Column loop end
}// --Row loop end
setRow(newR);
setColumn(newC);
setGrid(newMatrix);
return true;
}

public boolean Transpose() {
// Swaps rows with columns and updates the elements list
double[][] Matrix = getGrid();
double[][] newMatrix = new double[column][row];

// Simple cyclic increment
// Cycle through the row for each column
for(int ro = 0; ro < row; ro++) {
for (int col = 0; col < column; col++) {
newMatrix[col][ro] = Matrix[ro][col];
}// --Column loop end
}// --Row loop end
setGrid(newMatrix);
return true;
}
}
122 changes: 122 additions & 0 deletions src/framework/Operator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package framework;

// index: 1-Plus; 2-Minus; 3-Multiply; 4-Divide;
public class Operator {

// private Matrix result;
// private Matrix error = null;

public Matrix Plus(Matrix leftMatrix, Matrix rightMatrix) {
// local data for easier use
int lRow = leftMatrix.getRow();
int lColumn = leftMatrix.getColumn();
int rRow = leftMatrix.getRow();
int rColumn = leftMatrix.getColumn();

// Addition/Subtraction can only be performed on similar matrices
if(lRow == rRow && lColumn == rColumn) {
Matrix result = new Matrix(lRow, lColumn);

// Matrix grid extraction for the operation
double[][] newGrid = new double[lRow][lColumn];
double[][] leftGrid = leftMatrix.getGrid();
double[][] rightGrid = rightMatrix.getGrid();

// Simple cyclic increment
for(int ro = 0; ro < lRow; ro++) {
for (int col = 0; col < lColumn; col++) {
newGrid[ro][col] = leftGrid[ro][col] + rightGrid[ro][col];
}// --Column loop end
}// --Row loop end

result.setGrid(newGrid);
return result;
}
return null;
}

public Matrix Minus(Matrix leftMatrix, Matrix rightMatrix) {
// local data for easier use
int lRow = leftMatrix.getRow();
int lColumn = leftMatrix.getColumn();
int rRow = leftMatrix.getRow();
int rColumn = leftMatrix.getColumn();

// Addition/Subtraction can only be performed on similar matrices
if(lRow == rRow && lColumn == rColumn) {
Matrix result = new Matrix(lRow, lColumn);

// Matrix grid extraction for the operation
double[][] newGrid = new double[lRow][lColumn];
double[][] leftGrid = leftMatrix.getGrid();
double[][] rightGrid = rightMatrix.getGrid();

// Simple cyclic increment
for(int ro = 0; ro < lRow; ro++) {
for (int col = 0; col < lColumn; col++) {
newGrid[ro][col] = leftGrid[ro][col] - rightGrid[ro][col];
}// --Column loop end
}// --Row loop end

result.setGrid(newGrid);
return result;
}
return null;
}

public Matrix Multiply(Matrix leftMatrix, Matrix rightMatrix) {
int lRow = leftMatrix.getRow();
int lColumn = leftMatrix.getColumn();
int rRow = leftMatrix.getRow();
int rColumn = leftMatrix.getColumn();

if(lColumn == rRow) {
Matrix result = new Matrix(lColumn, rRow);

// Matrix grid extraction for the operation
double[][] newGrid = new double[lRow][rColumn];
double[][] leftGrid = leftMatrix.getGrid();
double[][] rightGrid = rightMatrix.getGrid();

// Extended cyclic increment
for(int ro = 0; ro < lRow; ro++) {
for (int col = 0; col < rColumn; col++) {
for (int x = 0; x<lRow; x++) {
newGrid[ro][col] += leftGrid[ro][x] * rightGrid[x][col];
}// --X loop end
}// --Column loop end
}// --Row loop end

result.setGrid(newGrid);
return result;
}
return null;
}

public Matrix Divide(Matrix leftMatrix, Matrix rightMatrix) {
// local data for easier use
int lRow = leftMatrix.getRow();
int lColumn = leftMatrix.getColumn();
int rRow = leftMatrix.getRow();
int rColumn = leftMatrix.getColumn();

// Addition/Subtraction can only be performed on similar matrices
if(lRow == rRow && lColumn == rColumn) {
// Matrix grid extraction for the operation
double[][] newGrid = new double[lRow][lColumn];
double[][] rightGrid = rightMatrix.getGrid();

// Simple cyclic increment
for(int ro = 0; ro < lRow; ro++) {
for (int col = 0; col < lColumn; col++) {
newGrid[ro][col] = 1/rightGrid[ro][col];
}// --Column loop end
}// --Row loop end

// Forwards operation after preparing the rightMatrix
rightMatrix.setGrid(newGrid);
return Multiply(leftMatrix, rightMatrix);
}
return null;
}
}

0 comments on commit 813d346

Please sign in to comment.