javatools.datatypes
Class SparseVector

java.lang.Object
  extended by javatools.datatypes.SparseVector
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable

public class SparseVector
extends java.lang.Object
implements java.io.Serializable, java.lang.Cloneable

This class is part of the Java Tools (see http://mpii.de/yago-naga/javatools). It is licensed under the Creative Commons Attribution License (see http://creativecommons.org/licenses/by/3.0) by the YAGO-NAGA team (see http://mpii.de/yago-naga). This class represents a Sparse Vector, i.e. a vector that has only few non-zero entries. Supports special (space-)optimized routines for binary vectors. Supports k-means with eucledian, cosine or other distances.

See Also:
Serialized Form

Nested Class Summary
static interface SparseVector.Distance
          Computes a distance of two vectors
 
Field Summary
static SparseVector.Distance cosineDistance
          Computes the cosine distance
static SparseVector.Distance eucledianDistance
          Computes the eucledian distance
 
Constructor Summary
SparseVector(double[] v, int[] d)
          Creates a Sparse Vector from (sorted) dimensions and values
SparseVector(double label, double... values)
          Creates a SparseVector from actual values
SparseVector(double l, int[] d)
          Creates a binary Sparse Vector from an unsorted array of dimensions and a label Zero-entries and duplicates in the dimension array will be ignored.
SparseVector(double l, int[] d, double[] v)
          Creates a Sparse Vector from (sorted) dimensions, (modifiable) values and a label
SparseVector(double l, java.util.List<java.lang.Integer> dimensions)
          Creates a binary Sparse Vector from an (unsorted) list of dimensions and a label
SparseVector(SparseVector v)
          Duplicates a vector
SparseVector(java.lang.String s)
          Creates a Sparse Vector from a SVM-light-like input string of the form label dim:val ...
 
Method Summary
 SparseVector add(SparseVector v)
          Adds a vector to this one
 char charLabel()
          Returns a char label for the vector
 SparseVector clone()
          Clones a vector
 SparseVector compress()
          Kicks out 0-entries (without affecting the vector in a mathematical sense)
 double cosine(SparseVector v)
          Returns the cosine of this vector with another vector
 double eucledianDistance(SparseVector v)
          Returns the distance to another vector
 double get(int i)
          Retrieves the i-th value
 int index(int dimension)
          Returns the index for a dimension (or -1)
 boolean isBinary()
          Tells whether this vector is binary
static void kMeans(SparseVector[] dots, SparseVector[] centers)
          Does a simple K-means
static void kMeans(SparseVector[] dots, SparseVector[] centers, SparseVector.Distance distanceFunction, double epsilon, int iterations)
          Does a simple K-means until the number of iterations is exceeded
 double l2norm()
          Returns this vector's two-norm
static void main(java.lang.String[] argv)
          Test method
 SparseVector multiply(double r)
          Multiplies this vector by a scalar
 java.util.Iterator<java.lang.Integer> nonZeroIndices()
          Gives an iterator over the non-zero indices
 int size()
          Returns the number of non-zero elements.
 double sprod(SparseVector v)
          Returns the scalar product (dot product) of this vector with another one.
 double squaredl2norm()
          Returns this vector's squared two-norm
 java.lang.String toString()
          Returns a String representation in an SVM-light-like format label dim:val ...
static java.lang.String visualize(java.util.List<SparseVector> vectors)
          Visualizes two-dimensional vectors
static java.lang.String visualize(java.util.List<SparseVector> vectors1, java.util.List<SparseVector> vectors2)
          Visualizes two-dimensional vectors
static java.lang.String visualize(SparseVector... vectors)
          Visualizes two-dimensional vectors
static java.lang.String visualize(SparseVector[] vectors1, SparseVector[] vectors2)
          Visualizes two-dimensional vectors
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

eucledianDistance

public static final SparseVector.Distance eucledianDistance
Computes the eucledian distance


cosineDistance

public static final SparseVector.Distance cosineDistance
Computes the cosine distance

Constructor Detail

SparseVector

public SparseVector(SparseVector v)
Duplicates a vector


SparseVector

public SparseVector(double l,
                    java.util.List<java.lang.Integer> dimensions)
Creates a binary Sparse Vector from an (unsorted) list of dimensions and a label


SparseVector

public SparseVector(double l,
                    int[] d)
Creates a binary Sparse Vector from an unsorted array of dimensions and a label Zero-entries and duplicates in the dimension array will be ignored. Left here as a legacy from LEILA


SparseVector

public SparseVector(double[] v,
                    int[] d)
Creates a Sparse Vector from (sorted) dimensions and values


SparseVector

public SparseVector(double l,
                    int[] d,
                    double[] v)
Creates a Sparse Vector from (sorted) dimensions, (modifiable) values and a label


SparseVector

public SparseVector(double label,
                    double... values)
Creates a SparseVector from actual values


SparseVector

public SparseVector(java.lang.String s)
Creates a Sparse Vector from a SVM-light-like input string of the form label dim:val ... # comments

Method Detail

compress

public SparseVector compress()
Kicks out 0-entries (without affecting the vector in a mathematical sense)


toString

public java.lang.String toString()
Returns a String representation in an SVM-light-like format label dim:val ...

Overrides:
toString in class java.lang.Object

sprod

public double sprod(SparseVector v)
Returns the scalar product (dot product) of this vector with another one. This code is optimized by speed, not by size


isBinary

public boolean isBinary()
Tells whether this vector is binary


size

public int size()
Returns the number of non-zero elements.


squaredl2norm

public double squaredl2norm()
Returns this vector's squared two-norm


l2norm

public double l2norm()
Returns this vector's two-norm


cosine

public double cosine(SparseVector v)
Returns the cosine of this vector with another vector


get

public double get(int i)
Retrieves the i-th value


visualize

public static java.lang.String visualize(SparseVector... vectors)
Visualizes two-dimensional vectors


visualize

public static java.lang.String visualize(java.util.List<SparseVector> vectors1,
                                         java.util.List<SparseVector> vectors2)
Visualizes two-dimensional vectors


visualize

public static java.lang.String visualize(SparseVector[] vectors1,
                                         SparseVector[] vectors2)
Visualizes two-dimensional vectors


visualize

public static java.lang.String visualize(java.util.List<SparseVector> vectors)
Visualizes two-dimensional vectors


charLabel

public char charLabel()
Returns a char label for the vector


kMeans

public static void kMeans(SparseVector[] dots,
                          SparseVector[] centers)
Does a simple K-means


kMeans

public static void kMeans(SparseVector[] dots,
                          SparseVector[] centers,
                          SparseVector.Distance distanceFunction,
                          double epsilon,
                          int iterations)
Does a simple K-means until the number of iterations is exceeded


eucledianDistance

public double eucledianDistance(SparseVector v)
Returns the distance to another vector


index

public int index(int dimension)
Returns the index for a dimension (or -1)


add

public SparseVector add(SparseVector v)
Adds a vector to this one


multiply

public SparseVector multiply(double r)
Multiplies this vector by a scalar


nonZeroIndices

public java.util.Iterator<java.lang.Integer> nonZeroIndices()
Gives an iterator over the non-zero indices


clone

public SparseVector clone()
Clones a vector

Overrides:
clone in class java.lang.Object

main

public static void main(java.lang.String[] argv)
                 throws java.lang.Exception
Test method

Throws:
java.lang.Exception