// an attempt for performance improvement: but does not seem faster for now public static RInt expandXVectorCacheFriendly(IntImpl xarg, int xsize, int count) { int nsize = xsize * count; int[] x = xarg.getContent(); int[] res = new int[nsize]; int offset = 0; int rep = 0; int lastOffset = 0; if (rep < count) { System.arraycopy(x, 0, res, offset, xsize); lastOffset = offset; offset += xsize; } for (rep = 1; rep < count; rep++) { System.arraycopy(res, lastOffset, res, offset, xsize); lastOffset = offset; offset += xsize; } return RInt.RIntFactory.getFor(res); }
public static RInt expandXVector(IntImpl xarg, int xsize, int count) { int nsize = xsize * count; int[] x = xarg.getContent(); int[] res = new int[nsize]; int offset = 0; for (int rep = 0; rep < count; rep++) { System.arraycopy(x, 0, res, offset, xsize); offset += xsize; } return RInt.RIntFactory.getFor(res); }
public static RDouble expandXVector(DoubleImpl xarg, int xsize, int count) { int nsize = xsize * count; double[] x = xarg.getContent(); double[] res = new double[nsize]; int offset = 0; for (int rep = 0; rep < count; rep++) { System.arraycopy(x, 0, res, offset, xsize); offset += xsize; } return RDouble.RDoubleFactory.getFor(res); }
public RAny outer(Frame frame, RAny xarg, RAny yarg, RAny farg) { // LICENSE: transcribed code from GNU R, which is licensed under GPL if (!(xarg instanceof RArray && yarg instanceof RArray)) { Utils.nyi("unsupported type"); return null; } RArray x = (RArray) xarg; RArray y = (RArray) yarg; int xsize = x.size(); int ysize = y.size(); RArray expy; RArray expx; if (EAGER) { x = x.materialize(); // FIXME: probably unnecessary (both x and y), could be done on-the-fly in the expansion methods y = y.materialize(); if (y instanceof DoubleImpl) { expy = expandYVector((DoubleImpl) y, ysize, xsize); } else if (y instanceof IntImpl) { expy = expandYVector((IntImpl) y, ysize, xsize); } else { expy = expandYVector(y, ysize, xsize); } if (xsize > 0) { if (x instanceof DoubleImpl) { expx = expandXVector((DoubleImpl) x, xsize, ysize); } else if (x instanceof IntImpl) { expx = expandXVector((IntImpl) x, xsize, ysize); } else { expx = expandXVector(x, xsize, ysize); } } else { expx = x; } } else { if (y instanceof RInt) { expy = lazyExpandYVector((RInt) y, ysize, xsize); } else { throw Utils.nyi(); } if (xsize > 0) { if (x instanceof RInt) { expx = lazyExpandXVector((RInt) x, xsize, ysize); } else { throw Utils.nyi(); } } else { expx = x; } } xArgProvider.setValue(expx); yArgProvider.setValue(expy); callableProvider.matchAndSet(frame, farg); RArray res = (RArray) callNode.execute(frame); int[] dimx = x.dimensions(); int[] dimy = y.dimensions(); int[] dim; if (dimx == null) { if (dimy == null) { dim = new int[]{xsize, ysize}; } else { dim = new int[1 + dimy.length]; dim[0] = xsize; System.arraycopy(dimy, 0, dim, 1, dimy.length); } } else { if (dimy == null) { dim = new int[dimx.length + 1]; System.arraycopy(dimx, 0, dim, 0, dimx.length); dim[dimx.length] = ysize; } else { dim = new int[dimx.length + dimy.length]; System.arraycopy(dimx, 0, dim, 0, dimx.length); System.arraycopy(dimy, 0, dim, dimx.length, dimy.length); } } return res.setDimensions(dim); // triggers materialization of the result }
@Override public Object execute(Frame frame) { RAny lhsVal = (RAny) lhs.execute(frame); RAny colVal = (RAny) columnExpr.execute(frame); boolean dropVal = dropExpr.executeLogical(frame) != RLogical.FALSE; // FIXME: what is the correct execution order of these args? int exactVal = exactExpr.executeLogical(frame); // TODO: GNU-R has different behavior when selecting from arrays that have some dimension zero if (!(lhsVal instanceof RArray)) { throw RError.getObjectNotSubsettable(ast, lhsVal.typeOf()); } RArray array = (RArray) lhsVal; int[] dim = array.dimensions(); if (dim == null || dim.length != nSelectors) { throw RError.getIncorrectDimensions(getAST()); } int n = dim[nSelectors - 1]; // limit for the column (last dimension) try { int col; if (colVal instanceof ScalarIntImpl) { col = ((ScalarIntImpl) colVal).getInt(); } else if (colVal instanceof ScalarDoubleImpl) { col = Convert.double2int(((ScalarDoubleImpl) colVal).getDouble()); } else { throw new UnexpectedResultException(null); } if (col > n || col <= 0) { throw new UnexpectedResultException(null); } int[] ndim; int m; // size of the result if (dropVal) { boolean hasNonTrivialDimension = false; boolean resultIsVector = true; m = 1; for (int i = 0; i < nSelectors - 1; i++) { int d = dim[i]; if (d != 1) { if (hasNonTrivialDimension) { resultIsVector = false; } else { hasNonTrivialDimension = true; } } m *= d; } if (resultIsVector) { ndim = null; } else { ndim = new int[nSelectors - 1]; System.arraycopy(dim, 0, ndim, 0, ndim.length); } } else { ndim = new int[nSelectors]; ndim[nSelectors - 1] = 1; m = 1; for (int i = 0; i < ndim.length - 1; i++) { int d = dim[i]; ndim[i] = d; m *= d; } } // note: also could be lazy here RArray res = Utils.createArray(array, m, ndim, null, null); // drop attributes int offset = (col - 1) * m; // note: col is 1-based for (int i = 0; i < m; i++) { res.set(i, array.getRef(offset + i)); } return res; } catch (UnexpectedResultException e) { SelectorNode[] selectorExprs = new SelectorNode[nSelectors]; for (int i = 0; i < nSelectors - 1; i++) { selectorExprs[i] = Selector.createSelectorNode(ast, true, null); } selectorExprs[nSelectors - 1] = Selector.createSelectorNode(ast, true, columnExpr); GenericRead gr = new GenericRead(ast, true, lhs, selectorExprs, dropExpr, exactExpr); replace(gr, "install GenericRead from ArrayColumnSubset"); for (int i = 0; i < nSelectors - 1; i++) { gr.selectorVals[i] = selectorExprs[i].executeSelector(frame); } gr.selectorVals[nSelectors - 1] = selectorExprs[nSelectors - 1].executeSelector(colVal); return gr.executeLoop(array, dropVal, exactVal); } }