Commit 1c08b9c7 authored by Nico Mack's avatar Nico Mack

Implementation of n-dimensional Vector object

parent 44c7ce72
......@@ -16,6 +16,10 @@
*/
package lu.list.itis.dkd.tui.utility.vector;
import com.jgoodies.common.base.Preconditions;
import java.util.Arrays;
/**
* @author mack
* @since [major].[minor]
......@@ -25,25 +29,82 @@ package lu.list.itis.dkd.tui.utility.vector;
// * Class Definition and Members *
// ***************************************************************************
public abstract class Vector {
public class Vector {
private double[] dimensions;
private int numberOfDimensions;
private static boolean[] pins;
// ***************************************************************************
// * Constants *
// ***************************************************************************
private static final String OUT_OF_DIMENSIONS_MESSAGE = "Requested dimension higher than vector dimensionality!"; //$NON-NLS-1$
// ---------------------------------------------------------------------------
// ***************************************************************************
// * Constructor(s)
// ***************************************************************************
// ---------------------------------------------------------------------------
/**
* @param axes
*/
// ---------------------------------------------------------------------------
public Vector(double... axes) {
numberOfDimensions = axes.length;
dimensions = new double[numberOfDimensions];
int dimension = 0;
for (double coordinate : axes) {
dimensions[dimension++] = coordinate;
}
}
// ---------------------------------------------------------------------------
// ***************************************************************************
// * Primitives
// ***************************************************************************
// ---------------------------------------------------------------------------
// private double nthRoot(double value) {
// return Math.pow(Math.E, Math.log(numberOfDimensions) / value);
// }
// ---------------------------------------------------------------------------
// ***************************************************************************
// * Class Body
// ***************************************************************************
// ---------------------------------------------------------------------------
/**
* @param newPins
*/
// ---------------------------------------------------------------------------
public static void pinDimensions(boolean... newPins) {
Vector.pins = newPins;
}
// ---------------------------------------------------------------------------
/**
* @param dimension
* @return
*/
// ---------------------------------------------------------------------------
public static boolean dimensionPinned(int dimension) {
return ((pins != null) && (dimension < pins.length)) ? pins[dimension] : false;
}
// ---------------------------------------------------------------------------
/**
* @return
*/
// ---------------------------------------------------------------------------
public abstract int getNumberOfDimensions();
public int getNumberOfDimensions() {
return numberOfDimensions;
}
// ---------------------------------------------------------------------------
/**
......@@ -52,7 +113,13 @@ public abstract class Vector {
*/
// ---------------------------------------------------------------------------
public abstract Vector random(double scale);
public Vector random(double scale) {
double[] randoms = new double[numberOfDimensions];
for (int dimension = 0; dimension < numberOfDimensions; dimension++) {
randoms[dimension] = (dimensionPinned(dimension)) ? scale * (Math.random() - 0.5) : dimensions[dimension];
}
return new Vector(randoms);
}
// ---------------------------------------------------------------------------
/**
......@@ -60,7 +127,12 @@ public abstract class Vector {
*/
// ---------------------------------------------------------------------------
public abstract Vector zero();
public Vector zero() {
for (int dimension = 0; dimension < numberOfDimensions; dimension++) {
dimensions[dimension] = 0;
}
return this;
}
// ---------------------------------------------------------------------------
/**
......@@ -69,7 +141,15 @@ public abstract class Vector {
*/
// ---------------------------------------------------------------------------
public abstract Vector add(Vector operand);
public Vector add(Vector operand) {
Preconditions.checkArgument(operand.numberOfDimensions == this.numberOfDimensions, "Can't add Vectors with mismatched dimensionality"); //$NON-NLS-1$
double[] sum = new double[numberOfDimensions];
for (int dimension = 0; dimension < numberOfDimensions; dimension++) {
sum[dimension] = this.dimensions[dimension] + ((dimensionPinned(dimension)) ? operand.dimensions[dimension] : 0d);
}
return new Vector(sum);
}
// ---------------------------------------------------------------------------
/**
......@@ -78,7 +158,15 @@ public abstract class Vector {
*/
// ---------------------------------------------------------------------------
public abstract Vector subtract(Vector operand);
public Vector subtract(Vector operand) {
Preconditions.checkArgument(operand.numberOfDimensions == this.numberOfDimensions, "Can't subtrcat Vectors with mismatched dimensionality"); //$NON-NLS-1$
double[] difference = new double[numberOfDimensions];
for (int dimension = 0; dimension < numberOfDimensions; dimension++) {
difference[dimension] = this.dimensions[dimension] - ((dimensionPinned(dimension)) ? operand.dimensions[dimension] : 0d);
}
return new Vector(difference);
}
// ---------------------------------------------------------------------------
/**
......@@ -88,7 +176,14 @@ public abstract class Vector {
*/
// ---------------------------------------------------------------------------
public abstract Vector multiplyBy(double factor);
public Vector multiplyBy(double factor) {
double[] product = new double[numberOfDimensions];
for (int dimension = 0; dimension < numberOfDimensions; dimension++) {
product[dimension] = this.dimensions[dimension] * ((dimensionPinned(dimension)) ? factor : 1d);
}
return new Vector(product);
}
// ---------------------------------------------------------------------------
/**
......@@ -98,17 +193,33 @@ public abstract class Vector {
*/
// ---------------------------------------------------------------------------
public abstract Vector atAngle(double angle);
public Vector divideBy(double factor) {
double[] division = new double[numberOfDimensions];
if (factor > 0) {
for (int dimension = 0; dimension < numberOfDimensions; dimension++) {
division[dimension] = this.dimensions[dimension] / ((dimensionPinned(dimension)) ? factor : 1d);
}
} else {
Arrays.fill(division, 0);
}
return new Vector(division);
}
// ---------------------------------------------------------------------------
/**
* @param factor
* specifies the factor to multiply this dimension with.
* @return a new Dimension object representing this instance scaled by the specified factor.
* @return
*/
// ---------------------------------------------------------------------------
public abstract Vector divideBy(double factor);
public double magnitude() {
double sumOfSquares = 0;
for (int dimension = 0; dimension < numberOfDimensions; dimension++) {
sumOfSquares += this.dimensions[dimension] * this.dimensions[dimension];
}
return Math.sqrt(sumOfSquares);
}
// ---------------------------------------------------------------------------
/**
......@@ -116,25 +227,35 @@ public abstract class Vector {
*/
// ---------------------------------------------------------------------------
public abstract double magnitude();
public Vector normalize() {
return this.divideBy(this.magnitude());
}
// ---------------------------------------------------------------------------
/**
* @param dimension
* @return
*/
// ---------------------------------------------------------------------------
public Vector normalize() {
return this.divideBy(this.magnitude());
public double getDimension(int dimension) {
Preconditions.checkArgument(dimension < this.numberOfDimensions, OUT_OF_DIMENSIONS_MESSAGE);
return dimensions[dimension];
}
// ---------------------------------------------------------------------------
/**
* @return
* @param dimension
* @param value
*/
// ---------------------------------------------------------------------------
public abstract Vector normal();
public void setDimension(int dimension, double value) {
Preconditions.checkArgument(dimension < this.numberOfDimensions, OUT_OF_DIMENSIONS_MESSAGE);
dimensions[dimension] = value;
}
// ---------------------------------------------------------------------------
// ***************************************************************************
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment