Class OpenMapRealVector

  • All Implemented Interfaces:
    Serializable

    public class OpenMapRealVector
    extends SparseRealVector
    implements Serializable
    This class implements the RealVector interface with a OpenIntToDoubleHashMap backing store.

    Caveat: This implementation assumes that, for any x, the equality x * 0d == 0d holds. But it is is not true for NaN. Moreover, zero entries will lose their sign. Some operations (that involve NaN and/or infinities) may thus give incorrect results, like multiplications, divisions or functions mapping.

    See Also:
    Serialized Form
    • Field Detail

      • DEFAULT_ZERO_TOLERANCE

        public static final double DEFAULT_ZERO_TOLERANCE
        Default Tolerance for having a value considered zero.
        See Also:
        Constant Field Values
    • Constructor Detail

      • OpenMapRealVector

        public OpenMapRealVector​(int dimension)
        Construct a vector of zeroes.
        Parameters:
        dimension - Size of the vector.
      • OpenMapRealVector

        public OpenMapRealVector​(int dimension,
                                 double epsilon)
        Construct a vector of zeroes, specifying zero tolerance.
        Parameters:
        dimension - Size of the vector.
        epsilon - Tolerance below which a value considered zero.
      • OpenMapRealVector

        protected OpenMapRealVector​(OpenMapRealVector v,
                                    int resize)
        Build a resized vector, for use with append.
        Parameters:
        v - Original vector.
        resize - Amount to add.
      • OpenMapRealVector

        public OpenMapRealVector​(int dimension,
                                 int expectedSize)
        Build a vector with known the sparseness (for advanced use only).
        Parameters:
        dimension - Size of the vector.
        expectedSize - The expected number of non-zero entries.
      • OpenMapRealVector

        public OpenMapRealVector​(int dimension,
                                 int expectedSize,
                                 double epsilon)
        Build a vector with known the sparseness and zero tolerance setting (for advanced use only).
        Parameters:
        dimension - Size of the vector.
        expectedSize - Expected number of non-zero entries.
        epsilon - Tolerance below which a value is considered zero.
      • OpenMapRealVector

        public OpenMapRealVector​(double[] values)
        Create from an array. Only non-zero entries will be stored.
        Parameters:
        values - Set of values to create from.
      • OpenMapRealVector

        public OpenMapRealVector​(double[] values,
                                 double epsilon)
        Create from an array, specifying zero tolerance. Only non-zero entries will be stored.
        Parameters:
        values - Set of values to create from.
        epsilon - Tolerance below which a value is considered zero.
      • OpenMapRealVector

        public OpenMapRealVector​(Double[] values)
        Create from an array. Only non-zero entries will be stored.
        Parameters:
        values - The set of values to create from
      • OpenMapRealVector

        public OpenMapRealVector​(Double[] values,
                                 double epsilon)
        Create from an array. Only non-zero entries will be stored.
        Parameters:
        values - Set of values to create from.
        epsilon - Tolerance below which a value is considered zero.
      • OpenMapRealVector

        public OpenMapRealVector​(OpenMapRealVector v)
        Copy constructor.
        Parameters:
        v - Instance to copy from.
      • OpenMapRealVector

        public OpenMapRealVector​(RealVector v)
        Generic copy constructor.
        Parameters:
        v - Instance to copy from.
    • Method Detail

      • isDefaultValue

        protected boolean isDefaultValue​(double value)
        Determine if this value is within epsilon of zero.
        Parameters:
        value - Value to test
        Returns:
        true if this value is within epsilon to zero, false otherwise.
      • append

        public OpenMapRealVector append​(OpenMapRealVector v)
        Optimized method to append a OpenMapRealVector.
        Parameters:
        v - vector to append
        Returns:
        The result of appending v to self
      • append

        public OpenMapRealVector append​(RealVector v)
        Construct a new vector by appending a vector to this vector.
        Specified by:
        append in class RealVector
        Parameters:
        v - vector to append to this one.
        Returns:
        a new vector.
      • append

        public OpenMapRealVector append​(double d)
        Construct a new vector by appending a double to this vector.
        Specified by:
        append in class RealVector
        Parameters:
        d - double to append.
        Returns:
        a new vector.
      • getDimension

        public int getDimension()
        Returns the size of the vector.
        Specified by:
        getDimension in class RealVector
        Returns:
        the size of this vector.
      • getL1Distance

        public double getL1Distance​(OpenMapRealVector v)
                             throws MathIllegalArgumentException
        Distance between two vectors. This method computes the distance consistent with L1 norm, i.e. the sum of the absolute values of elements differences.
        Parameters:
        v - Vector to which distance is requested.
        Returns:
        distance between this vector and v.
        Throws:
        MathIllegalArgumentException - if the dimensions do not match.
      • getL1Distance

        public double getL1Distance​(RealVector v)
                             throws MathIllegalArgumentException
        Distance between two vectors.

        This method computes the distance consistent with L1 norm, i.e. the sum of the absolute values of the elements differences.

        Overrides:
        getL1Distance in class RealVector
        Parameters:
        v - Vector to which distance is requested.
        Returns:
        the distance between two vectors.
        Throws:
        MathIllegalArgumentException - if v is not the same size as this vector.
      • isInfinite

        public boolean isInfinite()
        Check whether any coordinate of this vector is infinite and none are NaN.
        Specified by:
        isInfinite in class RealVector
        Returns:
        true if any coordinate of this vector is infinite and none are NaN, false otherwise.
      • isNaN

        public boolean isNaN()
        Check whether any coordinate of this vector is NaN.
        Specified by:
        isNaN in class RealVector
        Returns:
        true if any coordinate of this vector is NaN, false otherwise.
      • mapAdd

        public OpenMapRealVector mapAdd​(double d)
        Add a value to each entry. Returns a new vector. Does not change instance data.
        Overrides:
        mapAdd in class RealVector
        Parameters:
        d - Value to be added to each entry.
        Returns:
        this + d.
      • mapAddToSelf

        public OpenMapRealVector mapAddToSelf​(double d)
        Add a value to each entry. The instance is changed in-place.
        Overrides:
        mapAddToSelf in class RealVector
        Parameters:
        d - Value to be added to each entry.
        Returns:
        this.
      • set

        public void set​(double value)
        Set all elements to a single value.
        Overrides:
        set in class RealVector
        Parameters:
        value - Single value to set for all elements.
      • toArray

        public double[] toArray()
        Convert the vector to an array of doubles. The array is independent from this vector data: the elements are copied.
        Overrides:
        toArray in class RealVector
        Returns:
        an array containing a copy of the vector elements.
      • hashCode

        public int hashCode()
        . This method must be overriden by concrete subclasses of RealVector (current implementation throws an exception). Implementation Note: This works on exact values, and as a result it is possible for a.subtract(b) to be the zero vector, while a.hashCode() != b.hashCode().
        Overrides:
        hashCode in class RealVector
      • equals

        public boolean equals​(Object obj)

        Test for the equality of two real vectors. If all coordinates of two real vectors are exactly the same, and none are NaN, the two real vectors are considered to be equal. NaN coordinates are considered to affect globally the vector and be equals to each other - i.e, if either (or all) coordinates of the real vector are equal to NaN, the real vector is equal to a vector with all NaN coordinates.

        This method must be overriden by concrete subclasses of RealVector (the current implementation throws an exception).

        Implementation Note: This performs an exact comparison, and as a result it is possible for a.subtract(b} to be the zero vector, while a.equals(b) == false.
        Overrides:
        equals in class RealVector
        Parameters:
        obj - Object to test for equality.
        Returns:
        true if two vector objects are equal, false if other is null, not an instance of RealVector, or not equal to this RealVector instance.
      • getSparsity

        public double getSparsity()
        Get percentage of none zero elements as a decimal percent.
        Returns:
        the percentage of none zero elements as a decimal percent
      • sparseIterator

        public Iterator<RealVector.Entry> sparseIterator()
        Create a sparse iterator over the vector, which may omit some entries. The ommitted entries are either exact zeroes (for dense implementations) or are the entries which are not stored (for real sparse vectors). No guarantees are made about order of iteration.

        Note: derived classes are required to return an Iterator that returns non-null RealVector.Entry objects as long as Iterator.hasNext() returns true.

        Overrides:
        sparseIterator in class RealVector
        Returns:
        a sparse iterator.