Java Code Examples for org.nd4j.linalg.util.ArrayUtil#prodLong()

The following examples show how to use org.nd4j.linalg.util.ArrayUtil#prodLong() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NDArrayIndex.java    From nd4j with Apache License 2.0 6 votes vote down vote up
public static long offset(int[] strides, long[] offsets) {
    int ret = 0;

    if (ArrayUtil.prodLong(offsets) == 1) {
        for (int i = 0; i < offsets.length; i++) {
            ret += offsets[i] * strides[i];
        }
    } else {
        for (int i = 0; i < offsets.length; i++) {
            ret += offsets[i] * strides[i];
        }

    }

    return ret;
}
 
Example 2
Source File: DefaultRandom.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Override
public INDArray nextGaussian(char order, long[] shape) {
    long length = ArrayUtil.prodLong(shape);
    INDArray ret = Nd4j.create(shape, order);

    DataBuffer data = ret.data();
    for (long i = 0; i < length; i++) {
        data.put(i, nextGaussian());
    }

    return ret;
}
 
Example 3
Source File: DefaultRandom.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Override
public INDArray nextDouble(char order, long[] shape) {
    long length = ArrayUtil.prodLong(shape);
    INDArray ret = Nd4j.create(shape, order);

    DataBuffer data = ret.data();
    for (long i = 0; i < length; i++) {
        data.put(i, nextDouble());
    }

    return ret;
}
 
Example 4
Source File: DefaultRandom.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Override
public INDArray nextFloat(char order, long[] shape) {
    long length = ArrayUtil.prodLong(shape);
    INDArray ret = Nd4j.create(shape, order);

    DataBuffer data = ret.data();
    for (long i = 0; i < length; i++) {
        data.put(i, nextFloat());
    }

    return ret;
}
 
Example 5
Source File: Tile.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Override
public List<long[]> calculateOutputShape() {
    /**
     * This op is special case: we can't infer its shape before both inputs are available.
     * So if reps argument is full of 0.0s - we skip shape inference
     *
     * And during actual op invocation both inputs should be available due to topo sort
     */
    if (is_static_reps)
        return Nd4j.getExecutioner().calculateOutputShape(this);

    if (inputArguments().length < 2)
        return Collections.emptyList();

    val array = inputArguments()[1];

    // FIXME: int cast
    val reps = new long[(int) array.length()];

    for (int e = 0; e < reps.length; e++)
        reps[e] = (int) array.getDouble(e);

    if (ArrayUtil.prodLong(reps) == 0)
        return Collections.emptyList();
    else
        return Nd4j.getExecutioner().calculateOutputShape(this);
}
 
Example 6
Source File: BaseSparseNDArrayCOO.java    From nd4j with Apache License 2.0 5 votes vote down vote up
/**
 * Compute the sparse offsets of the view we are getting, for each dimension according to the original ndarray
 * @param offset the offset of the view
 * @return an int array containing the sparse offsets
 * */
private long[] createSparseOffsets(long offset) {

    // resolve the offsets in the view dimension
    int underlyingRank = sparseOffsets().length;
    long[] newOffsets = new long[rank()];
    List<Long> shapeList = Longs.asList(shape());
    int penultimate = rank() - 1;
    for (int i = 0; i < penultimate; i++) {
        long prod = ArrayUtil.prodLong(shapeList.subList(i + 1, rank()));
        newOffsets[i] = offset / prod;
        offset = offset - newOffsets[i] * prod;
    }
    newOffsets[rank() - 1] = offset % shape()[rank() - 1];

    // Merge the offsets with the original sparseOffsets
    long[] finalOffsets = new long[underlyingRank];
    int dimNotFixed = 0;
    for (int dim = 0; dim < underlyingRank; dim++) {
        if (flags()[dim] == 1) {
            finalOffsets[dim] = sparseOffsets()[dim];
        } else {
            finalOffsets[dim] = newOffsets[dimNotFixed] + sparseOffsets()[dim];
            dimNotFixed++;
        }
    }
    return finalOffsets;
}
 
