Class Vector

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.Comparable<Vector>

    public class Vector
    extends java.lang.Object
    implements java.io.Serializable, java.lang.Comparable<Vector>
    This class is a class representing a Vector of any number of dimensions
    Author:
    Bhagat
    See Also:
    Serialized Form
    • Field Detail

      • data

        private double[] data
        the array storing the data of the vector
      • size

        private int size
        the length of the stored data
    • Constructor Detail

      • Vector

        public Vector​(double... args)
        constructs a new Vector using components
        Parameters:
        args - The components to set into the Vector
      • Vector

        public Vector​(java.lang.Double... args)
        constructs a new Vector using components
        Parameters:
        args - The components to set into the Vector
      • Vector

        public Vector​(int... args)
        constructs a new Vector using int components
        Parameters:
        args - The components to set into the Vector
      • Vector

        public Vector​(int length)
        constructs a new Vector with a set number of dimensions
        Parameters:
        length - The number of dimensions in the Vector
    • Method Detail

      • add

        public Vector add​(Vector v)
        Performs element wise addition
        Parameters:
        v - the vector to add to this one
        Returns:
        a reference to this vector
      • subtract

        public Vector subtract​(Vector v)
        Performs element wise subtraction
        Parameters:
        v - the vector to subtract from this one
        Returns:
        a reference to this vector
      • add

        public Vector add​(double x)
        adds a scalar to each value in the Vector
        Parameters:
        x - the scalar to add
        Returns:
        this vector
      • subtract

        public Vector subtract​(double x)
        subtracts a scalar to each value in the Vector
        Parameters:
        x - the scalar to subtract
        Returns:
        this vector
      • multiply

        public Vector multiply​(double scalar)
        Performs scalar multiplication (element wise) on the Vector
        Parameters:
        scalar - the scalar to multiply with
        Returns:
        returns this vector after the multiplication
      • multiply

        public Vector multiply​(Vector v)
        element wise multiplication
        Parameters:
        v - vector to multiply with
        Returns:
        returns this vector after the multiplication
      • divide

        public Vector divide​(double scalar)
        Performs scalar division (element wise) on the Vector
        Parameters:
        scalar - the scalar to divide with
        Returns:
        returns this vector after the division
      • divide

        public Vector divide​(Vector vector)
        Element-wise Vector division
        Parameters:
        vector - the vector to divide by
        Returns:
        this vector after the division
      • randomize

        public void randomize​(double min,
                              double max)
        Fills the Vector with random values from min to max
        Parameters:
        min - the minimum random number
        max - the maximum random number
      • randomize

        public void randomize()
        Fills the Vector with random values from -1 to 1
      • getMagnitude

        public double getMagnitude()
        Returns:
        the magnitude of the vector
      • compareTo

        public int compareTo​(Vector v)
        compares two vectors by determinant
        Specified by:
        compareTo in interface java.lang.Comparable<Vector>
        Parameters:
        v - the vector to compare to
        Returns:
        the integer representing which vector's magnitude is greater
      • equals

        public boolean equals​(Vector v)
        checks if two matrices have the same data
        Parameters:
        v - the vector to compare with
        Returns:
        whether or not they are the same
      • getSum

        public double getSum()
        Returns:
        the sum of each component
      • map

        public Vector map​(Function<java.lang.Double,​java.lang.Double> function)
        maps a function onto each element in the vector
        Parameters:
        function - the function to map
        Returns:
        a reference of this vector after the mapping
      • mapWithIndex

        public Vector mapWithIndex​(Function<Vector.VectorIndex,​java.lang.Double> function)
        maps a function onto each element in the vector
        Parameters:
        function - the function to map that receives a VectorIndex
        Returns:
        a reference of this vector after the mapping
      • toMatrixColumn

        public Matrix toMatrixColumn()
        converts the vector into a matrix column
        Returns:
        the matrix with one column
      • toMatrixRow

        public Matrix toMatrixRow()
        converts the vector into a matrix row
        Returns:
        the matrix with one row
      • toMatrix

        public Matrix toMatrix()
        converts the vector into a matrix column
        Returns:
        the matrix with one column
      • clone

        public Vector clone()
        Overrides:
        clone in class java.lang.Object
        Returns:
        a copy with a new memory allocation
      • orthogonal

        public boolean orthogonal​(Vector v)
        Checks if this vector and the one sent in are orthogonal
        Parameters:
        v - the second vector
        Returns:
        a boolean that is true if they are orthogonal
      • normalize

        public Vector normalize()
        Returns:
        the unit vector with the same direction as this vector
      • getData

        public double[] getData()
        Returns:
        the data
      • getSize

        public int getSize()
        Returns:
        the size
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
        Returns:
        a string representing this Vector object in the form <x1, x2, . . . xn>
      • clone

        public static Vector clone​(Vector v)
        static clone function that calls the clone function of a vector
        Parameters:
        v - the vector to clone
        Returns:
        the cloned vector with a new memory allocation
      • multiply

        public static Vector multiply​(Vector a,
                                      double scalar)
        static multiply scalar function
        Parameters:
        a - the vector to multiply
        scalar - the scalar to multiply
        Returns:
        the resultant vector
      • multiply

        public static Vector multiply​(double scalar,
                                      Vector a)
        static multiply scalar function
        Parameters:
        scalar - the scalar to multiply
        a - the vector to multiply
        Returns:
        the resultant vector
      • divide

        public static Vector divide​(Vector a,
                                    double scalar)
        static divide scalar function
        Parameters:
        a - the vector to divide
        scalar - the scalar to divide
        Returns:
        the resultant vector
      • divide

        public static Vector divide​(double scalar,
                                    Vector a)
        static divide scalar function
        Parameters:
        scalar - the scalar to divide
        a - the vector to divide
        Returns:
        the resultant vector
      • cross

        public static Vector cross​(Vector... vectors)
        computes the cross product of the input vectors
        Parameters:
        vectors - the vectors to cross
        Returns:
        the resultant vector
      • inner

        public static double inner​(Vector a,
                                   Vector b)
        performs the inner product on two vectors
        Parameters:
        a - a vector
        b - another vector
        Returns:
        the inner product
      • outer

        public static Matrix outer​(Vector a,
                                   Vector b)
        performs the outer product on two vectors
        Parameters:
        a - a vector
        b - another vector
        Returns:
        the matrix result
      • orthogonalize

        public static Vector[] orthogonalize​(Vector[] x)
        make the input vectors orthogonal
        Parameters:
        x - the original vectors
        Returns:
        the orthogonalized vectors
      • generateUnitVector

        public static Vector generateUnitVector​(int index,
                                                int size)
        creates a unit vector
        Parameters:
        index - this value will be 1
        size - the size of the vector
        Returns:
        the unit vector
      • print

        public static void print​(Vector... vectors)
        prints any number of vectors
        Parameters:
        vectors - the vectors to print
      • orthogonal

        public static boolean orthogonal​(Vector v1,
                                         Vector v2)
        Checks if two vectors are orthogonal
        Parameters:
        v1 - first vector
        v2 - second vector
        Returns:
        boolean that if true if they are orthogonal