类 SparseVector
- java.lang.Object
-
- edu.princeton.cs.algs4.SparseVector
-
public class SparseVector extends java.lang.Object
TheSparseVector
class represents a d-dimensional mathematical vector. Vectors are mutable: their values can be changed after they are created. It includes methods for addition, subtraction, dot product, scalar product, unit vector, and Euclidean norm.The implementation is a symbol table of indices and values for which the vector coordinates are nonzero. This makes it efficient when most of the vector coordindates are zero.
For additional documentation, see Section 3.5 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne. See also
Vector
for an immutable (dense) vector data type.
-
-
构造器概要
构造器 构造器 说明 SparseVector(int d)
Initializes a d-dimensional zero vector.
-
方法概要
修饰符和类型 方法 说明 int
dimension()
Returns the dimension of this vector.double
dot(double[] that)
Returns the inner product of this vector with the specified array.double
dot(SparseVector that)
Returns the inner product of this vector with the specified vector.double
get(int i)
Returns the ith coordinate of this vector.double
magnitude()
Returns the magnitude of this vector.static void
main(java.lang.String[] args)
Unit tests theSparseVector
data type.int
nnz()
Returns the number of nonzero entries in this vector.double
norm()
已过时。Replaced bymagnitude()
.SparseVector
plus(SparseVector that)
Returns the sum of this vector and the specified vector.void
put(int i, double value)
Sets the ith coordinate of this vector to the specified value.SparseVector
scale(double alpha)
Returns the scalar-vector product of this vector with the specified scalar.int
size()
已过时。Replaced bydimension()
.java.lang.String
toString()
Returns a string representation of this vector.
-
-
-
方法详细资料
-
put
public void put(int i, double value)
Sets the ith coordinate of this vector to the specified value.- 参数:
i
- the indexvalue
- the new value- 抛出:
java.lang.IllegalArgumentException
- unless i is between 0 and d-1
-
get
public double get(int i)
Returns the ith coordinate of this vector.- 参数:
i
- the index- 返回:
- the value of the ith coordinate of this vector
- 抛出:
java.lang.IllegalArgumentException
- unless i is between 0 and d-1
-
nnz
public int nnz()
Returns the number of nonzero entries in this vector.- 返回:
- the number of nonzero entries in this vector
-
size
@Deprecated public int size()
已过时。Replaced bydimension()
.Returns the dimension 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(SparseVector that)
Returns the inner product of this vector with the specified vector.- 参数:
that
- the other vector- 返回:
- the dot product between this vector and that vector
- 抛出:
java.lang.IllegalArgumentException
- if the lengths of the two vectors are not equal
-
dot
public double dot(double[] that)
Returns the inner product of this vector with the specified array.- 参数:
that
- the array- 返回:
- the dot product between this vector and that array
- 抛出:
java.lang.IllegalArgumentException
- if the dimensions of the vector and the array 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
-
norm
@Deprecated public double norm()
已过时。Replaced bymagnitude()
.Returns the Euclidean norm of this vector.- 返回:
- the Euclidean norm of this vector
-
scale
public SparseVector scale(double alpha)
Returns the scalar-vector product of this vector with the specified scalar.- 参数:
alpha
- the scalar- 返回:
- the scalar-vector product of this vector with the specified scalar
-
plus
public SparseVector plus(SparseVector that)
Returns the sum of this vector and the specified vector.- 参数:
that
- the vector to add to this vector- 返回:
- the sum of this vector and that vector
- 抛出:
java.lang.IllegalArgumentException
- if the dimensions of the two vectors are not equal
-
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 commas, enclosed in parentheses
-
main
public static void main(java.lang.String[] args)
Unit tests theSparseVector
data type.- 参数:
args
- the command-line arguments
-
-