diff --git a/src/main/java/ch/hepia/numeric/Matrix.java b/src/main/java/ch/hepia/numeric/Matrix.java index 3809e962b5eca84bde4a6958d71fd65f33675e28..63a8e2900650c38704b24249d67a10822805290c 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 7c6b60a6e1a67b94a1c42ab417210631993d37a6..fcf0f790abe942dc1576eed0bc7a1a5289c49084 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; + })); + } }