类 Vector
- java.lang.Object
-
- edu.princeton.cs.algs4.Vector
-
public class Vector extends java.lang.Object
TheVector
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.
-
-
方法概要
修饰符和类型 方法 说明 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 dot product of this vector with the specified vector.int
length()
已过时。Replaced bydimension()
.double
magnitude()
Returns the magnitude of this vector.static void
main(java.lang.String[] args)
Unit tests theVector
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 scalarVector
times(double alpha)
已过时。Replaced byscale(double)
.java.lang.String
toString()
Returns a string representation of this vector.
-
-
-
构造器详细资料
-
Vector
public Vector(int d)
Initializes a d-dimensional zero vector.- 参数:
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).- 参数:
a
- the array or vararg list
-
-
方法详细资料
-
length
@Deprecated public int length()
已过时。Replaced bydimension()
.Returns the length of this vector.- 返回:
- the dimension of this vector
-
dimension
public int dimension()
Returns the dimension of this vector.- 返回:
- the dimension of this vector
-
dot
public double dot(Vector that)
Returns the dot product of this vector with the specified vector.- 参数:
that
- the other vector- 返回:
- the dot product of this vector and that vector
- 抛出:
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.- 返回:
- the magnitude of this vector
-
distanceTo
public double distanceTo(Vector that)
Returns the Euclidean distance between this vector and the specified vector.- 参数:
that
- the other vector- 返回:
- the Euclidean distance between this vector and that vector
- 抛出:
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.- 参数:
that
- the vector to add to this vector- 返回:
- the vector whose value is
(this + that)
- 抛出:
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.- 参数:
that
- the vector to subtract from this vector- 返回:
- the vector whose value is
(this - that)
- 抛出:
java.lang.IllegalArgumentException
- if the dimensions of the two vectors are not equal
-
cartesian
public double cartesian(int i)
Returns the ith cartesian coordinate.- 参数:
i
- the coordinate index- 返回:
- the ith cartesian coordinate
-
times
@Deprecated public Vector times(double alpha)
已过时。Replaced byscale(double)
.Returns the scalar-vector product of this vector and the specified scalar- 参数:
alpha
- the scalar- 返回:
- 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- 参数:
alpha
- the scalar- 返回:
- the vector whose value is
(alpha * this)
-
direction
public Vector direction()
Returns a unit vector in the direction of this vector.- 返回:
- a unit vector in the direction of this vector
- 抛出:
java.lang.ArithmeticException
- if this vector is the zero vector
-
toString
public java.lang.String toString()
Returns a string representation of this vector.- 覆盖:
toString
在类中java.lang.Object
- 返回:
- 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 theVector
data type.- 参数:
args
- the command-line arguments
-
-