From e7de925373f19f9df5d18b1fd6a467e67533d304 Mon Sep 17 00:00:00 2001 From: lluni Date: Sat, 28 May 2022 03:14:52 +0200 Subject: [PATCH] Added support for creating matrices with random values --- .../java/de/lluni/javann/util/Utilities.java | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/main/java/de/lluni/javann/util/Utilities.java b/src/main/java/de/lluni/javann/util/Utilities.java index cadd566..bd98b08 100644 --- a/src/main/java/de/lluni/javann/util/Utilities.java +++ b/src/main/java/de/lluni/javann/util/Utilities.java @@ -14,6 +14,9 @@ import java.util.Random; public class Utilities { private static final double STANDARD_GAUSSIAN_FACTOR = 1.0d; + private static final double STANDARD_RANDOM_ORIGIN = -1.0d; + private static final double STANDARD_RANDOM_BOUND = 1.0d; + /** * Creates a matrix filled with ones * @param rows amount of rows @@ -50,6 +53,24 @@ public class Utilities { return gaussianMatrix(rows, columns, mean, stddev, STANDARD_GAUSSIAN_FACTOR); } + /** + * Creates a matrix with random values + * @param rows amount of rows + * @param columns amount of columns + * @param origin minimum random value + * @param bound maximum random value + * @return matrix with random values + */ + public static SimpleMatrix randomMatrix(int rows, int columns, double origin, double bound) { + Random random = new Random(); + return new SimpleMatrix(rows, columns, true, + random.doubles((long) rows * columns, origin, bound).toArray()); + } + + public static SimpleMatrix randomMatrix(int rows, int columns) { + return randomMatrix(rows, columns, STANDARD_RANDOM_ORIGIN, STANDARD_RANDOM_BOUND); + } + /** * Creates an array of evenly spaced values from the interval [start, end) * @param start start value