Example usage for java.lang.reflect Array setDouble

List of usage examples for java.lang.reflect Array setDouble

Introduction

In this page you can find the example usage for java.lang.reflect Array setDouble.

Prototype

public static native void setDouble(Object array, int index, double d)
        throws IllegalArgumentException, ArrayIndexOutOfBoundsException;

Source Link

Document

Sets the value of the indexed component of the specified array object to the specified double value.

Usage

From source file:org.kchine.r.server.DirectJNI.java

public static Object getJavaArrayFromRArray__(RArray array) {
    int[] dim = array.getDim();

    RVector vector = array.getValue();/* w w  w  .j ava 2  s .  c  o m*/
    Class<?> componentType = null;
    if (vector instanceof RInteger)
        componentType = int.class;
    else if (vector instanceof RNumeric)
        componentType = double.class;
    else if (vector instanceof RChar)
        componentType = String.class;
    else if (vector instanceof RLogical)
        componentType = boolean.class;

    Object result = null;
    try {
        result = Array.newInstance(componentType, dim);
    } catch (Exception e) {
        e.printStackTrace();
    }
    Vector<Integer> v = new Vector<Integer>();
    int p = 1;
    for (int i = dim.length - 1; i > 0; --i) {
        p = p * dim[i];
        v.add(0, p);
    }

    for (int bi = 0; bi < p * dim[0]; ++bi) {
        int bindex = bi;
        int[] indexes = new int[dim.length];
        for (int i = 0; i < indexes.length - 1; ++i) {
            indexes[i] = bindex / v.elementAt(i);
            bindex = bindex % v.elementAt(i);
        }
        indexes[indexes.length - 1] = bindex;

        Object arrayTail = null;
        if (dim.length == 1) {
            arrayTail = result;
        } else {
            arrayTail = Array.get(result, indexes[0]);
            for (int i = 1; i < indexes.length - 1; ++i)
                arrayTail = Array.get(arrayTail, indexes[i]);
        }
        if (vector instanceof RInteger)
            Array.setInt(arrayTail, indexes[indexes.length - 1], ((RInteger) vector).getValue()[bi]);
        else if (vector instanceof RNumeric)
            Array.setDouble(arrayTail, indexes[indexes.length - 1], ((RNumeric) vector).getValue()[bi]);
        else if (vector instanceof RChar)
            Array.set(arrayTail, indexes[indexes.length - 1], ((RChar) vector).getValue()[bi]);
        else if (vector instanceof RLogical)
            Array.setBoolean(arrayTail, indexes[indexes.length - 1], ((RLogical) vector).getValue()[bi]);
    }

    return result;
}

From source file:org.kchine.r.server.DirectJNI.java

public static Object getJavaArrayFromRArray(RArray array) {
    int[] dim = array.getDim();

    RVector vector = array.getValue();/* ww w. j a v a  2  s .  c o m*/
    Class<?> componentType = null;
    if (vector instanceof RInteger)
        componentType = int.class;
    else if (vector instanceof RNumeric)
        componentType = double.class;
    else if (vector instanceof RChar)
        componentType = String.class;
    else if (vector instanceof RLogical)
        componentType = boolean.class;

    Object result = null;
    try {
        result = Array.newInstance(componentType, dim);
    } catch (Exception e) {
        e.printStackTrace();
    }

    Vector<Integer> v1 = new Vector<Integer>();
    int p1 = 1;
    for (int i = dim.length - 1; i > 0; --i) {
        p1 = p1 * dim[i];
        v1.add(0, p1);
    }
    Vector<Integer> v2 = new Vector<Integer>();
    int p2 = 1;
    for (int i = 0; i < dim.length - 1; ++i) {
        p2 = p2 * dim[i];
        v2.add(0, p2);
    }

    for (int bi = 0; bi < p1 * dim[0]; ++bi) {
        int bindex = bi;
        int[] indexes = new int[dim.length];
        for (int i = 0; i < indexes.length - 1; ++i) {
            indexes[i] = bindex / v1.elementAt(i);
            bindex = bindex % v1.elementAt(i);
        }
        indexes[indexes.length - 1] = bindex;

        Object arrayTail = null;
        if (dim.length == 1) {
            arrayTail = result;
        } else {
            arrayTail = Array.get(result, indexes[0]);
            for (int i = 1; i < indexes.length - 1; ++i)
                arrayTail = Array.get(arrayTail, indexes[i]);
        }

        int linearVectorIndex = 0;
        for (int i = (indexes.length - 1); i > 0; --i)
            linearVectorIndex += indexes[i] * v2.elementAt((indexes.length - 1) - i);
        linearVectorIndex += indexes[0];
        // System.out.println("linearVectorIndex:"+linearVectorIndex);

        if (vector instanceof RInteger)
            Array.setInt(arrayTail, indexes[indexes.length - 1],
                    ((RInteger) vector).getValue()[linearVectorIndex]);
        else if (vector instanceof RNumeric)
            Array.setDouble(arrayTail, indexes[indexes.length - 1],
                    ((RNumeric) vector).getValue()[linearVectorIndex]);
        else if (vector instanceof RChar)
            Array.set(arrayTail, indexes[indexes.length - 1], ((RChar) vector).getValue()[linearVectorIndex]);
        else if (vector instanceof RLogical)
            Array.setBoolean(arrayTail, indexes[indexes.length - 1],
                    ((RLogical) vector).getValue()[linearVectorIndex]);
    }

    return result;
}