Java Code Examples for org.nd4j.linalg.api.shape.Shape#ind2sub()

The following examples show how to use org.nd4j.linalg.api.shape.Shape#ind2sub() . 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: BaseSparseNDArrayCOO.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Override
public INDArray putScalar(long i, double value) {
    if (i < 0)
        i += rank();
    if (isScalar()) {
        if (Nd4j.getExecutioner().getProfilingMode() != OpExecutioner.ProfilingMode.DISABLED && Nd4j.getExecutioner().getProfilingMode() != OpExecutioner.ProfilingMode.SCOPE_PANIC)
            OpProfiler.getInstance().processScalarCall();

        addOrUpdate(new long[] {0, 0}, value);
        return this;
    }
    if (isRowVector()) {
        addOrUpdate(new long[] {0, i}, value);
        return this;
    } else if (isColumnVector()) {
        addOrUpdate(new long[] {i, 0}, value);
        return this;
    }
    long[] indexes = ordering() == 'c' ? Shape.ind2subC(this, i) : Shape.ind2sub(this, i);
    return putScalar(indexes, value);
}
 
Example 2
Source File: BaseSparseNDArrayCOO.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Override
public double getDouble(long i) {
    if (i >= length()) {
        throw new IllegalArgumentException("Unable to get linear index >= " + length());
    }

    if (Nd4j.getExecutioner().getProfilingMode() != OpExecutioner.ProfilingMode.DISABLED && Nd4j.getExecutioner().getProfilingMode() != OpExecutioner.ProfilingMode.SCOPE_PANIC)
        OpProfiler.getInstance().processScalarCall();

    if (i == 0)
        return data().getDouble(i);

    long[] dimensions = ordering() == 'c' ? Shape.ind2subC(this, i) : Shape.ind2sub(this, i);
    Shape.assertShapeLessThan(dimensions, shape());
    return getDouble(dimensions);
}
 
Example 3
Source File: BaseNDArray.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Override
public INDArray putScalar(long i, double value) {
    if (i < 0)
        i += rank();

    // TODO: i'm not sure that rank == 1 has fair shortcut here
    if (isScalar() || rank() == 1) {
        autoProcessScalarCall();
        data.put(i, value);
        return this;
    }

    // we cant raise rank here, if original rank is 1
    if (isRowVector() && rank() == 2) {
        return putScalar(0, i, value);
    } else if (isColumnVector() && rank() == 2) {
        return putScalar(i, 0, value);
    }
    long[] indexes = ordering() == 'c' ? Shape.ind2subC(this, i) : Shape.ind2sub(this, i);
    return putScalar(indexes, value);
}
 
Example 4
Source File: BaseNDArray.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Override
public double getDouble(long i) {
    Nd4j.getCompressor().autoDecompress(this);

    if (i >= length()) {
        throw new IllegalArgumentException("Unable to get linear index >= " + length());
    }

    autoProcessScalarCall();

    if (i == 0)
        return data().getDouble(i);

    long[] dimensions = ordering() == 'c' ? Shape.ind2subC(this, i) : Shape.ind2sub(this, i);
    Shape.assertShapeLessThan(dimensions, shape());
    return getDouble(dimensions);

}
 
Example 5
Source File: BaseNDArray.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Override
public INDArray putScalar(long i, double value) {
    Preconditions.checkArgument(dataType() != DataType.BOOL || value == 0.0 || value == 1.0, "Cannot put value %s into boolean array" +
            " - only putScalar with values 0 or 1 is allowed on boolean arrays", value);
    if (i < 0)
        i += rank();

    // TODO: i'm not sure that rank == 1 has fair shortcut here
    if (isScalar()) {
        autoProcessScalarCall();
        data.put(i, value);
        return this;
    } else if (rank() == 1) {
        data.put(i * stride(0), value);
        return this;
    }

    // we cant raise rank here, if original rank is 1
    if (isRowVector() && rank() == 2) {
        return putScalar(0, i, value);
    } else if (isColumnVector() && rank() == 2) {
        return putScalar(i, 0, value);
    }
    long[] indexes = ordering() == 'c' ? Shape.ind2subC(this, i) : Shape.ind2sub(this, i);
    return putScalar(indexes, value);
}
 
