Skip to content
Snippets Groups Projects
Commit 5c52644f authored by tanguy.dietrich's avatar tanguy.dietrich
Browse files

Fusion des branche David et Tanguy + ecriture de norms et sum, et divers test pour Transposed

parent e04d44a2
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.
package ch.hepia.numeric;
import java.util.List;
import java.util.ArrayList;
public class Transposed{
final private Vector v;
......@@ -18,4 +21,15 @@ public class Transposed{
public Vector t(){
return this.v;
}
@Override
public String toString() {
List<String> strings = new ArrayList<String>();
for(int i=0;i<this.t().len();i++)
{
strings.add(Double.valueOf(this.t().get(i)).toString());
}
return "Transposed"+String.valueOf(strings);
}
}
......@@ -7,7 +7,7 @@ import java.util.function.Function;
final public class Vector {
private List<Double> lstVector;// = new ArrayList<Double>();
final private List<Double> lstVector;// = new ArrayList<Double>();
private Vector() {
this.lstVector=new ArrayList<Double>();
......@@ -19,7 +19,6 @@ final public class Vector {
{
lstVector.add(e);
}
//throw new UnsupportedOperationException("This feature isn't implemented yet");
}
private Vector(List<Double> elements) {
this.lstVector=new ArrayList<Double>();
......@@ -27,7 +26,6 @@ final public class Vector {
{
lstVector.add(e);
}
//throw new UnsupportedOperationException("This feature isn't implemented yet");
}
public Transposed t() {
......@@ -35,50 +33,52 @@ final public class Vector {
}
public int len() {
//throw new UnsupportedOperationException("This feature isn't implemented yet");
return this.lstVector.size();
}
public double get(int i) {
throw new UnsupportedOperationException("This feature isn't implemented yet");
return this.lstVector.get(i);
}
void set(int i, double value) {
throw new UnsupportedOperationException("This feature isn't implemented yet");
this.lstVector.set(i,value);
}
public Vector add(Vector that) {
//throw new UnsupportedOperationException("This feature isn't implemented yet");
Vector v = new Vector();
for(int i=0;i<this.lstVector.size();i++)//a ameliorer avec for(e:element)
for(int i=0;i<this.len();i++)
{
v.lstVector.add(Double.sum(this.lstVector.get(i),that.lstVector.get(i)));
v.lstVector.add(Double.sum(this.get(i),that.get(i)));
}
return v;
}
public Vector mul(double m) {
//throw new UnsupportedOperationException("This feature isn't implemented yet");
Vector v = new Vector();
for(int i=0;i<this.lstVector.size();i++)//a ameliorer avec for(e:element)
for(int i=0;i<this.len();i++)
{
v.lstVector.add(this.lstVector.get(i)*m);
v.lstVector.add(this.get(i)*m);
}
return v;
}
public Vector sub(Vector that) {
Vector v = new Vector();
for(int i=0;i<this.lstVector.size();i++)//a ameliorer avec for(e:element)
for(int i=0;i<this.len();i++)
{
v.lstVector.add(this.lstVector.get(i)-that.lstVector.get(i));
v.lstVector.add(this.get(i)-that.get(i));
}
return v;
//throw new UnsupportedOperationException("This feature isn't implemented yet");
}
public double norm() {
throw new UnsupportedOperationException("This feature isn't implemented yet");
double norme=0;
double power=0;
for(int i=0;i<this.len();i++)
{
power=power +(this.get(i)*this.get(i));
}
norme=Math.sqrt(power);
return norme;
}
public Vector sliceFrom(int i) {
......@@ -120,25 +120,38 @@ final public class Vector {
v.lstVector.add(e);
}
return v;
//throw new UnsupportedOperationException("This feature isn't implemented yet");
}
public static Vector of(List<Double> elements) {
throw new UnsupportedOperationException("This feature isn't implemented yet");
Vector v = new Vector();
for(double e:elements)
{
v.lstVector.add(e);
}
return v;
}
public static Vector empty() {
Vector v = new Vector();
return v;
//throw new UnsupportedOperationException("This feature isn't implemented yet");
}
public static Vector fill(int nb, double value) {
throw new UnsupportedOperationException("This feature isn't implemented yet");
Vector v = new Vector();
for(int i = 0; i < nb; i++)
{
v.lstVector.add(value);
}
return v;
}
public static Vector zeros(int nb) {
throw new UnsupportedOperationException("This feature isn't implemented yet");
Vector v = new Vector();
for(int i=0;i<nb;i++)
{
v.lstVector.add(0.0);
}
return v;
}
public static Vector ones(int nb) {
......@@ -153,12 +166,22 @@ final public class Vector {
throw new UnsupportedOperationException("This feature isn't implemented yet");
}
public static Vector sum(List<Vector> vs) {
throw new UnsupportedOperationException("This feature isn't implemented yet");
public static Vector sum(List<Vector> vs)
{
int length=vs.get(0).len();
Vector v = Vector.zeros(length);
for(int e=0;e<length;e++)
{
for(Vector vLst:vs)
{
v.set(e,Double.sum(v.get(e),vLst.get(e)));
}
}
return v;
}
public static double norms(List<Vector> vs) {
throw new UnsupportedOperationException("This feature isn't implemented yet");
return sum(vs).norm();
}
......@@ -169,15 +192,7 @@ final public class Vector {
// Apply formatting to the string if necessary
strings.add(d.toString());
}
if(this instanceof ch.hepia.numeric.Vector)
{
return "Vector"+String.valueOf(strings);
}
else
{
return "Transposed"+String.valueOf(strings);
}
//throw new UnsupportedOperationException("This feature isn't implemented yet");
return "Vector"+String.valueOf(strings);
}
/*
......@@ -186,7 +201,7 @@ final public class Vector {
if(this instanceof ch.hepia.numeric.Vector)
{
for(int i=0;i<this.lstVector.size();i++)//a ameliorer avec for(e:element)
for(int i=0;i<this.lstVector.size();i++)
{
if(((Vector)obj).lstVector.get(i)!=this.lstVector.get(i))
{
......
......@@ -15,28 +15,31 @@ class VectorTest {
Vector v1 = Vector.of(1.0, 2.0, 3.0);
Vector v2 = Vector.of(-1.0, -2.0, -3.0);
Vector v3 = Vector.of(-1.0, -2.0, -3.0);
//Transposed t1= new Transposed(v2);
//System.out.println(v1.toString());
//assertEquals(v1.add(v2), Vector.of(0.0, 0.0, 0.0));
assertEquals(v1.add(v2).toString(), Vector.of(0.0, 0.0, 0.0).toString());
assertEquals(v1.sub(v2).toString(), Vector.of(2.0, 4.0, 6.0).toString());
assertEquals(v1.mul(3.0).toString(), Vector.of(3.0, 6.0, 9.0).toString());
assertEquals(Vector.empty().len(), 0);
assertEquals(Vector.of(1.0, 2.0, 1.0).len(), 3);
}
/*
@Test
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.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.zeros(3).toString(), Vector.of(0.0, 0.0, 0.0).toString());
//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.fill(3, 1.7).toString(), Vector.of(1.7, 1.7, 1.7).toString());
//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));
}
/*
@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);
......@@ -45,29 +48,31 @@ 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))).toString(), Vector.of(1.0, 2.0, 2.0).toString());
}
@Test
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().sub(v4.t()), Vector.of(0.0, 0.0, 0.0).t() );
//assertEquals( v3.t().dot(v4), 14.0 );
assertEquals( v3.t().sub(v4.t()).toString(), Vector.of(0.0, 0.0, 0.0).t().toString() );
//assertEquals( v3.t().sub(v4.t()), Vector.of(0.0, 0.0, 0.0).t());
}
*/
@Test
void vectorToString() {
Vector v = Vector.of(1.0, 2.0, 3.0);
assertEquals(v.toString(), "Vector[1.0, 2.0, 3.0]");
//System.out.println(v.t().toString());
//assertEquals(v.t().toString(), "Transposed[1.0, 2.0, 3.0]");
assertEquals(v.t().toString(), "Transposed[1.0, 2.0, 3.0]");
}
/*
@Test
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment