Package dsa
Class SparseVector
- java.lang.Object
-
- dsa.SparseVector
-
public class SparseVector extends Object
A data type to represent ann
-dimensional, real-valued sparse vector.
-
-
Constructor Summary
Constructors Constructor Description SparseVector(int n)
Constructs ann
-dimensional zero vector.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description int
dimension()
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
magnitude()
Returns the magnitude of this vector.static void
main(String[] args)
Unit tests the data type.SparseVector
plus(SparseVector other)
Returns the sum of this vector andother
.void
put(int i, double value)
Sets thei
th component of this vector tovalue
.SparseVector
scale(double alpha)
Returns the scalar-vector product of this vector andalpha
.int
size()
Returns the number of nonzero entries in this vector.String
toString()
Returns a string representation of this vector.
-
-
-
Method Detail
-
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
public SparseVector plus(SparseVector other)
Returns the sum of this vector andother
.- Parameters:
other
- the other vector.- Returns:
- the sum of this vector and
other
.
-
scale
public SparseVector scale(double alpha)
Returns the scalar-vector product of this vector andalpha
.- Parameters:
alpha
- the scalar.- Returns:
- the scalar-vector product of this vector and
alpha
.
-
dot
public double dot(SparseVector other)
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
public String toString()
Returns a string representation of this vector.
-
main
public static void main(String[] args)
Unit tests the data type.- Parameters:
args
- the command-line arguments.
-
-