gov.sandia.cognition.math.matrix
Class DefaultInfiniteVector<KeyType>

java.lang.Object
  extended by gov.sandia.cognition.util.AbstractCloneableSerializable
      extended by gov.sandia.cognition.collection.AbstractScalarMap<KeyType>
          extended by gov.sandia.cognition.collection.AbstractMutableDoubleMap<KeyType>
              extended by gov.sandia.cognition.math.matrix.DefaultInfiniteVector<KeyType>
Type Parameters:
KeyType - The type of the keys (indices) into the infinite dimensional vector.
All Implemented Interfaces:
NumericMap<KeyType>, ScalarMap<KeyType>, InfiniteVector<KeyType>, VectorSpace<InfiniteVector<KeyType>,InfiniteVector.Entry<KeyType>>, Ring<InfiniteVector<KeyType>>, CloneableSerializable, Serializable, Cloneable, Iterable<InfiniteVector.Entry<KeyType>>

public class DefaultInfiniteVector<KeyType>
extends AbstractMutableDoubleMap<KeyType>
implements InfiniteVector<KeyType>

An implementation of an InfiniteVector backed by a LinkedHashMap.

Since:
3.2.1
Author:
Justin Basilico, Kevin R. Dixon
See Also:
Serialized Form

Nested Class Summary
protected  class DefaultInfiniteVector.SimpleIterator
          Simple iterator for DefaultInfiniteVector
 
Nested classes/interfaces inherited from class gov.sandia.cognition.collection.AbstractMutableDoubleMap
AbstractMutableDoubleMap.SimpleEntry<KeyType>, AbstractMutableDoubleMap.SimpleEntrySet<KeyType>
 
Nested classes/interfaces inherited from class gov.sandia.cognition.collection.AbstractScalarMap
AbstractScalarMap.MapWrapper
 
Nested classes/interfaces inherited from interface gov.sandia.cognition.math.matrix.InfiniteVector
InfiniteVector.Entry<KeyType>
 
Field Summary
static int DEFAULT_INITIAL_CAPACITY
          The default capacity is 16.
 
Fields inherited from class gov.sandia.cognition.collection.AbstractMutableDoubleMap
map
 
Constructor Summary
  DefaultInfiniteVector()
          Creates a new, empty instance of DefaultInfiniteVector.
  DefaultInfiniteVector(int initialCapacity)
          Creates a new, empty instance of DefaultInfiniteVector with the given initial capacity.
protected DefaultInfiniteVector(LinkedHashMap<KeyType,MutableDouble> map)
          Creates a new AbstractMapInfiniteVector with the given backing map.
 
Method Summary
 double angle(InfiniteVector<KeyType> other)
          Computes the angle between two Vectors.
 DefaultInfiniteVector<KeyType> clone()
          This makes public the clone method on the Object class and removes the exception that it throws.
 void compact()
          Removes entries from the map with value of 0.0
 double cosine(InfiniteVector<KeyType> other)
          Computes the cosine between two Vectors
 double dotProduct(InfiniteVector<KeyType> other)
          Inner Vector product between two Vectors
 InfiniteVector<KeyType> dotTimes(InfiniteVector<KeyType> other)
          Element-wise multiplication of this and other
 void dotTimesEquals(InfiniteVector<KeyType> other)
          Inline element-wise multiplication of this and other
 boolean equals(InfiniteVector<KeyType> other, double effectiveZero)
          Determines if two RingType objects are effectively equal
 boolean equals(Object other)
          Determines if two RingType objects are equal
 double euclideanDistance(InfiniteVector<KeyType> other)
          Euclidean distance between this and other, which is the 2-norm between the difference of the Vectors
 double euclideanDistanceSquared(InfiniteVector<KeyType> other)
          Squared Euclidean distance between this and other, which is the 2-norm between the difference of the Vectors
 boolean isUnitVector()
          Determines if this vector is a unit vector (norm2 = 1.0).
 boolean isUnitVector(double tolerance)
          Determines if this vector is a unit vector within some tolerance for the 2-norm.
 boolean isZero()
          Determines if this ring is equal to zero.
 boolean isZero(double effectiveZero)
          Determines if this ring is equal to zero using the element-wise effective zero value.
 Iterator<InfiniteVector.Entry<KeyType>> iterator()
           
 InfiniteVector<KeyType> minus(InfiniteVector<KeyType> other)
          Arithmetic subtraction of other from this
 void minusEquals(InfiniteVector<KeyType> other)
          Inline arithmetic subtraction of other from this
 InfiniteVector<KeyType> negative()
          Returns the element-wise negation of this, such that this.plus( this.negative() ) has only zero elements.
 void negativeEquals()
          Inline element-wise negation of this
 double norm(double power)
          Returns the p-norm of the Vector with the given power.
 double norm1()
          1-norm of the vector (sum of absolute values in the vector)
 double norm2()
          2-norm of the vector (aka Euclidean distance of the vector)
 double norm2Squared()
          Squared 2-norm of the vector (aka squared Euclidean distance of the vector)
 double normInfinity()
          Returns the infinity norm of the Vector, which is the maximum absolute value of an element in the Vector.
 InfiniteVector<KeyType> plus(InfiniteVector<KeyType> other)
          Arithmetic addition of this and other
 void plusEquals(InfiniteVector<KeyType> other)
          Inline arithmetic addition of this and other
 InfiniteVector<KeyType> scale(double scaleFactor)
          Element-wise scaling of this by scaleFactor
 InfiniteVector<KeyType> scaledMinus(double scaleFactor, InfiniteVector<KeyType> other)
          Arithmetic subtraction other after element-wise scaling of other by scaleFactor from this.
 void scaledMinusEquals(double scaleFactor, InfiniteVector<KeyType> other)
          Inline arithmetic subtraction of other after element-wise scaling of other by scaleFactor from this.
 InfiniteVector<KeyType> scaledPlus(double scaleFactor, InfiniteVector<KeyType> other)
          Arithmetic addition of this and other after element-wise scaling of other by scaleFactor.
 void scaledPlusEquals(double scaleFactor, InfiniteVector<KeyType> other)
          Inline arithmetic addition of this and other after element-wise scaling of other by scaleFactor.
 void scaleEquals(double scaleFactor)
          Inline element-wise scaling of this by scaleFactor
 double sum()
          Computes the sum of the elements in the vector.
 String toString()
           
 InfiniteVector<KeyType> unitVector()
          Returns the unit vector of this vector.
 void unitVectorEquals()
          Modifies this vector to be a the unit vector.
 void zero()
          Zeros out all elements of this, so that the following are equivalent r1.scaleEquals( 0.0 ); and r1.zero(); Furthermore, r1.zero(); anything.dotTimes( r1 ).equals( r1 );
 
Methods inherited from class gov.sandia.cognition.collection.AbstractMutableDoubleMap
asMap, clear, containsKey, entrySet, get, increment, keySet, set, size
 
Methods inherited from class gov.sandia.cognition.collection.AbstractScalarMap
decrement, decrement, decrementAll, decrementAll, getMaxValue, getMaxValueKey, getMaxValueKeys, getMinValue, getMinValueKey, getMinValueKeys, increment, incrementAll, incrementAll, isEmpty, setAll
 
Methods inherited from class java.lang.Object
finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface gov.sandia.cognition.collection.ScalarMap
asMap, decrement, decrement, decrementAll, decrementAll, entrySet, get, getMaxValue, getMinValue, increment, increment, incrementAll, incrementAll, set, setAll
 
Methods inherited from interface gov.sandia.cognition.collection.NumericMap
clear, containsKey, getMaxValueKey, getMaxValueKeys, getMinValueKey, getMinValueKeys, isEmpty, keySet, size
 

