Class VectorAsMDArray

java.lang.Object
com.linkedin.dagli.math.mdarray.AbstractMDArray
com.linkedin.dagli.math.mdarray.VectorAsMDArray
All Implemented Interfaces:
MDArray, java.io.Serializable, java.lang.AutoCloseable

public class VectorAsMDArray
extends AbstractMDArray
A MDArray backed by a Vector. Changes to the underlying vector will by reflected by this instance.
See Also:
Serialized Form
  • Field Summary

    Fields inherited from class com.linkedin.dagli.math.mdarray.AbstractMDArray

    _shape
  • Constructor Summary

    Constructors
    Constructor Description
    VectorAsMDArray​(com.linkedin.dagli.math.vector.Vector vector, long... shape)
    Creates a new instance backed by the specified vector.
  • Method Summary

    Modifier and Type Method Description
    com.linkedin.dagli.math.vector.DenseVector asVector()
    Returns a flattened, vectorized representation of this MDArray.
    double getAsDouble​(long offset)
    Gets the component at the specified linear offset.
    double getAsDouble​(long... indices)
    Gets a component at the specified indices.
    double getAsDoubleUnsafe​(long offset)
    Gets the component at the specified linear offset, possibly without checking that it is in-bounds for the MDArray.
    long getAsLong​(long offset)
    Gets the component at the specified linear offset.
    long getAsLong​(long... indices)
    Gets a component at the specified indices.
    long getAsLongUnsafe​(long offset)
    Gets the component at the specified linear offset, possibly without checking that it is in-bounds for the MDArray.
    java.lang.Class<? extends java.lang.Number> valueType()
    Gets the smallest primitive number type capable of losslessly holding the values stored in this array.

    Methods inherited from class com.linkedin.dagli.math.mdarray.AbstractMDArray

    shape, toString

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait

    Methods inherited from interface com.linkedin.dagli.math.mdarray.MDArray

    close, subarrayAt
  • Constructor Details

    • VectorAsMDArray

      public VectorAsMDArray​(com.linkedin.dagli.math.vector.Vector vector, long... shape)
      Creates a new instance backed by the specified vector. The shape can be any (valid) size; any components of the MDArray with no corresponding vector element will simply be 0.
      Parameters:
      vector - a vector
      shape - the shape of the array
  • Method Details

    • valueType

      public java.lang.Class<? extends java.lang.Number> valueType()
      Description copied from interface: MDArray
      Gets the smallest primitive number type capable of losslessly holding the values stored in this array. If no primitive type is capable of losslessly representing the array's values (e.g. they are BigIntegers), double.class will be returned. For example, for half-precision components, the returned type will be float.class. For {0, 1} booleans, the returned type will be byte.class.
      Returns:
      the smallest primitive number type capable of losslessly holding the values stored in this array
    • asVector

      public com.linkedin.dagli.math.vector.DenseVector asVector()
      Description copied from interface: MDArray
      Returns a flattened, vectorized representation of this MDArray. No data is copied; changes to the MDArray will be reflected in the returned vector. MDArrays have a canonical row-major layout of elements that determines how the indices of an element translate to its offset, which applies even if the underlying data storage of this MDArray happens to use a different memory layout (e.g. column-major). Let index(d) be the index of a component within the d'th dimension of the original array, and let size(d) be the size of the d'th dimension of the original array. Then the index of the vector element corresponding with that component is calculated as: index(0) * (size(1) * size(2)...) + index(1) * (size(2) * size(3)...) + index(2) * (size(3) * size(4)...) + ... A consequence of this is that components whose position in the original array differs only in the last dimension will have vector indices closer than those whose position differs in the second-to-last dimension, which will themselves have vector indices closer than those whose position differs in the third-to-last dimension, etc.
      Returns:
      a DenseVector representation of this MDArray
    • getAsDouble

      public double getAsDouble​(long... indices)
      Description copied from interface: MDArray
      Gets a component at the specified indices. As a special case, the value of a 0-dimension "scalar" array is retrieved by passing a 0-length indices array. For better performance in tight loops, reusing the same index array passed to this method avoids unnecessary long[] creation (and collection).
      Parameters:
      indices - the indices to fetch
      Returns:
      the value at the specified index
    • getAsDouble

      public double getAsDouble​(long offset)
      Description copied from interface: MDArray
      Gets the component at the specified linear offset. The component corresponding to an offset is determined as it the array was vectorized according to MDArray.asVector(). The value of a 0-dimensional scalar "array" is retrievable at offset 0.
      Parameters:
      offset - the offset of the component whose value should be retrieved
      Returns:
      the value at the specified index
    • getAsDoubleUnsafe

      public double getAsDoubleUnsafe​(long offset)
      Description copied from interface: MDArray
      Gets the component at the specified linear offset, possibly without checking that it is in-bounds for the MDArray. Depending on the implementation, this may be slightly faster than the MDArray.getAsDouble(long). This method can be used when the offset is already known to be in-bounds. The component corresponding to an offset is determined as it the array was vectorized according to MDArray.asVector(). The value of a 0-dimensional scalar "array" is retrievable at offset 0.
      Parameters:
      offset - the offset of the component whose value should be retrieved
      Returns:
      the value at the specified index
    • getAsLong

      public long getAsLong​(long... indices)
      Description copied from interface: MDArray
      Gets a component at the specified indices. Non-integer values will be silently truncated. As a special case, the value of a 0-dimension "scalar" array is retrieved by passing a 0-length indices array. For better performance in tight loops, reusing the same index array passed to this method avoids unnecessary long[] creation (and collection).
      Parameters:
      indices - the indices to fetch
      Returns:
      the value at the specified index
    • getAsLong

      public long getAsLong​(long offset)
      Description copied from interface: MDArray
      Gets the component at the specified linear offset. Non-integer values will be silently truncated. The component corresponding to an offset is determined as it the array was vectorized according to MDArray.asVector(). The value of a 0-dimensional scalar "array" is retrievable at offset 0.
      Parameters:
      offset - the offset of the component whose value should be retrieved
      Returns:
      the value at the specified index
    • getAsLongUnsafe

      public long getAsLongUnsafe​(long offset)
      Description copied from interface: MDArray
      Gets the component at the specified linear offset, possibly without checking that it is in-bounds for the MDArray. Non-integer values will be silently truncated. Depending on the implementation, this may be slightly faster than the MDArray.getAsLong(long). This method can be used when the offset is already known to be in-bounds. The component corresponding to an offset is determined as it the array was vectorized according to MDArray.asVector(). The value of a 0-dimensional scalar "array" is retrievable at offset 0.
      Parameters:
      offset - the offset of the component whose value should be retrieved
      Returns:
      the value at the specified index