common
Class SparseVector

java.lang.Object
  extended by common.ImplicitVector
      extended by common.SparseVector

public class SparseVector
extends ImplicitVector

Simple implementation of a sparse vector, that is, a vector of doubles in which many elements are zero. Only the non-zero elements are stored explicitly. The class uses two arrays: one to hold the non-zero values, and one to hold their indices. The indices (and corresponding values) are kept in sorted order, so accessing an element by index takes time logarithmic in the number of non-zero entries.


Constructor Summary
SparseVector()
          Creates a new vector with no non-zero entries.
 
Method Summary
 double get(int i)
          Returns the value at the given index.
 int ithExplicitIndex(int i)
          Returns the ith explicitly represented index in this vector.
 double ithExplicitValue(int i)
          Returns the value at the ith explicitly represented index.
static void main(java.lang.String[] args)
          Test program.
 int numExplicit()
          Returns the number of explicitly represented entries in this vector.
 void set(int i, double value)
          Sets the value at the given index.
 java.lang.String toString()
           
 
Methods inherited from class common.ImplicitVector
dotProduct, dotProduct, maxExplicitIndex, sampleIndex, sum
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

SparseVector

public SparseVector()
Creates a new vector with no non-zero entries.

Method Detail

get

public double get(int i)
Description copied from class: ImplicitVector
Returns the value at the given index. This is zero if no value is explicitly stored for the given index.

Specified by:
get in class ImplicitVector

set

public void set(int i,
                double value)
Sets the value at the given index. The value is stored explicitly only if it is non-zero.

Specified by:
set in class ImplicitVector

numExplicit

public int numExplicit()
Description copied from class: ImplicitVector
Returns the number of explicitly represented entries in this vector.

Specified by:
numExplicit in class ImplicitVector

ithExplicitIndex

public int ithExplicitIndex(int i)
Description copied from class: ImplicitVector
Returns the ith explicitly represented index in this vector.

Specified by:
ithExplicitIndex in class ImplicitVector

ithExplicitValue

public double ithExplicitValue(int i)
Description copied from class: ImplicitVector
Returns the value at the ith explicitly represented index.

Specified by:
ithExplicitValue in class ImplicitVector

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

main

public static void main(java.lang.String[] args)
Test program.