From c458dcf4d686117a032fd91afaa44fdeccf950d8 Mon Sep 17 00:00:00 2001
From: fefe <hofer.francois.01@gmail.com>
Date: Fri, 10 Jan 2020 00:08:39 +0100
Subject: [PATCH]  Ended tabulate function and tested

---
 src/main/java/ch/hepia/numeric/Matrix.java  | 16 ++++++++++---
 src/test/java/ch/hepia/numeric/AppTest.java | 26 +++++++++++++++++++++
 2 files changed, 39 insertions(+), 3 deletions(-)

diff --git a/src/main/java/ch/hepia/numeric/Matrix.java b/src/main/java/ch/hepia/numeric/Matrix.java
index 3809e96..63a8e29 100644
--- a/src/main/java/ch/hepia/numeric/Matrix.java
+++ b/src/main/java/ch/hepia/numeric/Matrix.java
@@ -40,7 +40,7 @@ public class Matrix {
         return this.cols().get(i).get(j);
     }
     void set(int row, int col, double value) {
-        throw new UnsupportedOperationException("This feature isn't implemented yet");
+        this.col.get(col).set(row, value);
     }
     public Matrix map(DoubleFunction<Double> f) {
         throw new UnsupportedOperationException("This feature isn't implemented yet");
@@ -174,7 +174,11 @@ public class Matrix {
         throw new UnsupportedOperationException("This feature isn't implemented yet");
     }
     public static Matrix fill(int rows, int cols, double value) {
-        throw new UnsupportedOperationException("This feature isn't implemented yet");
+        List<Vector> newLst = new ArrayList<>();
+        for (int i = 0; i < cols; i++){
+            newLst.add(Vector.fill(rows, value));
+        }
+        return Matrix.of(newLst);
     }
     public static Matrix zeros(int rows, int cols) {
         throw new UnsupportedOperationException("This feature isn't implemented yet");
@@ -183,7 +187,13 @@ public class Matrix {
         throw new UnsupportedOperationException("This feature isn't implemented yet"); 
     }
     public static Matrix tabulate(int rows, int cols, BiFunction<Integer, Integer, Double> f) {
-        throw new UnsupportedOperationException("This feature isn't implemented yet");
+        Matrix newMat = Matrix.fill(rows, cols, 0);
+        for (int i=0; i < newMat.nbCols();i++){
+            for (int y=0; y < newMat.nbRows();y++){
+                newMat.set(y, i, f.apply(y, i));
+            }
+        }
+        return newMat;
     }
     public static Matrix diag(double... ds) {
         throw new UnsupportedOperationException("This feature isn't implemented yet");
diff --git a/src/test/java/ch/hepia/numeric/AppTest.java b/src/test/java/ch/hepia/numeric/AppTest.java
index 7c6b60a..fcf0f79 100644
--- a/src/test/java/ch/hepia/numeric/AppTest.java
+++ b/src/test/java/ch/hepia/numeric/AppTest.java
@@ -214,4 +214,30 @@ class VectorTest {
         lstVec.add(Vector.of(4.0,8.0,12.0, 16.0));
         assertEquals(Matrix.of(lstVec).det(), 0.0);
     }
+
+    @Test
+    void matrixSet() {
+        List<Vector> lstVec = new ArrayList<>();
+        lstVec.add(Vector.of(1.0,4.0,7.0));
+        lstVec.add(Vector.of(2.0,5.0,8.0));
+        lstVec.add(Vector.of(3.0,6.0,9.0));
+        Matrix mat = Matrix.of(lstVec);
+        mat.set(1, 1, 10.0);
+        lstVec = new ArrayList<>();
+        lstVec.add(Vector.of(1.0,4.0,7.0));
+        lstVec.add(Vector.of(2.0,10.0,8.0));
+        lstVec.add(Vector.of(3.0,6.0,9.0));
+        assertEquals(mat.cols().get(1).get(1),10.0);
+    }
+
+    @Test
+    void matrixTabulate() {
+        List<Vector> lstVec = new ArrayList<>();
+        lstVec.add(Vector.of(0.0, 1.0, 2.0));
+        lstVec.add(Vector.of(1.0, 2.0, 3.0));
+        Matrix m = Matrix.of(lstVec);
+        assertEquals(m, Matrix.tabulate(3, 2, (i, j) -> {
+            return (double)i + j;
+        }));
+    }
 }
-- 
GitLab