prea.datastructure
Class SparseVector

java.lang.Object
  extended by prea.datastructure.SparseVector

public class SparseVector
extends java.lang.Object

This class implements sparse vector, containing empty values for most space.

Since:
2012. 3. 26
Version:
1.1
Author:
Joonseok Lee

Field Summary
private  DataMap<java.lang.Integer,java.lang.Double> map
          Data map for pairs.
private  int N
          The length (maximum number of items to be stored) of sparse vector.
 
Constructor Summary
SparseVector()
          Construct an empty sparse vector, with capacity 0.
SparseVector(int n)
          Construct a new sparse vector with size n.
 
Method Summary
 double absoluteSum()
          Sum of absolute value of every element in the vector.
 SparseVector add(double alpha)
          Scalar addition operator.
 double average()
          Average of every element.
 SparseVector commonMinus(SparseVector b)
          Vector subtraction (a - b), for only existing values.
 SparseVector copy()
          Copy the whole sparse vector and make a clone.
 SparseVector exp(double alpha)
          Exponential of a given constant.
 double getValue(int i)
          Retrieve a stored value from the given index.
 int[] indexList()
          Get a list of existing indices.
 void initialize(double value)
          Set a same value to every element.
 void initialize(int[] index, double value)
          Set same value to given indices.
 double innerProduct(SparseVector b)
          Inner product of two vectors.
 int itemCount()
          Actual number of items in the vector.
 int length()
          Capacity of this vector.
 SparseVector minus(SparseVector b)
          Vector subtraction (a - b)
 double norm()
          2-norm of the vector.
 SparseMatrix outerProduct(SparseVector b)
          Outer product of two vectors.
 double partInnerProduct(SparseVector b, int[] indexList)
          Inner-product for indices only in the given indices.
 SparseVector partMinus(SparseVector b, int[] indexList)
          Vector subtraction (a - b) for indices only in the given indices.
 SparseMatrix partOuterProduct(SparseVector b, int[] indexList)
          Outer-product for indices only in the given indices.
 SparseVector partPlus(SparseVector b, int[] indexList)
          Vector sum (a + b) for indices only in the given indices.
 SparseVector plus(SparseVector b)
          Vector sum (a + b)
 SparseVector power(double alpha)
          Scalar power operator.
 void remove(int i)
          Delete a value stored at the given index.
 SparseVector scale(double alpha)
          Scalar multiplication operator.
 void setLength(int n)
          Set a new capacity of the vector.
 void setValue(int i, double value)
          Set a new value at the given index.
 double stdev()
          Standard Deviation of every element.
 SparseVector sub(double alpha)
          Scalar subtraction operator.
 double sum()
          Sum of every element in the vector.
 DenseVector toDenseSubset(int[] indexList)
          Convert the vector into the array-based dense representation, but only with the selected indices.
 DenseVector toDenseVector()
          Convert the vector into the array-based dense representation.
 java.lang.String toString()
          Convert the vector to a printable string.
 double[] valueList()
          Get a list of existing values in array form.
 double variance()
          Variance of every element.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

N

private int N
The length (maximum number of items to be stored) of sparse vector.


map

private DataMap<java.lang.Integer,java.lang.Double> map
Data map for pairs.

Constructor Detail

SparseVector

public SparseVector()
Construct an empty sparse vector, with capacity 0. Capacity can be reset with setLength method later.


SparseVector

public SparseVector(int n)
Construct a new sparse vector with size n.

Parameters:
n - The capacity of new sparse vector.
Method Detail

setValue

public void setValue(int i,
                     double value)
Set a new value at the given index.

Parameters:
i - The index to store new value.
value - The value to store.

getValue

public double getValue(int i)
Retrieve a stored value from the given index.

Parameters:
i - The index to retrieve.
Returns:
The value stored at the given index.

remove

public void remove(int i)
Delete a value stored at the given index.

Parameters:
i - The index to delete the value in it.

copy

public SparseVector copy()
Copy the whole sparse vector and make a clone.

Returns:
A clone of the current sparse vector, containing same values.

indexList

public int[] indexList()
Get a list of existing indices. This can be useful to traverse the whole vector efficiently only with existing values.

Returns:
An array of integer, containing indices with valid items.

valueList

public double[] valueList()
Get a list of existing values in array form. The order of data is compatible with the index list returned by indexList method.

Returns:
An array of data values contained in the vector.

initialize

public void initialize(double value)
Set a same value to every element.

Parameters:
value - The value to assign to every element.

initialize

public void initialize(int[] index,
                       double value)
Set same value to given indices.

