medusa.georgios.enhanced_mcl
Class SparseMatrix
- java.lang.Object
-
- java.util.AbstractCollection<E>
-
- java.util.AbstractList<E>
-
- java.util.ArrayList<SparseVector>
-
- medusa.georgios.enhanced_mcl.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.
- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor and Description SparseMatrix()empty sparse matrixSparseMatrix(double[][] x)create sparse matrix from full matrixSparseMatrix(int rows, int cols)empty sparse matrix with allocated number of rowsSparseMatrix(SparseMatrix matrix)copy contructor
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method and Description doubleadd(int i, int j, double a)adds a to the specified element, growing the matrix if necessary.voidadjustMaxIndex(int i, int j)adjusts the size of the matrix.SparseMatrixcopy()copy the matrix and its elementsdoubleget(int i, int j)get number at index or 0.SparseVectorgetColum(int i)get a column of the sparse matrix (expensive).double[][]getDense()create dense representationint[]getSize()get the size of the matrixvoidhadamardPower(double s)mutable m2 = m .^ svoidhadamardProduct(SparseMatrix m)mutable Hadamard productSparseMatrixmatrixTimes(SparseMatrix m)immutable multiply matrix M with this (A) : M * ASparseVectornormalise(double rowsum)normalise rows to rowsumvoidnormaliseCols()normalise by minor dimension (columns), expensive.voidnormaliseRows()normalise by major dimension (rows)voidprune(double threshold)prune all values whose magnitude is below thresholddoubleset(int i, int j, double a)set the value at the index i,j, returning the old value or 0.SparseVectorset(int i, SparseVector x)set the sparse vector at index i.SparseMatrixtimes(SparseMatrix m)immutable multiply this matrix (A) with M : A * MSparseVectortimes(SparseVector v)immutable multiply this times the vector: A * x, i.e., rowwise.SparseMatrixtimesTransposed(SparseMatrix m)mutable multiply this matrix (A) with M : A * M'java.lang.StringtoString()java.lang.StringtoStringDense()prints a dense representationSparseMatrixtranspose()immutable transpose.SparseVectorvectorTimes(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, forEach, get, indexOf, isEmpty, iterator, lastIndexOf, listIterator, listIterator, remove, remove, removeAll, removeIf, replaceAll, retainAll, size, sort, spliterator, subList, toArray, toArray, trimToSize
-
-
-
-
Constructor Detail
-
SparseMatrix
public SparseMatrix()
empty sparse matrix
-
SparseMatrix
public SparseMatrix(int rows, int cols)empty sparse matrix with allocated number of rows- Parameters:
rows-cols-
-
SparseMatrix
public SparseMatrix(double[][] x)
create sparse matrix from full matrix- Parameters:
x-
-
SparseMatrix
public SparseMatrix(SparseMatrix matrix)
copy contructor- Parameters:
matrix-
-
-
Method Detail
-
getDense
public double[][] getDense()
create dense representation- Returns:
-
set
public SparseVector set(int i, SparseVector x)
set the sparse vector at index i.- Specified by:
setin interfacejava.util.List<SparseVector>- Overrides:
setin classjava.util.ArrayList<SparseVector>- Parameters:
i-x-- Returns:
- the old value of the element
-
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:
-
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:
-
adjustMaxIndex
public void adjustMaxIndex(int i, int j)adjusts the size of the matrix.- Parameters:
i- index addressedj- index addressed
-
getSize
public int[] getSize()
get the size of the matrix- Returns:
-
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
-
normalise
public SparseVector normalise(double rowsum)
normalise rows to rowsum- Parameters:
rowsum- for each row- Returns:
- vector of old row sums
-
normaliseRows
public void normaliseRows()
normalise by major dimension (rows)
-
normaliseCols
public void normaliseCols()
normalise by minor dimension (columns), expensive.
-
copy
public SparseMatrix copy()
copy the matrix and its elements
-
times
public SparseVector times(SparseVector v)
immutable multiply this times the vector: A * x, i.e., rowwise.- Parameters:
v-- Returns:
-
vectorTimes
public SparseVector vectorTimes(SparseVector v)
immutable multiply the vector times this: x' * A, i.e., colwise.- Parameters:
v-- Returns:
-
timesTransposed
public SparseMatrix timesTransposed(SparseMatrix m)
mutable multiply this matrix (A) with M : A * M'- Parameters:
m-- Returns:
- modified this
-
times
public SparseMatrix times(SparseMatrix m)
immutable multiply this matrix (A) with M : A * M- Parameters:
m-- Returns:
- matrix product
-
matrixTimes
public SparseMatrix matrixTimes(SparseMatrix m)
immutable multiply matrix M with this (A) : M * A- Parameters:
m-- Returns:
-
transpose
public SparseMatrix transpose()
immutable transpose.- Returns:
-
getColum
public SparseVector getColum(int i)
get a column of the sparse matrix (expensive).- Returns:
-
hadamardProduct
public void hadamardProduct(SparseMatrix m)
mutable Hadamard product- Parameters:
m-
-
hadamardPower
public void hadamardPower(double s)
mutable m2 = m .^ s- Parameters:
s-
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.util.AbstractCollection<SparseVector>
-
toStringDense
public java.lang.String toStringDense()
prints a dense representation- Returns:
-
prune
public void prune(double threshold)
prune all values whose magnitude is below threshold
-
-
DMelt 3.0 © DataMelt by jWork.ORG