Search in sources :

Example 36 with IComplexNDArray

use of org.nd4j.linalg.api.complex.IComplexNDArray in project nd4j by deeplearning4j.

the class ComplexNDArrayUtil method padWithZeros.

/**
 * Pads an ndarray with zeros
 *
 * @param nd          the ndarray to pad
 * @param targetShape the the new shape
 * @return the padded ndarray
 */
public static IComplexNDArray padWithZeros(IComplexNDArray nd, int[] targetShape) {
    if (Arrays.equals(nd.shape(), targetShape))
        return nd;
    // no padding required
    if (ArrayUtil.prod(nd.shape()) >= ArrayUtil.prod(targetShape))
        return nd;
    IComplexNDArray ret = Nd4j.createComplex(targetShape);
    INDArrayIndex[] targetShapeIndex = NDArrayIndex.createCoveringShape(nd.shape());
    ret.put(targetShapeIndex, nd);
    return ret;
}
Also used : INDArrayIndex(org.nd4j.linalg.indexing.INDArrayIndex) IComplexNDArray(org.nd4j.linalg.api.complex.IComplexNDArray)

Example 37 with IComplexNDArray

use of org.nd4j.linalg.api.complex.IComplexNDArray in project nd4j by deeplearning4j.

the class ComplexNDArrayUtil method expi.

/**
 * Returns the exponential of a complex ndarray
 *
 * @param toExp the ndarray to convert
 * @return the exponential of the specified
 * ndarray
 */
public static IComplexNDArray expi(IComplexNDArray toExp) {
    IComplexNDArray flattened = toExp.ravel();
    for (int i = 0; i < flattened.length(); i++) {
        IComplexNumber n = flattened.getComplex(i);
        flattened.put(i, Nd4j.scalar(ComplexUtil.exp(n)));
    }
    return flattened.reshape(toExp.shape());
}
Also used : IComplexNumber(org.nd4j.linalg.api.complex.IComplexNumber) IComplexNDArray(org.nd4j.linalg.api.complex.IComplexNDArray)

Example 38 with IComplexNDArray

use of org.nd4j.linalg.api.complex.IComplexNDArray in project nd4j by deeplearning4j.

the class Shape method newShapeNoCopy.

/**
 * A port of numpy's reshaping algorithm that leverages
 * no copy where possible and returns
 * null if the reshape
 * couldn't happen without copying
 * @param arr  the array to reshape
 * @param newShape the new shape
 * @param isFOrder whether the array will be fortran ordered or not
 * @return null if a reshape isn't possible, or a new ndarray
 */
public static INDArray newShapeNoCopy(INDArray arr, int[] newShape, boolean isFOrder) {
    int oldnd;
    int[] olddims = ArrayUtil.copy(arr.shape());
    int[] oldstrides = ArrayUtil.copy(arr.stride());
    int np, op, last_stride;
    int oi, oj, ok, ni, nj, nk;
    int[] newStrides = new int[newShape.length];
    oldnd = 0;
    /*
         * Remove axes with dimension 1 from the old array. They have no effect
         * but would need special cases since their strides do not matter.
         */
    for (oi = 0; oi < arr.rank(); oi++) {
        if (arr.size(oi) != 1) {
            olddims[oldnd] = arr.size(oi);
            oldstrides[oldnd] = arr.stride(oi);
            oldnd++;
        }
    }
    np = 1;
    for (ni = 0; ni < newShape.length; ni++) {
        np *= newShape[ni];
    }
    op = 1;
    for (oi = 0; oi < oldnd; oi++) {
        op *= olddims[oi];
    }
    if (np != op) {
        /* different total sizes; no hope */
        return null;
    }
    if (np == 0) {
        /* the current code does not handle 0-sized arrays, so give up */
        return null;
    }
    /* oi to oj and ni to nj give the axis ranges currently worked with */
    oi = 0;
    oj = 1;
    ni = 0;
    nj = 1;
    while (ni < newShape.length && oi < oldnd) {
        np = newShape[ni];
        op = olddims[oi];
        while (np != op) {
            if (np < op) {
                /* Misses trailing 1s, these are handled later */
                np *= newShape[nj++];
            } else {
                op *= olddims[oj++];
            }
        }
        /* Check whether the original axes can be combined */
        for (ok = oi; ok < oj - 1; ok++) {
            if (isFOrder) {
                if (oldstrides[ok + 1] != olddims[ok] * oldstrides[ok]) {
                    /* not contiguous enough */
                    return null;
                }
            } else {
                /* C order */
                if (oldstrides[ok] != olddims[ok + 1] * oldstrides[ok + 1]) {
                    /* not contiguous enough */
                    return null;
                }
            }
        }
        /* Calculate new strides for all axes currently worked with */
        if (isFOrder) {
            newStrides[ni] = oldstrides[oi];
            for (nk = ni + 1; nk < nj; nk++) {
                newStrides[nk] = newStrides[nk - 1] * newShape[nk - 1];
            }
        } else {
            /* C order */
            newStrides[nj - 1] = oldstrides[oj - 1];
            for (nk = nj - 1; nk > ni; nk--) {
                newStrides[nk - 1] = newStrides[nk] * newShape[nk];
            }
        }
        ni = nj++;
        oi = oj++;
    }
    /*
         * Set strides corresponding to trailing 1s of the new shape.
         */
    if (ni >= 1) {
        last_stride = newStrides[ni - 1];
    } else {
        last_stride = arr.elementStride();
    }
    if (isFOrder && ni >= 1) {
        last_stride *= newShape[ni - 1];
    }
    for (nk = ni; nk < newShape.length; nk++) {
        newStrides[nk] = last_stride;
    }
    if (arr instanceof IComplexNDArray)
        return Nd4j.createComplex(arr.data(), newShape, newStrides, arr.offset());
    INDArray ret = Nd4j.create(arr.data(), newShape, newStrides, arr.offset(), isFOrder ? 'f' : 'c');
    return ret;
}
Also used : INDArray(org.nd4j.linalg.api.ndarray.INDArray) IComplexNDArray(org.nd4j.linalg.api.complex.IComplexNDArray)

