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);
}
}
Aggregations