Package dsa

Class SparseVector

java.lang.Object
dsa.SparseVector

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

    Constructors
    Constructor
    Description
    SparseVector(int n)
    Constructs an n-dimensional zero vector.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    Returns the dimension of this vector.
    double
    Returns the dot product of this vector and other.
    double
    get(int i)
    Returns the ith component of this vector.
    double
    Returns the magnitude of this vector.
    static void
    main(String[] args)
    Unit tests the data type.
    Returns the sum of this vector and other.
    void
    put(int i, double value)
    Sets the ith component of this vector to value.
    scale(double alpha)
    Returns the scalar-vector product of this vector and alpha.
    int
    Returns the number of nonzero entries in this vector.
    Returns a string representation of this vector.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • SparseVector

      public SparseVector(int n)
      Constructs an n-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 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.