Example 39 with IComplexNDArray

use of org.nd4j.linalg.api.complex.IComplexNDArray in project nd4j by deeplearning4j.

the class Shape method toOffsetZeroCopyHelper.

private static INDArray toOffsetZeroCopyHelper(final INDArray arr, char order, boolean anyOrder) {
    if (arr instanceof IComplexNDArray) {
        if (arr.isRowVector()) {
            IComplexNDArray ret = Nd4j.createComplex(arr.shape(), order);
            for (int i = 0; i < ret.length(); i++) ret.putScalar(i, ((IComplexNDArray) arr).getComplex(i));
            return ret;
        }
        IComplexNDArray ret = Nd4j.createComplex(arr.shape(), order);
        for (int i = 0; i < ret.slices(); i++) ret.putSlice(i, arr.slice(i));
        return ret;
    } else {
        // Use CopyOp:
        char outOrder = (anyOrder ? arr.ordering() : order);
        if (outOrder == 'a')
            outOrder = Nd4j.order();
        INDArray z = Nd4j.createUninitialized(arr.shape(), outOrder);
        z.assign(arr);
        return z;
    }
}
Also used : INDArray(org.nd4j.linalg.api.ndarray.INDArray) IComplexNDArray(org.nd4j.linalg.api.complex.IComplexNDArray)

Example 40 with IComplexNDArray

use of org.nd4j.linalg.api.complex.IComplexNDArray in project nd4j by deeplearning4j.

the class Shape method toOffsetZero.

/**
 * Create a copy of the matrix
 * where the new offset is zero
 *
 * @param arr the array to copy to offset 0
 * @return the same array if offset is zero
 * otherwise a copy of the array with
 * elements set to zero
 */
public static INDArray toOffsetZero(INDArray arr) {
    if (arr.offset() < 1 && arr.data().length() == arr.length() || arr instanceof IComplexNDArray && arr.length() * 2 == arr.data().length())
        if (arr.ordering() == 'f' && arr.stride(-1) != arr.elementStride() || arr.ordering() == 'c' && arr.stride(0) != arr.elementStride())
            return arr;
    if (arr.isRowVector()) {
        if (arr instanceof IComplexNDArray) {
            IComplexNDArray ret = Nd4j.createComplex(arr.shape());
            for (int i = 0; i < ret.length(); i++) ret.putScalar(i, ((IComplexNDArray) arr).getComplex(i));
            return ret;
        } else {
            INDArray ret = Nd4j.create(arr.shape());
            for (int i = 0; i < ret.length(); i++) ret.putScalar(i, arr.getDouble(i));
            return ret;
        }
    }
    if (arr instanceof IComplexNDArray) {
        IComplexNDArray ret = Nd4j.createComplex(arr.shape());
        for (int i = 0; i < ret.slices(); i++) ret.putSlice(i, arr.slice(i));
        return ret;
    } else {
        INDArray ret = Nd4j.create(arr.shape(), arr.ordering());
        ret.assign(arr);
        return ret;
    }
}
Also used : INDArray(org.nd4j.linalg.api.ndarray.INDArray) IComplexNDArray(org.nd4j.linalg.api.complex.IComplexNDArray)

Aggregations

IComplexNDArray (org.nd4j.linalg.api.complex.IComplexNDArray)74 ND4JIllegalStateException (org.nd4j.linalg.exception.ND4JIllegalStateException)6 INDArray (org.nd4j.linalg.api.ndarray.INDArray)5 IComplexNumber (org.nd4j.linalg.api.complex.IComplexNumber)3 DecimalFormat (java.text.DecimalFormat)2 INDArrayIndex (org.nd4j.linalg.indexing.INDArrayIndex)2 AllocationPoint (org.nd4j.jita.allocator.impl.AllocationPoint)1 DataBuffer (org.nd4j.linalg.api.buffer.DataBuffer)1 IComplexDouble (org.nd4j.linalg.api.complex.IComplexDouble)1