Parameters:
index - The list of indices, which will be assigned the new value.
value - The new value to be assigned.

toDenseVector

public DenseVector toDenseVector()
Convert the vector into the array-based dense representation.

Returns:
The dense version of same vector.

toDenseSubset

public DenseVector toDenseSubset(int[] indexList)
Convert the vector into the array-based dense representation, but only with the selected indices.

Parameters:
indexList - The list of indices converting to dense vector.
Returns:
The dense representation of same vector, with given indices.

length

public int length()
Capacity of this vector.

Returns:
The length of sparse vector

itemCount

public int itemCount()
Actual number of items in the vector.

Returns:
The number of items in the vector.

setLength

public void setLength(int n)
Set a new capacity of the vector.

Parameters:
n - The new capacity value.

add

public SparseVector add(double alpha)
Scalar addition operator.

Parameters:
alpha - The scalar value to be added to the original vector.
Returns:
The resulting vector, added by alpha.

sub

public SparseVector sub(double alpha)
Scalar subtraction operator.

Parameters:
alpha - The scalar value to be subtracted from the original vector.
Returns:
The resulting vector, subtracted by alpha.

scale

public SparseVector scale(double alpha)
Scalar multiplication operator.

Parameters:
alpha - The scalar value to be multiplied to the original vector.
Returns:
The resulting vector, multiplied by alpha.

power

public SparseVector power(double alpha)
Scalar power operator.

Parameters:
alpha - The scalar value to be powered to the original vector.
Returns:
The resulting vector, powered by alpha.

exp

public SparseVector exp(double alpha)
Exponential of a given constant.

Parameters:
alpha - The exponent.
Returns:
The resulting exponential vector.

norm

public double norm()
2-norm of the vector.

Returns:
2-norm value of the vector.

sum

public double sum()
Sum of every element in the vector.

Returns:
Sum value of every element.

absoluteSum

public double absoluteSum()
Sum of absolute value of every element in the vector.

Returns:
Sum of absolute value of every element.

average

public double average()
Average of every element. It ignores non-existing values.

Returns:
The average value.

variance

public double variance()
Variance of every element. It ignores non-existing values.

Returns:
The variance value.

stdev

public double stdev()
Standard Deviation of every element. It ignores non-existing values.

Returns:
The standard deviation value.

plus

public SparseVector plus(SparseVector b)
Vector sum (a + b)

Parameters:
b - The vector to be added to this vector.
Returns:
The resulting vector after summation.

minus

public SparseVector minus(SparseVector b)
Vector subtraction (a - b)

Parameters:
b - The vector to be subtracted from this vector.
Returns:
The resulting vector after subtraction.

commonMinus

public SparseVector commonMinus(SparseVector b)
Vector subtraction (a - b), for only existing values. The resulting vector can have a non-zero value only if both vectors have a value at the index.

Parameters:
b - The vector to be subtracted from this vector.
Returns:
The resulting vector after subtraction.

innerProduct

public double innerProduct(SparseVector b)
Inner product of two vectors.

Parameters:
b - The vector to be inner-producted with this vector.
Returns:
The inner-product value.

outerProduct

public SparseMatrix outerProduct(SparseVector b)
Outer product of two vectors.

Parameters:
b - The vector to be outer-producted with this vector.
Returns:
The resulting outer-product matrix.

partPlus

public SparseVector partPlus(SparseVector b,
                             int[] indexList)
Vector sum (a + b) for indices only in the given indices.

Parameters:
b - The vector to be added to this vector.
indexList - The list of indices to be applied summation.
Returns:
The resulting vector after summation.

partMinus

public SparseVector partMinus(SparseVector b,
                              int[] indexList)
Vector subtraction (a - b) for indices only in the given indices.

Parameters:
b - The vector to be subtracted from this vector.
indexList - The list of indices to be applied subtraction.
Returns:
The resulting vector after subtraction.

partInnerProduct

public double partInnerProduct(SparseVector b,
                               int[] indexList)
Inner-product for indices only in the given indices.

Parameters:
b - The vector to be inner-producted with this vector.
indexList - The list of indices to be applied inner-product.
Returns:
The inner-product value.

partOuterProduct

public SparseMatrix partOuterProduct(SparseVector b,
                                     int[] indexList)
Outer-product for indices only in the given indices.

Parameters:
b - The vector to be outer-producted with this vector.
indexList - The list of indices to be applied outer-product.
Returns:
The outer-product value.

toString

public java.lang.String toString()
Convert the vector to a printable string.

Overrides:
toString in class java.lang.Object
Returns:
The resulted string in the form of "(1: 5.0) (2: 4.5)"