Class Vector


  • public class Vector
    extends Object
    The Vector class represents a d-dimensional Euclidean vector. Vectors are immutable: their values cannot be changed after they are created. It includes methods for addition, subtraction, dot product, scalar product, unit vector, Euclidean norm, and the Euclidean distance between two vectors.

    For additional documentation, see Section 1.2 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.

    Author:
    Robert Sedgewick, Kevin Wayne
    • Constructor Summary

      Constructors 
      Constructor Description
      Vector​(double... a)
      Initializes a vector from either an array or a vararg list.
      Vector​(int d)
      Initializes a d-dimensional zero vector.
    • Constructor Detail

      • Vector

        public Vector​(int d)
        Initializes a d-dimensional zero vector.
        Parameters:
        d - the dimension of the vector
      • Vector

        public Vector​(double... a)
        Initializes a vector from either an array or a vararg list. The vararg syntax supports a constructor that takes a variable number of arguments such as Vector x = new Vector(1.0, 2.0, 3.0, 4.0).
        Parameters:
        a - the array or vararg list
    • Method Detail

      • dimension

        public int dimension()
        Returns the dimension of this vector.
        Returns:
        the dimension of this vector
      • dot

        public double dot​(Vector that)
        Returns the dot product of this vector with the specified vector.
        Parameters:
        that - the other vector
        Returns:
        the dot product of this vector and that vector
        Throws:
        IllegalArgumentException - if the dimensions of the two vectors are not equal
      • magnitude

        public double magnitude()
        Returns the magnitude of this vector. This is also known as the L2 norm or the Euclidean norm.
        Returns:
        the magnitude of this vector
      • distanceTo

        public double distanceTo​(Vector that)
        Returns the Euclidean distance between this vector and the specified vector.
        Parameters:
        that - the other vector
        Returns:
        the Euclidean distance between this vector and that vector
        Throws:
        IllegalArgumentException - if the dimensions of the two vectors are not equal
      • plus

        public Vector plus​(Vector that)
        Returns the sum of this vector and the specified vector.
        Parameters:
        that - the vector to add to this vector
        Returns:
        the vector whose value is (this + that)
        Throws:
        IllegalArgumentException - if the dimensions of the two vectors are not equal
      • minus

        public Vector minus​(Vector that)
        Returns the difference between this vector and the specified vector.
        Parameters:
        that - the vector to subtract from this vector
        Returns:
        the vector whose value is (this - that)
        Throws:
        IllegalArgumentException - if the dimensions of the two vectors are not equal
      • cartesian

        public double cartesian​(int i)
        Returns the ith cartesian coordinate.
        Parameters:
        i - the coordinate index
        Returns:
        the ith cartesian coordinate
      • scale

        public Vector scale​(double alpha)
        Returns the scalar-vector product of this vector and the specified scalar
        Parameters:
        alpha - the scalar
        Returns:
        the vector whose value is (alpha * this)
      • direction

        public Vector direction()
        Returns a unit vector in the direction of this vector.
        Returns:
        a unit vector in the direction of this vector
        Throws:
        ArithmeticException - if this vector is the zero vector
      • toString

        public String toString()
        Returns a string representation of this vector.
        Overrides:
        toString in class Object
        Returns:
        a string representation of this vector, which consists of the vector entries, separates by single spaces
      • main

        public static void main​(String[] args)
        Unit tests the Vector data type.
        Parameters:
        args - the command-line arguments