Class Vector


  • public final class Vector
    extends java.lang.Object
    A vector with double elements
    • Constructor Summary

      Constructors 
      Constructor Description
      Vector​(double... numbers)
      Constructs a vector with the given elements
      Vector​(int dimension)
      Constructs the zero vector of the given dimension
      Vector​(int dimension, java.util.function.IntToDoubleFunction generator)
      Generates a vector with the given dimension, applying a function to the index to compute the value of each element
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      Vector add​(Vector a)
      Adds the given vector to this vector, stores the result in a new vector and returns that vector
      Vector addAndSet​(Vector a)
      Adds the given vector to this vector, modifying this vector
      Vector copy()
      Creates a copy of this vector
      Vector divide​(double scalar)
      Divides this vector by the given scalar, stores the result in a new vector and returns that vector
      Vector divideAndSet​(double scalar)
      Divides this vector by the given scalar, modifying this vector
      double dot​(Vector v)
      Calculates the dot product of this vector and the given vector
      boolean equals​(Vector other, double tolerance)
      Returns whether this vector has the same dimension as the given vector, and all elements of this vector are within tolerance of the corresponding elements in the given vector
      boolean equals​(java.lang.Object other)  
      static Vector fromBigVector​(BigVector v)
      Converts a BigVector to a vector of doubles
      static Vector fromString​(java.lang.String raw)
      Parses a string in wolfram-style vector notation
      double get​(int i)
      Gets the element at the given index in the vector
      int getDimension()
      Gets the dimension of the vector
      double gramSchmidtCoefficient​(Vector v)
      Returns the Gram-Schmidt coefficient when this vector is projected onto the given vector, that is, the ratio of the length of the projection of this vector onto the given vector, to the given vector's length.
      int hashCode()  
      boolean isZero()
      Returns whether this vector is the zero vector
      double magnitude()
      Returns the magnitude (norm) of the vector.
      double magnitudeSq()
      Returns the square of the magnitude (norm) of this vector
      Vector multiply​(double scalar)
      Multiplies this vector by the given scalar, stores the result in a new vector and returns that vector
      Vector multiply​(Matrix m)
      Computes this * m, interpreting this vector as a row vector, stores the result in a new vector and returns that vector
      Vector multiplyAndSet​(double scalar)
      Multiplies this vector by the given scalar, modifying this vector
      static Vector parse​(StringParser parser)
      Parses a vector from a string parser
      Vector projectOnto​(Vector v)
      Calculates the projection of this vector onto the given vector, stores the result in a new vector and returns that vector
      void set​(int i, double value)
      Sets the element at the given index in the vector
      Vector subtract​(Vector a)
      Subtracts the given vector from this vector, stores the result in a new vector and returns that vector
      Vector subtractAndSet​(Vector a)
      Subtracts the given vector from this vector, modifying this vector
      Vector swapNums​(int i, int j)
      Swaps the two numbers at the given indices, stores the result in a new vector and returns that matrix
      Vector swapNumsAndSet​(int i, int j)
      Swaps the two numbers at the given indices, modifying the vector
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

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

      • Vector

        public Vector​(int dimension)
        Constructs the zero vector of the given dimension
        Parameters:
        dimension - The dimension of the vector to create
      • Vector

        public Vector​(double... numbers)
        Constructs a vector with the given elements
        Parameters:
        numbers - The elements of the vector
      • Vector

        public Vector​(int dimension,
                      java.util.function.IntToDoubleFunction generator)
        Generates a vector with the given dimension, applying a function to the index to compute the value of each element
        Parameters:
        dimension - The dimension of the vector
        generator - A function accepting the index and producing the desired value for that index
    • Method Detail

      • getDimension

        public int getDimension()
        Gets the dimension of the vector
        Returns:
        The dimension of the vector
      • get

        public double get​(int i)
        Gets the element at the given index in the vector
        Parameters:
        i - The index
        Returns:
        The element at the given index
        Throws:
        java.lang.IndexOutOfBoundsException - If i is out of bounds
      • set

        public void set​(int i,
                        double value)
        Sets the element at the given index in the vector
        Parameters:
        i - The index
        value - The value to put in that index
        Throws:
        java.lang.IndexOutOfBoundsException - If i is out of bounds
      • magnitude

        public double magnitude()
        Returns the magnitude (norm) of the vector. magnitudeSq() should be used instead of this method where possible
        Returns:
        The magnitude of this vector
      • magnitudeSq

        public double magnitudeSq()
        Returns the square of the magnitude (norm) of this vector
        Returns:
        The square of the magnitude of this vector
      • isZero

        public boolean isZero()
        Returns whether this vector is the zero vector
        Returns:
        Whether this vector is the zero vector
      • add

        public Vector add​(Vector a)
        Adds the given vector to this vector, stores the result in a new vector and returns that vector
        Parameters:
        a - The vector to add
        Returns:
        A new vector containing the result
        Throws:
        java.lang.IllegalArgumentException - If the dimension of the given vector is not the same as the dimension of this vector
      • subtract

        public Vector subtract​(Vector a)
        Subtracts the given vector from this vector, stores the result in a new vector and returns that vector
        Parameters:
        a - The vector to subtract
        Returns:
        A new vector containing the result
        Throws:
        java.lang.IllegalArgumentException - If the dimension of the given vector is not the same as the dimension of this vector
      • multiply

        public Vector multiply​(double scalar)
        Multiplies this vector by the given scalar, stores the result in a new vector and returns that vector
        Parameters:
        scalar - The scalar to multiply by
        Returns:
        A new vector containing the result
      • multiply

        public Vector multiply​(Matrix m)
        Computes this * m, interpreting this vector as a row vector, stores the result in a new vector and returns that vector
        Parameters:
        m - The matrix to right-multiply by
        Returns:
        A new vector containing the result
        Throws:
        java.lang.IllegalArgumentException - If the dimension of this vector is not equal to the number of rows in the given matrix
      • divide

        public Vector divide​(double scalar)
        Divides this vector by the given scalar, stores the result in a new vector and returns that vector
        Parameters:
        scalar - The scalar to divide by
        Returns:
        A new vector containing the result
      • swapNums

        public Vector swapNums​(int i,
                               int j)
        Swaps the two numbers at the given indices, stores the result in a new vector and returns that matrix
        Parameters:
        i - The row to swap with j
        j - The row to swap with i
        Returns:
        A new vector containing the result
        Throws:
        java.lang.IndexOutOfBoundsException - If i or j is out of bounds
      • addAndSet

        public Vector addAndSet​(Vector a)
        Adds the given vector to this vector, modifying this vector
        Parameters:
        a - The vector to add to this vector
        Returns:
        This vector
        Throws:
        java.lang.IllegalArgumentException - If the dimension of the given vector is not the same as the dimension of this vector
      • subtractAndSet

        public Vector subtractAndSet​(Vector a)
        Subtracts the given vector from this vector, modifying this vector
        Parameters:
        a - The vector to subtract from this vector
        Returns:
        This vector
        Throws:
        java.lang.IllegalArgumentException - If the dimension of the given vector is not the same as the dimension of this vector
      • multiplyAndSet

        public Vector multiplyAndSet​(double scalar)
        Multiplies this vector by the given scalar, modifying this vector
        Parameters:
        scalar - The scalar to multiply this vector by
        Returns:
        This vector
      • divideAndSet

        public Vector divideAndSet​(double scalar)
        Divides this vector by the given scalar, modifying this vector
        Parameters:
        scalar - The scalar to divide this vector by
        Returns:
        This vector
      • swapNumsAndSet

        public Vector swapNumsAndSet​(int i,
                                     int j)
        Swaps the two numbers at the given indices, modifying the vector
        Parameters:
        i - The row to swap with j
        j - The row to swap with i
        Returns:
        This vector
        Throws:
        java.lang.IndexOutOfBoundsException - If i or j is out of bounds
      • dot

        public double dot​(Vector v)
        Calculates the dot product of this vector and the given vector
        Parameters:
        v - The vector to find the dot product with
        Returns:
        The dot product of this vector and the given vector
        Throws:
        java.lang.IllegalArgumentException - If the dimension of the given vector is not the same as the dimension of this vector
      • gramSchmidtCoefficient

        public double gramSchmidtCoefficient​(Vector v)
        Returns the Gram-Schmidt coefficient when this vector is projected onto the given vector, that is, the ratio of the length of the projection of this vector onto the given vector, to the given vector's length. In other words, multiplying the given vector by the Gram-Schmidt coefficient gives the projection of this vector onto the given vector
        Parameters:
        v - The vector to project onto
        Returns:
        The Gram-Schmidt coefficient when this vector is projected onto the given vector
        Throws:
        java.lang.IllegalArgumentException - If the dimension of the given vector is not the same as the dimension of this vector
      • projectOnto

        public Vector projectOnto​(Vector v)
        Calculates the projection of this vector onto the given vector, stores the result in a new vector and returns that vector
        Parameters:
        v - The vector to project onto
        Returns:
        A new vector containing the result
        Throws:
        java.lang.IllegalArgumentException - If the dimension of the given vector is not the same as the dimension of this vector
      • copy

        public Vector copy()
        Creates a copy of this vector
        Returns:
        A copy of this vector
      • equals

        public boolean equals​(Vector other,
                              double tolerance)
        Returns whether this vector has the same dimension as the given vector, and all elements of this vector are within tolerance of the corresponding elements in the given vector
        Parameters:
        other - The vector to test against
        tolerance - The maximum amount each element is allowed to differ
        Returns:
        Whether this vector is close enough to the given vector
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • equals

        public boolean equals​(java.lang.Object other)
        Overrides:
        equals in class java.lang.Object
      • toString

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

        public static Vector fromString​(java.lang.String raw)
        Parses a string in wolfram-style vector notation
        Parameters:
        raw - The string in wolfram-style vector notation
        Returns:
        The parsed vector
        Throws:
        ParseException - If the input is malformed
      • parse

        public static Vector parse​(StringParser parser)
        Parses a vector from a string parser
        Parameters:
        parser - The parser to parse the vector from
        Returns:
        The parsed vector
        Throws:
        ParseException - If the input is malformed
      • fromBigVector

        public static Vector fromBigVector​(BigVector v)
        Converts a BigVector to a vector of doubles
        Parameters:
        v - The BigVector to copy from
        Returns:
        A vector of doubles with the same values as the input vector, where possible