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

All tests are good , ended all the functions

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