Beispiel #1
0
 public static ASTNode create(boolean isSuper, ASTNode lhs, ASTNode rhs) {
   if (lhs instanceof SimpleAccessVariable) {
     return writeVariable(isSuper, ((SimpleAccessVariable) lhs).symbol, rhs);
   } else if (lhs instanceof AccessVector) {
     return writeVector(isSuper, (AccessVector) lhs, rhs);
   } else if (lhs instanceof FieldAccess) {
     return writeField(isSuper, (FieldAccess) lhs, rhs);
   } else if (lhs instanceof FunctionCall) {
     return writeFunction(isSuper, (FunctionCall) lhs, rhs);
   } else if (lhs instanceof Constant) { // TODO: move this to the parser?
     RAny value = ((Constant) lhs).getValue();
     if (value instanceof RString) {
       RString svalue = (RString) value;
       if (svalue.size() == 1) {
         String name = svalue.getString(0);
         return writeVariable(isSuper, RSymbol.getSymbol(name), rhs);
       }
     }
     throw RError.getUnknownObject(rhs); // TODO it's own exception
   }
   Utils.nyi();
   return null;
 }
Beispiel #2
0
        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
        }
Beispiel #3
0
    @Override public RNode create(ASTNode call, RSymbol[] names, RNode[] exprs) {
        /*ArgumentInfo ia = */
        check(call, names, exprs);
        if (names.length == 1) {
            return new Builtin.Builtin1(call, names, exprs) {
                @Override public RAny doBuiltIn(Frame frame, RAny xarg) {
                    // x must be matrix-like
                    // method is pearson
                    // use is everything

                    RDouble xd;
                    if (xarg instanceof RDouble || xarg instanceof RInt || xarg instanceof RLogical) {
                        xd = xarg.asDouble().materialize();
                    } else {
                        if (xarg instanceof RArray) {
                            throw RError.getXNumeric(ast);
                        } else {
                            throw RError.getSupplyXYMatrix(ast);
                        }
                    }
                    int[] dim = xd.dimensions();
                    if (dim == null || dim.length != 2) {
                        throw RError.getSupplyXYMatrix(ast);
                    }

                    int nrow = dim[0];
                    int ncol = dim[1];
                    double[] res = new double[ncol * ncol];
                    double[] x = xd.getContent();

                    boolean[] hasNA = new boolean[ncol];
                    boolean anyNA = columnsNAMap(x, nrow, ncol, hasNA);
                    int n1 = nrow - 1;
                    boolean sdZero = false;

                    if (!anyNA) {
                        double[] colMeans = columnMeansNoNA(x, nrow, ncol);
                        for (int i = 0; i < ncol ; i++) {
                            double imean = colMeans[i];
                            int ioffset = i * nrow;
                            for (int j = 0; j <= i; j++) {
                                double jmean = colMeans[j];
                                int joffset = j * nrow;
                                double sum = 0;
                                for (int k = 0; k < nrow; k++) {
                                    sum += (x[ioffset + k] - imean) * (x[joffset + k] - jmean);
                                }
                                double tmp = sum / n1;
                                res[j * ncol + i] = tmp;
                                res[i * ncol + j] = tmp;
                            }
                        }
                        double[] srcov = colMeans;  // colMeans no longer needed
                        for (int i = 0; i < ncol; i++) {
                            srcov[i] = Math.sqrt(res[i * ncol + i]);
                        }
                        for (int i = 0; i < ncol; i++) {
                            for (int j = 0; j < i; j++) {
                                if (srcov[i] == 0 || srcov[j] == 0) {
                                    sdZero = true;
                                    res[j * ncol + i] = RDouble.NA;
                                    res[i * ncol + j] = RDouble.NA;
                                } else {
                                    double tmp = res[i * ncol + j]  / (srcov[i] * srcov[j]);
                                    if (tmp > 1) {
                                        tmp = 1;
                                    }
                                    res[j * ncol + i] = tmp;
                                    res[i * ncol + j] = tmp;
                                }
                            }
                            res[i * ncol + i] = 1;
                        }
                    } else {
                        double[] colMeans = columnMeans(x, nrow, ncol, hasNA);
                        for (int i = 0; i < ncol ; i++) {
                            if (hasNA[i]) {
                                for (int j = 0; j <= i; j++ ) {
                                    res[j * ncol + i] = RDouble.NA; // FIXME: break this into two loops?
                                    res[i * ncol + j] = RDouble.NA;
                                }
                                continue;
                            }
                            double imean = colMeans[i];
                            int ioffset = i * nrow;
                            for (int j = 0; j <= i; j++) {
                                if (hasNA[j]) {
                                    res[j * ncol + i] = RDouble.NA;
                                    res[i * ncol + j] = RDouble.NA;
                                    continue;
                                }
                                double jmean = colMeans[j];
                                int joffset = j * nrow;
                                double sum = 0;
                                for (int k = 0; k < nrow; k++) {
                                    sum += (x[ioffset + k] - imean) * (x[joffset + k] - jmean);
                                }
                                double tmp = sum / n1;
                                res[j * ncol + i] = tmp;
                                res[i * ncol + j] = tmp;
                            }
                        }
                        double[] srcov = colMeans;  // colMeans no longer needed
                        for (int i = 0; i < ncol; i++) {
                            if (!hasNA[i]) {
                                srcov[i] = Math.sqrt(res[i * ncol + i]);
                            }
                        }
                        for (int i = 0; i < ncol; i++) {
                            if (!hasNA[i]) {
                                for (int j = 0; j < i; j++) {
                                    if (srcov[i] == 0 || srcov[j] == 0) {
                                        sdZero = true;
                                        res[j * ncol + i] = RDouble.NA;
                                        res[i * ncol + j] = RDouble.NA;
                                    } else {
                                        double tmp = res[i * ncol + j]  / (srcov[i] * srcov[j]);
                                        if (tmp > 1) {
                                            tmp = 1;
                                        }
                                        res[j * ncol + i] = tmp;
                                        res[i * ncol + j] = tmp;
                                    }
                                }
                            }
                            res[i * ncol + i] = 1;
                        }
                    }
                    if (sdZero) {
                        RContext.warning(ast, RError.SD_ZERO);
                    }
                    return RDouble.RDoubleFactory.getFor(res, new int[] {ncol, ncol}, null);
                }
            };
        }
//        final int xPosition = ia.position("x");
//        final int yPosition = ia.position("y");
//        final int usePosition = ia.position("use");
//        final int methodPosition = ia.position("method");

        Utils.nyi("finish cor");
        return null;
    }