mikera.arrayz.NDArray Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of vectorz Show documentation
Show all versions of vectorz Show documentation
Fast double-precision vector and matrix maths library for Java, supporting N-dimensional numeric arrays.
package mikera.arrayz;
import java.nio.DoubleBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import mikera.arrayz.impl.BaseNDArray;
import mikera.arrayz.impl.IStridedArray;
import mikera.arrayz.impl.ImmutableArray;
import mikera.matrixx.Matrix;
import mikera.matrixx.Matrixx;
import mikera.vectorz.AVector;
import mikera.vectorz.IOperator;
import mikera.vectorz.Op;
import mikera.vectorz.Vector;
import mikera.vectorz.Vectorz;
import mikera.vectorz.impl.ArrayIndexScalar;
import mikera.vectorz.impl.ArraySubVector;
import mikera.vectorz.impl.SingleDoubleIterator;
import mikera.vectorz.impl.Vector0;
import mikera.vectorz.util.ErrorMessages;
import mikera.vectorz.util.IntArrays;
import mikera.vectorz.util.VectorzException;
/**
* General purpose dense strided NDArray class.
*
* Allows arbitrary strided access over a dense double[] array.
*
* @author Mike
*
*/
public final class NDArray extends BaseNDArray {
private static final long serialVersionUID = -262272579159731240L;
private NDArray(int... shape) {
super(new double[(int)IntArrays.arrayProduct(shape)],
shape.length,
0,
shape,
IntArrays.calcStrides(shape));
}
NDArray(double[] data, int offset, int[] shape, int[] stride) {
this(data,shape.length,offset,shape,stride);
}
NDArray(double[] data, int dimensions, int offset, int[] shape, int[] stride) {
super(data,shape.length,offset,shape,stride);
}
/**
* Wraps the given double[] data in an NDArray with the given shape.
* Does *not* take a defensive copy of either the data or shape array.
* @param data
* @param shape
* @return
*/
public static NDArray wrap(double[] data, int[] shape) {
int dims=shape.length;
return new NDArray(data,dims,0,shape,IntArrays.calcStrides(shape));
}
public static NDArray wrap(Vector v) {
return wrap(v.getArray(),v.getShape());
}
public static NDArray wrap(Matrix m) {
return wrap(m.data,m.getShape());
}
public static NDArray wrap(IStridedArray a) {
return new NDArray(a.getArray(),a.getArrayOffset(),a.getShape(),a.getStrides());
}
public static NDArray wrap(INDArray a) {
if (!(a instanceof IStridedArray)) throw new IllegalArgumentException(a.getClass()+" is not a strided array!");
return wrap((IStridedArray)a);
}
/**
* Creates a new zero-filled NDArray with the given shape.
* @param shape
* @return
*/
public static NDArray newArray(int... shape) {
return new NDArray(shape.clone());
}
@Override
public void set(double value) {
if (dimensions==0) {
data[offset]=value;
} else if (dimensions==1) {
int n=sliceCount();
int st=getStride(0);
for (int i=0; i getSlices() {
if (dimensions==0) {
throw new IllegalArgumentException(ErrorMessages.noSlices(this));
} else {
int n=getShape(0);
ArrayList al=new ArrayList(n);
for (int i=0; i elementIterator() {
if (dimensionality()==0) {
return new SingleDoubleIterator(data[offset]);
} else {
return super.elementIterator();
}
}
@Override public void validate() {
if (dimensions>shape.length) throw new VectorzException("Insufficient shape data");
if (dimensions>stride.length) throw new VectorzException("Insufficient stride data");
if ((offset<0)||(offset>=data.length)) throw new VectorzException("Offset out of bounds");
int[] endIndex=IntArrays.decrementAll(shape);
int endOffset=offset+IntArrays.dotProduct(endIndex, stride);
if ((endOffset<0)||(endOffset>data.length)) throw new VectorzException("End offset out of bounds");
super.validate();
}
@Override
public INDArray immutable() {
return ImmutableArray.create(this);
}
@Override
public double[] getArray() {
return data;
}
public static INDArray wrapStrided(double[] data, int offset,
int[] shape, int[] strides) {
return new NDArray(data,offset,shape,strides);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy