Skip to content
Snippets Groups Projects

David

Open david.carballo requested to merge david.carballo/poo2019numeric:David into master
Files
2
package ch.hepia.numeric;
import java.util.List;
import java.util.ArrayList;
import java.util.function.DoubleFunction;
import java.util.function.Function;
final public class Vector {
private Vector() {}
final private List<Double> lstVector;// = new ArrayList<Double>();
private Vector() {
this.lstVector=new ArrayList<Double>();
}
private Vector(Double... elements) {
throw new UnsupportedOperationException("This feature isn't implemented yet");
this.lstVector=new ArrayList<Double>();
for(double e:elements)
{
lstVector.add(e);
}
}
private Vector(List<Double> elements) {
throw new UnsupportedOperationException("This feature isn't implemented yet");
this.lstVector=elements;
}
public Transposed t() {
throw new UnsupportedOperationException("This feature isn't implemented yet");
return new Transposed(this);
}
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.len();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(double e:this.lstVector)
{
v.lstVector.add(e*m);
}
return v;
}
public Vector sub(Vector that) {
throw new UnsupportedOperationException("This feature isn't implemented yet");
Vector v = new Vector();
for(int i=0;i<this.len();i++)
{
v.lstVector.add(this.get(i)-that.get(i));
}
return v;
}
public double norm() {
throw new UnsupportedOperationException("This feature isn't implemented yet");
double power=0;
for(double e:this.lstVector)
{
power+=(e*e);
}
return Math.sqrt(power);
}
public Vector sliceFrom(int i) {
throw new UnsupportedOperationException("This feature isn't implemented yet");
return slice (i,this.len());
}
public Vector sliceTo(int i) {
throw new UnsupportedOperationException("This feature isn't implemented yet");
return slice (0,i);
}
public Vector slice(int from, int to) {
throw new UnsupportedOperationException("This feature isn't implemented yet");
}
public Vector removed(int i) {
throw new UnsupportedOperationException("This feature isn't implemented yet");
}
public Vector concat(Vector that) {
throw new UnsupportedOperationException("This feature isn't implemented yet");
}
public Vector map(DoubleFunction<Double> f) {
throw new UnsupportedOperationException("This feature isn't implemented yet");
}
public Vector copy() {
throw new UnsupportedOperationException("This feature isn't implemented yet");
Vector v =new Vector();
if (from>=to){
return v;
}
else
{
for (double d : this.lstVector.subList(from,to))
{
v.lstVector.add(d);
}
return v;
}
}
public Vector removed(int i)
{
Vector v = this.copy();
v.lstVector.remove(i);
return v;
}
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) {//ne pas faire
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){
@@ -77,53 +142,100 @@ final public class Vector {
}
public static Vector of(Double... elements) {
throw new UnsupportedOperationException("This feature isn't implemented yet");
return new Vector(elements);
}
public static Vector of(List<Double> elements) {
throw new UnsupportedOperationException("This feature isn't implemented yet");
return new Vector(elements);
}
public static Vector empty() {
throw new UnsupportedOperationException("This feature isn't implemented yet");
return new Vector();
}
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");
return Vector.fill(nb,0.0);
}
public static Vector ones(int nb) {
throw new UnsupportedOperationException("This feature isn't implemented yet");
return Vector.fill(nb,1.0);
}
public static Vector linespace(double from, double to, int nb) {
throw new UnsupportedOperationException("This feature isn't implemented yet");
public static Vector linespace(double from, double to, int nb)
{
double step=(to-from)/(nb-1);
Vector v = Vector.zeros(nb);
for(int i=0;i<nb;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");
}
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();
}
/*
Overrides ecrits avant le cours sur le sujet ces fonctions sont
a reecrire avec les nouvelles connaisances d'ici le deuxieme tag.
*/
@Override
public String toString() {
throw new UnsupportedOperationException("This feature isn't implemented yet");
List<String> strings = new ArrayList<String>();
for (Double d : this.lstVector) {
// Apply formatting to the string if necessary
strings.add(d.toString());
}
return "Vector"+String.valueOf(strings);
}
@Override
public boolean equals(Object obj) {
throw new UnsupportedOperationException("This feature isn't implemented yet");
if(obj instanceof Vector)
{
for(int i=0;i<this.len();i++)
{
if(((Vector)obj).get(i)!=this.get(i))
{
return false;
}
}
return true;
}
else
{
return false;
}
}
}
Loading