diff --git a/src/main/java/ch/hepia/numeric/Transposed.java b/src/main/java/ch/hepia/numeric/Transposed.java index ceb7a5287309d86fd969570beb84d88c71b7f3c8..9154658958fe87fa3224a8c4ece78a5f554e5296 100644 --- a/src/main/java/ch/hepia/numeric/Transposed.java +++ b/src/main/java/ch/hepia/numeric/Transposed.java @@ -17,7 +17,15 @@ public class Transposed{ public List<Double> getValues(){ return this.vecT; } + + public int len() { + return this.getValues().size(); + } + public double get(int i) { + return this.getValues().get(i); + } + public double dot(Vector That){ double res = 0.0; Integer count = 0; @@ -28,11 +36,11 @@ public class Transposed{ return res; //throw new UnsupportedOperationException("This feature isn't implemented yet"); }; - public Transposed sub(Vector That){ + public Transposed sub(Transposed That){ Transposed resT = new Transposed(); Integer count = 0; - for (double coo : this.vecT) { - //resT.add(coo - That.get(count)); + for (double coo : this.getValues()) { + resT.vecT.add(coo - That.getValues().get(count)); count = count + 1; } return resT; @@ -49,6 +57,23 @@ public class Transposed{ return tr; } + @Override + public boolean equals(Object obj){ + if (this == obj) return true; + if (obj == null || obj.getClass() != this.getClass()) { + return false; + } + Transposed tra = (Transposed) obj; + if (tra.len() != this.len()){ + return false; + } + for (int i=0;i<tra.len();i++){ + if (Double.compare(tra.getValues().get(i), this.getValues().get(i))!=0){ + return false; + } + } + return true; + } diff --git a/src/main/java/ch/hepia/numeric/Vector.java b/src/main/java/ch/hepia/numeric/Vector.java index 81649abfdf7bc963b93f92758a0f288e8bd4a21d..7ed7bf3ea1af995eb59e8cd2c117fd432323b5b3 100644 --- a/src/main/java/ch/hepia/numeric/Vector.java +++ b/src/main/java/ch/hepia/numeric/Vector.java @@ -107,7 +107,13 @@ final public class Vector { } public Vector map(DoubleFunction<Double> f) { - throw new UnsupportedOperationException("This feature isn't implemented yet"); + List<Double> accVec = this.getValues(); + List<Double> newVec = new ArrayList<>(); + + for (Double val : accVec){ + newVec.add(f.apply(val)); + } + return new Vector(newVec); } public Vector copy() { @@ -155,11 +161,21 @@ final public class Vector { } public static Vector linespace(double from, double to, int nb) { - throw new UnsupportedOperationException("This feature isn't implemented yet"); + double diff = to - from; + double hop = diff / (nb-1); + List<Double> newVec = new ArrayList<>(); + for (int i = 0; i <= nb -1; i++){ + newVec.add(hop*i); + } + return new Vector(newVec); } public static Vector tabulate(int nb, Function<Integer, Double> f) { - throw new UnsupportedOperationException("This feature isn't implemented yet"); + List<Double> newVec = new ArrayList<>(); + for (int i=0; i < nb; i++){ + newVec.add(f.apply(i)); + } + return new Vector(newVec); } public static Vector sum(List<Vector> vs) { @@ -171,11 +187,7 @@ final public class Vector { } public static double norms(List<Vector> vs) { - double normsTotal = 0.0; - for (Vector vec : vs){ - normsTotal += vec.norm(); - } - return normsTotal; + return Vector.sum(vs).norm(); } diff --git a/src/test/java/ch/hepia/numeric/AppTest.java b/src/test/java/ch/hepia/numeric/AppTest.java index 7a5d18e08eff92c66ae3b032a40c536b2b44746a..3b2ddd7cdb355cfc0b6cad3e147e18926480a292 100644 --- a/src/test/java/ch/hepia/numeric/AppTest.java +++ b/src/test/java/ch/hepia/numeric/AppTest.java @@ -1,6 +1,9 @@ package ch.hepia.numeric; import org.junit.jupiter.api.Test; + +//import sun.jvmstat.monitor.VmIdentifier; + import java.util.List; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -25,17 +28,17 @@ class VectorTest { void vectorCreation() { assertEquals(Vector.zeros(3), Vector.of(0.0, 0.0, 0.0)); assertEquals(Vector.ones(3), Vector.of(1.0, 1.0, 1.0)); - //assertEquals(Vector.of(1.0, 2.0).map( d -> d * 2.0), Vector.of(2.0, 4.0)); + assertEquals(Vector.of(1.0, 2.0).map( d -> d * 2.0), Vector.of(2.0, 4.0)); assertEquals(Vector.fill(3, 1.7), Vector.of(1.7, 1.7, 1.7)); - //assertEquals(Vector.tabulate(4, i -> i*5.0), Vector.of(0.0, 5.0, 10.0, 15.0)); + assertEquals(Vector.tabulate(4, i -> i*5.0), Vector.of(0.0, 5.0, 10.0, 15.0)); } - /*@Test + @Test void vectorLinespace() { assertEquals(Vector.linespace(0.0, 1.0, 3), Vector.of(0.0, 0.5, 1.0)); assertEquals(Vector.linespace(0.0, 1.0, 5), Vector.of(0.0, 0.25, 0.5, 0.75, 1.0)); - }*/ - + } + @Test void vectorNormAndSum() { assertEquals(Vector.of(1.0, 2.0, 2.0).norm(), 3.0); @@ -59,7 +62,7 @@ class VectorTest { 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().sub(v4.t()), Vector.of(0.0, 0.0, 0.0).t() ); + assertEquals( v3.t().sub(v4.t()), Vector.of(0.0, 0.0, 0.0).t() ); } @Test @@ -85,9 +88,4 @@ class VectorTest { 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)); } - - @Test - void create() { - Vector newVec = Vector.of(2.0, 3.0, 4.0); - } }