diff --git a/src/main/java/ch/hepia/numeric/Transposed.java b/src/main/java/ch/hepia/numeric/Transposed.java index c5c6c2ad729e662cee8dc1d4e9f30f5cbfbf5cae..4aa2138c2301015e8632ee8ad101592a897daac0 100644 --- a/src/main/java/ch/hepia/numeric/Transposed.java +++ b/src/main/java/ch/hepia/numeric/Transposed.java @@ -10,8 +10,15 @@ public class Transposed{ Transposed(Vector v) { this.v = v; } - public double dot(Vector vect){ - throw new UnsupportedOperationException("This feature isn't implemented yet"); + + public double dot(Vector vect) + { + double resultat=0; + for (int index=0;index<=vect.len()-1;index++) + { + resultat+=v.get(index)*vect.get(index); + } + return resultat; } public Transposed sub(Transposed t){ diff --git a/src/main/java/ch/hepia/numeric/Vector.java b/src/main/java/ch/hepia/numeric/Vector.java index 5cdb4f9154ff9450132162ea4c7336122171c3e2..6efb5a5bc967a6dd75935b5b9bfe6e93bd88da38 100644 --- a/src/main/java/ch/hepia/numeric/Vector.java +++ b/src/main/java/ch/hepia/numeric/Vector.java @@ -89,30 +89,52 @@ final public class Vector { if (from>=to){ return v; } - else{ + else + { for (double d : this.lstVector.subList(from,to)) { v.lstVector.add(d); } - return v; + return v; } - } - public Vector removed(int i) { - throw new UnsupportedOperationException("This feature isn't implemented yet"); + public Vector removed(int i) + { + Vector v = this.copy(); + v.lstVector.remove(i); + return v; } - public Vector concat(Vector that) { - throw new UnsupportedOperationException("This feature isn't implemented yet"); + public Vector concat(Vector that) + { + Vector v = Vector.zeros(this.len()+that.len()); + for(int i=0;i<v.len();i++) + { + if(i<this.len()) + { + v.set(i,this.get(i)); + } + else + { + v.set(i,that.get(i-this.len())); + } + } + return v; } - public Vector map(DoubleFunction<Double> f) { + public Vector map(DoubleFunction<Double> f) {//ne pas faire throw new UnsupportedOperationException("This feature isn't implemented yet"); } - public Vector copy() { - throw new UnsupportedOperationException("This feature isn't implemented yet"); + public Vector copy() + { + Vector v = Vector.zeros(this.len()); + for(int i=0;i<v.len();i++) + { + v.set(i,this.get(i)); + } + return v; } void checkVectorLengthOrThrow(Vector that){ @@ -150,16 +172,18 @@ final public class Vector { public static Vector linespace(double from, double to, int nb) { - double step=to/(nb-1); + double step=(to-from)/(nb-1); Vector v = Vector.zeros(nb); for(int i=0;i<nb;i++) { - v.set(i,step*i); + v.set(i,from+(step*i)); } return v; } - public static Vector tabulate(int nb, Function<Integer, Double> f) { + //ne pas faire + public static Vector tabulate(int nb, Function<Integer, Double> f) + { 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 91415e1cd3713f417486a6607add03e544e08251..c9a604f3c767f10f8a9a6db744db426f12888e68 100644 --- a/src/test/java/ch/hepia/numeric/AppTest.java +++ b/src/test/java/ch/hepia/numeric/AppTest.java @@ -49,7 +49,7 @@ class VectorTest { Vector.sum(List.of(Vector.of(1.0, 2.0, 1.0), Vector.of(0.0, 0.0, 1.0))).norm(), Vector.norms(List.of(Vector.of(1.0, 2.0, 1.0), Vector.of(0.0, 0.0, 1.0))) ); - //assertEquals(Vector.sum(List.of(Vector.of(1.0, 2.0, 1.0), Vector.of(0.0, 0.0, 1.0))), Vector.of(1.0, 2.0, 2.0)); + assertEquals(Vector.sum(List.of(Vector.of(1.0, 2.0, 1.0), Vector.of(0.0, 0.0, 1.0))), Vector.of(1.0, 2.0, 2.0)); assertEquals(Vector.sum(List.of(Vector.of(1.0, 2.0, 1.0), Vector.of(0.0, 0.0, 1.0))), Vector.of(1.0, 2.0, 2.0)); } @@ -57,14 +57,14 @@ class VectorTest { void vectorTranspose() { Vector v1 = Vector.of(1.0, 2.0, 3.0); Vector v2 = Vector.of(1.0, 0.0, -1.0); - //assertEquals(v1.t().dot(v2), -2.0); + assertEquals(v1.t().dot(v2), -2.0); // * v1.dot(v2) should not compile !!! // * only a transposed vector with a vector Vector v3 = Vector.of(1.0, 2.0, 3.0); Vector v4 = Vector.of(1.0, 2.0, 3.0); - //assertEquals( v3.t().dot(v4), 14.0 ); + assertEquals( v3.t().dot(v4), 14.0 ); assertEquals( v3.t().sub(v4.t()), Vector.of(0.0, 0.0, 0.0).t()); } @@ -83,14 +83,14 @@ class VectorTest { assertEquals(v.slice(2,4), Vector.of(3.0, 4.0)); assertEquals(v.slice(4,4), Vector.empty()); assertEquals(v.slice(4,2), Vector.empty()); - // assertEquals(v.removed(2), Vector.of(1.0, 2.0, 4.0, 5.0)); + assertEquals(v.removed(2), Vector.of(1.0, 2.0, 4.0, 5.0)); } - /* + @Test void vectorConcat() { Vector v1 = Vector.of(1.0, 2.0, 3.0); Vector v2 = Vector.of(-1.0, -2.0, -3.0); assertEquals(v1.concat(v2), Vector.of(1.0, 2.0, 3.0, -1.0, -2.0, -3.0)); } - */ + }