Field Detail

DEFAULT_INITIAL_CAPACITY

public static final int DEFAULT_INITIAL_CAPACITY
The default capacity is 16.

See Also:
Constant Field Values
Constructor Detail

DefaultInfiniteVector

public DefaultInfiniteVector()
Creates a new, empty instance of DefaultInfiniteVector.


DefaultInfiniteVector

public DefaultInfiniteVector(int initialCapacity)
Creates a new, empty instance of DefaultInfiniteVector with the given initial capacity.

Parameters:
initialCapacity - The initial capacity of the data structure. Must be positive.

DefaultInfiniteVector

protected DefaultInfiniteVector(LinkedHashMap<KeyType,MutableDouble> map)
Creates a new AbstractMapInfiniteVector with the given backing map.

Parameters:
map - The backing map that the data is stored in.
Method Detail

clone

public DefaultInfiniteVector<KeyType> clone()
Description copied from class: AbstractCloneableSerializable
This makes public the clone method on the Object class and removes the exception that it throws. Its default behavior is to automatically create a clone of the exact type of object that the clone is called on and to copy all primitives but to keep all references, which means it is a shallow copy. Extensions of this class may want to override this method (but call super.clone() to implement a "smart copy". That is, to target the most common use case for creating a copy of the object. Because of the default behavior being a shallow copy, extending classes only need to handle fields that need to have a deeper copy (or those that need to be reset). Some of the methods in ObjectUtil may be helpful in implementing a custom clone method. Note: The contract of this method is that you must use super.clone() as the basis for your implementation.

Specified by:
clone in interface Ring<InfiniteVector<KeyType>>
Specified by:
clone in interface CloneableSerializable
Overrides:
clone in class AbstractMutableDoubleMap<KeyType>
Returns:
A clone of this object.

equals

public boolean equals(Object other)
Description copied from interface: Ring
Determines if two RingType objects are equal

Specified by:
equals in interface Ring<InfiniteVector<KeyType>>
Overrides:
equals in class Object
Parameters:
other - RingType to compare against this
Returns:
True if the two objects are equal, false otherwise

equals

public boolean equals(InfiniteVector<KeyType> other,
                      double effectiveZero)
Description copied from interface: Ring
Determines if two RingType objects are effectively equal

Specified by:
equals in interface Ring<InfiniteVector<KeyType>>
Parameters:
other - RingType to compare against this
effectiveZero - tolerance threshold for element-wise equality
Returns:
True if the two objects are equal, false otherwise

sum

public double sum()
Description copied from interface: VectorSpace
Computes the sum of the elements in the vector.

Specified by:
sum in interface VectorSpace<InfiniteVector<KeyType>,InfiniteVector.Entry<KeyType>>
Returns:
The sum of the elements in the vector.

norm1

public double norm1()
Description copied from interface: VectorSpace
1-norm of the vector (sum of absolute values in the vector)

Specified by:
norm1 in interface VectorSpace<InfiniteVector<KeyType>,InfiniteVector.Entry<KeyType>>
Returns:
1-norm of the vector, [0,\infty)

norm2

public double norm2()
Description copied from interface: VectorSpace
2-norm of the vector (aka Euclidean distance of the vector)

Specified by:
norm2 in interface VectorSpace<InfiniteVector<KeyType>,InfiniteVector.Entry<KeyType>>
Returns:
2-norm of the vector, [0,\infty)

norm2Squared

public double norm2Squared()
Description copied from interface: VectorSpace
Squared 2-norm of the vector (aka squared Euclidean distance of the vector)

Specified by:
norm2Squared in interface VectorSpace<InfiniteVector<KeyType>,InfiniteVector.Entry<KeyType>>
Returns:
Squared 2-norm of the vector, [0,\infty)

normInfinity

public double normInfinity()
Description copied from interface: VectorSpace
Returns the infinity norm of the Vector, which is the maximum absolute value of an element in the Vector.

Specified by:
normInfinity in interface VectorSpace<InfiniteVector<KeyType>,InfiniteVector.Entry<KeyType>>
Returns:
Maximum absolute value of any element in the Vector.

norm

public double norm(double power)
Description copied from interface: VectorSpace
Returns the p-norm of the Vector with the given power.

Specified by:
norm in interface VectorSpace<InfiniteVector<KeyType>,InfiniteVector.Entry<KeyType>>
Parameters:
power - Power to exponentiate each entry, must be greater than 0.0, Double.POSITIVE_INFINITY
Returns:
p-norm with the given power.

cosine

public double cosine(InfiniteVector<KeyType> other)
Description copied from interface: VectorSpace
Computes the cosine between two Vectors

Specified by:
cosine in interface VectorSpace<InfiniteVector<KeyType>,InfiniteVector.Entry<KeyType>>
Parameters:
other - another vector with which to compute the cosine, must be the same dimension as this
Returns:
cosine between the vectors, [0,1]

angle

public double angle(InfiniteVector<KeyType> other)
Description copied from interface: VectorSpace
Computes the angle between two Vectors.

Specified by:
angle in interface VectorSpace<InfiniteVector<KeyType>,InfiniteVector.Entry<KeyType>>
Parameters:
other - Another vector with which to compute the angle. Must be the same dimensionality.
Returns:
The angle between the two vectors in [0, PI].

euclideanDistance

public double euclideanDistance(InfiniteVector<KeyType> other)
Description copied from interface: VectorSpace
Euclidean distance between this and other, which is the 2-norm between the difference of the Vectors

Specified by:
euclideanDistance in interface VectorSpace<InfiniteVector<KeyType>,InfiniteVector.Entry<KeyType>>
Parameters:
other - Vector to which to compute the distance, must be the same dimension as this
Returns:
this.minus( other ).norm2(), which is [0,\infty)

euclideanDistanceSquared

public double euclideanDistanceSquared(InfiniteVector<KeyType> other)
Description copied from interface: VectorSpace
Squared Euclidean distance between this and other, which is the 2-norm between the difference of the Vectors

Specified by:
euclideanDistanceSquared in interface VectorSpace<InfiniteVector<KeyType>,InfiniteVector.Entry<KeyType>>
Parameters:
other - Vector to which to compute the squared distance, must be the same dimension as this
Returns:
this.minus( other ).norm2Squared(), which is [0,\infty)

unitVector

public InfiniteVector<KeyType> unitVector()
Description copied from interface: VectorSpace
Returns the unit vector of this vector. That is, a vector in the same "direction" where the length (norm2) is 1.0. This is computed by dividing each element buy the length (norm2). If this vector is all zeros, then the vector returned will be all zeros.

Specified by:
unitVector in interface VectorSpace<InfiniteVector<KeyType>,InfiniteVector.Entry<KeyType>>
Returns:
The unit vector of this vector.

unitVectorEquals

public void unitVectorEquals()
Description copied from interface: VectorSpace
Modifies this vector to be a the unit vector. That is, a vector in the same "direction" where the length (norm2) is 1.0. This is computed by dividing each element buy the length (norm2). If this vector is all zeros, then this vector will all zeros.

Specified by:
unitVectorEquals in interface VectorSpace<InfiniteVector<KeyType>,InfiniteVector.Entry<KeyType>>

isUnitVector

public boolean isUnitVector()
Description copied from interface: VectorSpace
Determines if this vector is a unit vector (norm2 = 1.0).

Specified by:
isUnitVector in interface VectorSpace<InfiniteVector<KeyType>,InfiniteVector.Entry<KeyType>>
Returns:
True if this vector is a unit vector; otherwise, false.

isUnitVector

public boolean isUnitVector(double tolerance)
Description copied from interface: VectorSpace
Determines if this vector is a unit vector within some tolerance for the 2-norm.

Specified by:
isUnitVector in interface VectorSpace<InfiniteVector<KeyType>,InfiniteVector.Entry<KeyType>>
Parameters:
tolerance - The tolerance around 1.0 to allow the length.
Returns:
True if this is a unit vector within the given tolerance; otherwise, false.

dotProduct

public double dotProduct(InfiniteVector<KeyType> other)
Description copied from interface: VectorSpace
Inner Vector product between two Vectors

Specified by:
dotProduct in interface VectorSpace<InfiniteVector<KeyType>,InfiniteVector.Entry<KeyType>>
Parameters:
other - the Vector with which to compute the dot product with this, must be the same dimension as this
Returns:
dot product, (0,\infty)

plus

public InfiniteVector<KeyType> plus(InfiniteVector<KeyType> other)
Description copied from interface: Ring
Arithmetic addition of this and other

Specified by:
plus in interface Ring<InfiniteVector<KeyType>>
Parameters:
other - object to add to this
Returns:
sum of this and other

plusEquals

public void plusEquals(InfiniteVector<KeyType> other)
Description copied from interface: Ring
Inline arithmetic addition of this and other

Specified by:
plusEquals in interface Ring<InfiniteVector<KeyType>>
Parameters:
other - object to add to this

minus

public InfiniteVector<KeyType> minus(InfiniteVector<KeyType> other)
Description copied from interface: Ring
Arithmetic subtraction of other from this

Specified by:
minus in interface Ring<InfiniteVector<KeyType>>
Parameters:
other - object to subtract from this
Returns:
difference of this and other

minusEquals

public void minusEquals(InfiniteVector<KeyType> other)
Description copied from interface: Ring
Inline arithmetic subtraction of other from this

Specified by:
minusEquals in interface Ring<InfiniteVector<KeyType>>
Parameters:
other - object to subtract from this

dotTimes

public InfiniteVector<KeyType> dotTimes(InfiniteVector<KeyType> other)
Description copied from interface: Ring
Element-wise multiplication of this and other

Specified by:
dotTimes in interface Ring<InfiniteVector<KeyType>>
Parameters:
other - elements of other will be multiplied to the corresponding elements of this
Returns:
element-wise multiplication of this and other

dotTimesEquals

public void dotTimesEquals(InfiniteVector<KeyType> other)
Description copied from interface: Ring
Inline element-wise multiplication of this and other

Specified by:
dotTimesEquals in interface Ring<InfiniteVector<KeyType>>
Parameters:
other - elements of other will be multiplied to the corresponding elements of this

scale

public InfiniteVector<KeyType> scale(double scaleFactor)
Description copied from interface: Ring
Element-wise scaling of this by scaleFactor

Specified by:
scale in interface Ring<InfiniteVector<KeyType>>
Parameters:
scaleFactor - amount to scale the elements of this
Returns:
scaling of this

scaleEquals

public void scaleEquals(double scaleFactor)
Description copied from interface: Ring
Inline element-wise scaling of this by scaleFactor

Specified by:
scaleEquals in interface Ring<InfiniteVector<KeyType>>
Parameters:
scaleFactor - amount to scale the elements of this

scaledPlus

public InfiniteVector<KeyType> scaledPlus(double scaleFactor,
                                          InfiniteVector<KeyType> other)
Description copied from interface: Ring
Arithmetic addition of this and other after element-wise scaling of other by scaleFactor. If this is x, other is y, and scaleFactor is a, then this method is equivalent to x + a * y. It is typically a more efficient way of doing this.plus(other.scale(scaleFactor)) since it can avoid intermediate object creation.

Specified by:
scaledPlus in interface Ring<InfiniteVector<KeyType>>
Parameters:
scaleFactor - The scale factor to multiply by the elements of other before adding to the elements of this.
other - Object to scale and then add to this.
Returns:
The result of applying the scale factor to other then adding to this.

scaledPlusEquals

public void scaledPlusEquals(double scaleFactor,
                             InfiniteVector<KeyType> other)
Description copied from interface: Ring
Inline arithmetic addition of this and other after element-wise scaling of other by scaleFactor. If this is x, other is y, and scaleFactor is a, then this method is equivalent to x += a * y. It is typically a more efficient way of doing this.plusEquals(other.scale(scaleFactor)) since it can avoid intermediate object creation.

Specified by:
scaledPlusEquals in interface Ring<InfiniteVector<KeyType>>
Parameters:
scaleFactor - The scale factor to multiply by the elements of other before adding to the elements of this.
other - Object to scale and then add to this.

scaledMinus

public InfiniteVector<KeyType> scaledMinus(double scaleFactor,
                                           InfiniteVector<KeyType> other)
Description copied from interface: Ring
Arithmetic subtraction other after element-wise scaling of other by scaleFactor from this. If this is x, other is y, and scaleFactor is a, then this method is equivalent to x - a * y. It is typically a more efficient way of doing this.minus(other.scale(scaleFactor)) since it can avoid intermediate object creation.

Specified by:
scaledMinus in interface Ring<InfiniteVector<KeyType>>
Parameters:
scaleFactor - The scale factor to multiply by the elements of other before subtracting from the elements of this.
other - Object to scale and then subtract from this.
Returns:
The result of applying the scale factor to other then subtract from this.

scaledMinusEquals

public void scaledMinusEquals(double scaleFactor,
                              InfiniteVector<KeyType> other)
Description copied from interface: Ring
Inline arithmetic subtraction of other after element-wise scaling of other by scaleFactor from this. If this is x, other is y, and scaleFactor is a, then this method is equivalent to x -= a * y. It is typically a more efficient way of doing this.minusEquals(other.scale(scaleFactor)) since it can avoid intermediate object creation.

Specified by:
scaledMinusEquals in interface Ring<InfiniteVector<KeyType>>
Parameters:
scaleFactor - The scale factor to multiply by the elements of other before adding to the elements of this.
other - Object to scale and then add to this.

zero

public void zero()
Description copied from interface: Ring
Zeros out all elements of this, so that the following are equivalent r1.scaleEquals( 0.0 ); and r1.zero(); Furthermore, r1.zero(); anything.dotTimes( r1 ).equals( r1 );

Specified by:
zero in interface Ring<InfiniteVector<KeyType>>

isZero

public boolean isZero()
Description copied from interface: Ring
Determines if this ring is equal to zero.

Specified by:
isZero in interface Ring<InfiniteVector<KeyType>>
Returns:
True if all of the elements of this ring are zero.

isZero

public boolean isZero(double effectiveZero)
Description copied from interface: Ring
Determines if this ring is equal to zero using the element-wise effective zero value.

Specified by:
isZero in interface Ring<InfiniteVector<KeyType>>
Parameters:
effectiveZero - Tolerance threshold for element-wise equality
Returns:
True if all of the elements of this ring are effectively zero.

compact

public void compact()
Description copied from class: AbstractMutableDoubleMap
Removes entries from the map with value of 0.0

Specified by:
compact in interface InfiniteVector<KeyType>
Overrides:
compact in class AbstractMutableDoubleMap<KeyType>

toString

public String toString()
Overrides:
toString in class Object

iterator

public Iterator<InfiniteVector.Entry<KeyType>> iterator()
Specified by:
iterator in interface Iterable<InfiniteVector.Entry<KeyType>>

negative

public InfiniteVector<KeyType> negative()
Description copied from interface: Ring
Returns the element-wise negation of this, such that this.plus( this.negative() ) has only zero elements.

Specified by:
negative in interface Ring<InfiniteVector<KeyType>>
Returns:
element-wise negation of this

negativeEquals

public void negativeEquals()
Description copied from interface: Ring
Inline element-wise negation of this

Specified by:
negativeEquals in interface Ring<InfiniteVector<KeyType>>