Skip to content
Snippets Groups Projects
Commit c458dcf4 authored by fefe's avatar fefe
Browse files

Ended tabulate function and tested

parent e30233e7
Branches
No related tags found
No related merge requests found
......@@ -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");
......
......@@ -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;
}));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment