Package dsa

Class SparseVector


  • public class SparseVector
    extends Object
    A data type to represent an n-dimensional, real-valued sparse vector.
    • Constructor Detail

      • SparseVector

        public SparseVector​(int n)
        Constructs an n-dimensional zero vector.
        Parameters:
        n - dimension of the 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 the ith component of this vector to value.
        Parameters:
        i - the component index.
        value - the component value.
      • get

        public double get​(int i)
        Returns the ith component of this vector.
        Parameters:
        i - the component index
        Returns:
        the ith component of this vector.
      • plus

        public SparseVector plus​(SparseVector other)
        Returns the sum of this vector and other.
        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 and alpha.
        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 and other.
        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.
        Overrides:
        toString in class Object
        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.