de.uni_leipzig.mcl.cluster
Class SparseMatrix

java.lang.Object
  extended by java.util.AbstractCollection<E>
      extended by java.util.AbstractList<E>
          extended by java.util.ArrayList<SparseVector>
              extended by de.uni_leipzig.mcl.cluster.SparseMatrix
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, java.lang.Iterable<SparseVector>, java.util.Collection<SparseVector>, java.util.List<SparseVector>, java.util.RandomAccess

public class SparseMatrix
extends java.util.ArrayList<SparseVector>

SparseMatrix is a sparse matrix with row-major format.

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
 
Fields inherited from class java.util.AbstractList
modCount
 
Constructor Summary
SparseMatrix()
          empty sparse matrix
SparseMatrix(double[][] x)
          create sparse matrix from full matrix
SparseMatrix(int rows, int cols)
          empty sparse matrix with allocated number of rows
SparseMatrix(SparseMatrix matrix)
          copy contructor
 
Method Summary
 double add(int i, int j, double a)
          adds a to the specified element, growing the matrix if necessary.
 void adjustMaxIndex(int i, int j)
          adjusts the size of the matrix.
 SparseMatrix copy()
          copy the matrix and its elements
 double get(int i, int j)
          get number at index or 0.
 SparseVector getColum(int i)
          get a column of the sparse matrix (expensive).
 double[][] getDense()
          create dense representation
 int[] getSize()
          get the size of the matrix
 void hadamardPower(double s)
          mutable m2 = m .^ s
 void hadamardProduct(SparseMatrix m)
          mutable Hadamard product
 SparseMatrix matrixTimes(SparseMatrix m)
          immutable multiply matrix M with this (A) : M * A
 SparseVector normalise(double rowsum)
          normalise rows to rowsum
 void normaliseCols()
          normalise by minor dimension (columns), expensive.
 void normaliseRows()
          normalise by major dimension (rows)
 void prune(double threshold)
          prune all values whose magnitude is below threshold
 double set(int i, int j, double a)
          set the value at the index i,j, returning the old value or 0.
 SparseVector set(int i, SparseVector x)
          set the sparse vector at index i.
 SparseMatrix times(SparseMatrix m)
          immutable multiply this matrix (A) with M : A * M
 SparseVector times(SparseVector v)
          immutable multiply this times the vector: A * x, i.e., rowwise.
 SparseMatrix timesTransposed(SparseMatrix m)
          mutable multiply this matrix (A) with M : A * M'
 java.lang.String toString()
           
 SparseMatrix transpose()
          immutable transpose.
 SparseVector vectorTimes(SparseVector v)
          immutable multiply the vector times this: x' * A, i.e., colwise.
 
Methods inherited from class java.util.ArrayList
add, add, addAll, addAll, clear, clone, contains, ensureCapacity, get, indexOf, isEmpty, lastIndexOf, remove, remove, removeRange, size, toArray, toArray, trimToSize
 
Methods inherited from class java.util.AbstractList
equals, hashCode, iterator, listIterator, listIterator, subList
 
Methods inherited from class java.util.AbstractCollection
containsAll, removeAll, retainAll
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.List
containsAll, equals, hashCode, iterator, listIterator, listIterator, removeAll, retainAll, subList
 

Constructor Detail

SparseMatrix

public SparseMatrix()
empty sparse matrix


SparseMatrix

public SparseMatrix(double[][] x)
create sparse matrix from full matrix

Parameters:
x -

SparseMatrix

public SparseMatrix(int rows,
                    int cols)
empty sparse matrix with allocated number of rows

Parameters:
rows -
cols -

SparseMatrix

public SparseMatrix(SparseMatrix matrix)
copy contructor

Parameters:
matrix -
Method Detail

add

public double add(int i,
                  int j,
                  double a)
adds a to the specified element, growing the matrix if necessary.

Parameters:
i -
j -
a -
Returns:
new value

adjustMaxIndex

public void adjustMaxIndex(int i,
                           int j)
adjusts the size of the matrix.

Parameters:
i - index addressed
j - index addressed

copy

public SparseMatrix copy()
copy the matrix and its elements


get

public double get(int i,
                  int j)
get number at index or 0. if not set. If index > size, returns 0.

Parameters:
i -
j -
Returns:

getColum

public SparseVector getColum(int i)
get a column of the sparse matrix (expensive).

Returns:

getDense

public double[][] getDense()
create dense representation

Returns:

getSize

public int[] getSize()
get the size of the matrix

Returns:

hadamardPower

public void hadamardPower(double s)
mutable m2 = m .^ s

Parameters:
s -

hadamardProduct

public void hadamardProduct(SparseMatrix m)
mutable Hadamard product

Parameters:
m -

matrixTimes

public SparseMatrix matrixTimes(SparseMatrix m)
immutable multiply matrix M with this (A) : M * A

Parameters:
m -
Returns:

normalise

public SparseVector normalise(double rowsum)
normalise rows to rowsum

Parameters:
rowsum - for each row
Returns:
vector of old row sums

normaliseCols

public void normaliseCols()
normalise by minor dimension (columns), expensive.


normaliseRows

public void normaliseRows()
normalise by major dimension (rows)


prune

public void prune(double threshold)
prune all values whose magnitude is below threshold


set

public double set(int i,
                  int j,
                  double a)
set the value at the index i,j, returning the old value or 0. Increase matrix size if index exceeds the dimension.

Parameters:
i -
j -
a -
Returns:

set

public SparseVector set(int i,
                        SparseVector x)
set the sparse vector at index i.

Specified by:
set in interface java.util.List<SparseVector>
Overrides:
set in class java.util.ArrayList<SparseVector>
Parameters:
i -
x -
Returns:
the old value of the element

times

public SparseMatrix times(SparseMatrix m)
immutable multiply this matrix (A) with M : A * M

Parameters:
m -
Returns:
matrix product

times

public SparseVector times(SparseVector v)
immutable multiply this times the vector: A * x, i.e., rowwise.

Parameters:
v -
Returns:

timesTransposed

public SparseMatrix timesTransposed(SparseMatrix m)
mutable multiply this matrix (A) with M : A * M'

Parameters:
m -
Returns:
modified this

toString

public java.lang.String toString()
Overrides:
toString in class java.util.AbstractCollection<SparseVector>

transpose

public SparseMatrix transpose()
immutable transpose.

Returns:

vectorTimes

public SparseVector vectorTimes(SparseVector v)
immutable multiply the vector times this: x' * A, i.e., colwise.

Parameters:
v -
Returns: