Documentation of 'edu.princeton.cs.algs4.Vector' Java class
Vector
edu.princeton.cs.algs4

Class Vector



  • public class Vector
    extends java.lang.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.

    • Constructor Summary

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

      All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method and Description
      double cartesian(int i)
      Returns the ith cartesian coordinate.
      int dimension()
      Returns the dimension of this vector.
      Vector direction()
      Returns a unit vector in the direction of this vector.
      double distanceTo(Vector that)
      Returns the Euclidean distance between this vector and the specified vector.
      double dot(Vector that)
      Returns the do product of this vector with the specified vector.
      int length()
      Deprecated. 
      Replaced by dimension().
      double magnitude()
      Returns the magnitude of this vector.
      static void main(java.lang.String[] args)
      Unit tests the Vector data type.
      Vector minus(Vector that)
      Returns the difference between this vector and the specified vector.
      Vector plus(Vector that)
      Returns the sum of this vector and the specified vector.
      Vector scale(double alpha)
      Returns the scalar-vector product of this vector and the specified scalar
      Vector times(double alpha)
      Deprecated. 
      Replaced by scale(double).
      java.lang.String toString()
      Returns a string representation of this vector.
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • 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 arugments such as Vector x = new Vector(1.0, 2.0, 3.0, 4.0).
        Parameters:
        a - the array or vararg list
    • Method Detail

      • length

        @Deprecated
        public int length()
        Deprecated. Replaced by dimension().
        Returns the length of this vector.
        Returns:
        the dimension of this vector
      • dimension

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

        public double dot(Vector that)
        Returns the do product of this vector with the specified vector.
        Parameters:
        that - the other vector
        Returns:
        the dot product of this vector and that vector
        Throws:
        java.lang.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:
        java.lang.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:
        java.lang.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:
        java.lang.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
      • times

        @Deprecated
        public Vector times(double alpha)
        Deprecated. Replaced by scale(double).
        Returns the scalar-vector product of this vector and the specified scalar
        Parameters:
        alpha - the scalar
        Returns:
        the vector whose value is (alpha * this)
      • 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:
        java.lang.ArithmeticException - if this vector is the zero vector
      • toString

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

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

DataMelt 3.0 © DataMelt by jWork.ORG

You see the box below because you did not login.