Added support for creating matrices with Gaussian distributed values

This commit is contained in:
lluni 2022-05-25 15:33:17 +02:00
parent ffcf9fa975
commit adfd701817

View file

@ -7,14 +7,45 @@ import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Random;
public class Utilities { public class Utilities {
private static final double STANDARD_GAUSSIAN_FACTOR = 1.0d;
public static SimpleMatrix ones(int rows, int columns) { public static SimpleMatrix ones(int rows, int columns) {
SimpleMatrix mat = new SimpleMatrix(rows, columns); SimpleMatrix mat = new SimpleMatrix(rows, columns);
Arrays.fill(mat.getDDRM().data, 1); Arrays.fill(mat.getDDRM().data, 1);
return mat; return mat;
} }
public static SimpleMatrix gaussianMatrix(int rows, int columns, double mean, double stddev, double factor) {
SimpleMatrix mat = new SimpleMatrix(rows, columns);
Random random = new Random();
for (int i = 0; i < mat.getNumElements(); i++) {
mat.set(i, factor * random.nextGaussian(mean, stddev));
}
return mat;
}
public static SimpleMatrix gaussianMatrix(int rows, int columns, double mean, double stddev) {
return gaussianMatrix(rows, columns, mean, stddev, STANDARD_GAUSSIAN_FACTOR);
}
public static double[] linspace(double start, double end, int num) {
double[] result = new double[num];
double stepSize = Math.abs(end - start) / num;
double nextEntry = start;
for (int i = 0; i < num; i++) {
result[i] = nextEntry;
nextEntry += stepSize;
}
return result;
}
public static List<List<String>> readCSV(String filename) { public static List<List<String>> readCSV(String filename) {
List<List<String>> entries = new ArrayList<>(); List<List<String>> entries = new ArrayList<>();
try (CSVReader csvReader = new CSVReader(new FileReader(filename))) { try (CSVReader csvReader = new CSVReader(new FileReader(filename))) {