Added support for creating matrices with random values

This commit is contained in:
lluni 2022-05-28 03:14:52 +02:00
parent b20f62309b
commit e7de925373

View file

@ -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