Package dsa

Class Vector


  • public class Vector
    extends Object
    An immutable data type to represent an n-dimensional vector.
    • Constructor Detail

      • Vector

        public Vector​(double[] coords)
        Constructs a vector given its components.
        Parameters:
        coords - the vector components.
    • Method Detail

      • get

        public double get​(int i)
        Returns the ith component of this vector.
        Parameters:
        i - the component index.
        Returns:
        the ith component of this vector.
      • add

        public Vector add​(Vector other)
        Returns the sum of this vector and other.
        Parameters:
        other - the other vector.
        Returns:
        the sum of this vector and other.
      • subtract

        public Vector subtract​(Vector other)
        Returns the difference of this vector and other.
        Parameters:
        other - the other vector.
        Returns:
        the difference of this vector and other.
      • dot

        public double dot​(Vector other)
        Returns the dot product of this vector and other.
        Parameters:
        other - the other vector.
        Returns:
        the dot product of this vector and other.
      • scale

        public Vector scale​(double alpha)
        Returns a scaled (by factor other) copy of this vector.
        Parameters:
        alpha - the scale factor.
        Returns:
        a scaled (by factor other) copy of this vector.
      • direction

        public Vector direction()
        Returns a unit vector in the direction of this vector.
        Returns:
        a unit vector in the direction of this vector.
      • magnitude

        public double magnitude()
        Returns the magnitude of this vector.
        Returns:
        the magnitude of this vector.
      • dimension

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

        public String toString()
        Returns a string representation of this vector.
        Overrides:
        toString in class Object
        Returns:
        a string representation of this vector.
      • main

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