package mikera.arrayz.impl;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import mikera.arrayz.Arrayz;
import mikera.arrayz.INDArray;
import mikera.arrayz.ISparse;
import mikera.matrixx.Matrixx;
import mikera.matrixx.impl.ZeroMatrix;
import mikera.vectorz.AVector;
import mikera.vectorz.Vectorz;
import mikera.vectorz.impl.ImmutableScalar;
import mikera.vectorz.impl.ZeroVector;
import mikera.vectorz.util.DoubleArrays;
import mikera.vectorz.util.ErrorMessages;
import mikera.vectorz.util.IntArrays;
/**
* Class representing an immutable array filled entirely with zeros.
*
* @author Mike
*
*/
public final class ZeroArray extends BaseShapedArray implements ISparse {
private static final long serialVersionUID = 7355257027343666183L;
// we cache an instance of a slice, for performance and to save memory
private INDArray sliceValue;
private ZeroArray(int[] shape) {
super(shape);
int dims=this.dimensionality();
switch(dims) {
case 1: sliceValue= ImmutableScalar.ZERO; break;
case 2: sliceValue= ZeroVector.create(shape[1]); break;
case 3: sliceValue= ZeroMatrix.create(shape[1],shape[2]); break;
default: sliceValue= ZeroArray.wrap(IntArrays.removeIndex(shape, 0)); break;
}
}
public static ZeroArray wrap(int... shape) {
return new ZeroArray(shape);
}
/**
* Returns a zero array with specified shape. Takes a defensive clone of the shape array.
* @param shape
* @return
*/
public static ZeroArray create(int... shape) {
return new ZeroArray(shape.clone());
}
@Override
public long nonZeroCount() {
return 0;
}
@Override
public double get(int... indexes) {
if (!IntArrays.validIndex(indexes,shape)) throw new IndexOutOfBoundsException(ErrorMessages.invalidIndex(this, indexes));
return 0.0;
}
@Override
public double get() {
if (shape.length!=0) throw new IllegalArgumentException(ErrorMessages.invalidIndex(this));
return 0.0;
}
@Override
public double get(int x) {
if (shape.length!=1) throw new IllegalArgumentException(ErrorMessages.invalidIndex(this));
if ((x<0)||(x>=shape[0])) throw new IndexOutOfBoundsException(ErrorMessages.invalidIndex(this, x));
return 0.0;
}
@Override
public double get(int x, int y) {
if (shape.length!=2) throw new IllegalArgumentException(ErrorMessages.invalidIndex(this));
if ((x<0)||(x>=shape[0])) throw new IndexOutOfBoundsException(ErrorMessages.invalidIndex(this, x,y));
if ((y<0)||(y>=shape[1])) throw new IndexOutOfBoundsException(ErrorMessages.invalidIndex(this, x,y));
return 0.0;
}
@Override
public void set(int[] indexes, double value) {
throw new UnsupportedOperationException(ErrorMessages.immutable(this));
}
@Override
public INDArray slice(int majorSlice) {
if ((majorSlice<0)||(majorSlice>=shape[0])) throw new IndexOutOfBoundsException(ErrorMessages.invalidSlice(this, majorSlice));
return sliceValue;
}
@Override
public INDArray slice(int dimension, int index) {
if (dimension==0) return slice(index);
return Arrayz.createZeroArray(IntArrays.removeIndex(shape, dimension));
}
@Override
public List getSlices() {
int sc=sliceCount();
if (sc==0) return Collections.emptyList();
ArrayList al=new ArrayList(sc);
INDArray z=slice(0);
for (int i=0; i al=new ArrayList(n);
for (int i=0; i
Ads help maintain this website.