mikera.arrayz
Class Array
- java.lang.Object
-
- mikera.arrayz.impl.AbstractArray<INDArray>
-
- mikera.arrayz.impl.BaseShapedArray
-
- mikera.arrayz.Array
-
- All Implemented Interfaces:
- java.io.Serializable, java.lang.Cloneable, java.lang.Comparable<INDArray>, java.lang.Iterable<INDArray>, IDense, IDenseArray, IStridedArray, INDArray
public final class Array extends BaseShapedArray implements IStridedArray, IDenseArray
General purpose, mutable, packed, dense N-dimensional array This is the general multi-dimensional equivalent of Matrix and Vector, and as such is the most efficient storage type for dense 3D+ arrays- See Also:
- Serialized Form
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method and Description voidabs()Replaces all elements of this array with their absolute values, according to Math.abs(double)voidadd(double x)Adds a double value to all elements in this array.voidapplyOp(IOperator op)Applies a unary operator to all elements of the array (in-place)voidapplyOp(Op op)Applies a unary operator to all elements of the array (in-place)double[]asDoubleArray()Returns the underlying double array representing the densely packed elements of this array.VectorasVector()Constructs a view of all elements in the array as a single vector in row-major order.INDArrayclone()Returns a clone of the array data, as a new array which will be fully mutable and may be of a different class to the original.static Arraycreate(INDArray a)Crates a new Array with a copy of the data from the specified array, in the same shapestatic ArraycreateFromVector(AVector a, int... shape)Creates a new matrix using the elements in the specified vector.intdimensionality()Returns the dimensionality of the array (i.e.longelementCount()Returns the total number of elements in this array.java.util.Iterator<java.lang.Double>elementIterator()Returns an iterator over all elements in this array, in row-major orderdoubleelementMax()Returns the maximum element value in this array.doubleelementMin()Returns the maximum element value in this array.doubleelementSquaredSum()Returns the sum of squared elements in this array.doubleelementSum()Returns the sum of all elements in this array.booleanequals(INDArray a)Checks if two arrays are equal exactly in terms of both value and shape Element equality checks are consistent with compareTobooleanequalsArray(double[] data, int offset)Returns true if the elements in this array exactly match the elements in the given array, in row-major orderArrayexactClone()Creates a deep clone of an array, using the same class implementation as the original arrayvoidexp()Computes the function e^x (in-place) for all array elementsdoubleget()Returns the double value of a scalar arraydoubleget(int... indexes)Returns the double value at the specified index position in an arraydoubleget(int x)Returns the double value at the specified position in a 1D vectordoubleget(int x, int y)Returns the double value at the specified position in a 2D matrixdouble[]getArray()Gets the underlying data array for this strided array Unsafe operation - may allow modification of otherwise immutable arraysintgetArrayOffset()Gets the data array offset for the first element of this strided arrayvoidgetElements(double[] values, int offset)Copies all elements of this array a double array at the specified offsetlong[]getLongShape()Returns the shape of the array as an array of longs.java.util.List<?>getSlices()Returns a list of all major slices of this array.intgetStride(int dim)Gets the stide for a specific dimension in this strided array.int[]getStrides()Gets the strides for each dimension of this strided array.INDArraygetTranspose()Returns the transpose of this array.INDArraygetTransposeView()Returns a transposed view of the array.INDArrayimmutable()Returns an immutable version of this array.booleanisElementConstrained()Returns true if this array has some constraints on element valuesbooleanisFullyMutable()If this method returns true, the INDArray is guaranteed to be fully mutable in all positions i.e.booleanisMutable()Returns true if the INDArray is mutable (at least partially)booleanisPackedArray()Checks if this array is fully packed in row major order.booleanisView()Return true if this array is considered as a view type.booleanisZero()Returns true if the array is zero (all elements equal to zero)voidlog()Computes the natural logarithm (in-place) for all array elementsvoidmultiply(double factor)Multiplies all elements of the array in place by a given double valuestatic ArraynewArray(int... shape)Creates a new zero-filled mutable Array of the specified shape.doublereduce(Op2 op)Reduces over all elements of the array in row-major order.doublereduce(Op2 op, double init)Reduces over all elements of the array in row-major order.voidset(int[] indexes, double value)Sets the element at the given indexed position in a mutable arrayvoidsetElements(int pos, double[] values, int offset, int length)Sets elements in an array using the given double values.voidsignum()Calculates the signum of all elements of this mutable array Sets each component of the array to its sign value (-1, 0 or 1)INDArrayslice(int majorSlice)Returns the specified major slice of this array as a view (slice along dimension 0) 0 dimensional slice results are permitted, but attempting to slice a 0 dimensional array will result in an error.INDArrayslice(int dimension, int index)Returns a slice view of this array along the specified dimensionvoidsquare()Squares all elements of the array in placeINDArraysubArray(int[] offsets, int[] shape)Returns a subarray view of a larger arraydouble[]toDoubleArray()Copies the elements of this array to a new double[] array.voidtoDoubleBuffer(java.nio.DoubleBuffer dest)Copies the elements of this INDArray to the specified double buffer.VectortoVector()Coerces the array into a flattened dense Vector, in row-major order.voidvalidate()Validates the internal data structure of the INDArray.static Arraywrap(double[] data, int... shape)Wraps double[] array data in an Array of the specified shape.static Arraywrap(Matrix m)Wraps the underlying data from a dense Matrix as an array with the same shapestatic Arraywrap(Vector v)Wraps the underlying data from a dense Vector as an array with the same shape-
Methods inherited from class mikera.arrayz.impl.BaseShapedArray
getElement, getShape, getShape, getShapeClone, sliceCount
-
Methods inherited from class mikera.arrayz.impl.AbstractArray
absCopy, absDiff, absDiffCopy, add, add, addApplyOp, addAt, addCopy, addCopy, addCopy, addInnerProduct, addInnerProduct, addInnerProduct, addInnerProduct, addMultiple, addMultipleSparse, addOuterProduct, addOuterProductSparse, addPower, addPower, addSparse, addSparse, addToArray, applyOp, applyOp, applyOpCopy, asElementList, broadcast, broadcastCloneLike, broadcastCopyLike, broadcastLike, broadcastLike, broadcastLike, clamp, compareTo, componentCount, copy, dense, denseClone, density, divide, divide, divideCopy, divideCopy, elementAbsPowSum, elementMaxAbs, elementPowSum, elementProduct, elementsEqual, ensureMutable, epsilonEquals, epsilonEquals, equals, equals, equalsArray, fill, get, get, get, get, get, getComponent, getComponents, getElements, getElements, getElements, getSlices, getSliceViews, getTransposeCopy, hashCode, hasUncountable, innerProduct, innerProduct, innerProduct, innerProduct, isBoolean, isDense, isSameShape, isSparse, iterator, join, join, multiply, multiplyCopy, multiplyCopy, mutable, negate, negateCopy, nonZeroCount, outerProduct, pow, reciprocal, reciprocalCopy, reduceSlices, reduceSlices, reorder, reorder, reshape, rotateView, scale, scaleAdd, scaleAdd, scaleCopy, set, set, set, set, set, set, setApplyOp, setElements, setElements, setInnerProduct, setMultiple, setMultiple, setSparse, setSparse, signumCopy, sliceValue, sparse, sparseClone, sqrt, squareCopy, sub, sub, sub, subCopy, toSliceArray, toString, toStringFull, withComponents
-
Methods inherited from interface mikera.arrayz.INDArray
absCopy, absDiff, absDiffCopy, add, addApplyOp, addAt, addCopy, addCopy, addInnerProduct, addInnerProduct, addMultiple, addMultipleSparse, addOuterProduct, addOuterProductSparse, addPower, addPower, addSparse, addSparse, addToArray, applyOp, applyOp, applyOpCopy, asElementList, broadcast, broadcastCloneLike, broadcastCopyLike, broadcastLike, broadcastLike, broadcastLike, clamp, componentCount, copy, dense, denseClone, divide, divide, divideCopy, divideCopy, elementAbsPowSum, elementMaxAbs, elementPowSum, elementProduct, elementsEqual, ensureMutable, epsilonEquals, epsilonEquals, equalsArray, fill, get, get, get, get, get, getComponent, getComponents, getElement, getElements, getElements, getElements, getShape, getShape, getShapeClone, getSlices, getSliceViews, getTransposeCopy, hasUncountable, innerProduct, innerProduct, innerProduct, innerProduct, isBoolean, isDense, isSameShape, isSparse, join, join, multiply, multiplyCopy, multiplyCopy, mutable, negate, negateCopy, nonZeroCount, outerProduct, pow, reciprocal, reciprocalCopy, reduceSlices, reduceSlices, reorder, reorder, reshape, rotateView, scale, scaleAdd, scaleAdd, scaleCopy, set, set, set, set, set, set, setApplyOp, setElements, setElements, setInnerProduct, setMultiple, setMultiple, setSparse, setSparse, signumCopy, sliceCount, sliceValue, sparse, sparseClone, sqrt, squareCopy, sub, sub, subCopy, toSliceArray, withComponents
-
-
-
-
Method Detail
-
wrap
public static Array wrap(double[] data, int... shape)
Wraps double[] array data in an Array of the specified shape. The number of elements in the array must exactly match the length of the array.- Parameters:
data-shape-- Returns:
-
wrap
public static Array wrap(Vector v)
Wraps the underlying data from a dense Vector as an array with the same shape- Parameters:
v-- Returns:
-
wrap
public static Array wrap(Matrix m)
Wraps the underlying data from a dense Matrix as an array with the same shape- Parameters:
v-- Returns:
-
newArray
public static Array newArray(int... shape)
Creates a new zero-filled mutable Array of the specified shape.- Parameters:
shape-- Returns:
-
create
public static Array create(INDArray a)
Crates a new Array with a copy of the data from the specified array, in the same shape- Parameters:
a-- Returns:
-
dimensionality
public int dimensionality()
Description copied from interface:INDArrayReturns the dimensionality of the array (i.e. the number of dimensions in the array shape) e.g. 0 for a scalar, 1 for a vector, 2 for a matrix etc.- Specified by:
dimensionalityin interfaceINDArray- Overrides:
dimensionalityin classBaseShapedArray
-
getLongShape
public long[] getLongShape()
Description copied from interface:INDArrayReturns the shape of the array as an array of longs.- Specified by:
getLongShapein interfaceINDArray- Overrides:
getLongShapein classAbstractArray<INDArray>
-
getStride
public int getStride(int dim)
Description copied from interface:IStridedArrayGets the stide for a specific dimension in this strided array.- Specified by:
getStridein interfaceIStridedArray- Returns:
-
get
public double get(int... indexes)
Description copied from interface:INDArrayReturns the double value at the specified index position in an array
-
set
public void set(int[] indexes, double value)Description copied from interface:INDArraySets the element at the given indexed position in a mutable array
-
asVector
public Vector asVector()
Description copied from interface:INDArrayConstructs a view of all elements in the array as a single vector in row-major order.- Specified by:
asVectorin interfaceINDArray- Overrides:
asVectorin classAbstractArray<INDArray>
-
toVector
public Vector toVector()
Description copied from interface:INDArrayCoerces the array into a flattened dense Vector, in row-major order. May return the same array if it is already a Vector instance.- Specified by:
toVectorin interfaceINDArray- Overrides:
toVectorin classAbstractArray<INDArray>
-
slice
public INDArray slice(int majorSlice)
Description copied from interface:INDArrayReturns the specified major slice of this array as a view (slice along dimension 0) 0 dimensional slice results are permitted, but attempting to slice a 0 dimensional array will result in an error.
-
slice
public INDArray slice(int dimension, int index)
Description copied from interface:INDArrayReturns a slice view of this array along the specified dimension
-
getTranspose
public INDArray getTranspose()
Description copied from interface:INDArrayReturns the transpose of this array. A transpose of an array is equivalent to reversing the order of dimensions. May or may not return a view depending on the array type.- Specified by:
getTransposein interfaceINDArray- Overrides:
getTransposein classAbstractArray<INDArray>
-
getTransposeView
public INDArray getTransposeView()
Description copied from interface:INDArrayReturns a transposed view of the array. May throw UnsupportedOperationException if the array type does not support this capability- Specified by:
getTransposeViewin interfaceINDArray- Overrides:
getTransposeViewin classAbstractArray<INDArray>
-
subArray
public INDArray subArray(int[] offsets, int[] shape)
Description copied from interface:INDArrayReturns a subarray view of a larger array- Specified by:
subArrayin interfaceINDArray- Overrides:
subArrayin classAbstractArray<INDArray>
-
elementCount
public long elementCount()
Description copied from interface:INDArrayReturns the total number of elements in this array. Equivalent to the product of all dimension sizes.- Specified by:
elementCountin interfaceINDArray- Overrides:
elementCountin classBaseShapedArray
-
elementSum
public double elementSum()
Description copied from interface:INDArrayReturns the sum of all elements in this array.- Specified by:
elementSumin interfaceINDArray- Overrides:
elementSumin classAbstractArray<INDArray>
-
elementMax
public double elementMax()
Description copied from interface:INDArrayReturns the maximum element value in this array. Throws an error if there are no elements.- Specified by:
elementMaxin interfaceINDArray- Overrides:
elementMaxin classAbstractArray<INDArray>
-
elementMin
public double elementMin()
Description copied from interface:INDArrayReturns the maximum element value in this array. Throws an error if there are no elements.- Specified by:
elementMinin interfaceINDArray- Overrides:
elementMinin classAbstractArray<INDArray>
-
elementSquaredSum
public double elementSquaredSum()
Description copied from interface:INDArrayReturns the sum of squared elements in this array.- Specified by:
elementSquaredSumin interfaceINDArray- Overrides:
elementSquaredSumin classAbstractArray<INDArray>
-
abs
public void abs()
Description copied from interface:INDArrayReplaces all elements of this array with their absolute values, according to Math.abs(double)- Specified by:
absin interfaceINDArray- Overrides:
absin classAbstractArray<INDArray>
-
signum
public void signum()
Description copied from interface:INDArrayCalculates the signum of all elements of this mutable array Sets each component of the array to its sign value (-1, 0 or 1)- Specified by:
signumin interfaceINDArray- Overrides:
signumin classAbstractArray<INDArray>
-
square
public void square()
Description copied from interface:INDArraySquares all elements of the array in place- Specified by:
squarein interfaceINDArray- Overrides:
squarein classAbstractArray<INDArray>
-
exp
public void exp()
Description copied from interface:INDArrayComputes the function e^x (in-place) for all array elements- Specified by:
expin interfaceINDArray- Overrides:
expin classAbstractArray<INDArray>
-
log
public void log()
Description copied from interface:INDArrayComputes the natural logarithm (in-place) for all array elements- Specified by:
login interfaceINDArray- Overrides:
login classAbstractArray<INDArray>
-
isMutable
public boolean isMutable()
Description copied from interface:INDArrayReturns true if the INDArray is mutable (at least partially)- Specified by:
isMutablein interfaceINDArray- Overrides:
isMutablein classAbstractArray<INDArray>
-
isFullyMutable
public boolean isFullyMutable()
Description copied from interface:INDArrayIf this method returns true, the INDArray is guaranteed to be fully mutable in all positions i.e. every position can store any valid double value- Specified by:
isFullyMutablein interfaceINDArray- Overrides:
isFullyMutablein classAbstractArray<INDArray>
-
isElementConstrained
public boolean isElementConstrained()
Description copied from interface:INDArrayReturns true if this array has some constraints on element values- Specified by:
isElementConstrainedin interfaceINDArray- Overrides:
isElementConstrainedin classAbstractArray<INDArray>
-
isView
public boolean isView()
Description copied from interface:INDArrayReturn true if this array is considered as a view type. This indicates (but does not guarantee): a) Data is *likely* to be shared with other arrays, so mutation of one may affect others b) It is *unlikely* to be in the most efficient general purpose format isView is intended to be used as for heuristics. It should *not* be used where the outcome might affect correctness.
-
applyOp
public void applyOp(Op op)
Description copied from interface:INDArrayApplies a unary operator to all elements of the array (in-place)- Specified by:
applyOpin interfaceINDArray- Overrides:
applyOpin classAbstractArray<INDArray>
-
reduce
public double reduce(Op2 op)
Description copied from interface:INDArrayReduces over all elements of the array in row-major order. Applies the operator to the previous result at each step. Returns the final result.- Specified by:
reducein interfaceINDArray- Overrides:
reducein classBaseShapedArray
-
reduce
public double reduce(Op2 op, double init)
Description copied from interface:INDArrayReduces over all elements of the array in row-major order. Applies the operator to the initial/previous result at each step. Returns the final result.- Specified by:
reducein interfaceINDArray- Overrides:
reducein classBaseShapedArray
-
applyOp
public void applyOp(IOperator op)
Description copied from interface:INDArrayApplies a unary operator to all elements of the array (in-place)- Specified by:
applyOpin interfaceINDArray- Overrides:
applyOpin classAbstractArray<INDArray>- Parameters:
op- The operator to apply to the array
-
equals
public boolean equals(INDArray a)
Description copied from interface:INDArrayChecks if two arrays are equal exactly in terms of both value and shape Element equality checks are consistent with compareTo- Specified by:
equalsin interfaceINDArray- Overrides:
equalsin classAbstractArray<INDArray>
-
exactClone
public Array exactClone()
Description copied from interface:INDArrayCreates a deep clone of an array, using the same class implementation as the original array- Specified by:
exactClonein interfaceINDArray- Returns:
- A clone of the original array
-
setElements
public void setElements(int pos, double[] values, int offset, int length)Description copied from interface:INDArraySets elements in an array using the given double values. The source array must contain at least the specified length in terms of number of elements- Specified by:
setElementsin interfaceINDArray- Overrides:
setElementsin classAbstractArray<INDArray>- Parameters:
pos- Offset into this array, expressed in terms of number of elements in row major ordervalues- Element values in a double[] array. There must be at least as many element values as elements in this array.offset- Position in the values array from which to start taking values.length- Number of elements to set.
-
getElements
public void getElements(double[] values, int offset)Description copied from interface:INDArrayCopies all elements of this array a double array at the specified offset- Specified by:
getElementsin interfaceINDArray- Overrides:
getElementsin classAbstractArray<INDArray>
-
elementIterator
public java.util.Iterator<java.lang.Double> elementIterator()
Description copied from interface:INDArrayReturns an iterator over all elements in this array, in row-major order- Specified by:
elementIteratorin interfaceINDArray- Overrides:
elementIteratorin classAbstractArray<INDArray>
-
multiply
public void multiply(double factor)
Description copied from interface:INDArrayMultiplies all elements of the array in place by a given double value- Specified by:
multiplyin interfaceINDArray- Overrides:
multiplyin classAbstractArray<INDArray>
-
getSlices
public java.util.List<?> getSlices()
Description copied from interface:INDArrayReturns a list of all major slices of this array. Returns a list of Double values if a 1-dimensional array is sliced, otherwise a list of INDArray instances- Specified by:
getSlicesin interfaceINDArray- Overrides:
getSlicesin classAbstractArray<INDArray>
-
toDoubleBuffer
public void toDoubleBuffer(java.nio.DoubleBuffer dest)
Description copied from interface:INDArrayCopies the elements of this INDArray to the specified double buffer.- Specified by:
toDoubleBufferin interfaceINDArray- Overrides:
toDoubleBufferin classAbstractArray<INDArray>
-
toDoubleArray
public double[] toDoubleArray()
Description copied from interface:INDArrayCopies the elements of this array to a new double[] array.- Specified by:
toDoubleArrayin interfaceINDArray- Overrides:
toDoubleArrayin classAbstractArray<INDArray>
-
asDoubleArray
public double[] asDoubleArray()
Description copied from interface:INDArrayReturns the underlying double array representing the densely packed elements of this array. Modifications to this double array will change the original array. Returns null if there is no such array.- Specified by:
asDoubleArrayin interfaceINDArray- Overrides:
asDoubleArrayin classAbstractArray<INDArray>
-
clone
public INDArray clone()
Description copied from interface:INDArrayReturns a clone of the array data, as a new array which will be fully mutable and may be of a different class to the original. Clone should attempt to return the most efficient possible array type. Clone should preserve sparsity property where possible, but this is not guaranteed.- Specified by:
clonein interfaceINDArray- Overrides:
clonein classAbstractArray<INDArray>
-
validate
public void validate()
Description copied from interface:INDArrayValidates the internal data structure of the INDArray. Throws an exception on failure. Failure indicates a serious bug and/or data corruption.- Specified by:
validatein interfaceINDArray- Overrides:
validatein classAbstractArray<INDArray>
-
createFromVector
public static Array createFromVector(AVector a, int... shape)
Creates a new matrix using the elements in the specified vector. Truncates or zero-pads the data as required to fill the new matrix- Parameters:
data-rows-columns-- Returns:
-
getArray
public double[] getArray()
Description copied from interface:IStridedArrayGets the underlying data array for this strided array Unsafe operation - may allow modification of otherwise immutable arrays- Specified by:
getArrayin interfaceIDenseArray- Specified by:
getArrayin interfaceIStridedArray
-
getArrayOffset
public int getArrayOffset()
Description copied from interface:IStridedArrayGets the data array offset for the first element of this strided array- Specified by:
getArrayOffsetin interfaceIDenseArray- Specified by:
getArrayOffsetin interfaceIStridedArray
-
getStrides
public int[] getStrides()
Description copied from interface:IStridedArrayGets the strides for each dimension of this strided array. The returned array should not be modified- Specified by:
getStridesin interfaceIStridedArray- Returns:
-
isPackedArray
public boolean isPackedArray()
Description copied from interface:IStridedArrayChecks if this array is fully packed in row major order.- Specified by:
isPackedArrayin interfaceIStridedArray- Returns:
-
isZero
public boolean isZero()
Description copied from interface:INDArrayReturns true if the array is zero (all elements equal to zero)- Specified by:
isZeroin interfaceINDArray- Overrides:
isZeroin classBaseShapedArray
-
immutable
public INDArray immutable()
Description copied from interface:INDArrayReturns an immutable version of this array. May return the same array if already immutable. Guarantees a defensive copy if the array is mutable.- Specified by:
immutablein interfaceINDArray- Overrides:
immutablein classAbstractArray<INDArray>
-
get
public double get()
Description copied from interface:INDArrayReturns the double value of a scalar array- Specified by:
getin interfaceINDArray- Specified by:
getin classAbstractArray<INDArray>
-
add
public void add(double x)
Description copied from interface:INDArrayAdds a double value to all elements in this array. Implementations may optimize the case of 0.0- Specified by:
addin interfaceINDArray- Overrides:
addin classAbstractArray<INDArray>
-
get
public double get(int x)
Description copied from interface:INDArrayReturns the double value at the specified position in a 1D vector- Specified by:
getin interfaceINDArray- Specified by:
getin classAbstractArray<INDArray>
-
get
public double get(int x, int y)Description copied from interface:INDArrayReturns the double value at the specified position in a 2D matrix- Specified by:
getin interfaceINDArray- Specified by:
getin classAbstractArray<INDArray>
-
equalsArray
public boolean equalsArray(double[] data, int offset)Description copied from interface:INDArrayReturns true if the elements in this array exactly match the elements in the given array, in row-major order- Specified by:
equalsArrayin interfaceINDArray- Overrides:
equalsArrayin classAbstractArray<INDArray>- Parameters:
data- Array of double element values to check for equalityoffset- Offset into the data array- Returns:
-
-
DMelt 3.0 © DataMelt by jWork.ORG