Implemented matrix multiplication

This commit is contained in:
Ziver Koc 2017-12-03 18:04:52 +01:00
parent 0e664ba3ca
commit 9562d8615a
2 changed files with 132 additions and 46 deletions

View file

@ -13,60 +13,61 @@ public class MatrixMath {
* Scalar addition, every element in the matrix will
* be added by the provided number
*
* @return same reference as matrix with the resulting addition
* @return a new matrix with the result
*/
public static double[][] add(double[][] matrix, double num){
double[][] result = new double[matrix.length][matrix[0].length];
for (int y=0; y < matrix.length; ++y) {
for (int x=0; x < matrix[y].length; ++x){
matrix[y][x] = matrix[y][x] + num;
result[y][x] = matrix[y][x] + num;
}
}
return matrix;
return result;
}
/**
* Scalar subtraction, every element in the matrix will
* be subtracted by the provided number
*
* @return same reference as matrix with the resulting subtraction
* @return a new matrix with the result
*/
public static double[][] subtract(double[][] matrix, double num){
double[][] result = new double[matrix.length][matrix[0].length];
for (int y=0; y < matrix.length; ++y) {
for (int x=0; x < matrix[y].length; ++x){
matrix[y][x] = matrix[y][x] - num;
result[y][x] = matrix[y][x] - num;
}
}
return matrix;
return result;
}
/**
* Scalar multiplication, every element in the matrix will
* be multiplied by the provided number
*
* @return same reference as matrix with the resulting multiplication
* @return a new matrix with the result
*/
public static double[][] multiply(double[][] matrix, double num){
double[][] result = new double[matrix.length][matrix[0].length];
for (int y=0; y < matrix.length; ++y) {
for (int x=0; x < matrix[y].length; ++x){
matrix[y][x] = matrix[y][x] * num;
result[y][x] = matrix[y][x] * num;
}
}
return matrix;
return result;
}
/**
* Scalar division, every element in the matrix will
* be multiplied by the provided number
*
* @return same reference as matrix with the resulting division
* @return a new matrix with the result
*/
public static double[][] divide(double[][] matrix, double num){
for (int y=0; y < matrix.length; ++y) {
for (int x=0; x < matrix[y].length; ++x){
matrix[y][x] = matrix[y][x] / num;
}
}
return matrix;
return multiply(matrix, 1/num);
}
/***********************************************************************
@ -77,59 +78,122 @@ public class MatrixMath {
* Element addition, each element in matrix1 will be
* added with the corresponding element in matrix2.
*
* @return same reference as matrix1 with the resulting addition
* @return a new matrix with the result
*/
public static double[][] add(double[][] matrix1, double[][] matrix2){
elementalPreCheck(matrix1, matrix2);
double[][] result = new double[matrix1.length][matrix1[0].length];
for (int y=0; y < matrix1.length; ++y) {
for (int x=0; x < matrix1[y].length; ++x){
matrix1[y][x] = matrix1[y][x] + matrix2[y][x];
result[y][x] = matrix1[y][x] + matrix2[y][x];
}
}
return matrix1;
return result;
}
/**
* Element subtraction, each element in matrix1 will be
* subtracted with the corresponding element in matrix2.
*
* @return same reference as matrix1 with the resulting subtraction
* @return a new matrix with the result
*/
public static double[][] subtract(double[][] matrix1, double[][] matrix2){
elementalPreCheck(matrix1, matrix2);
double[][] result = new double[matrix1.length][matrix1[0].length];
for (int y=0; y < matrix1.length; ++y) {
for (int x=0; x < matrix1[y].length; ++x){
matrix1[y][x] = matrix1[y][x] - matrix2[y][x];
result[y][x] = matrix1[y][x] - matrix2[y][x];
}
}
return matrix1;
return result;
}
private static void elementalPreCheck(double[][] matrix1, double[][] matrix2) {
if (matrix1.length != matrix2.length || matrix1[0].length != matrix2[0].length)
throw new IllegalArgumentException("Matrices need to be of same dimension: " +
"matrix1 " + matrix1.length + "x" + matrix1[0].length + ", " +
"matrix2 " + matrix2.length + "x" + matrix2[0].length + ", ");
}
/***********************************************************************
* Vector
**********************************************************************/
/**
* Matrix Vector multiplication, each element column in the matrix will be
* multiplied with the corresponding element row in the vector.
*
* @return a new vector with the result
*/
public static double[] multiply(double[][] matrix, double[] vector){
vectorPreCheck(matrix, vector);
double[] result = new double[matrix.length];
for (int y=0; y < matrix.length; ++y) {
for (int x=0; x < matrix[y].length; ++x){
result[y] += matrix[y][x] * vector[x];
}
}
return result;
}
/**
* Matrix Vector division, each element column in the matrix will be
* divided with the corresponding element row in the vector.
*
* @return a new vector with the result
*/
public static double[] divide(double[][] matrix, double[] vector){
vectorPreCheck(matrix, vector);
double[] result = new double[matrix.length];
for (int y=0; y < matrix.length; ++y) {
for (int x=0; x < matrix[y].length; ++x){
result[y] += matrix[y][x] / vector[x];
}
}
return result;
}
private static void vectorPreCheck(double[][] matrix, double[] vector) {
if (matrix[0].length != vector.length)
throw new IllegalArgumentException("Matrix columns need to have same length as vector length: " +
"matrix " + matrix.length + "x" + matrix[0].length + ", " +
"vector " + vector.length + "x1");
}
/***********************************************************************
* Matrix
**********************************************************************/
/**
* Element multiplication, each element in matrix1 will be
* multiplied with the corresponding element in matrix2.
*
* @return same reference as matrix1 with the resulting multiplication
* @return a new matrix with the result
*/
public static double[][] multiply(double[][] matrix1, double[][] matrix2){
for (int y=0; y < matrix1.length; ++y) {
for (int x=0; x < matrix1[y].length; ++x){
matrix1[y][x] = matrix1[y][x] * matrix2[y][x];
matrixPreCheck(matrix1, matrix2);
double[][] result = new double[matrix1.length][matrix2[0].length];
for (int y=0; y < result.length; ++y) {
for (int x=0; x < result[y].length; ++x){
for (int i=0; i<matrix1[0].length; ++i) {
result[y][x] += matrix1[y][i] * matrix2[i][x];
}
}
}
return matrix1;
return result;
}
/**
* Element division, each element in matrix1 will be
* dicided with the corresponding element in matrix2.
*
* @return same reference as matrix1 with the resulting division
*/
public static double[][] divide(double[][] matrix1, double[][] matrix2){
for (int y=0; y < matrix1.length; ++y) {
for (int x=0; x < matrix1[y].length; ++x){
matrix1[y][x] = matrix1[y][x] / matrix2[y][x];
}
}
return matrix1;
private static void matrixPreCheck(double[][] matrix1, double[][] matrix2) {
if (matrix1[0].length != matrix2.length)
throw new IllegalArgumentException("Matrix1 columns need to match Matrix2 rows: " +
"matrix1 " + matrix1.length + "x" + matrix1[0].length + ", " +
"matrix2 " + matrix2.length + "x" + matrix2[0].length + ", ");
}
}

View file

@ -47,15 +47,37 @@ public class MatrixMathTest {
MatrixMath.subtract(new double[][]{{2,3},{-4,9}}, new double[][]{{1,2},{3,4}}));
}
@Test
public void elementalMultiply(){
assertArrayEquals(new double[][]{{2,6},{-12,36}},
MatrixMath.multiply(new double[][]{{2,3},{-4,9}}, new double[][]{{1,2},{3,4}}));
public void vectorMultiply(){
assertArrayEquals(
new double[]{8,14},
MatrixMath.multiply(new double[][]{{2,3},{-4,9}}, new double[]{1,2}),
0.0
);
}
@Test
public void elementalDivision(){
assertArrayEquals(new double[][]{{2,2},{-3,3}},
MatrixMath.divide(new double[][]{{2,4},{-9,12}}, new double[][]{{1,2},{3,4}}));
public void vectorDivision(){
assertArrayEquals(
new double[]{4,1},
MatrixMath.divide(new double[][]{{2,4},{-4,10}}, new double[]{1,2}),
0.0
);
}
@Test
public void matrixMultiply(){
assertArrayEquals(
new double[][]{{486,410.4,691.6},{314,341.6,416.4},{343.5,353.4,463.6},{173,285.2,190.8}},
MatrixMath.multiply(
new double[][]{{1,2104},{1,1416},{1,1534},{1,852}},
new double[][]{{-40,200,-150},{0.25,0.1,0.4}})
);
}
}