I've seen in Java a class vector implemented, it's very useful because you can declare vectors without say the size of the vector explicitly, and it has a lot of functions operating with its elements. But it hasn't implemented mathematical operations: sum, subtract, inner and outer products and so on. So I've made and extended class with these math operators and functions. Here's the code:
package vectors.com;
import java.util.Collection;
import java.util.Vector;
public class Vectors extends Vector<Double> {
private static final long serialVersionUID = 1L;
public Vectors() {super();}
public Vectors(Collection<? extends Double> c) {super(c);}
public Vectors(int initialCapacity, int capacityIncrement) {super(initialCapacity, capacityIncrement);}
public Vectors(int initialCapacity) {super(initialCapacity);}
public static long getSerialversionuid() {return serialVersionUID;}
public Vectors opposite() {
for(int i = 0; i < this.size(); ++i) this.set(i, -this.get(i));
return this;
}
public Vectors sum(Vectors w) {
if(this.size() != w.size()) throw new IllegalArgumentException("The dimmensions must be equals.");
Vector<Double> v = new Vector<Double>();
Vectors z = new Vectors(v);
Double f = 0.;
for(int i = 0; i < this.size(); ++i) {
f = this.get(i) + w.get(i);
z.add(i, f);
}
return z;
}
public Vectors subtract(Vectors w) {
if(this.size() != w.size()) throw new IllegalArgumentException("The dimmensions must be equals.");
return this.sum(w.opposite());
}
public Vectors externProduct(Double lambda) {
for(int i = 0; i < this.size(); ++i) {
this.set(i, lambda * this.get(i));
}
return this;
}
public Double scalarProduct(Vectors w) {
if(this.size() != w.size()) throw new IllegalArgumentException("The dimmensions must be equals.");
Double s = 0.;
for(int i = 0; i < this.size(); ++i) {
s += this.get(i) * w.get(i);
}
return s;
}
public Double absolute() {
Double radicand = 0.;
for(int i = 0; i < this.size(); ++i) radicand += Math.pow(this.get(i), 2);
return Math.sqrt(radicand);
}
public Double angle(Vectors w) {
return Math.acos(this.scalarProduct(w) / (this.absolute() * w.absolute()));
}
public Vectors makeVector(Vectors w) {
return w.subtract(this);
}
private Double determinant(Double[][] A) {
int rows = A.length;
int columns = A[0].length;
if(rows != columns) throw new IllegalArgumentException("Rows and Columns must be equals.");
if(rows == 1) return A[0][0];
if(rows == 2) return A[0][0] * A[1][1] - A[1][0] * A[0][1];
return A[0][0] * A[1][1] * A[2][2] + A[1][0] * A[2][1] * A[0][2] + A[0][1] * A[1][2] * A[2][0] -
( A[0][2] * A[1][1] * A[2][0] + A[0][1] * A[1][0] * A[2][2] + A[1][2] * A[2][1] * A[0][0] );
}
public Vectors vectorialProduct(Vectors w) {
if(this.size() != w.size()) throw new IllegalArgumentException("Number of components must be equals.");
if(this.size() != 3) throw new IllegalArgumentException("Sorry, only for 3D vectors.");
Double[][] A = {{this.get(1), this.get(2)}, {w.get(1), w.get(2)}};
Double[][] B = {{this.get(0), this.get(2)}, {w.get(0), w.get(2)}};
Double[][] C = {{this.get(0), this.get(1)}, {w.get(0), w.get(1)}};
Vector<Double> z = new Vector<Double>();
Vectors z1 = new Vectors(z);
z1.set(0, determinant(A));
z1.set(1, determinant(B));
z1.set(2, determinant(C));
return z1;
}
public Double mixProduct(Vectors v, Vectors w) {
if(this.size() != v.size() || this.size() != w.size() || v.size() != w.size())
throw new IllegalArgumentException("Number of components must be equals.");
if(this.size() != 3 || v.size() != 3 || this.size() != 3)
throw new IllegalArgumentException("Sorry, only for 3D vectors.");
Double[][] A = new Double[3][3];
for(int i = 0; i < this.size(); ++i) {
A[0][i] = this.get(i);
A[1][i] = v.get(i);
A[2][i] = w.get(i);
}
return determinant(A);
}
@Override
public int hashCode() {return super.hashCode();}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (!super.equals(obj)) return false;
if (getClass() != obj.getClass()) return false;
return true;
}
}
Thanks