Class SparseVector

  • All Implemented Interfaces:
    ConstantMatrix, Vector, java.io.Serializable
    Direct Known Subclasses:
    FeatureVector, HashedSparseVector, IndexedSparseVector

    public class SparseVector
    extends java.lang.Object
    implements ConstantMatrix, Vector, java.io.Serializable
    A vector that allocates memory only for non-zero values. When you create a SparseVector, you pass in a list of indices. These are the only elements of the vector you will be allowed to change. The rest are fixed at 0. The interface to Sparse vector uses the concept of a location, which is an integer in the range 0..numLocations which can be mapped to the index (and value) of a non zero element of the vector. A SparseVector can be sparse or dense depending on whether or not an array if indices is specified at construction time. If the SparseVector is dense, the mapping from location to index is the identity mapping. The type of the value an element in a SparseVector (or FeatureVector) can be double or binary (0.0 or 1.0), depending on whether an array of doubles is specified at contruction time.
    Author:
    Andrew McCallum mccallum@cs.umass.edu
    See Also:
    Serialized Form
    • Field Detail

      • indices

        protected int[] indices
        If the vector is sparse, then both indices and values are sparse. Indices into these arrays are called ``locations'' in the below. The indices[] array maps locations to indices of the (virtual) dense array that's being represented. value[] maps locations to values.
      • values

        protected double[] values
      • hasInfinite

        protected boolean hasInfinite
    • Constructor Detail

      • SparseVector

        public SparseVector​(int[] indices,
                            double[] values,
                            int capacity,
                            int size,
                            boolean copy,
                            boolean checkIndicesSorted,
                            boolean removeDuplicates)
        If "indices" is null, the vector will be dense. If "values" is null, the vector will be binary. The capacity and size arguments are used by AugmentableFeatureVector.
      • SparseVector

        public SparseVector​(double[] values,
                            boolean copy)
      • SparseVector

        public SparseVector​(double[] values)
      • SparseVector

        public SparseVector​(int size,
                            double fillValue)
      • SparseVector

        public SparseVector​(int[] indices,
                            double[] values,
                            boolean copy,
                            boolean checkIndicesSorted,
                            boolean removeDuplicates)
      • SparseVector

        public SparseVector​(int[] indices,
                            double[] values)
      • SparseVector

        public SparseVector​(int[] indices,
                            double[] values,
                            boolean copy)
      • SparseVector

        public SparseVector​(int[] indices,
                            double[] values,
                            boolean copy,
                            boolean checkIndicesSorted)
      • SparseVector

        public SparseVector​(int[] indices,
                            boolean copy,
                            boolean checkIndicesSorted,
                            boolean removeDuplicates,
                            boolean binary)
      • SparseVector

        public SparseVector​(int[] indices,
                            int capacity,
                            int size,
                            boolean copy,
                            boolean checkIndicesSorted,
                            boolean removeDuplicates)
      • SparseVector

        public SparseVector​(int[] indices,
                            boolean copy,
                            boolean checkIndicesSorted)
      • SparseVector

        public SparseVector​(int[] indices,
                            boolean copy)
      • SparseVector

        public SparseVector​(int[] indices)
      • SparseVector

        public SparseVector()
        An empty vector, with all zero values
      • SparseVector

        public SparseVector​(Alphabet dict,
                            PropertyList pl,
                            boolean binary,
                            boolean growAlphabet)
    • Method Detail

      • isBinary

        public boolean isBinary()
      • makeBinary

        public void makeBinary()
      • makeNonBinary

        public void makeNonBinary()
      • getIndices

        public int[] getIndices()
      • getValues

        public double[] getValues()
      • value

        public double value​(int[] indices)
        Specified by:
        value in interface ConstantMatrix
      • value

        public double value​(int index)
        Specified by:
        value in interface Vector
      • addTo

        public void addTo​(double[] accumulator,
                          double scale)
      • addTo

        public void addTo​(double[] accumulator)
      • toString

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

        public java.lang.String toString​(boolean onOneLine)
      • plusEqualsSparse

        public void plusEqualsSparse​(SparseVector v)
        For each index i that is present in this vector, set this[i] += v[i]. If v has indices that are not present in this, these are just ignored.
      • plusEqualsSparse

        public void plusEqualsSparse​(SparseVector v,
                                     double factor)
        For each index i that is present in this vector, set this[i] += factor * v[i]. If v has indices that are not present in this, these are just ignored.
      • timesEqualsSparse

        public void timesEqualsSparse​(SparseVector v)
        For each index i that is present in this vector, set this[i] *= v[i]. If v has indices that are not present in this, these are just ignored.
      • timesEqualsSparse

        public void timesEqualsSparse​(SparseVector v,
                                      double factor)
        For each index i that is present in this vector, set this[i] *= factor * v[i]. If v has indices that are not present in this, these are just ignored.
      • timesEqualsSparseZero

        public void timesEqualsSparseZero​(SparseVector v,
                                          double factor)
        For each index i that is present in this vector, set this[i] *= factor * v[i]. If v has indices that are not present in this, these are set to zero
      • timesEquals

        public void timesEquals​(double factor)
        Scale all elements by the same factor.
      • incrementValue

        public void incrementValue​(int index,
                                   double value)
                            throws java.lang.IllegalArgumentException
        Increments this[index] by value.
        Throws:
        java.lang.IllegalArgumentException - If index is not present.
      • setAll

        public void setAll​(double v)
        Sets every present index in the vector to v.
      • setValue

        public void setValue​(int index,
                             double value)
                      throws java.lang.IllegalArgumentException
        Sets the value at the given index.
        Throws:
        java.lang.IllegalArgumentException - If index is not present.
      • setValueAtLocation

        public void setValueAtLocation​(int location,
                                       double value)
        Sets the value at the given location.
      • arrayCopyFrom

        public final void arrayCopyFrom​(double[] a)
        Copy values from an array into this vector. The array should have the same size as the vector
      • arrayCopyFrom

        public final int arrayCopyFrom​(double[] a,
                                       int startingArrayLocation)
        Copy values from an array starting at a particular location into this vector. The array must have at least as many values beyond the starting location as there are in the vector.
        Returns:
        Next uncopied location in the array.
      • map

        public final void map​(java.lang.reflect.Method f)
                       throws java.lang.IllegalAccessException,
                              java.lang.Throwable
        Applies the method argument to each value in a non-binary vector. The method should both accept a Double as an argument and return a Double.
        Throws:
        java.lang.IllegalArgumentException - If the method argument has an inappropriate signature.
        java.lang.UnsupportedOperationException - If vector is binary
        java.lang.IllegalAccessException - If the method is inaccessible
        java.lang.Throwable - If the method throws an exception it is relayed
      • arrayCopyInto

        public final int arrayCopyInto​(double[] array,
                                       int startingArrayLocation)
        Copy the contents of this vector into an array starting at a particular location.
        Returns:
        Next available location in the array
      • dotProduct

        public double dotProduct​(double[] v)
        VECTOR OPERATIONS
      • dotProduct

        public double dotProduct​(DenseVector v)
      • extendedDotProduct

        public double extendedDotProduct​(DenseVector v)
      • extendedDotProduct

        public double extendedDotProduct​(SparseVector v)
      • isInfinite

        public boolean isInfinite()
      • isNaNOrInfinite

        public boolean isNaNOrInfinite()
      • sortIndices

        protected void sortIndices()
      • removeDuplicates

        protected void removeDuplicates​(int numDuplicates)