From 7738781bb54b6fb86b1c7dfb10464bb26ffdf9a0 Mon Sep 17 00:00:00 2001 From: lluni Date: Mon, 23 May 2022 19:07:12 +0200 Subject: [PATCH] Added some utility functions (not yet needed) --- build.gradle | 1 + src/main/java/Utilities.java | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 src/main/java/Utilities.java diff --git a/build.gradle b/build.gradle index 9100d7c..a85b811 100644 --- a/build.gradle +++ b/build.gradle @@ -11,6 +11,7 @@ repositories { dependencies { implementation 'org.ejml:ejml-all:0.41' + implementation 'com.opencsv:opencsv:5.6' testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2' testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.2' } diff --git a/src/main/java/Utilities.java b/src/main/java/Utilities.java new file mode 100644 index 0000000..f444927 --- /dev/null +++ b/src/main/java/Utilities.java @@ -0,0 +1,33 @@ +import com.opencsv.CSVReader; +import com.opencsv.exceptions.CsvValidationException; +import org.ejml.simple.SimpleMatrix; + +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class Utilities { + public static SimpleMatrix ones(int rows, int columns) { + SimpleMatrix mat = new SimpleMatrix(rows, columns); + Arrays.fill(mat.getDDRM().data, 1); + return mat; + } + + public static List> readCSV(String filename) { + List> entries = new ArrayList<>(); + try (CSVReader csvReader = new CSVReader(new FileReader(filename))) { + String[] values; + while ((values = csvReader.readNext()) != null) { + entries.add(Arrays.asList(values)); + } + return entries; + } catch (IOException e) { + System.out.println(filename + " does not exist"); + } catch (CsvValidationException e) { + System.out.println("Invalid line in " + filename); + } + return null; + } +}