public class Vector extends java.lang.Object
Vectors
includes methods for addition, subtraction,
dot product, scalar product, unit vector, Euclidean distance, and
Euclidean norm.
For additional documentation, see Section 1.2 of Algorithms, 4th Edition, by Robert Sedgewick and Kevin Wayne.
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.
|
Modifier and Type | Method and Description |
---|---|
double |
cartesian(int i)
Returns the ith cartesian coordinate.
|
Vector |
direction()
Returns a unit vector in the direction of this vector.
|
double |
distanceTo(Vector that)
Returns the Euclidean distance between this vector and that vector.
|
double |
dot(Vector that)
Returns the inner product of this vector with that vector.
|
int |
length()
Returns the length of this vector.
|
double |
magnitude()
Returns the Euclidean norm of this vector.
|
static void |
main(java.lang.String[] args)
Unit tests the data type methods.
|
Vector |
minus(Vector that)
Returns the difference between this vector and that vector: this - that.
|
Vector |
plus(Vector that)
Returns the sum of this vector and that vector: this + that.
|
Vector |
times(double factor)
Returns the product of this factor multiplied by the scalar factor: this * factor.
|
java.lang.String |
toString()
Returns a string representation of this vector.
|
public Vector(int d)
d
- the dimension of the vectorpublic Vector(double... a)
a
- the array or vararg listpublic int length()
public double dot(Vector that)
that
- the other vectorjava.lang.IllegalArgumentException
- if the lengths of the two vectors are not equal.public double magnitude()
public double distanceTo(Vector that)
that
- the other vectorjava.lang.IllegalArgumentException
- if the lengths of the two vectors are not equal.public Vector plus(Vector that)
that
- the vector to add to this vectorjava.lang.IllegalArgumentException
- if the lengths of the two vectors are not equal.public Vector minus(Vector that)
that
- the vector to subtract from this vectorjava.lang.IllegalArgumentException
- if the lengths of the two vectors are not equal.public double cartesian(int i)
i
- the coordinate indexpublic Vector times(double factor)
factor
- the multiplierpublic Vector direction()
java.lang.ArithmeticException
- if this vector is the zero vector.public java.lang.String toString()
toString
in class java.lang.Object
public static void main(java.lang.String[] args)