Example 7
Source File: LinearIndexLookup.java    From nd4j with Apache License 2.0 5 votes vote down vote up
public LinearIndexLookup(long[] shape, char ordering) {
    this.shape = shape;
    this.ordering = ordering;
    numIndexes = ArrayUtil.prodLong(shape);

    // FIMXE: long!
    indexes = new long[(int) numIndexes][shape.length];
    exists = new boolean[(int) numIndexes];
}
 
Example 8
Source File: Shape.java    From nd4j with Apache License 2.0 5 votes vote down vote up
/**
 * Returns whether the given shape is a vector
 *
 * @param shape the shape to test
 * @return whether the given shape is a vector
 */
public static boolean isVector(int[] shape) {
    if (shape.length > 2 || shape.length < 1)
        return false;
    else {
        long len = ArrayUtil.prodLong(shape);
        return shape[0] == len || shape[1] == len;
    }
}
 
Example 9
Source File: Shape.java    From nd4j with Apache License 2.0 5 votes vote down vote up
public static boolean isVector(long[] shape) {
    if (shape.length > 2 || shape.length < 1)
        return false;
    else {
        long len = ArrayUtil.prodLong(shape);
        return shape[0] == len || shape[1] == len;
    }
}
 
Example 10
Source File: NDArrayCreationUtil.java    From nd4j with Apache License 2.0 5 votes vote down vote up
public static List<Pair<INDArray, String>> getAll3dTestArraysWithShape(long seed, long... shape) {
    if (shape.length != 3)
        throw new IllegalArgumentException("Shape is not length 3");

    List<Pair<INDArray, String>> list = new ArrayList<>();

    String baseMsg = "getAll3dTestArraysWithShape(" + seed + "," + Arrays.toString(shape) + ").get(";


    val len = ArrayUtil.prodLong(shape);
    //Basic 3d in C and F orders:
    Nd4j.getRandom().setSeed(seed);
    INDArray stdC = Nd4j.linspace(1, len, len).reshape('c', shape);
    INDArray stdF = Nd4j.linspace(1, len, len).reshape('f', shape);
    list.add(new Pair<>(stdC, baseMsg + "0)/Nd4j.linspace(1,len,len)(" + Arrays.toString(shape) + ",'c')"));
    list.add(new Pair<>(stdF, baseMsg + "1)/Nd4j.linspace(1,len,len(" + Arrays.toString(shape) + ",'f')"));

    //Various sub arrays:
    list.addAll(get3dSubArraysWithShape(seed, shape));

    //TAD
    list.addAll(get3dTensorAlongDimensionWithShape(seed, shape));

    //Permuted
    list.addAll(get3dPermutedWithShape(seed, shape));

    //Reshaped
    list.addAll(get3dReshapedWithShape(seed, shape));

    return list;
}
 
Example 11
Source File: CudaGridExecutioner.java    From nd4j with Apache License 2.0 4 votes vote down vote up
protected void buildZ(Accumulation op, int... dimension) {
    Arrays.sort(dimension);

    for (int i = 0; i < dimension.length; i++) {
        if (dimension[i] < 0)
            dimension[i] += op.x().rank();
    }

    //do op along all dimensions
    if (dimension.length == op.x().rank())
        dimension = new int[] {Integer.MAX_VALUE};


    long[] retShape = Shape.wholeArrayDimension(dimension) ? new long[] {1, 1}
            : ArrayUtil.removeIndex(op.x().shape(), dimension);
    //ensure vector is proper shape
    if (retShape.length == 1) {
        if (dimension[0] == 0)
            retShape = new long[] {1, retShape[0]};
        else
            retShape = new long[] {retShape[0], 1};
    } else if (retShape.length == 0) {
        retShape = new long[] {1, 1};
    }

    /*
    if(op.x().isVector() && op.x().length() == ArrayUtil.prod(retShape))
        return op.noOp();
    */

    INDArray ret = null;
    if (op.z() == null || op.z() == op.x()) {
        if (op.isComplexAccumulation()) {
            val xT = op.x().tensorssAlongDimension(dimension);
            val yT = op.y().tensorssAlongDimension(dimension);

            ret = Nd4j.create(xT, yT);
        } else {
            if (Math.abs(op.zeroDouble()) < Nd4j.EPS_THRESHOLD) {
                ret = Nd4j.zeros(retShape);
            } else {
                ret = Nd4j.valueArrayOf(retShape, op.zeroDouble());
            }
        }

        op.setZ(ret);
    } else {
        // compare length
        if (op.z().lengthLong() != ArrayUtil.prodLong(retShape))
            throw new ND4JIllegalStateException("Shape of target array for reduction [" + Arrays.toString(op.z().shape()) + "] doesn't match expected [" + Arrays.toString(retShape) + "]");

        if (op.x().data().dataType() == DataBuffer.Type.DOUBLE) {
            op.z().assign(op.zeroDouble());
        } else if (op.x().data().dataType() == DataBuffer.Type.FLOAT) {
            op.z().assign(op.zeroFloat());
        } else if (op.x().data().dataType() == DataBuffer.Type.HALF) {
            op.z().assign(op.zeroHalf());
        }

        ret = op.z();
    }
}
 
