medusa.georgios.enhanced_mcl
Class SparseVector
- java.lang.Object
-
- java.util.AbstractMap<K,V>
-
- java.util.HashMap<java.lang.Integer,java.lang.Double>
-
- medusa.georgios.enhanced_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.
- See Also:
- Serialized Form
-
-
Nested Class Summary
-
Constructor Summary
Constructors Constructor and Description SparseVector()create empty vectorSparseVector(double[] x)create vector from dense vectorSparseVector(int i)create empty vector with lengthSparseVector(SparseVector v)copy constructor
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method and Description voidadd(int i, double a)mutable addvoidadd(SparseVector v)mutable addSparseVectorcopy()copy the contents of the sparse vectordoubleexpSum(int p)exponential sum, i.e., sum (elements^p)voidfactor(double a)mutable factorisationjava.lang.Doubleget(java.lang.Object key)get ensures it returns 0 for empty hash values or if index exceeds length.double[]getDense()get dense represenationintgetLength()get the length of the vectorvoidhadamardPower(double s)mutable Hadamard powervoidhadamardProduct(SparseVector v)mutable Hadamard product (elementwise multiplication)doublemax()maximum element valuevoidmult(int i, double a)mutable multvoidnormalise()normalises the vector to 1.doublenormalise(double newsum)normalises the vector to newsumvoidprune(double threshold)remove all elements whose magnitude is < thresholdjava.lang.Doubleput(java.lang.Integer key, java.lang.Double value)put increases the matrix size if the index exceeds the current size.voidsetLength(int length)set the new length of the vector (regardless of the maximum index).doublesum(double s)power sum of the elementsdoubletimes(SparseVector v)immutable scalar productjava.lang.StringtoString()java.lang.StringtoStringDense()create string representation of dense equivalent.
-
-
-
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:
getin interfacejava.util.Map<java.lang.Integer,java.lang.Double>- Overrides:
getin classjava.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:
putin interfacejava.util.Map<java.lang.Integer,java.lang.Double>- Overrides:
putin classjava.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
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- indexa- 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:
toStringin classjava.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-
-
-
DMelt 3.0 © DataMelt by jWork.ORG