edu.princeton.cs.algs4

Class Vector

• Object
• edu.princeton.cs.algs4.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 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

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.
`double` `magnitude()`
Returns the magnitude of this vector.
`static void` `main(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.
`String` `toString()`
Returns a string representation of this vector.
• Methods inherited from class Object

`clone, equals, finalize, 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:
`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
• 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:
`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 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