Package dsa
Class SparseVector
java.lang.Object
dsa.SparseVector
A data type to represent an
n
-dimensional, real-valued sparse vector.-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionint
Returns the dimension of this vector.double
dot
(SparseVector other) Returns the dot product of this vector andother
.double
get
(int i) Returns thei
th component of this vector.double
Returns the magnitude of this vector.static void
Unit tests the data type.plus
(SparseVector other) Returns the sum of this vector andother
.void
put
(int i, double value) Sets thei
th component of this vector tovalue
.scale
(double alpha) Returns the scalar-vector product of this vector andalpha
.int
size()
Returns the number of nonzero entries in this vector.toString()
Returns a string representation of this vector.
-
Constructor Details
-
SparseVector
public SparseVector(int n) Constructs ann
-dimensional zero vector.- Parameters:
n
- dimension of the vector.
-
-
Method Details
-
dimension
public int dimension()Returns the dimension of this vector.- Returns:
- the dimension of this vector
-
size
public int size()Returns the number of nonzero entries in this vector.- Returns:
- the number of nonzero entries in this vector
-
put
public void put(int i, double value) Sets thei
th component of this vector tovalue
.- Parameters:
i
- the component index.value
- the component value.
-
get
public double get(int i) Returns thei
th component of this vector.- Parameters:
i
- the component index- Returns:
- the
i
th component of this vector.
-
plus
Returns the sum of this vector andother
.- Parameters:
other
- the other vector.- Returns:
- the sum of this vector and
other
.
-
scale
Returns the scalar-vector product of this vector andalpha
.- Parameters:
alpha
- the scalar.- Returns:
- the scalar-vector product of this vector and
alpha
.
-
dot
Returns the dot product of this vector andother
.- Parameters:
other
- the other vector.- Returns:
- the dot product of this vector and
other
.
-
magnitude
public double magnitude()Returns the magnitude of this vector.- Returns:
- the magnitude of this vector.
-
toString
Returns a string representation of this vector. -
main
Unit tests the data type.- Parameters:
args
- the command-line arguments.
-