mikera.vectorz
Class Vector
- java.lang.Object
-
- All Implemented Interfaces:
- java.io.Serializable, java.lang.Cloneable, java.lang.Comparable<INDArray>, java.lang.Iterable<java.lang.Double>, IDense, IDenseArray, IStridedArray, INDArray, IVector
public final class Vector extends ADenseArrayVector
General purpose vector of arbitrary length, backed by an densely packed double[] array. This is the most efficient type for general purpose dense 1D vectors.- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor and Description Vector(AVector source)Copy constructor from an arbitrary vector
-
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(ADenseArrayVector src, int srcOffset)voidadd(AVector v)Adds another vector to this onevoidadd(double constant)Adds a double value to all elements in this array.voidadd(double[] srcData, int srcOffset)Adds to this vector from a given double array.voidaddAt(int i, double v)Adds a value to a specific element of the vector This function does not perform bounds checking, i.e.voidaddMultiple(ADenseArrayVector v, double factor)voidaddMultiple(AVector v, double factor)Adds a scaled multiple of another vector to this vectorvoidaddMultiple(Vector source, Index index, double factor)Adds source vector to this vector at the specified indexes which should map from source->thisvoidaddProduct(AVector a, AVector b)Adds the element-wise product of two vectors to this vector.voidaddProduct(Vector a, Vector b)voidaddProduct(Vector a, Vector b, double factor)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.voidclamp(double min, double max)Clamps all values in the vector to a given rangeVectorclone()Clones the vector, creating a new mutable copy of all data.static Vectorcreate(AIndex a)static Vectorcreate(java.util.ArrayList<java.lang.Number> al)static Vectorcreate(AVector a)static Vectorcreate(double[] data)Creates a new Vector from the given double[] data.static Vectorcreate(double[] data, int start, int length)Creates a new Vector from the given double[] data.static Vectorcreate(INDArray a)static Vectorcreate(java.lang.Iterable<java.lang.Number> iterable)Creates a Vector from the specified Iterable objectstatic Vectorcreate(java.util.Iterator<java.lang.Number> iterator)Creates a Vector from the specified Iterator objectstatic Vectorcreate(java.util.List<java.lang.Number> al)static Vectorcreate(java.lang.Object o)static VectorcreateFromVector(AVector source, int length)Creates a new vector using the elements in the specified vector.static VectorcreateLength(int length)Create an empty (zero-filled) vector of a specified lengthVectordense()Coerces this INDArray to a dense format, without changing its element values.doubledistance(AVector v)Returns the Euclidean distance to another vector.doubledistanceSquared(Vector v)voiddivide(AVector v)voiddivide(double factor)Divides all elements by a given factorvoiddivide(Vector v)Performance Override for dividedoubledotProduct(AVector v, Index ix)Returns the dotProduct of this vector with the elements of another vector mapped to specified indexes in this vector.doubledotProduct(double[] data, int offset)Fast dot product with a double[] array.doubledotProduct(Vector v, Index ix)doubleelementMax()Returns the maximum element value in this array.doubleelementMin()Returns the maximum element value in this array.doubleelementSum()Returns the sum of all elements in a vectorVectorensureMutable()Ensures the array is a fully mutable representation.booleanequals(AVector v)Returns true if this array is exactly equal to a vector.booleanequalsArray(double[] arr, int offset)Returns true if this vector exactly matches the elements in double[] array, starting from the specified offsetVectorexactClone()Returns an exact clone of this vector, i.e.voidfill(double value)Fills the entire vector with a given valuedoubleget(int i)Returns the double value at the specified position in a 1D vectorintgetArrayOffset()Gets the offset into the underlying double[] data array for the first element of this vectorvoidgetElements(double[] dest, int destOffset)Copies all elements of this array a double array at the specified offsetvoidgetElements(double[] data, int offset, int[] indices)Gets the elements in this vector from the specified indices, storing at the specified offset in the destination arrayinthashCode()Computes the hashcode of a vector.booleanisPackedArray()Checks if this array is fully packed in row major order.booleanisView()Returns true if this vector is of a view type that references other vectors / data.voidlogistic()Computes the logistic function for every element in this vector.voidmultiply(AVector v)Multiplies this vector by another vector, elementwise.voidmultiply(double factor)Multiplies the vector by a constant factorvoidmultiply(Vector v)VectormultiplyCopy(double factor)Returns a copy of the array with all elements multiplied by a single constant valuelongnonZeroCount()Returns the number of non-zero elements in the array.static Vectorof(double... values)Create a vector with specific component values.voidscaleAdd(double factor, double constant)Scales this vector and adds a constant to every elementvoidset(AVector a)Sets the vector to equal the value of another vectorvoidset(int i, double value)Sets a value at a given position in a mutable 1D arrayvoidsetElements(double[] values, int offset)Sets all elements in an array using the given double valuesvoidsignum()Calculates the signum of all elements of this mutable array Sets each component of the array to its sign value (-1, 0 or 1)voidsquare()Squares all elements of the array in placevoidsub(ADenseArrayVector v)voidsub(ADenseArrayVector src, int srcOffset)voidsub(AVector v)Subtracts a vector from this vectorvoidtanh()Computes the tanh of every element in this vector.voidtoDoubleBuffer(java.nio.DoubleBuffer dest)Copies the elements of this INDArray to the specified double buffer.VectortoNormal()Creates a new mutable vector representing the normalised value of this vector Returns null if the vector cannot be normalised (i.e.VectortoVector()Coerces this vector to the standard dense Vector format.doubleunsafeGet(int i)Like get, but performs no bounds checking.voidunsafeSet(int i, double value)Like set, but performs no bounds checking.static Vectorwrap(double[] source)Wraps a double array into a Vector, does *no defensive copy* so use with caution-
Methods inherited from class mikera.vectorz.impl.ADenseArrayVector
add, add, add, add, addMultiple, addMultipleToArray, addMultipleToArray, addProduct, addProduct, addProductToArray, addProductToArray, addToArray, addToArray, applyOp, copyTo, copyTo, copyTo, copyTo, divide, divideTo, dotProduct, dotProduct, elementAbsPowSum, elementPowSum, elementProduct, elementsEqual, elementSquaredSum, equals, exp, fillRange, getStride, isRangeZero, isZero, join, join, join, log, magnitude, maxAbsElement, maxAbsElementIndex, maxElementIndex, minElementIndex, multiply, multiplyTo, nonZeroIndices, pow, reciprocal, reduce, reduce, scaleAdd, selectView, set, setElements, slice, sqrt, squareCopy, subVector, toDoubleArray, tryEfficientJoin, validate, visitNonZero
-
Methods inherited from class mikera.vectorz.impl.AStridedVector
applyOpCopy, broadcast, broadcastLike, getArray, getStride, getStrides, hasUncountable, iterator
-
Methods inherited from class mikera.vectorz.impl.ASizedVector
checkIndex, checkLength, checkRange, elementCount, equalsArray, get, getShape, getShapeClone, isSameShape, length, sliceCount
-
Methods inherited from class mikera.vectorz.AVector
absCopy, absDiff, absDiff, absDiff, absDiffCopy, add, add, add, add, addAt, addCopy, addCopy, addCopy, addInnerProduct, addInnerProduct, addInnerProduct, addInnerProduct, addMultiple, addMultiple, addMultiple, addMultiple, addMultiple, addMultipleCopy, addMultipleSparse, addMultipleSparse, addPower, addPower, addProductCopy, addProductCopy, addSparse, addSparse, addToArray, addWeighted, angle, applyOp, applyOp, applyOp, asElementList, asVector, broadcastLike, broadcastLike, clampMax, clampMin, compareTo, compareTo, copy, copyTo, copyTo, crossProduct, crossProduct, denseClone, dimensionality, distanceL1, distanceLinf, distanceSquared, divide, divideCopy, divideCopy, divideCopy, dotProduct, elementIterator, elementMaxAbs, epsilonEquals, epsilonEquals, epsilonEquals, equals, get, get, get, get, get, getElement, getElements, getLongShape, getShape, getSlices, getTranspose, getTransposeCopy, getTransposeView, immutable, innerProduct, innerProduct, innerProduct, innerProduct, innerProduct, innerProduct, interpolate, isElementConstrained, isFullyMutable, isMutable, isSameShape, isUnitLengthVector, isUnitLengthVector, join, join, magnitudeSquared, maxElement, minElement, multiply, multiply, multiplyCopy, multiplyCopy, mutable, negate, negateCopy, nonSparseIndex, nonZeroValues, normalise, normaliseCopy, normaliseMaxAbsElement, outerProduct, outerProduct, projectToPlane, reciprocalCopy, reduceSlices, reduceSlices, reorder, reorder, reshape, rotateCopy, rotateView, rotateView, scale, scaleAdd, scaleAdd, scaleCopy, scaleToMagnitude, select, selectClone, set, set, set, set, set, set, set, set, setApplyOp, setApplyOp, setApplyOp, setElements, setInnerProduct, setInnerProduct, setInnerProduct, setMultiple, setMultiple, setMultiple, setMultiple, setSparse, setSparse, setSparse, shiftCopy, slice, sliceValue, softmax, softmaxCopy, sparse, sparseClone, sqrtCopy, sub, subArray, subAt, subCopy, subCopy, subMultiple, toList, toSliceArray, toString, toStringFull
-
Methods inherited from class mikera.arrayz.impl.AbstractArray
add, addApplyOp, addCopy, addInnerProduct, addInnerProduct, addOuterProduct, addOuterProductSparse, addSparse, broadcastCloneLike, broadcastCopyLike, componentCount, density, epsilonEquals, get, get, getComponent, getComponents, getElements, getElements, getSlices, getSliceViews, isBoolean, isDense, isSparse, scale, set, set, signumCopy, sub, sub, withComponents
-
Methods inherited from interface mikera.arrayz.impl.IDenseArray
getArray
-
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, applyOp, asElementList, asVector, broadcastCloneLike, broadcastCopyLike, broadcastLike, broadcastLike, componentCount, copy, denseClone, dimensionality, divide, divideCopy, divideCopy, elementCount, elementIterator, elementMaxAbs, epsilonEquals, epsilonEquals, equalsArray, get, get, get, get, get, get, get, get, getComponent, getComponents, getElement, getElements, getElements, getElements, getLongShape, getShape, getShape, getShapeClone, getSlices, getSlices, getSliceViews, getTranspose, getTransposeCopy, getTransposeView, immutable, innerProduct, innerProduct, innerProduct, innerProduct, isBoolean, isDense, isElementConstrained, isFullyMutable, isMutable, isSameShape, isSparse, join, join, multiply, multiplyCopy, mutable, negate, negateCopy, outerProduct, reciprocalCopy, reduceSlices, reduceSlices, reorder, reorder, reshape, rotateView, scale, scaleAdd, scaleCopy, set, set, set, set, set, set, setApplyOp, setElements, setInnerProduct, setMultiple, setMultiple, setSparse, setSparse, signumCopy, slice, sliceCount, sliceValue, sparse, sparseClone, sub, sub, subArray, subCopy, toSliceArray, withComponents
-
-
-
-
Field Detail
-
EMPTY
public static final Vector EMPTY
-
-
Constructor Detail
-
Vector
public Vector(AVector source)
Copy constructor from an arbitrary vector- Parameters:
source-
-
-
Method Detail
-
wrap
public static Vector wrap(double[] source)
Wraps a double array into a Vector, does *no defensive copy* so use with caution- Parameters:
source-- Returns:
-
create
public static Vector create(double[] data)
Creates a new Vector from the given double[] data. Takes a defensive copy.
-
create
public static Vector create(java.util.ArrayList<java.lang.Number> al)
-
create
public static Vector create(java.util.List<java.lang.Number> al)
-
create
public static Vector create(java.lang.Iterable<java.lang.Number> iterable)
Creates a Vector from the specified Iterable object- Parameters:
iterable- An Iterable containing java.lang.Number instances- Returns:
-
create
public static Vector create(java.util.Iterator<java.lang.Number> iterator)
Creates a Vector from the specified Iterator object- Parameters:
iterator- An Iterator over java.lang.Number instances- Returns:
-
create
public static Vector create(java.lang.Object o)
-
create
public static Vector create(double[] data, int start, int length)
Creates a new Vector from the given double[] data. Takes a defensive copy.
-
createFromVector
public static Vector createFromVector(AVector source, int length)
Creates a new vector using the elements in the specified vector. Truncates or zero-pads the data as required to fill the new vector- Parameters:
data-- Returns:
-
of
public static Vector of(double... values)
Create a vector with specific component values. Creates a copy of the value array- Parameters:
values-- Returns:
-
createLength
public static Vector createLength(int length)
Create an empty (zero-filled) vector of a specified length- Parameters:
length-- Returns:
-
get
public double get(int i)
Description copied from interface:INDArrayReturns the double value at the specified position in a 1D vector
-
unsafeGet
public double unsafeGet(int i)
Description copied from class:AVectorLike get, but performs no bounds checking. Results are undefined if the index is out of range- Specified by:
unsafeGetin classADenseArrayVector
-
set
public void set(int i, double value)Description copied from interface:INDArraySets a value at a given position in a mutable 1D array
-
unsafeSet
public void unsafeSet(int i, double value)Description copied from class:AVectorLike set, but performs no bounds checking. Results are undefined if the index is out of range- Specified by:
unsafeSetin classADenseArrayVector
-
set
public void set(AVector a)
Description copied from class:AVectorSets the vector to equal the value of another vector- Overrides:
setin classADenseArrayVector
-
getElements
public void getElements(double[] data, int offset, int[] indices)Description copied from class:AVectorGets the elements in this vector from the specified indices, storing at the specified offset in the destination array- Overrides:
getElementsin classAStridedVector
-
getArrayOffset
public int getArrayOffset()
Description copied from class:AStridedVectorGets the offset into the underlying double[] data array for the first element of this vector- Specified by:
getArrayOffsetin interfaceIDenseArray- Specified by:
getArrayOffsetin interfaceIStridedArray- Specified by:
getArrayOffsetin classAStridedVector- Returns:
-
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- Specified by:
applyOpin classAStridedVector
-
fill
public void fill(double value)
Description copied from class:AVectorFills the entire vector with a given value- Specified by:
fillin interfaceINDArray- Overrides:
fillin classADenseArrayVector
-
clamp
public void clamp(double min, double max)Description copied from class:AVectorClamps all values in the vector to a given range- Specified by:
clampin interfaceINDArray- Overrides:
clampin classADenseArrayVector
-
square
public void square()
Description copied from interface:INDArraySquares all elements of the array in place- Specified by:
squarein interfaceINDArray- Overrides:
squarein classADenseArrayVector
-
tanh
public void tanh()
Description copied from class:AVectorComputes the tanh of every element in this vector. Mutates this vector.
-
logistic
public void logistic()
Description copied from class:AVectorComputes the logistic function for every element in this vector. Mutates this vector.
-
elementSum
public double elementSum()
Description copied from class:AVectorReturns the sum of all elements in a vector- Specified by:
elementSumin interfaceINDArray- Overrides:
elementSumin classADenseArrayVector
-
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 classADenseArrayVector
-
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 classADenseArrayVector
-
nonZeroCount
public long nonZeroCount()
Description copied from interface:INDArrayReturns the number of non-zero elements in the array.- Specified by:
nonZeroCountin interfaceINDArray- Overrides:
nonZeroCountin classADenseArrayVector
-
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 classADenseArrayVector
-
abs
public void abs()
Description copied from interface:INDArrayReplaces all elements of this array with their absolute values, according to Math.abs(double)
-
add
public void add(ADenseArrayVector src, int srcOffset)
- Overrides:
addin classADenseArrayVector
-
addMultiple
public void addMultiple(ADenseArrayVector v, double factor)
- Overrides:
addMultiplein classADenseArrayVector
-
add
public void add(AVector v)
Description copied from class:AVectorAdds another vector to this one- Overrides:
addin classADenseArrayVector
-
add
public void add(double[] srcData, int srcOffset)Description copied from class:AVectorAdds to this vector from a given double array.- Overrides:
addin classADenseArrayVector
-
scaleAdd
public void scaleAdd(double factor, double constant)Description copied from class:AVectorScales this vector and adds a constant to every element- Specified by:
scaleAddin interfaceINDArray- Overrides:
scaleAddin classADenseArrayVector
-
add
public void add(double constant)
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 classADenseArrayVector
-
addProduct
public void addProduct(AVector a, AVector b)
Description copied from class:AVectorAdds the element-wise product of two vectors to this vector. Mutates this vector.- Overrides:
addProductin classADenseArrayVector
-
addAt
public void addAt(int i, double v)Description copied from class:AVectorAdds a value to a specific element of the vector This function does not perform bounds checking, i.e. is an unsafe operation- Specified by:
addAtin classAStridedVector
-
sub
public void sub(AVector v)
Description copied from class:AVectorSubtracts a vector from this vector
-
dotProduct
public double dotProduct(AVector v, Index ix)
Description copied from class:AVectorReturns the dotProduct of this vector with the elements of another vector mapped to specified indexes in this vector.- Overrides:
dotProductin classAVector- Returns:
-
dotProduct
public double dotProduct(double[] data, int offset)Description copied from class:AVectorFast dot product with a double[] array. Performs no bounds checking. Likely to be faster than most other dot product operations- Overrides:
dotProductin classADenseArrayVector
-
multiplyCopy
public Vector multiplyCopy(double factor)
Description copied from interface:INDArrayReturns a copy of the array with all elements multiplied by a single constant value- Specified by:
multiplyCopyin interfaceINDArray- Overrides:
multiplyCopyin classAVector- Parameters:
factor- A scalar factor- Returns:
- A new array, with all element values scaled by the given factor
-
distanceSquared
public double distanceSquared(Vector v)
-
distance
public double distance(AVector v)
Description copied from class:AVectorReturns the Euclidean distance to another vector.
-
sub
public void sub(ADenseArrayVector v)
-
sub
public void sub(ADenseArrayVector src, int srcOffset)
-
addMultiple
public void addMultiple(AVector v, double factor)
Description copied from class:AVectorAdds a scaled multiple of another vector to this vector- Overrides:
addMultiplein classADenseArrayVector
-
addMultiple
public void addMultiple(Vector source, Index index, double factor)
Description copied from class:AVectorAdds source vector to this vector at the specified indexes which should map from source->this- Overrides:
addMultiplein classAVector
-
multiply
public void multiply(double factor)
Description copied from class:AVectorMultiplies the vector by a constant factor- Specified by:
multiplyin interfaceINDArray- Overrides:
multiplyin classADenseArrayVector- Parameters:
factor- Factor by which to multiply each component of the vector
-
multiply
public void multiply(AVector v)
Description copied from class:AVectorMultiplies this vector by another vector, elementwise.- Overrides:
multiplyin classADenseArrayVector
-
multiply
public void multiply(Vector v)
-
divide
public void divide(AVector v)
- Overrides:
dividein classADenseArrayVector
-
divide
public void divide(Vector v)
Performance Override for divide
-
divide
public void divide(double factor)
Description copied from interface:INDArrayDivides all elements by a given factor
-
isView
public boolean isView()
Description copied from class:AVectorReturns true if this vector is of a view type that references other vectors / data.- Specified by:
isViewin interfaceINDArray- Overrides:
isViewin classADenseArrayVector
-
clone
public Vector clone()
Description copied from class:AVectorClones the vector, creating a new mutable copy of all data. The clone is: - not guaranteed to be of the same type. - guaranteed to be fully mutable - guaranteed not to contain a reference (i.e. is a full deep copy)
-
hashCode
public int hashCode()
Description copied from class:AVectorComputes the hashcode of a vector. Currently defined to be equal to List.hashCode for a equivalent list of Double values, this may change in future versions.
-
ensureMutable
public Vector ensureMutable()
Description copied from interface:INDArrayEnsures the array is a fully mutable representation. Returns either the same array or a new clone.- Specified by:
ensureMutablein interfaceINDArray- Overrides:
ensureMutablein classAStridedVector
-
exactClone
public Vector exactClone()
Description copied from class:AVectorReturns an exact clone of this vector, i.e. of the same type- Specified by:
exactClonein interfaceINDArray- Specified by:
exactClonein classAVector- Returns:
- A clone of the original array
-
isPackedArray
public boolean isPackedArray()
Description copied from interface:IStridedArrayChecks if this array is fully packed in row major order.- Specified by:
isPackedArrayin interfaceIStridedArray- Overrides:
isPackedArrayin classADenseArrayVector- Returns:
-
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 classADenseArrayVector
-
toNormal
public Vector toNormal()
Description copied from class:AVectorCreates a new mutable vector representing the normalised value of this vector Returns null if the vector cannot be normalised (i.e. is a zero length vector)
-
toVector
public Vector toVector()
Description copied from class:AVectorCoerces this vector to the standard dense Vector format. May return the same vector is this is already a dense Vector, otherwise returns a Vector containing a new clone of the vector elements.
-
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 classAStridedVector
-
dense
public Vector dense()
Description copied from interface:INDArrayCoerces this INDArray to a dense format, without changing its element values. May return the same INDArray if already dense. May also mutate the internal structure of the original NDArray, or create a view over parts of the original INDArray. You should take a defensive copy of the original NDArray if any of this concerns you. The returned dense array may not be fully mutable in all elements.- Specified by:
densein interfaceINDArray- Overrides:
densein classADenseArrayVector
-
equals
public boolean equals(AVector v)
Description copied from class:AbstractArrayReturns true if this array is exactly equal to a vector.- Overrides:
equalsin classADenseArrayVector- Returns:
-
equalsArray
public boolean equalsArray(double[] arr, int offset)Description copied from class:AVectorReturns true if this vector exactly matches the elements in double[] array, starting from the specified offset- Specified by:
equalsArrayin interfaceINDArray- Overrides:
equalsArrayin classADenseArrayVector- Parameters:
arr- Array of double element values to check for equalityoffset- Offset into the data array- Returns:
-
setElements
public void setElements(double[] values, int offset)Description copied from interface:INDArraySets all elements in an array using the given double values- Specified by:
setElementsin interfaceINDArray- Specified by:
setElementsin classAStridedVector- Parameters:
values- 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.
-
getElements
public void getElements(double[] dest, int destOffset)Description copied from interface:INDArrayCopies all elements of this array a double array at the specified offset- Specified by:
getElementsin interfaceINDArray- Overrides:
getElementsin classADenseArrayVector
-
-
DMelt 3.0 © DataMelt by jWork.ORG