Skip to content
Snippets Groups Projects
Commit 7c3e3b58 authored by david's avatar david
Browse files

Ecriture de .dot() .copy() .removed() .concat(), et correction de linespace()

parent acd989f8
No related branches found
No related tags found
2 merge requests!2Tanguy,!1David
This commit is part of merge request !1. Comments created here will be created in the context of that merge request.
...@@ -10,8 +10,15 @@ public class Transposed{ ...@@ -10,8 +10,15 @@ public class Transposed{
Transposed(Vector v) { Transposed(Vector v) {
this.v = 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){ public Transposed sub(Transposed t){
......
...@@ -89,30 +89,52 @@ final public class Vector { ...@@ -89,30 +89,52 @@ final public class Vector {
if (from>=to){ if (from>=to){
return v; return v;
} }
else{ else
{
for (double d : this.lstVector.subList(from,to)) for (double d : this.lstVector.subList(from,to))
{ {
v.lstVector.add(d); v.lstVector.add(d);
} }
return v; return v;
} }
} }
public Vector removed(int i) { public Vector removed(int i)
throw new UnsupportedOperationException("This feature isn't implemented yet"); {
Vector v = this.copy();
v.lstVector.remove(i);
return v;
} }
public Vector concat(Vector that) { public Vector concat(Vector that)
throw new UnsupportedOperationException("This feature isn't implemented yet"); {
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"); throw new UnsupportedOperationException("This feature isn't implemented yet");
} }
public Vector copy() { public Vector copy()
throw new UnsupportedOperationException("This feature isn't implemented yet"); {
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){ void checkVectorLengthOrThrow(Vector that){
...@@ -150,16 +172,18 @@ final public class Vector { ...@@ -150,16 +172,18 @@ final public class Vector {
public static Vector linespace(double from, double to, int nb) 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); Vector v = Vector.zeros(nb);
for(int i=0;i<nb;i++) for(int i=0;i<nb;i++)
{ {
v.set(i,step*i); v.set(i,from+(step*i));
} }
return v; 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"); throw new UnsupportedOperationException("This feature isn't implemented yet");
} }
......
...@@ -49,7 +49,7 @@ class VectorTest { ...@@ -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.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))) 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)); 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 { ...@@ -57,14 +57,14 @@ class VectorTest {
void vectorTranspose() { void vectorTranspose() {
Vector v1 = Vector.of(1.0, 2.0, 3.0); Vector v1 = Vector.of(1.0, 2.0, 3.0);
Vector v2 = Vector.of(1.0, 0.0, -1.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 !!! // * v1.dot(v2) should not compile !!!
// * only a transposed vector with a vector // * only a transposed vector with a vector
Vector v3 = Vector.of(1.0, 2.0, 3.0); Vector v3 = Vector.of(1.0, 2.0, 3.0);
Vector v4 = 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()); assertEquals( v3.t().sub(v4.t()), Vector.of(0.0, 0.0, 0.0).t());
} }
...@@ -83,14 +83,14 @@ class VectorTest { ...@@ -83,14 +83,14 @@ class VectorTest {
assertEquals(v.slice(2,4), Vector.of(3.0, 4.0)); assertEquals(v.slice(2,4), Vector.of(3.0, 4.0));
assertEquals(v.slice(4,4), Vector.empty()); assertEquals(v.slice(4,4), Vector.empty());
assertEquals(v.slice(4,2), 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 @Test
void vectorConcat() { void vectorConcat() {
Vector v1 = Vector.of(1.0, 2.0, 3.0); Vector v1 = Vector.of(1.0, 2.0, 3.0);
Vector v2 = 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)); assertEquals(v1.concat(v2), Vector.of(1.0, 2.0, 3.0, -1.0, -2.0, -3.0));
} }
*/
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment