Package org.moeaframework.util
Class Vector
java.lang.Object
org.moeaframework.util.Vector
Mathematical operators for manipulating vectors (double arrays).
-
Method Summary
Modifier and TypeMethodDescriptionstatic double[]
add
(double[] u, double[] v) Returns the sum of the two specified vectors,u + v
.static double[]
divide
(double[] u, double a) Returns the scalar division of the specified vector,u / a
.static double
dot
(double[] u, double[] v) Returns the dot (inner) product of the two specified vectors.static boolean
isZero
(double[] u) Returnstrue
if the specified vector contains all zeros;false
otherwise.static double
magnitude
(double[] u) Returns the magnitude (Euclidean norm) of the specified vector.static double[]
mean
(double[][] vs) Returns the mean vector of the specified vectors.static double[]
multiply
(double a, double[] u) Returns the scalar multiple of the specified vector,a * u
.static double[]
negate
(double[] u) Returns the negation of the specified vector,-u
.static double[]
normalize
(double[] u) Returns the specified vector normalized to have a magnitude of 1.static double[]
of
(int n, double v) Returns a vector filled with the given value.static double[][]
orthogonalize
(double[][] vs) Returns the orthogonal basis for the specified vectors using the Gram-Schmidt process.static double[]
orthogonalize
(double[] u, Iterable<double[]> vs) Returns the vectoru
orthogonal to the already orthogonalized vectorsvs
.static double
pointLineDistance
(double[] point, double[] line) Returns the perpendicular distance between a point and a line passing through the origin(0, ..., 0)
.static double[]
project
(double[] u, double[] v) Returns the projection ofu
ontov
.static double[]
subtract
(double[] u, double[] v) Returns the difference between the two specified vectors,u - v
.static double[]
uniform
(int n) Returns a vector filled with randomly generated numbers that sum to1.0
.
-
Method Details
-
of
public static double[] of(int n, double v) Returns a vector filled with the given value.- Parameters:
n
- the length of the vectorv
- the fill value- Returns:
- the vector containing the given fill value
-
uniform
public static double[] uniform(int n) Returns a vector filled with randomly generated numbers that sum to1.0
. Usage of this method is generally fine if only a small number of random vectors is needed, as the generated vectors are biased towards the center and less likely near the boundaries. If needing many vectors that are uniformly distributed in space, prefer usingWeightGenerator
.- Parameters:
n
- the length of the vector- Returns:
- the generated vector
-
subtract
public static double[] subtract(double[] u, double[] v) Returns the difference between the two specified vectors,u - v
. The two vectors must be of the same length.- Parameters:
u
- the first vectorv
- the second vector- Returns:
- the difference between the two specified vectors,
u - v
- Throws:
IllegalArgumentException
- if the two vectors are not the same length
-
add
public static double[] add(double[] u, double[] v) Returns the sum of the two specified vectors,u + v
. The two vectors must be of the same length.- Parameters:
u
- the first vectorv
- the second vector- Returns:
- the sum of the two specified vectors,
u + v
- Throws:
IllegalArgumentException
- if the two vectors are not the same length
-
multiply
public static double[] multiply(double a, double[] u) Returns the scalar multiple of the specified vector,a * u
.- Parameters:
a
- the scalar valueu
- the vector- Returns:
- the scalar multiple of the specified vector,
a * u
-
negate
public static double[] negate(double[] u) Returns the negation of the specified vector,-u
. This is equivalent to callingmultiply(-1, u)
.- Parameters:
u
- the vector- Returns:
- the negation of the specified vector,
-u
-
divide
public static double[] divide(double[] u, double a) Returns the scalar division of the specified vector,u / a
.- Parameters:
u
- the vectora
- the scalar value (the denominator)- Returns:
- the scalar division of the specified vector,
u / a
-
dot
public static double dot(double[] u, double[] v) Returns the dot (inner) product of the two specified vectors. The two vectors must be the same length.- Parameters:
u
- the first vectorv
- the second vector- Returns:
- the dot (inner) product of the two specified vectors
- Throws:
IllegalArgumentException
- if the two vectors are not the same length
-
magnitude
public static double magnitude(double[] u) Returns the magnitude (Euclidean norm) of the specified vector.- Parameters:
u
- the vector- Returns:
- the magnitude (Euclidean norm) of the specified vector
-
normalize
public static double[] normalize(double[] u) Returns the specified vector normalized to have a magnitude of 1. The specified vector must contain at least one non-zero component; otherwise an exception is thrown.- Parameters:
u
- the vector- Returns:
- the specified vector normalized to have a magnitude of 1
- Throws:
IllegalArgumentException
- if the specified vector contains all zeros
-
project
public static double[] project(double[] u, double[] v) Returns the projection ofu
ontov
. The two vectors must be the same length.- Parameters:
u
- the vector being projectedv
- the vector onto whichu
is being projected- Returns:
- the projection of
u
ontov
- Throws:
IllegalArgumentException
- if the two vectors are not the same length
-
orthogonalize
public static double[][] orthogonalize(double[][] vs) Returns the orthogonal basis for the specified vectors using the Gram-Schmidt process.- Parameters:
vs
- the vectors to be orthogonalized- Returns:
- the orthogonal basis
-
orthogonalize
Returns the vectoru
orthogonal to the already orthogonalized vectorsvs
. This method is provided to allow incremental construction of the orthogonal basis:List<double[]> basis = new ArrayList<double[]>(); for (double[] v : vectors) { double[] e = orthogonalize(v, basis); basis.add(e); }
- Parameters:
u
- the vectorvs
- the already orthogonalized vectors- Returns:
- the vector
u
orthogonal to the already orthogonalized vectorsvs
-
mean
public static double[] mean(double[][] vs) Returns the mean vector of the specified vectors.- Parameters:
vs
- the vectors- Returns:
- the mean vector of the specified vectors
- Throws:
IllegalArgumentException
- if the specified vectors is empty
-
isZero
public static boolean isZero(double[] u) Returnstrue
if the specified vector contains all zeros;false
otherwise.- Parameters:
u
- the vector- Returns:
true
if the specified vector contains all zeros;false
otherwise
-
pointLineDistance
public static double pointLineDistance(double[] point, double[] line) Returns the perpendicular distance between a point and a line passing through the origin(0, ..., 0)
.- Parameters:
point
- the pointline
- the line- Returns:
- the minimum distance
-