org.knowceans.mcl
Class SparseVector

java.lang.Object
  extended by java.util.AbstractMap<K,V>
      extended by java.util.HashMap<java.lang.Integer,java.lang.Double>
          extended by org.knowceans.mcl.SparseVector
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, java.util.Map<java.lang.Integer,java.lang.Double>

public class SparseVector
extends java.util.HashMap<java.lang.Integer,java.lang.Double>

SparseVector represents a sparse vector.

Conventions: except for the inherited methods and normalise(double), operations leave this ummodified (immutable) if there is a return value. Within operations, no pruning of values close to zero is done. Pruning can be controlled via the prune() method.

Author:
gregor :: arbylon . net
See Also:
Serialized Form

Field Summary
private  int length
           
private static long serialVersionUID
           
 
Constructor Summary
SparseVector()
          create empty vector
SparseVector(double[] x)
          create vector from dense vector
SparseVector(int i)
          create empty vector with length
SparseVector(SparseVector v)
          copy constructor
 
Method Summary
 void add(int i, double a)
          mutable add
 void add(SparseVector v)
          mutable add
 SparseVector copy()
          copy the contents of the sparse vector
 double expSum(int p)
          exponential sum, i.e., sum (elements^p)
 void factor(double a)
          mutable factorisation
 java.lang.Double get(java.lang.Object key)
          get ensures it returns 0 for empty hash values or if index exceeds length.
 double[] getDense()
          get dense represenation
 int getLength()
          get the length of the vector
 void hadamardPower(double s)
          mutable Hadamard power
 void hadamardProduct(SparseVector v)
          mutable Hadamard product (elementwise multiplication)
 double max()
          maximum element value
 void mult(int i, double a)
          mutable mult
 void normalise()
          normalises the vector to 1.
 double normalise(double newsum)
          normalises the vector to newsum
 void prune(double threshold)
          remove all elements whose magnitude is < threshold
 java.lang.Double put(java.lang.Integer key, java.lang.Double value)
          put increases the matrix size if the index exceeds the current size.
 void setLength(int length)
          set the new length of the vector (regardless of the maximum index).
private  double sum()
          sum of the elements
 double sum(double s)
          power sum of the elements
 double times(SparseVector v)
          immutable scalar product
 java.lang.String toString()
           
 java.lang.String toStringDense()
          create string representation of dense equivalent.
 
Methods inherited from class java.util.HashMap
clear, clone, containsKey, containsValue, entrySet, isEmpty, keySet, putAll, remove, size, values
 
Methods inherited from class java.util.AbstractMap
equals, hashCode
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Map
equals, hashCode
 

Field Detail

serialVersionUID

private static final long serialVersionUID
See Also:
Constant Field Values

length

private int length
Constructor Detail

SparseVector

public SparseVector()
create empty vector


SparseVector

public SparseVector(int i)
create empty vector with length


SparseVector

public SparseVector(double[] x)
create vector from dense vector

Parameters:
x -

SparseVector

public SparseVector(SparseVector v)
copy constructor

Parameters:
v -
Method Detail

get

public java.lang.Double get(java.lang.Object key)
get ensures it returns 0 for empty hash values or if index exceeds length.

Specified by:
get in interface java.util.Map<java.lang.Integer,java.lang.Double>
Overrides:
get in class java.util.HashMap<java.lang.Integer,java.lang.Double>
Parameters:
key -
Returns:
val

put

public java.lang.Double put(java.lang.Integer key,
                            java.lang.Double value)
put increases the matrix size if the index exceeds the current size.

Specified by:
put in interface java.util.Map<java.lang.Integer,java.lang.Double>
Overrides:
put in class java.util.HashMap<java.lang.Integer,java.lang.Double>
Parameters:
key -
value -
Returns:

normalise

public void normalise()
normalises the vector to 1.


normalise

public double normalise(double newsum)
normalises the vector to newsum

Parameters:
the - value to which the element sum
Returns:
the old element sum

sum

private double sum()
sum of the elements

Returns:

sum

public double sum(double s)
power sum of the elements

Returns:

add

public void add(SparseVector v)
mutable add

Parameters:
v -

mult

public void mult(int i,
                 double a)
mutable mult

Parameters:
i - index
a - value

factor

public void factor(double a)
mutable factorisation

Parameters:
a -

times

public double times(SparseVector v)
immutable scalar product

Parameters:
v -
Returns:
scalar product

hadamardProduct

public void hadamardProduct(SparseVector v)
mutable Hadamard product (elementwise multiplication)

Parameters:
v -

hadamardPower

public void hadamardPower(double s)
mutable Hadamard power

Parameters:
s -

add

public void add(int i,
                double a)
mutable add

Parameters:
i -
a -

getLength

public final int getLength()
get the length of the vector

Returns:

setLength

public final void setLength(int length)
set the new length of the vector (regardless of the maximum index).

Parameters:
length -

copy

public SparseVector copy()
copy the contents of the sparse vector

Returns:

toString

public java.lang.String toString()
Overrides:
toString in class java.util.AbstractMap<java.lang.Integer,java.lang.Double>

toStringDense

public java.lang.String toStringDense()
create string representation of dense equivalent.

Returns:

getDense

public double[] getDense()
get dense represenation

Returns:

max

public double max()
maximum element value

Returns:

expSum

public double expSum(int p)
exponential sum, i.e., sum (elements^p)

Parameters:
p -
Returns:

prune

public void prune(double threshold)
remove all elements whose magnitude is < threshold

Parameters:
threshold -