Class ArrayVector

java.lang.Object
civitas.celestis.util.tuple.DoubleArrayTuple
civitas.celestis.math.vector.ArrayVector
All Implemented Interfaces:
Vector<ArrayVector>, BaseTuple<Double>, DoubleTuple, Serializable

public class ArrayVector extends DoubleArrayTuple implements Vector<ArrayVector>
An arbitrary dimensional array-based vector.
See Also:
  • Constructor Details

    • ArrayVector

      public ArrayVector(@Nonnull double... components)
      Creates a new vector.
      Parameters:
      components - The components of this vector in sequential order
    • ArrayVector

      public ArrayVector(@Nonnull DoubleTuple t)
      Creates a new vector.
      Parameters:
      t - The tuple of which to copy component values from
  • Method Details

    • norm

      public double norm()
      Returns the Euclidean norm (the magnitude) of this vector.
      Specified by:
      norm in interface Vector<ArrayVector>
      Returns:
      The Euclidean norm of this vector
    • norm2

      public double norm2()
      Returns the squared Euclidean norm (the squared magnitude) of this vector.
      Specified by:
      norm2 in interface Vector<ArrayVector>
      Returns:
      The squared Euclidean norm of this vector
    • normManhattan

      public double normManhattan()
      Returns the Manhattan norm of this vector.
      Specified by:
      normManhattan in interface Vector<ArrayVector>
      Returns:
      The Manhattan norm of this vector
    • add

      @Nonnull public ArrayVector add(double s)
      Adds a scalar to this vector component-wise, then returns the resulting vector.
      Specified by:
      add in interface Vector<ArrayVector>
      Parameters:
      s - The scalar of which to add to each component of this vector
      Returns:
      The resulting vector
    • subtract

      @Nonnull public ArrayVector subtract(double s)
      Subtracts this vector by a scalar component-wise, then returns the resulting vector.
      Specified by:
      subtract in interface Vector<ArrayVector>
      Parameters:
      s - The scalar of which to subtract from each component of this vector
      Returns:
      The resulting vector
    • multiply

      @Nonnull public ArrayVector multiply(double s)
      Multiplies this vector by a scalar component-wise, then returns the resulting vector.
      Specified by:
      multiply in interface Vector<ArrayVector>
      Parameters:
      s - The scalar of which to multiply each component of this vector by
      Returns:
      The resulting vector
    • divide

      @Nonnull public ArrayVector divide(double s) throws ArithmeticException
      Divides this vector by a scalar component-wise, then returns the resulting vector.
      Specified by:
      divide in interface Vector<ArrayVector>
      Parameters:
      s - The scalar of which to divide each component of this vector by
      Returns:
      The resulting vector
      Throws:
      ArithmeticException - When the denominator s is zero
    • add

      @Nonnull public ArrayVector add(@Nonnull ArrayVector v)
      Adds another vector to this vector, then returns the resulting vector.
      Specified by:
      add in interface Vector<ArrayVector>
      Parameters:
      v - The vector of which to add to this vector
      Returns:
      The resulting vector
    • subtract

      @Nonnull public ArrayVector subtract(@Nonnull ArrayVector v)
      Subtracts another vector from this vector, then returns the resulting vector.
      Specified by:
      subtract in interface Vector<ArrayVector>
      Parameters:
      v - The vector of which to subtract from this vector
      Returns:
      The resulting vector
    • dot

      public double dot(@Nonnull ArrayVector v)
      Returns the dot product between this vector and the provided vector v.
      Specified by:
      dot in interface Vector<ArrayVector>
      Parameters:
      v - The vector of which to get the dot product between
      Returns:
      The dot product between the two vectors
    • normalize

      @Nonnull public ArrayVector normalize() throws ArithmeticException
      Normalizes this vector to have a Euclidean norm (magnitude) of 1.
      Specified by:
      normalize in interface Vector<ArrayVector>
      Returns:
      The normalized vector of this vector
      Throws:
      ArithmeticException - When the Euclidean norm of this vector is zero
    • normalizeOrZero

      @Nonnull public ArrayVector normalizeOrZero()
      Normalizes this vector ot have a Euclidean norm (magnitude) of 1. If this vector's Euclidean norm is zero, this will return itself. (which is guaranteed to be a vector where all components are zero, as only zero vectors have a Euclidean norm of zero)
      Specified by:
      normalizeOrZero in interface Vector<ArrayVector>
      Returns:
      The normalized vector of this vector if successful, zero otherwise
    • normalizeOrDefault

      @Nonnull public ArrayVector normalizeOrDefault(@Nonnull ArrayVector v)
      Normalizes this vector to have a Euclidean norm (magnitude) of 1. If this vector's Euclidean norm is zero, this will return the provided fallback value v.
      Specified by:
      normalizeOrDefault in interface Vector<ArrayVector>
      Parameters:
      v - The value of which to return in case of division by zero
      Returns:
      The normalized vector of this vector if successful, the fallback value v otherwise
    • negate

      @Nonnull public ArrayVector negate()
      Negates all components of this vector, then returns the resulting vector.
      Specified by:
      negate in interface Vector<ArrayVector>
      Returns:
      The negation of this vector
    • min

      @Nonnull public ArrayVector min(@Nonnull ArrayVector v)
      Between this vector and the provided boundary vector v, this takes the minimum of each corresponding pair of components, then returns a new vector whose components are populated from that of the minimum values between each pair of components.
      Specified by:
      min in interface Vector<ArrayVector>
      Parameters:
      v - The boundary vector of which to compare to
      Returns:
      The minimum vector of the two vectors
    • max

      @Nonnull public ArrayVector max(@Nonnull ArrayVector v)
      Between this vector and the provided boundary vector v, this takes the maximum of each corresponding pair of components, then returns a new vector whose components are populated from that of the maximum values between each pair of components.
      Specified by:
      max in interface Vector<ArrayVector>
      Parameters:
      v - The boundary vector of which to compare to
      Returns:
      The maximum vector of the two vectors
    • clamp

      @Nonnull public ArrayVector clamp(@Nonnull ArrayVector min, @Nonnull ArrayVector max)
      Between this vector and the provided boundary vectors min and max, this returns a new vector whose components are clamped to respect the range of [min, max]. This is the equivalent of clamping each component to the corresponding pair of minimum and maximum values.
      Specified by:
      clamp in interface Vector<ArrayVector>
      Parameters:
      min - The minimum boundary vector to compare to
      max - The maximum boundary vector to compare to
      Returns:
      The clamped vector which respects the boundaries of [min, max]
    • distance

      public double distance(@Nonnull ArrayVector v)
      Returns the Euclidean distance between this vector and the provided vector v.
      Specified by:
      distance in interface Vector<ArrayVector>
      Parameters:
      v - The vector of which get the Euclidean distance between
      Returns:
      The Euclidean distance between the two vectors
    • distance2

      public double distance2(@Nonnull ArrayVector v)
      Returns the squared Euclidean distance between this vector and the provided vector v.
      Specified by:
      distance2 in interface Vector<ArrayVector>
      Parameters:
      v - The vector of which to get the squared Euclidean distance between
      Returns:
      The squared Euclidean distance between the two vectors
    • distanceManhattan

      public double distanceManhattan(@Nonnull ArrayVector v)
      Returns the Manhattan distance between this vector and the provided vector v.
      Specified by:
      distanceManhattan in interface Vector<ArrayVector>
      Parameters:
      v - The vector of which to get the Manhattan distance between
      Returns:
      The Manhattan distance between the two vectors
    • map

      @Nonnull public ArrayVector map(@Nonnull DoubleUnaryOperator f)
      Applies the provided mapper function f to each component of this tuple, then returns a new tuple containing the return values of the function f.
      Specified by:
      map in interface DoubleTuple
      Specified by:
      map in interface Vector<ArrayVector>
      Overrides:
      map in class DoubleArrayTuple
      Parameters:
      f - The function of which to apply to each component of this vector
      Returns:
      The resulting tuple
    • merge

      @Nonnull public ArrayVector merge(@Nonnull ArrayVector v, @Nonnull BinaryOperator<Double> f)
      Between this vector and the provided vector v, this applies the merger function f to each corresponding pair of components, then returns a new vector whose components are populated from the return values of the function f.
      Specified by:
      merge in interface Vector<ArrayVector>
      Parameters:
      v - The vector of which to merge this vector with
      f - The merger function to handle the merging of the two vectors
      Returns:
      The resulting vector
    • equals

      public boolean equals(@Nullable ArrayVector v)
      Checks for equality between this vector and the provided vector v.
      Specified by:
      equals in interface Vector<ArrayVector>
      Parameters:
      v - The vector to compare to
      Returns:
      true if the component values are equal