diff --git a/src/zutil/math/MatrixMath.java b/src/zutil/math/MatrixMath.java new file mode 100755 index 0000000..a6694c5 --- /dev/null +++ b/src/zutil/math/MatrixMath.java @@ -0,0 +1,135 @@ +package zutil.math; + +/** + * Some basic matrix match functions. + * Matrix definition: double[y][x]. + */ +public class MatrixMath { + + /*********************************************************************** + * Scalar + **********************************************************************/ + /** + * Scalar addition, every element in the matrix will + * be added by the provided number + * + * @return same reference as matrix with the resulting addition + */ + public static double[][] add(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; + } + + /** + * Scalar subtraction, every element in the matrix will + * be subtracted by the provided number + * + * @return same reference as matrix with the resulting subtraction + */ + public static double[][] subtract(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; + } + + /** + * Scalar multiplication, every element in the matrix will + * be multiplied by the provided number + * + * @return same reference as matrix with the resulting multiplication + */ + public static double[][] multiply(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; + } + + /** + * Scalar division, every element in the matrix will + * be multiplied by the provided number + * + * @return same reference as matrix with the resulting division + */ + 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; + } + + /*********************************************************************** + * Elemental + **********************************************************************/ + + /** + * Element addition, each element in matrix1 will be + * added with the corresponding element in matrix2. + * + * @return same reference as matrix1 with the resulting addition + */ + public static double[][] add(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; + } + + /** + * Element subtraction, each element in matrix1 will be + * subtracted with the corresponding element in matrix2. + * + * @return same reference as matrix1 with the resulting subtraction + */ + public static double[][] subtract(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; + } + + /** + * Element multiplication, each element in matrix1 will be + * multiplied with the corresponding element in matrix2. + * + * @return same reference as matrix1 with the resulting multiplication + */ + 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]; + } + } + return matrix1; + } + + /** + * 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; + } +} diff --git a/src/zutil/ml/neural/Perceptron.java b/src/zutil/ml/neural/Perceptron.java new file mode 100755 index 0000000..1192397 --- /dev/null +++ b/src/zutil/ml/neural/Perceptron.java @@ -0,0 +1,9 @@ +package zutil.ml.neural; + +/** + * This class represents one "nuron" in a neural network + */ +public class Perceptron { + private float[] inputs; + private float[] inputWeights; +} diff --git a/test/zutil/math/MatrixMathTest.java b/test/zutil/math/MatrixMathTest.java new file mode 100755 index 0000000..20755b8 --- /dev/null +++ b/test/zutil/math/MatrixMathTest.java @@ -0,0 +1,61 @@ +package zutil.math; + +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * + */ +public class MatrixMathTest { + + @Test + public void scalarAdd(){ + assertArrayEquals(new double[][]{{4,5},{-2,11}}, + MatrixMath.add(new double[][]{{2,3},{-4,9}}, 2)); + } + + @Test + public void scalarSubtraction(){ + assertArrayEquals(new double[][]{{0,1},{-6,7}}, + MatrixMath.subtract(new double[][]{{2,3},{-4,9}}, 2)); + } + + @Test + public void scalarMultiply(){ + assertArrayEquals(new double[][]{{4,6},{-8,18}}, + MatrixMath.multiply(new double[][]{{2,3},{-4,9}}, 2)); + } + + @Test + public void scalarDivision(){ + assertArrayEquals(new double[][]{{1,2},{-2,5}}, + MatrixMath.divide(new double[][]{{2,4},{-4,10}}, 2)); + } + + + + @Test + public void elementalAdd(){ + assertArrayEquals(new double[][]{{3,5},{-1,13}}, + MatrixMath.add(new double[][]{{2,3},{-4,9}}, new double[][]{{1,2},{3,4}})); + } + + @Test + public void elementalSubtract(){ + assertArrayEquals(new double[][]{{1,1},{-7,5}}, + 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}})); + } + + @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}})); + } +} \ No newline at end of file