Class Vector

java.lang.Object
org.moeaframework.util.Vector

public class Vector extends Object
Mathematical operators for manipulating vectors (double arrays).
  • Method Summary

    Modifier and Type
    Method
    Description
    static 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)
    Returns true 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 vector u orthogonal to the already orthogonalized vectors vs.
    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 of u onto v.
    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 to 1.0.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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 vector
      v - 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 to 1.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 using WeightGenerator.
      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 vector
      v - 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 vector
      v - 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 value
      u - 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 calling multiply(-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 vector
      a - 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 vector
      v - 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 of u onto v. The two vectors must be the same length.
      Parameters:
      u - the vector being projected
      v - the vector onto which u is being projected
      Returns:
      the projection of u onto v
      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

      public static double[] orthogonalize(double[] u, Iterable<double[]> vs)
      Returns the vector u orthogonal to the already orthogonalized vectors vs. 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 vector
      vs - the already orthogonalized vectors
      Returns:
      the vector u orthogonal to the already orthogonalized vectors vs
    • 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)
      Returns true 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 point
      line - the line
      Returns:
      the minimum distance