public class SparseTensor extends java.lang.Object implements DataSet, java.lang.Iterable<TensorEntry>, java.io.Serializable
For easy documentation, here we use (keys, value)
to indicate each entry of a tensor, and index
is
used to indicate the position in which the entry is stored in the lists.
Modifier and Type | Field and Description |
---|---|
int[] |
dimensions |
java.util.List<java.lang.Integer>[] |
ndKeys |
int |
numDimensions
number of dimensions, i.e., the order (or modes, ways) of a tensor
|
java.util.List<java.lang.Double> |
values |
Constructor and Description |
---|
SparseTensor(int... dims)
Construct an empty sparse tensor
|
SparseTensor(int[] dims,
java.util.List<java.lang.Integer>[] nds,
java.util.List<java.lang.Double> vals)
Construct a sparse tensor with indices and values
|
Modifier and Type | Method and Description |
---|---|
void |
add(double val,
int... keys)
Add a value to a given i-entry
|
void |
buildIndex(int... dims)
build index at dimensions nd
|
void |
buildIndices()
build index for all dimensions
|
SparseTensor |
clone()
make a deep clone
|
boolean |
contains(int... keys)
Check if a given keys exists
|
int[] |
dimensions() |
SparseVector |
fiber(int dim,
int... keys)
A fiber is defined by fixing every index but one.
|
double |
get(int... keys)
Return a value given a specific i-entry.
|
java.util.Collection<java.lang.Integer> |
getIndex(int d,
int key)
Return indices (positions) of a key in dimension d.
|
int |
getIndexDimension(int index) |
java.util.List<java.lang.Integer> |
getIndices(int user,
int item)
Return all entries for a (user, item) pair
|
int |
getItemDimension() |
java.util.List<java.lang.Integer> |
getRelevantKeys(int sd,
int key,
int td)
Return keys in a target dimension
td related with a key in dimension sd . |
java.util.List<java.lang.Integer> |
getTargetKeyFromSubKey(java.lang.Integer[] subKey) |
int |
getUserDimension() |
double |
innerProduct(SparseTensor st) |
boolean |
isCubical() |
boolean |
isDiagonal() |
boolean |
isDimMatch(SparseTensor st)
Return whether two sparse tensors have the same dimensions
|
boolean |
isIndexed(int d)
Return whether a dimension d is indexed.
|
java.util.Iterator<TensorEntry> |
iterator() |
int |
key(int d,
int index)
Return key in the position
index of dimension d . |
int[] |
keys(int index)
Return keys in a given index
|
static void |
main(java.lang.String[] args)
Usage demonstration
|
SparseMatrix |
matricization(int n)
Re-ordering entries of a tensor into a matrix
|
double |
mean() |
SparseTensor |
modeProduct(DenseMatrix mat,
int dim)
n-mode product of a tensor A (I1 x I2 x ...
|
SparseTensor |
modeProduct(DenseVector vec,
int dim)
n-mode product of a tensor A (I1 x I2 x ...
|
double |
norm() |
int |
numDimensions() |
SparseMatrix |
rateMatrix()
retrieve a rating matrix from the tensor.
|
boolean |
remove(int... keys)
remove an entry with specific keys.
|
void |
set(double val,
int... keys)
Set a value to a specific i-entry
|
void |
setItemDimension(int itemDimension) |
void |
setUserDimension(int userDimension) |
void |
shuffle()
Shuffle a sparse tensor
|
int |
size() |
SparseMatrix |
slice(int rowDim,
int colDim,
int... otherKeys)
Slice is a two-dimensional sub-array of a tensor, defined by fixing all but two indices.
|
java.lang.String |
toString() |
double |
value(int index)
Return value in a given index.
|
public int numDimensions
public int[] dimensions
public java.util.List<java.lang.Integer>[] ndKeys
public java.util.List<java.lang.Double> values
public SparseTensor(int... dims)
dims
- dimensions of a tensorpublic SparseTensor(int[] dims, java.util.List<java.lang.Integer>[] nds, java.util.List<java.lang.Double> vals)
dims
- dimensions of a tensornds
- n-dimensional keysvals
- entry valuespublic SparseTensor clone()
clone
in class java.lang.Object
public void add(double val, int... keys) throws java.lang.Exception
val
- value to addkeys
- n-dimensional keysjava.lang.Exception
- if error occurs during addingpublic void set(double val, int... keys) throws java.lang.Exception
val
- value to setkeys
- n-dimensional keysjava.lang.Exception
- if error occurs during settingpublic boolean remove(int... keys) throws java.lang.Exception
keys
- n-dimensional keysjava.lang.Exception
- if error occurs during removingpublic java.util.List<java.lang.Integer> getIndices(int user, int item)
user
- user indexitem
- item indexpublic java.util.List<java.lang.Integer> getTargetKeyFromSubKey(java.lang.Integer[] subKey) throws java.lang.Exception
subKey
- a numDimensions-1 dimension subKeyjava.lang.Exception
- if error occurs during gettingpublic SparseVector fiber(int dim, int... keys)
dim
- the dimension where values can varykeys
- the other fixed dimension keyspublic boolean contains(int... keys) throws java.lang.Exception
keys
- keys to checkjava.lang.Exception
- if error occurs during checkingpublic boolean isIndexed(int d)
d
- a dimension dpublic boolean isCubical()
public boolean isDiagonal()
public double get(int... keys) throws java.lang.Exception
keys
- a specific i-entryjava.lang.Exception
- if error occurs during gettingpublic void shuffle()
public void buildIndex(int... dims)
dims
- dimensions to be indexedpublic void buildIndices()
public java.util.Collection<java.lang.Integer> getIndex(int d, int key)
d
- dimensionkey
- key valuepublic int[] keys(int index)
index
- a given indexpublic int key(int d, int index)
index
of dimension d
.d
- dimensionindex
- a given indexindex
of dimension d
public double value(int index)
index
- a given indexpublic java.util.List<java.lang.Integer> getRelevantKeys(int sd, int key, int td)
td
related with a key in dimension sd
.sd
- source dimensionkey
- key in the source dimensiontd
- target dimensiontd
related with a key in dimension sd
public int size()
public SparseMatrix slice(int rowDim, int colDim, int... otherKeys)
rowDim
- row dimensioncolDim
- column dimensionotherKeys
- keys of other dimensionspublic SparseMatrix matricization(int n)
n
- mode or dimensionpublic SparseTensor modeProduct(DenseMatrix mat, int dim) throws java.lang.Exception
mat
- mat to be multiplieddim
- mode/dimension of the tensor to be usedjava.lang.Exception
- if error occurs during productpublic SparseTensor modeProduct(DenseVector vec, int dim) throws java.lang.Exception
vec
- vector to be multiplieddim
- mode/dimension of the tensor to be usedjava.lang.Exception
- if error occurs during productpublic SparseMatrix rateMatrix()
public java.util.Iterator<TensorEntry> iterator()
iterator
in interface java.lang.Iterable<TensorEntry>
public double norm()
public double mean()
public double innerProduct(SparseTensor st) throws java.lang.Exception
st
- another tensorjava.lang.Exception
- if error occurs during productpublic boolean isDimMatch(SparseTensor st)
st
- a given SparseTensorpublic int getUserDimension()
public int getIndexDimension(int index)
public void setUserDimension(int userDimension)
public int getItemDimension()
public void setItemDimension(int itemDimension)
public int[] dimensions()
public int numDimensions()
public java.lang.String toString()
toString
in class java.lang.Object
public static void main(java.lang.String[] args) throws java.lang.Exception
args
- argumentsjava.lang.Exception
- if error occursCopyright © 2017. All Rights Reserved.