Example 6
Source File: BaseNDArray.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Override
public long getLong(long index) {
    Nd4j.getCompressor().autoDecompress(this);
    Preconditions.checkState(!isEmpty(), "Unable to get value from empty array");

    if (index >= length()) {
        throw new IllegalArgumentException("Unable to get linear index " + index + ": values is greater than length (" + length() + ")");
    }

    autoProcessScalarCall();

    if (index == 0)
        return data().getLong(index);

    long[] dimensions = ordering() == 'c' ? Shape.ind2subC(this, index) : Shape.ind2sub(this, index);
    Shape.assertShapeLessThan(dimensions, shape());
    return getLong(dimensions);
}
 
Example 7
Source File: BaseNDArray.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Override
public double getDouble(long i) {
    Nd4j.getCompressor().autoDecompress(this);
    Preconditions.checkState(!isEmpty(), "Unable to get value from empty array");

    if (i >= length()) {
        throw new IllegalArgumentException("Unable to get linear index " + i + ": values is greater than length (" + length() + ")");
    }

    autoProcessScalarCall();

    if (i == 0)
        return data().getDouble(i);

    long[] dimensions = ordering() == 'c' ? Shape.ind2subC(this, i) : Shape.ind2sub(this, i);
    Shape.assertShapeLessThan(dimensions, shape());
    return getDouble(dimensions);

}
 
Example 8
Source File: LinearIndexLookup.java    From nd4j with Apache License 2.0 5 votes vote down vote up
/**
 * Give back a sub
 * wrt the given linear index
 * @param index the index
 * @return the sub for the given index
 */
public long[] lookup(int index) {
    if (exists[index]) {
        return indexes[index];
    } else {
        exists[index] = true;
        indexes[index] = ordering == 'c' ? Shape.ind2subC(shape, index, numIndexes)
                        : Shape.ind2sub(shape, index, numIndexes);
        return indexes[index];
    }
}
 
Example 9
Source File: NdIndexIterator.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Override
public long[] next() {
    if (lookup != null)
        return lookup.lookup(i++);
    switch (order) {
        case 'c':
            return Shape.ind2subC(shape, i++);
        case 'f':
            return Shape.ind2sub(shape, i++);
        default:
            throw new IllegalArgumentException("Illegal ordering " + order);
    }

}
 
Example 10
Source File: BaseComplexNDArray.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Override
public IComplexNumber getComplex(int i) {
    if (i >= length())
        throw new IllegalArgumentException("Index " + i + " >= " + length());
    long[] dimensions = Shape.ind2sub(this, i);
    return getComplex(dimensions);
}
 
Example 11
Source File: LinearIndexLookup.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * Give back a sub
 * wrt the given linear index
 * @param index the index
 * @return the sub for the given index
 */
public long[] lookup(int index) {
    if (exists[index]) {
        return indexes[index];
    } else {
        exists[index] = true;
        indexes[index] = ordering == 'c' ? Shape.ind2subC(shape, index, numIndexes)
                        : Shape.ind2sub(shape, index, numIndexes);
        return indexes[index];
    }
}
 
Example 12
Source File: NdIndexIterator.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public long[] next() {
    if (lookup != null)
        return lookup.lookup(i++);
    switch (order) {
        case 'c':
            return Shape.ind2subC(shape, i++);
        case 'f':
            return Shape.ind2sub(shape, i++);
        default:
            throw new IllegalArgumentException("Illegal ordering " + order);
    }

}
 
Example 13
Source File: BaseComplexNDArray.java    From nd4j with Apache License 2.0 4 votes vote down vote up
@Override
public IComplexNDArray putScalar(int i, IComplexNumber value) {
    long[] dimensions = Shape.ind2sub(this, i);
    //return putScalar(dimensions, value);
    throw new ND4JComplexNumbersNotSupportedException();
}