Package com.linkedin.dagli.math.mdarray
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.DenseVectorasVector()Returns a flattened, vectorized representation of thisMDArray.doublegetAsDouble(long offset)Gets the component at the specified linear offset.doublegetAsDouble(long... indices)Gets a component at the specified indices.doublegetAsDoubleUnsafe(long offset)Gets the component at the specified linear offset, possibly without checking that it is in-bounds for the MDArray.longgetAsLong(long offset)Gets the component at the specified linear offset.longgetAsLong(long... indices)Gets a component at the specified indices.longgetAsLongUnsafe(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, toStringMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods 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 theMDArraywith no corresponding vector element will simply be 0.- Parameters:
vector- a vectorshape- the shape of the array
-
-
Method Details
-
valueType
public java.lang.Class<? extends java.lang.Number> valueType()Description copied from interface:MDArrayGets 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.classwill be returned. For example, for half-precision components, the returned type will befloat.class. For {0, 1} booleans, the returned type will bebyte.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:MDArrayReturns a flattened, vectorized representation of thisMDArray. No data is copied; changes to theMDArraywill 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
DenseVectorrepresentation of thisMDArray
-
getAsDouble
public double getAsDouble(long... indices)Description copied from interface:MDArrayGets 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:MDArrayGets the component at the specified linear offset. The component corresponding to an offset is determined as it the array was vectorized according toMDArray.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:MDArrayGets 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 theMDArray.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 toMDArray.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:MDArrayGets 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:MDArrayGets 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 toMDArray.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:MDArrayGets 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 theMDArray.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 toMDArray.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
-