Search in sources :

Example 1 with Matrix

use of org.renjin.primitives.matrix.Matrix in project rsession by yannrichet.

the class RenjinSession method asMatrix.

@Override
public double[][] asMatrix(Object o) throws ClassCastException {
    if (o == null) {
        return null;
    }
    if (o instanceof double[][]) {
        return (double[][]) o;
    }
    if (o instanceof double[]) {
        return t(new double[][] { (double[]) o });
    }
    if (o instanceof Double) {
        return new double[][] { { (double) o } };
    }
    if (!(o instanceof SEXP)) {
        throw new IllegalArgumentException("[asMatrix] Not an SEXP object: " + o);
    }
    if (!(o instanceof DoubleVector)) {
        throw new IllegalArgumentException("[asMatrix] Not a DoubleVector object: " + o);
    }
    try {
        Matrix m = new Matrix((DoubleVector) o);
        double[][] mm = new double[m.getNumRows()][m.getNumCols()];
        for (int i = 0; i < mm.length; i++) {
            for (int j = 0; j < mm[i].length; j++) {
                mm[i][j] = m.getElementAsDouble(i, j);
            }
        }
        return mm;
    } catch (Exception ex) {
        throw new ClassCastException("[asMatrix] Cannot cast to double[][] " + o);
    }
}
Also used : Matrix(org.renjin.primitives.matrix.Matrix) DoubleVector(org.renjin.sexp.DoubleVector) SEXP(org.renjin.sexp.SEXP) ScriptException(javax.script.ScriptException) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)1 ScriptException (javax.script.ScriptException)1 Matrix (org.renjin.primitives.matrix.Matrix)1 DoubleVector (org.renjin.sexp.DoubleVector)1 SEXP (org.renjin.sexp.SEXP)1