Class Vector1D

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  Vector1D.Sum
      Class used to create high-accuracy sums of vectors.
      static class  Vector1D.Unit
      Represent unit vectors.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private Vector1D​(double x)
      Simple constructor.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      Vector1D add​(double factor, Vector1D v)
      Add a scaled vector to the instance.
      Vector1D add​(Vector1D v)
      Add a vector to the instance.
      double angle​(Vector1D v)
      Compute the angular separation between two vectors in radians.
      Vector1D.Unit directionTo​(Vector1D v)
      Return the unit vector representing the direction of displacement from this vector to the given vector.
      double distance​(Vector1D v)
      Compute the distance between the instance and another vector.
      double distanceSq​(Vector1D v)
      Compute the square of the distance between the instance and another vector.
      double dot​(Vector1D v)
      Compute the dot-product of the instance and another vector.
      boolean eq​(Vector1D vec, org.apache.commons.numbers.core.Precision.DoubleEquivalence precision)
      Return true if the current instance and given vector are considered equal as evaluated by the given precision context.
      boolean equals​(java.lang.Object other)
      Test for the equality of two vectors.
      int getDimension()
      Returns the number of dimensions in the space that this element belongs to.
      double getX()
      Returns the abscissa (coordinate value) of the instance.
      Vector1D getZero()
      Get the zero (null) vector of the space.
      int hashCode()
      Get a hashCode for the vector.
      boolean isFinite()
      Returns true if all values in this element are finite, meaning they are not NaN or infinite.
      boolean isInfinite()
      Returns true if any value in this element is infinite and none are NaN; otherwise, returns false.
      boolean isNaN()
      Returns true if any value in this element is NaN; otherwise returns false.
      Vector1D lerp​(Vector1D p, double t)
      Get a vector constructed by linearly interpolating between this vector and the given vector.
      Vector1D multiply​(double a)
      Multiply the instance by a scalar.
      Vector1D negate()
      Get the negation of the instance.
      double norm()
      Get the L2 norm (commonly known as the Euclidean norm) for the vector.
      Vector1D.Unit normalize()
      Get a normalized vector aligned with the instance.
      Vector1D.Unit normalizeOrNull()
      Attempt to compute a normalized vector aligned with the instance, returning null if such a vector cannot be computed.
      double normSq()
      Get the square of the L2 norm (also known as the Euclidean norm) for the vector.
      static Vector1D of​(double x)
      Returns a vector with the given coordinate value.
      static Vector1D parse​(java.lang.String str)
      Parses the given string and returns a new vector instance.
      Vector1D subtract​(double factor, Vector1D v)
      Subtract a scaled vector from the instance.
      Vector1D subtract​(Vector1D v)
      Subtract a vector from the instance.
      java.lang.String toString()
      Vector1D transform​(java.util.function.UnaryOperator<Vector1D> fn)
      Convenience method to apply a function to this vector.
      Vector1D vectorTo​(Vector1D v)
      Return the vector representing the displacement from this vector to the given vector.
      Vector1D withNorm​(double magnitude)
      Returns a vector with the same direction but with the given norm.
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Field Detail

      • ZERO

        public static final Vector1D ZERO
        Zero vector (coordinates: 0).
      • NaN

        public static final Vector1D NaN
        A vector with all coordinates set to NaN.
      • POSITIVE_INFINITY

        public static final Vector1D POSITIVE_INFINITY
        A vector with all coordinates set to positive infinity.
      • NEGATIVE_INFINITY

        public static final Vector1D NEGATIVE_INFINITY
        A vector with all coordinates set to negative infinity.
      • COORDINATE_ASCENDING_ORDER

        public static final java.util.Comparator<Vector1D> COORDINATE_ASCENDING_ORDER
        Comparator that sorts vectors in component-wise ascending order. Vectors are only considered equal if their coordinates match exactly. Null arguments are evaluated as being greater than non-null arguments.
      • x

        private final double x
        Abscissa (coordinate value).
    • Constructor Detail

      • Vector1D

        private Vector1D​(double x)
        Simple constructor.
        Parameters:
        x - abscissa (coordinate value)
    • Method Detail

      • getX

        public double getX()
        Returns the abscissa (coordinate value) of the instance.
        Returns:
        the abscissa value
      • getDimension

        public int getDimension()
        Returns the number of dimensions in the space that this element belongs to.
        Returns:
        the number of dimensions in the element's space
      • isNaN

        public boolean isNaN()
        Returns true if any value in this element is NaN; otherwise returns false.
        Returns:
        true if any value in this element is NaN
      • isInfinite

        public boolean isInfinite()
        Returns true if any value in this element is infinite and none are NaN; otherwise, returns false.
        Returns:
        true if any value in this element is infinite and none are NaN
      • isFinite

        public boolean isFinite()
        Returns true if all values in this element are finite, meaning they are not NaN or infinite.
        Returns:
        true if all values in this element are finite
      • vectorTo

        public Vector1D vectorTo​(Vector1D v)
        Return the vector representing the displacement from this vector to the given vector. This is exactly equivalent to v.subtract(thisVector) but with a method name that is much easier to visualize.
        Specified by:
        vectorTo in class EuclideanVector<Vector1D>
        Parameters:
        v - the vector that the returned vector will be directed toward
        Returns:
        vector representing the displacement from this vector to the given vector
      • directionTo

        public Vector1D.Unit directionTo​(Vector1D v)
        Return the unit vector representing the direction of displacement from this vector to the given vector. This is exactly equivalent to v.subtract(thisVector).normalize() but without the intermediate vector instance.
        Specified by:
        directionTo in class EuclideanVector<Vector1D>
        Parameters:
        v - the vector that the returned vector will be directed toward
        Returns:
        unit vector representing the direction of displacement from this vector to the given vector
      • lerp

        public Vector1D lerp​(Vector1D p,
                             double t)
        Get a vector constructed by linearly interpolating between this vector and the given vector. The vector coordinates are generated by the equation V = (1 - t)*A + t*B, where A is the current vector and B is the given vector. This means that if t = 0, a vector equal to the current vector will be returned. If t = 1, a vector equal to the argument will be returned. The t parameter is not constrained to the range [0, 1], meaning that linear extrapolation can also be performed with this method.
        Specified by:
        lerp in class EuclideanVector<Vector1D>
        Parameters:
        p - other vector
        t - interpolation parameter
        Returns:
        interpolated or extrapolated vector
      • getZero

        public Vector1D getZero()
        Get the zero (null) vector of the space.
        Returns:
        zero vector of the space
      • norm

        public double norm()
        Get the L2 norm (commonly known as the Euclidean norm) for the vector. This corresponds to the common notion of vector magnitude or length and is defined as the square root of the sum of the squares of all vector components.
        Returns:
        L2 norm for the vector
        See Also:
        L2 Norm
      • normSq

        public double normSq()
        Get the square of the L2 norm (also known as the Euclidean norm) for the vector. This is equal to the sum of the squares of all vector components.
        Returns:
        square of the L2 norm for the vector
        See Also:
        Vector.norm()
      • withNorm

        public Vector1D withNorm​(double magnitude)
        Returns a vector with the same direction but with the given norm. This is equivalent to calling vec.normalize().scalarMultiply(mag) but without the intermediate vector.
        Parameters:
        magnitude - The vector norm
        Returns:
        a vector with the same direction as the current instance but the given norm
      • add

        public Vector1D add​(Vector1D v)
        Add a vector to the instance.
        Parameters:
        v - vector to add
        Returns:
        a new vector
      • add

        public Vector1D add​(double factor,
                            Vector1D v)
        Add a scaled vector to the instance.
        Parameters:
        factor - scale factor to apply to v before adding it
        v - vector to add
        Returns:
        a new vector
      • subtract

        public Vector1D subtract​(Vector1D v)
        Subtract a vector from the instance.
        Parameters:
        v - vector to subtract
        Returns:
        a new vector
      • subtract

        public Vector1D subtract​(double factor,
                                 Vector1D v)
        Subtract a scaled vector from the instance.
        Parameters:
        factor - scale factor to apply to v before subtracting it
        v - vector to subtract
        Returns:
        a new vector
      • negate

        public Vector1D negate()
        Get the negation of the instance.
        Returns:
        a new vector which is the negation of the instance
      • normalize

        public Vector1D.Unit normalize()
        Get a normalized vector aligned with the instance. The returned vector has a magnitude of 1.
        Returns:
        normalized vector
        See Also:
        Vector.normalizeOrNull()
      • normalizeOrNull

        public Vector1D.Unit normalizeOrNull()
        Attempt to compute a normalized vector aligned with the instance, returning null if such a vector cannot be computed. This method is equivalent to Vector.normalize() but returns null instead of throwing an exception on failure.
        Returns:
        normalized vector or null if such a vector cannot be computed, i.e. if the norm is zero, NaN, or infinite
        See Also:
        Vector.normalize()
      • multiply

        public Vector1D multiply​(double a)
        Multiply the instance by a scalar.
        Parameters:
        a - scalar
        Returns:
        a new vector
      • distance

        public double distance​(Vector1D v)
        Compute the distance between the instance and another vector.
        Parameters:
        v - second vector
        Returns:
        the distance between the instance and v
      • distanceSq

        public double distanceSq​(Vector1D v)
        Compute the square of the distance between the instance and another vector.

        Calling this method is equivalent to calling: q.subtract(p).getNormSq() except that no intermediate vector is built

        Parameters:
        v - second vector
        Returns:
        the square of the distance between the instance and p
        See Also:
        Vector.normSq()
      • dot

        public double dot​(Vector1D v)
        Compute the dot-product of the instance and another vector.
        Parameters:
        v - second vector
        Returns:
        the dot product (this · v)
      • angle

        public double angle​(Vector1D v)
        Compute the angular separation between two vectors in radians.

        For the one-dimensional case, this method returns 0 if the vector x values have the same sign and pi if they are opposite.

        Parameters:
        v - other vector
        Returns:
        angular separation between this instance and v in radians
      • transform

        public Vector1D transform​(java.util.function.UnaryOperator<Vector1D> fn)
        Convenience method to apply a function to this vector. This can be used to transform the vector inline with other methods.
        Parameters:
        fn - the function to apply
        Returns:
        the transformed vector
      • eq

        public boolean eq​(Vector1D vec,
                          org.apache.commons.numbers.core.Precision.DoubleEquivalence precision)
        Return true if the current instance and given vector are considered equal as evaluated by the given precision context.

        Equality is determined by comparing each pair of components in turn from the two vectors. If all components evaluate as equal, then the vectors are considered equal. If any are not equal, then the vectors are not considered equal. Note that this approach means that the calculated distance between two "equal" vectors may be as much as √(n * eps2), where n is the number of components in the vector and eps is the maximum epsilon value allowed by the precision context.

        Specified by:
        eq in class EuclideanVector<Vector1D>
        Parameters:
        vec - vector to check for equality
        precision - precision context used to determine floating point equality
        Returns:
        true if the current instance is considered equal to the given vector when using the given precision context; otherwise false
      • hashCode

        public int hashCode()
        Get a hashCode for the vector.

        All NaN values have the same hash code.

        Overrides:
        hashCode in class java.lang.Object
        Returns:
        a hash code value for this object
      • equals

        public boolean equals​(java.lang.Object other)
        Test for the equality of two vectors.

        If all coordinates of two vectors are exactly the same, and none are Double.NaN, the two vectors are considered to be equal.

        NaN coordinates are considered to globally affect the vector and be equal to each other - i.e, if either (or all) coordinates of the vector are equal to Double.NaN, the vector is equal to NaN.

        Overrides:
        equals in class java.lang.Object
        Parameters:
        other - Object to test for equality to this
        Returns:
        true if two vector objects are equal, false if object is null, not an instance of Vector1D, or not equal to this Vector1D instance
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • of

        public static Vector1D of​(double x)
        Returns a vector with the given coordinate value.
        Parameters:
        x - vector coordinate
        Returns:
        vector instance
      • parse

        public static Vector1D parse​(java.lang.String str)
        Parses the given string and returns a new vector instance. The expected string format is the same as that returned by toString().
        Parameters:
        str - the string to parse
        Returns:
        vector instance represented by the string
        Throws:
        java.lang.IllegalArgumentException - if the given string has an invalid format