Class LinearlyDerivedVector

All Implemented Interfaces:
Serializable, Iterable<Number>, Collection<Number>, List<Number>, RandomAccess

final class LinearlyDerivedVector extends Vector implements Serializable
A vector derived from another vector by application of a scale factor and an offset. This is used for example when data in a netCDF file are packed as integer values.

Several methods in this implementation requires a bijective relationship between this and base. This is mostly the case if coefficients are finite and scale is non-zero, as asserted at construction time.

Since:
1.0
Version:
1.1
See Also:
  • Field Details

    • serialVersionUID

      private static final long serialVersionUID
      For cross-version compatibility.
      See Also:
    • base

      private final Vector base
      The vector on which this vector is derived from.
    • scale

      private final double scale
      The scale factor to apply.
    • offset

      private final double offset
      The offset to apply after the scale factor.
  • Constructor Details

    • LinearlyDerivedVector

      LinearlyDerivedVector(Vector base, double scale, double offset)
      Creates a new vector of derived data.
  • Method Details

    • getElementType

      public Class<Double> getElementType()
      Returns the type of values resulting from the linear conversion applied by this vector. Note that it does not means that this vector can store all values of that type.
      Specified by:
      getElementType in class Vector
      Returns:
      the type of elements in this vector.
      See Also:
    • isSinglePrecision

      public boolean isSinglePrecision()
      Double-precision values are not guaranteed to be convertible to single-precision floating point type.
      Overrides:
      isSinglePrecision in class Vector
      Returns:
      whether values in this vector can be casted to float primitive type.
      See Also:
    • isInteger

      public boolean isInteger()
      Returns true if this vector contains only integer values. This implementation delegates to the base vector if coefficients are integers, or scans all values otherwise.
      Overrides:
      isInteger in class Vector
      Returns:
      true if this vector contains only integer values.
    • size

      public int size()
      Returns the number of elements in this vector. This is taken directly from the base vector.
      Specified by:
      size in interface Collection<Number>
      Specified by:
      size in interface List<Number>
      Specified by:
      size in class Vector
      Returns:
      the number of elements in this vector.
    • isEmptyOrNaN

      public boolean isEmptyOrNaN()
      Returns true if this vector is empty or contains only NaN values. The implementation delegates to the base vector since linear relationship does not change whether values are NaN.
      Overrides:
      isEmptyOrNaN in class Vector
      Returns:
      whether this vector is empty or contains only NaN values.
    • isNaN

      public boolean isNaN(int index)
      Returns true if the value at the given index is null or NaN. The implementation delegates to the base vector since linear relationship does not change whether a value is NaN.
      Specified by:
      isNaN in class Vector
      Parameters:
      index - the index in the [0 … size-1] range.
      Returns:
      true if the value at the given index is NaN.
    • doubleValue

      public double doubleValue(int index)
      Returns the value at the given index as a double. This method is invoked by all others fooValue().
      Specified by:
      doubleValue in class Vector
      Parameters:
      index - the index in the [0 … size-1] range.
      Returns:
      the value at the given index.
      See Also:
    • stringValue

      public String stringValue(int index)
      Returns a string representation of the value at the given index. This implementation uses the double representation.
      Specified by:
      stringValue in class Vector
      Parameters:
      index - the index in the [0 … size-1] range.
      Returns:
      a string representation of the value at the given index (may be null).
      See Also:
    • get

      public Number get(int index)
      Returns the number at the given index as a Double.
      Specified by:
      get in interface List<Number>
      Specified by:
      get in class Vector
      Parameters:
      index - the index in the [0 … size-1] range.
      Returns:
      the value at the given index (may be null).
      See Also:
    • set

      public Number set(int index, Number value)
      Sets the number at the given index. This action is likely to loose precision if the base vector stores values using integer primitive type.
      Specified by:
      set in interface List<Number>
      Specified by:
      set in class Vector
      Parameters:
      index - the index in the [0 … size-1] range.
      value - the value to set at the given index.
      Returns:
      the value previously stored at the given index.
    • fill

      public void fill(int fromIndex, int toIndex, Number value)
      Sets a range of elements to the given number. This action is likely to loose precision if the base vector stores values using integer primitive type.
      Overrides:
      fill in class Vector
      Parameters:
      fromIndex - index of the first element (inclusive) to be filled with the specified value.
      toIndex - index of the last element (exclusive) to be filled with the specified value.
      value - the value to be stored in elements of the vector.
    • indexOf

      final int indexOf(int toSearch, int index, boolean equality)
      Returns the index of a search based one values at given indices (see javadoc in parent class for details). This implementation forwards to backing vector on the assumption that the this ↔︎ base relationship is bijective.
      Overrides:
      indexOf in class Vector
      Parameters:
      toSearch - index of the value to search.
      index - index of the first value where to start the search.
      equality - whether we search the first equal value, or the first different value.
      Returns:
      index of the value found, or the vector size if the value has not been found.
    • repetitions

      public int[] repetitions(int... candidates)
      Detects repetition patterns in the values contained in this vector. This implementation forwards to backing vector on the assumption that the this ↔︎ base relationship is bijective.
      Overrides:
      repetitions in class Vector
      Parameters:
      candidates - probable values, or null or an empty array if unknown. If non-empty, those values will be used for narrowing the search, which may improve performances. There is no guarantee that the values returned by this method will be among the given candidates.
      Returns:
      the number of times that entities (numbers, or group of numbers) appears consecutively with identical values. If no such repetition is found, an empty array.
      See Also:
    • increment

      public Number increment(double tolerance)
      Returns the increment between all consecutive values if this increment is constant, or null otherwise. This implementation converts the value computed by the backing vector on the assumption that the this ↔︎ base relationship is a linear.
      Overrides:
      increment in class Vector
      Parameters:
      tolerance - the tolerance threshold for verifying if the increment is constant.
      Returns:
      the increment as a signed value, or null if the increment is not constant.
    • convert

      private Number convert(Number value)
      Applies the linear relationship on the given value.
      Parameters:
      value - the number to convert, or null.
      Returns:
      the converted number (may be null).
    • inverse

      private Number inverse(Number value)
      Applies the inverse linear function on the given value.
      Parameters:
      value - the number to inverse convert, or null.
      Returns:
      the inverse converted number (may be null).
    • convert

      private NumberRange<?> convert(NumberRange<?> range)
      Converts the given range. This is used for delegating range calculation to the base vector on the assumption that is will be more efficient than iterating ourselves on all values.
      Parameters:
      range - the range to convert, or null.
      Returns:
      the converted range, or null if the given range was null.
    • range

      public NumberRange<?> range()
      Returns the minimal and maximal values found in this vector. This implementation delegates to the base vector (which may provide fast implementation) then convert the result.
      Overrides:
      range in class Vector
      Returns:
      minimal and maximal values found in this vector.
    • range

      final NumberRange<?> range(IntSupplier indices, int n)
      Computes the range of values at the indices provided by the given supplier. This implementation delegates to the base vector (which may provide fast implementation) then convert the result.
      Overrides:
      range in class Vector
      Parameters:
      indices - supplier of indices of the values to examine for computing the range, or null for the 0, 1, 2, … n-1 sequence.
      n - number of indices to get from the supplier.
      Returns:
      the range of all values at the given indices.
    • subSampling

      public Vector subSampling(int first, int step, int length)
      Returns a view which contain the values of this vector in a given index range. This implementation delegates to base vector and wraps again.
      Overrides:
      subSampling in class Vector
      Parameters:
      first - index of the first value in this vector to be included in the returned view.
      step - the index increment between values in this vector to be included in the returned view. Can be positive, zero or negative.
      length - the length of the view to be returned. Cannot be greater than the length of this vector, except if the step is zero.
      Returns:
      a view of this vector containing values in the given index range.
    • pick

      public Vector pick(int... indices)
      Returns a view which contains the values of this vector at the given indexes. This implementation delegates to base vector and wraps again.
      Overrides:
      pick in class Vector
      Parameters:
      indices - indexes of the values to be returned.
      Returns:
      a view of this vector containing values at the given indexes.
    • createConcatenate

      final Vector createConcatenate(Vector toAppend)
      Returns the concatenation of this vector with the given one. This implementation potentially delegate to base vector.
      Overrides:
      createConcatenate in class Vector
    • createTransform

      final Vector createTransform(double s, double t)
      Concatenates the given transformation with the current transform.
      Overrides:
      createTransform in class Vector
    • doubleValues

      public double[] doubleValues()
      Copies all values in an array of double precision floating point numbers. We override this method for efficiency. We do not override the float versions for accuracy reasons.
      Overrides:
      doubleValues in class Vector
      Returns:
      a copy of all floating point values in this vector.
      See Also:
    • equals

      public boolean equals(Object object)
      Optimization of equals method for the case where the other object is another LinearlyDerivedVector using the same linear relationship.
      Specified by:
      equals in interface Collection<Number>
      Specified by:
      equals in interface List<Number>
      Overrides:
      equals in class Vector
      Parameters:
      object - the other object to compare with this vector.
      Returns:
      true if the given object is a vector containing the same values than this vector.
    • equals

      boolean equals(int lower, int upper, Vector other, int otherOffset)
      Optimization of equals method for the case where the other object is another LinearlyDerivedVector using the same linear relationship.
      Overrides:
      equals in class Vector
      Parameters:
      lower - index of the first value to compare in this vector, inclusive.
      upper - index after the last value to compare in this vector.
      other - the other vector to compare values with this vector. May be this.
      otherOffset - index of the first element to compare in the other vector.
      Returns:
      whether values over the specified range of the two vectors are equal.