Example 12
Source File: DifferentialFunctionFactory.java    From nd4j with Apache License 2.0 4 votes vote down vote up
/**
 * @param func
 * @return
 */
public long getInputLength(SDVariable func) {
    validateDifferentialFunctionsameDiff(func);
    long[] inputShape = func.arg().getShape();
    return ArrayUtil.prodLong(inputShape);
}
 
Example 13
Source File: BaseNDArray.java    From nd4j with Apache License 2.0 4 votes vote down vote up
@Override
public INDArray reshape(char order, long... newShape) {
    Nd4j.getCompressor().autoDecompress(this);

    if (newShape == null || newShape.length < 1)
        throw new ND4JIllegalStateException(
                "Can't reshape(int...) without shape arguments. Got empty shape instead.");

    // TODO: maybe toFlatten() makes more sense here?
    // reshape(-1) special case
    if (newShape.length == 1 && newShape[0] == -1)
        newShape[0] = this.length();

    int numberNegativesOnes = 0;
    long[] shape = ArrayUtil.copy(newShape);


    for (int i = 0; i < shape.length; i++) {
        if (shape[i] < 0) {
            if (numberNegativesOnes >= 1)
                throw new IllegalArgumentException("Only one dimension can be negative ones. Got shape "
                        + Arrays.toString(newShape));

            numberNegativesOnes++;

            int shapeLength = 1;
            for (int j = 0; j < shape.length; j++)
                if (shape[j] >= 1)
                    shapeLength *= shape[j];
            long realShape = Math.abs(length() / shapeLength);
            long[] thisNewShape = new long[shape.length];
            for (int j = 0; j < shape.length; j++) {
                if (i != j) {
                    thisNewShape[j] = shape[j];
                } else
                    thisNewShape[j] = realShape;
            }

            shape = thisNewShape;
            break;

        }
    }

    long prod = ArrayUtil.prodLong(shape);

    if (prod != this.lengthLong()){
        throw new ND4JIllegalStateException("New shape length doesn't match original length: [" + prod + "] vs [" + this.lengthLong() + "]. Original shape: "+Arrays.toString(this.shape())+" New Shape: "+Arrays.toString(newShape));
    }





    INDArray reshapeAttempt = Shape.newShapeNoCopy(this, shape, order == 'f');
    if (reshapeAttempt != null) {
        // kinda strange get/set usage
        //  reshapeAttempt.setOrder(Shape.getOrder(reshapeAttempt));
        return reshapeAttempt;
    }


    INDArray ret = Nd4j.createUninitialized(shape, order);
    if (order != ordering()) {
        ret.setData(dup(order).data());
    } else
        ret.assign(this);
    return ret;
}
 
Example 14
Source File: Shape.java    From nd4j with Apache License 2.0 4 votes vote down vote up
public static boolean shapeIsScalar(long[] shape) {
    return shape.length == 0 || ArrayUtil.prodLong(shape) == 1;
}
 
Example 15
Source File: Shape.java    From nd4j with Apache License 2.0 2 votes vote down vote up
/**
 * Returns true if this shape is scalar
 * @param shape the shape that is scalar
 * @return
 */
public static boolean shapeIsScalar(int[] shape) {
    return shape.length == 0 || ArrayUtil.prodLong(shape) == 1;
}