public final class MutableSparseVector extends SparseVector implements Serializable
This class extends the sparse vector with support for imperative mutation operations on their values.
Once created the domain of potential keys remains immutable. Since the vector is sparse, keys can be added, but only within the domain the vector was constructed with. These vectors separate the concepts of the key set, which is the current set of active keys from the key domain, which is the set of potential keys. Of course, the key domain must always include the key set.
Addition and subtraction are supported, though they are modified from the mathematical operations because they never change the set of keys. Mutation operations also operate in-place to reduce the reallocation and copying required. Therefore, a common pattern is:
MutableSparseVector normalized = vector.mutableCopy(); normalized.subtract(normFactor);
Create mutable sparse vectors using the various create
static methods provided
(create(java.util.Collection)
, create(java.util.Collection, double)
,
create(long...)
, create(java.util.Map)
).
Modifier and Type | Method and Description |
---|---|
void |
add(double value)
Add a value to all set keys in this array.
|
double |
add(long key,
double value)
Add a value to the specified entry.
|
void |
add(SparseVector other)
Add another rating vector to this one.
|
<K> Long2ObjectMap<K> |
addChannel(TypedSymbol<K> channelSymbol)
Add a typed channel to this vector.
|
MutableSparseVector |
addChannelVector(Symbol channelSymbol)
Add a channel to this vector.
|
void |
addScaled(SparseVector v,
double scale)
Add a vector to this vector with a scaling factor.
|
void |
clear()
Clear all values from the set.
|
MutableSparseVector |
combineWith(SparseVector o)
Combine this vector with another vector by taking the union of the key domains of two vectors.
|
MutableSparseVector |
copy()
Copy the rating vector.
|
static MutableSparseVector |
create(Collection<Long> domain)
Create a new empty mutable sparse vector with the specified key domain.
|
static MutableSparseVector |
create(Collection<Long> domain,
double value)
Create a new mutable sparse vector with the specified key domain and filled with a value.
|
static MutableSparseVector |
create(long... keys)
Create a mutable sparse vector with a fixed set of keys.
|
static MutableSparseVector |
create(Map<Long,Double> content)
Create a new mutable sparse vector with the specified content.
|
void |
fill(double value)
Set the values for all items in the key domain to
value . |
ImmutableSparseVector |
freeze()
Construct an immutable sparse vector from this vector's data (if possible),
invalidating this vector in the process.
|
<K> Long2ObjectMap<K> |
getChannel(TypedSymbol<K> channelSymbol)
Fetch the channel stored under a particular typed symbol.
|
Set<TypedSymbol<?>> |
getChannelSymbols()
Retrieve all symbols that map to typed side channels for this vector.
|
MutableSparseVector |
getChannelVector(Symbol channelSymbol)
Get the vector associated with a particular unboxed channel.
|
Set<Symbol> |
getChannelVectorSymbols()
Retrieve all symbols that map to side channels for this vector.
|
<K> Long2ObjectMap<K> |
getOrAddChannel(TypedSymbol<K> channelSymbol)
Add a typed channel to the vector, even if there is already a
channel with the same symbol.
|
MutableSparseVector |
getOrAddChannelVector(Symbol channelSymbol)
Add a channel to the vector, even if there is already a
channel with the same symbol.
|
boolean |
hasChannel(TypedSymbol<?> channelSymbol)
Return whether this sparse vector has a channel stored under a
particular typed symbol.
|
boolean |
hasChannelVector(Symbol channelSymbol)
Return whether this sparse vector has a channel vector stored under a
particular symbol.
|
ImmutableSparseVector |
immutable()
Return an immutable snapshot of this sparse vector.
|
LongSortedSet |
keySet()
Get the set of keys of this vector.
|
void |
multiply(double s)
Multiply the vector by a scalar.
|
void |
multiply(SparseVector other)
Multiply each element in the vector by the corresponding element in another vector.
|
MutableSparseVector |
mutableCopy()
Return a mutable copy of this sparse vector.
|
void |
removeAllChannels()
Remove all channels stored in this vector.
|
<K> Long2ObjectMap<K> |
removeChannel(TypedSymbol<K> channelSymbol)
Remove the typed channel stored under a particular symbol.
|
SparseVector |
removeChannelVector(Symbol channelSymbol)
Remove the channel stored under a particular symbol.
|
double |
set(long key,
double value)
Set a value in the vector.
|
void |
set(SparseVector other)
Set the values in this SparseVector to equal the values in
other for each key that is present in both vectors.
|
double |
set(VectorEntry entry,
double value)
Set the value in the vector corresponding to a vector entry.
|
MutableSparseVector |
shrinkDomain()
Create a new version of this MutableSparseVector that has keyDomain equal to this vector's
key set.
|
void |
subtract(SparseVector other)
Subtract another rating vector from this one.
|
void |
unset(long key)
Unset the value for a key.
|
void |
unset(VectorEntry entry)
Unset the value for a vector entry.
|
void |
unsetLessThan(double thresh)
Unset all values less than a threshold.
|
MutableSparseVector |
withDomain(LongSet keyDomain)
Create a new version of this MutableSparseVector that has a
different domain from the current version of the vector.
|
static MutableSparseVector |
wrap(long[] keys,
double[] values)
Wrap key and value arrays in a sparse vector.
|
static MutableSparseVector |
wrap(long[] keys,
double[] values,
int size)
Wrap key and value arrays in a sparse vector.
|
static MutableSparseVector |
wrap(LongArrayList keyList,
DoubleArrayList valueList)
Wrap key and value array lists in a mutable sparse vector.
|
static MutableSparseVector |
wrapUnsorted(long[] keys,
double[] values)
Create a new
MutableSparseVector from unsorted key and value
arrays. |
containsKey, countCommonKeys, dot, empty, equals, fast, fast, fastIterator, fastIterator, get, get, get, hashCode, isEmpty, isSet, iterator, iterator, keyDomain, keysByValue, keysByValue, mean, norm, size, sum, sumAbs, toString, unsetKeySet, values, view
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
forEach, spliterator
public static MutableSparseVector create(Collection<Long> domain)
domain
- The key domain. This method is more efficient if you pass some form of LongCollection
, particularly a LongSortedSet
.public static MutableSparseVector create(Collection<Long> domain, double value)
domain
- The key domain. This method is more efficient if you pass some form of LongCollection
, particularly a LongSortedSet
.value
- The value to fill the vector with.public static MutableSparseVector create(Map<Long,Double> content)
content
- The content of the vector. Pass a Long2DoubleMap
for more efficiency.
It may not contain any null
values.public LongSortedSet keySet()
SparseVector
keySet
in class SparseVector
public MutableSparseVector withDomain(LongSet keyDomain)
Channels from the current vector are copied over to the new vector, all with the changed keyDomain.
keyDomain
- the set of keys to use for the domain of the
new vector.public MutableSparseVector shrinkDomain()
Note: the domain of this vector is not changed.
public double set(long key, double value)
key
- The key of the value to set.value
- The value to set.Double.NaN
if the key had no value
(or if the original value was Double.NaN
).IllegalArgumentException
- if the key is not in the
domain for this sparse vector.public double set(@Nonnull VectorEntry entry, double value)
setValue
method on VectorEntry
,
and changes the value in constant time. The value on the entry is also changed
to reflect the new value.
Is guaranteed to work on any vector that has an identical set of keys as the
vector from which the VectorEntry was created (such as the channels of that
vector), but may throw an IllegalArgumentException if the keys are not an identical
object even if they are the same value, to permit more efficient implementations.entry
- The entry to update.value
- The new value.IllegalArgumentException
- if entry
does not come
from this vector, or if the index in the entry is corrupt.public void fill(double value)
value
.value
- The value to set.public void unset(long key)
key
- The key to unset.IllegalArgumentException
- if the key is not in the key domain.public void unset(@Nonnull VectorEntry entry)
entry
- The entry to unset.public void unsetLessThan(double thresh)
thresh
- The threshold.public void clear()
public double add(long key, double value)
key
- The key whose value should be added.value
- The value to increase it by.IllegalArgumentException
- if the key is not in the key set.public void add(double value)
value
- The value to add.public void subtract(SparseVector other)
After calling this method, every element of this vector has been decreased by the corresponding element in other. Elements with no corresponding element are unchanged.
other
- The vector to subtract.public void add(SparseVector other)
After calling this method, every element of this vector has been increased by the corresponding element in other. Elements with no corresponding element are unchanged.
other
- The vector to add.public void addScaled(SparseVector v, double scale)
v
by
the scaling factor scale
and adds it to this vector. Only keys set
in both v
and this
are modified. The scaling is done
on-the-fly; v
is unmodified.v
- The vector to add to this vector.scale
- The scaling factor to be applied to the vector.public void set(SparseVector other)
After calling this method, every element in this vector that has a key in other has its value set to the corresponding value in other. Elements with no corresponding key are unchanged, and elements in other that are not in this vector are not inserted.
other
- The vector to blit its values into this vectorpublic void multiply(double s)
s
- The scalar to rescale the vector by.public void multiply(SparseVector other)
other
- The vector to pairwise-multiply with this one.public MutableSparseVector copy()
public MutableSparseVector mutableCopy()
SparseVector
mutableCopy
in class SparseVector
public ImmutableSparseVector immutable()
SparseVector
immutable
in class SparseVector
public ImmutableSparseVector freeze()
immutable()
public static MutableSparseVector wrap(long[] keys, double[] values)
This method allows a new vector to be constructed from pre-created arrays. After wrapping arrays in a sparse vector, client code should not modify them (particularly the keys array). The key domain of the newly created vector will be the same as the keys.
keys
- Array of entry keys. This array must be in sorted order and
be duplicate-free.values
- The values for the vector, in key order.IllegalArgumentException
- if there is a problem with the provided
arrays (length mismatch, keys not sorted, etc.).public static MutableSparseVector wrap(long[] keys, double[] values, int size)
This method allows a new vector to be constructed from pre-created arrays. After wrapping arrays in a sparse vector, client code should not modify them (particularly the keys array). The key domain of the newly created vector will be the same as the keys.
keys
- Array of entry keys. This array must be in sorted order and
be duplicate-free.values
- The values for the vector.size
- The size of the vector; only the first size
entries from each array are actually used.IllegalArgumentException
- if there is a problem with the provided
arrays (length mismatch, keys not sorted, etc.).public static MutableSparseVector wrap(LongArrayList keyList, DoubleArrayList valueList)
keyList
- The list of keysvalueList
- The list of valuespublic static MutableSparseVector create(long... keys)
keys
- The key domain.public static MutableSparseVector wrapUnsorted(long[] keys, double[] values)
MutableSparseVector
from unsorted key and value
arrays. The provided arrays will be modified and should not be used
by the client after this operation has completed. The key domain of
the new MutableSparseVector
will be the same as keys
.keys
- Array of entry keys. This should be duplicate-free.values
- The values of the vector, in key order.IllegalArgumentException
- if there is a problem with the provided
arrays (length mismatch, etc.).public SparseVector removeChannelVector(Symbol channelSymbol)
channelSymbol
- the symbol under which the channel was
stored in the vector.IllegalArgumentException
- if this vector does not have
such a channel at this time.public <K> Long2ObjectMap<K> removeChannel(TypedSymbol<K> channelSymbol)
channelSymbol
- the symbol under which the channel was
stored in the vector.IllegalArgumentException
- if this vector does not have
such a channel at this time.public void removeAllChannels()
public MutableSparseVector addChannelVector(Symbol channelSymbol)
channelSymbol
- the symbol under which this new channel
should be created.IllegalArgumentException
- if there is already a channel
with that symbolpublic <K> Long2ObjectMap<K> addChannel(TypedSymbol<K> channelSymbol)
channelSymbol
- the symbol under which this new channel
should be created.IllegalArgumentException
- if there is already a channel
with that symbolpublic MutableSparseVector getOrAddChannelVector(Symbol channelSymbol)
channelSymbol
- the symbol under which this new channel
should be created.public <K> Long2ObjectMap<K> getOrAddChannel(TypedSymbol<K> channelSymbol)
channelSymbol
- the symbol under which this new channel
should be created.public boolean hasChannelVector(Symbol channelSymbol)
SparseVector
hasChannelVector
in class SparseVector
channelSymbol
- the symbol under which the channel was
stored in the vector.public boolean hasChannel(TypedSymbol<?> channelSymbol)
SparseVector
hasChannel
in class SparseVector
channelSymbol
- the typed symbol under which the channel was
stored in the vector.public MutableSparseVector getChannelVector(Symbol channelSymbol)
SparseVector
getChannelVector
in class SparseVector
channelSymbol
- the symbol under which the channel was/is
stored in the vector.null
if
there is no such channel.public <K> Long2ObjectMap<K> getChannel(TypedSymbol<K> channelSymbol)
SparseVector
getChannel
in class SparseVector
channelSymbol
- the typed symbol under which the channel was/is
stored in the vector.null
if there is no such channel.public Set<Symbol> getChannelVectorSymbols()
SparseVector
getChannelVectorSymbols
in class SparseVector
public Set<TypedSymbol<?>> getChannelSymbols()
SparseVector
getChannelSymbols
in class SparseVector
public MutableSparseVector combineWith(SparseVector o)
SparseVector
o
override those from the
current vector.combineWith
in class SparseVector
o
- The other vector