Done before I even started #2

Closed
AustrianToast wants to merge 1 commits from Feature/scalar_product into main
2 changed files with 59 additions and 11 deletions

View File

@ -8,10 +8,61 @@ public class Matrix {
public static void main(String[] args) {}
public Matrix(int rowLength, int columnLength) {
matrix = new int[rowLength][columnLength];
}
public Matrix(String file) {
matrix = this.readCSV(file);
}
public Matrix multiply( Matrix m) {
Matrix scalarProduct = null;
if(this.columnLength() != m.rowLength()) {
return scalarProduct;
}
scalarProduct = new Matrix(this.rowLength(), m.columnLength());
for(int columnIndex = 0; columnIndex < this.columnLength(); columnIndex++) {
for(int rowIndex = 0; rowIndex < m.rowLength(); rowIndex++) {
int sum = 0;
for(int k=0; k < this.rowLength(); k++) {
sum += this.getValue(columnIndex, k) * m.getValue(k, rowIndex);
}
scalarProduct.insert(columnIndex, rowIndex, sum);
}
}
return scalarProduct;
}
public int rowLength() {
return matrix.length;
}
public int columnLength() {
return matrix[0].length;
}
public int getValue(int columnIndex, int rowIndex) {
return matrix[columnIndex][rowIndex];
}
public void insert(int columnIndex, int rowIndex, int value) {
matrix[rowIndex][columnIndex] = value;
}
public void print() {
for(int columnIndex=0; columnIndex < matrix.length; columnIndex++) {
for(int rowIndex=0; rowIndex < matrix[columnIndex].length; rowIndex++) {
System.out.print(matrix[columnIndex][rowIndex]);
}
System.out.println();
}
}
public int[][] readCSV(String file){
int[][] intMatrix = null;
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
@ -19,7 +70,7 @@ public class Matrix {
int rowCount = line.trim().split(";").length;
int columnCount = rowCount;
String[] lineArray = null;
intMatrix = new int[rowCount][columnCount];
for(int columnIndex = 0; line != null && columnIndex < intMatrix.length; columnIndex++, line = br.readLine()) {
@ -37,13 +88,4 @@ public class Matrix {
return intMatrix;
}
public void print() {
for(int columnIndex=0; columnIndex < matrix.length; columnIndex++) {
for(int rowIndex=0; rowIndex < matrix[columnIndex].length; rowIndex++) {
System.out.print(matrix[columnIndex][rowIndex]);
}
System.out.println();
}
}
}
}

View File

@ -5,7 +5,13 @@ public class TestMatrix {
public static void test1(String file) {
Matrix matrix = new Matrix(file);
Matrix scalarProduct;
matrix.print();
System.out.println();
scalarProduct = matrix.multiply( matrix);
scalarProduct.print();
}
}