Java Code Examples for org.nd4j.linalg.api.ndarray.INDArray#linearView()

The following examples show how to use org.nd4j.linalg.api.ndarray.INDArray#linearView() . 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: BlasBufferUtil.java    From nd4j with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the float data
 * for this ndarray.
 * If possible (the offset is 0 representing the whole buffer)
 * it will return a direct reference to the underlying array
 * @param buf the ndarray to get the data for
 * @return the float data for this ndarray
 */
public static float[] getFloatData(INDArray buf) {
    if (buf.data().dataType() != DataBuffer.Type.FLOAT)
        throw new IllegalArgumentException("Float data must be obtained from a float buffer");

    if (buf.data().allocationMode() == DataBuffer.AllocationMode.HEAP) {
        return buf.data().asFloat();
    } else {
        float[] ret = new float[(int) buf.length()];
        INDArray linear = buf.linearView();

        for (int i = 0; i < buf.length(); i++)
            ret[i] = linear.getFloat(i);
        return ret;
    }
}
 
Example 2
Source File: BlasBufferUtil.java    From nd4j with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the double data
 * for this ndarray.
 * If possible (the offset is 0 representing the whole buffer)
 * it will return a direct reference to the underlying array
 * @param buf the ndarray to get the data for
 * @return the double data for this ndarray
 */
public static double[] getDoubleData(INDArray buf) {
    if (buf.data().dataType() != DataBuffer.Type.DOUBLE)
        throw new IllegalArgumentException("Double data must be obtained from a double buffer");

    if (buf.data().allocationMode() == DataBuffer.AllocationMode.HEAP) {
        return buf.data().asDouble();

    } else {
        double[] ret = new double[(int) buf.length()];
        INDArray linear = buf.linearView();
        for (int i = 0; i < buf.length(); i++)
            ret[i] = linear.getDouble(i);
        return ret;

    }
}
 
Example 3
Source File: NDArrayUtil.java    From nd4j with Apache License 2.0 6 votes vote down vote up
public static long[] toLongs(INDArray n) {
    if (n instanceof IComplexNDArray)
        throw new IllegalArgumentException("Unable to convert complex array");

    if (n.length() > Integer.MAX_VALUE)
        throw new ND4JIllegalStateException("Can't convert INDArray with length > Integer.MAX_VALUE");

    n = n.linearView();

    // FIXME: int cast
    long[] ret = new long[(int) n.length()];
    for (int i = 0; i < n.length(); i++)
        ret[i] = (long) n.getFloat(i);

    return ret;
}
 
Example 4
Source File: BaseComplexNDArray.java    From nd4j with Apache License 2.0 5 votes vote down vote up
/**
 *
 * @param real
 */
protected void copyFromReal(INDArray real) {
    if (!Shape.shapeEquals(shape(), real.shape()))
        throw new IllegalStateException("Unable to copy array. Not the same shape");
    INDArray linear = real.linearView();
    IComplexNDArray thisLinear = linearView();
    for (int i = 0; i < linear.length(); i++) {
        thisLinear.putScalar(i, Nd4j.createComplexNumber(linear.getDouble(i), 0.0));
    }
}
 
Example 5
Source File: BaseComplexNDArray.java    From nd4j with Apache License 2.0 5 votes vote down vote up
/**
 * Copy real numbers to arr
 * @param arr the arr to copy to
 */
protected void copyRealTo(INDArray arr) {
    INDArray linear = arr.linearView();
    IComplexNDArray thisLinear = linearView();
    if (arr.isScalar())
        arr.putScalar(0, getReal(0));
    else
        for (int i = 0; i < linear.length(); i++) {
            arr.putScalar(i, thisLinear.getReal(i));
        }

}
 
Example 6
Source File: BaseComplexNDArray.java    From nd4j with Apache License 2.0 5 votes vote down vote up
/**
 * Copy imaginary numbers to the given
 * ndarray
 * @param arr the array to copy imaginary numbers to
 */
protected void copyImagTo(INDArray arr) {
    INDArray linear = arr.linearView();
    IComplexNDArray thisLinear = linearView();
    if (arr.isScalar())
        arr.putScalar(0, getReal(0));
    else
        for (int i = 0; i < linear.length(); i++) {
            arr.putScalar(i, thisLinear.getImag(i));
        }

}
 
Example 7
Source File: BaseComplexNDArray.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Override
public INDArray getReal() {
    INDArray result = Nd4j.create(shape());
    IComplexNDArray linearView = linearView();
    INDArray linearRet = result.linearView();
    for (int i = 0; i < linearView.length(); i++) {
        linearRet.putScalar(i, linearView.getReal(i));
    }
    return result;
}
 
Example 8
Source File: BaseComplexNDArray.java    From nd4j with Apache License 2.0 5 votes vote down vote up
/**
 * in place addition of two matrices
 *
 * @param other  the second ndarray to add
 * @param result the result ndarray
 * @return the result of the addition
 */
@Override
public IComplexNDArray addi(INDArray other, INDArray result) {
    IComplexNDArray cOther = (IComplexNDArray) other;
    IComplexNDArray cResult = (IComplexNDArray) result;

    if (cOther.isScalar()) {
        return cResult.addi(cOther.getComplex(0), result);
    }
    if (isScalar()) {
        return cOther.addi(getComplex(0), result);
    }


    if (result == this) {

        Nd4j.getBlasWrapper().axpy(Nd4j.UNIT, cOther, cResult);
    } else if (result == other) {
        Nd4j.getBlasWrapper().axpy(Nd4j.UNIT, this, cResult);
    } else {
        INDArray resultLinear = result.linearView();
        INDArray otherLinear = other.linearView();
        INDArray linear = linearView();
        for (int i = 0; i < resultLinear.length(); i++) {
            resultLinear.putScalar(i, otherLinear.getDouble(i) + linear.getDouble(i));
        }

    }

    return (IComplexNDArray) result;
}
 
Example 9
Source File: BaseComplexNDArray.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Override
public IComplexNDArray addi(IComplexNumber n, INDArray result) {
    IComplexNDArray linear = linearView();
    IComplexNDArray cResult = (IComplexNDArray) result.linearView();

    for (int i = 0; i < length(); i++) {
        cResult.putScalar(i, linear.getComplex(i).add(n));
    }

    return (IComplexNDArray) result;
}
 
Example 10
Source File: LinAlgExceptions.java    From nd4j with Apache License 2.0 5 votes vote down vote up
public static void assertValidNum(INDArray n) {
    INDArray linear = n.linearView();
    for (int i = 0; i < linear.length(); i++) {
        double d = linear.getDouble(i);
        if (Double.isNaN(d) || Double.isInfinite(d))
            throw new IllegalStateException("Found infinite or nan");

    }
}
 
Example 11
Source File: NDArrayUtil.java    From nd4j with Apache License 2.0 5 votes vote down vote up
public static int[] toInts(INDArray n) {
    if (n instanceof IComplexNDArray)
        throw new IllegalArgumentException("Unable to convert complex array");

    if (n.length() > Integer.MAX_VALUE)
        throw new ND4JIllegalStateException("Can't convert INDArray with length > Integer.MAX_VALUE");

    n = n.linearView();
    int[] ret = new int[(int) n.length()];
    for (int i = 0; i < n.length(); i++)
        ret[i] = (int) n.getFloat(i);
    return ret;
}