Package org.renjin.eval

Examples of org.renjin.eval.EvalException


  }

  private static Function getElementAsFunction(ListVector m, String name) {
    SEXP element = m.get(name);
    if (element == Null.INSTANCE || ! (element instanceof Function)) {
      throw new EvalException("'%s' absent", "m$" + name + "()");
    }
    return (Function) element;
  }
View Full Code Here


   */
  public boolean updateParameters(double[] newParameters) {
    SEXP result = context.evaluate(FunctionCall.newCall(setPars, new DoubleArrayVector(newParameters)),
            context.getGlobalEnvironment());
    if(!(result instanceof AtomicVector)) {
      throw new EvalException("Unexpected result from setPars");
    }
    return ((AtomicVector) result).getElementAsLogical(0) == Logical.TRUE;
  }
View Full Code Here

  private static SEXP convergenceResult(NlsControl control, String msg, int iterationNumber,
                                        StopReason stopReason,
                                        double newConvergence) {
   
    if(!control.isWarnOnly() && stopReason != StopReason.CONVERGED) {
      throw new EvalException(msg);
    }
   
    ListVector.NamedBuilder result = ListVector.newNamedBuilder();
    result.add("isConv", stopReason == StopReason.CONVERGED);
    result.add("finIter", iterationNumber);
View Full Code Here

                                       StringVector theta,
                                       Environment rho,
                                       DoubleVector dir) {

    if(dir.length() != theta.length()) {
      throw new EvalException("'dir' is not a numeric vector of the correct length");
    }

    SEXP responseExpr = context.evaluate(modelExpr, rho);
    if(!(responseExpr instanceof Vector)) {
      throw new EvalException("Expected numeric response from model");
    }
    Vector response = (Vector)responseExpr;
    for(int i=0;i!=response.length();++i) {
      if(!DoubleVector.isFinite(response.getElementAsDouble(i))) {
        throw new EvalException("Missing value or an infinity produced when evaluating the model");
      }
    }

    double parameterValues[][] = new double[theta.length()][];
    int totalParameterValueCount = 0;
    for (int i = 0; i < theta.length(); i++) {
      String parameterName = theta.getElementAsString(i);
      SEXP parameterValue = rho.findVariable(Symbol.get(parameterName));

      if (!(parameterValue instanceof AtomicVector)) {
        throw new EvalException("variable '%s' is not numeric", parameterName);
      }
      parameterValues[i] = ((AtomicVector) parameterValue).toDoubleArray();
      totalParameterValueCount += parameterValues[i].length;
    }

    double eps = Math.sqrt(DoubleVector.EPSILON);

    DoubleMatrixBuilder gradient = new DoubleMatrixBuilder(response.length(), totalParameterValueCount);
    int gradientColumn = 0;
    for (int parameterIndex = 0; parameterIndex < theta.length(); parameterIndex++) {

      double direction = dir.getElementAsDouble(parameterIndex);

      // Parameters can be multivariate, not just single values, so loop over each
      // element in this parameter
      for (int parameterValueIndex = 0; parameterValueIndex < parameterValues[parameterIndex].length;
           parameterValueIndex++) {

        // update this individual parameter value
        double startingParameterValue = parameterValues[parameterIndex][parameterValueIndex];
        double absoluteParameterValue = Math.abs(startingParameterValue);
        double delta = (absoluteParameterValue == 0) ? eps : absoluteParameterValue * eps;

        parameterValues[parameterIndex][parameterValueIndex] +=
                direction * delta;

        // update this parameter in the model's environment
        rho.setVariable(theta.getElementAsString(parameterIndex), new DoubleArrayVector(parameterValues[parameterIndex]));

        // compute the new response given this updated parameter
        DoubleVector responseDelta = (DoubleVector) context.evaluate(modelExpr, rho);

        for (int k = 0; k < response.length(); k++) {
          if (!DoubleVector.isFinite(responseDelta.getElementAsDouble(k))) {
            throw new EvalException("Missing value or an infinity produced when evaluating the model");
          }
          double difference = responseDelta.getElementAsDouble(k) - response.getElementAsDouble(k);
          double relativeDifference = difference / delta;
          gradient.set(k, gradientColumn, direction * relativeDifference);
        }
View Full Code Here

  private static Point toDeviceCoordinates(GraphicsDevice active, Point point, int from) {
    switch(from) {
    case USER:
      return active.userToDevice(point);
    default:
      throw new EvalException("Invalid 'from' argument");
    }
  }
View Full Code Here

  private static Point fromDeviceCoordinates(GraphicsDevice active, Point point, int to) {
    switch(to) {
    case DEVICE:
       return point;
    default:
      throw new EvalException("Invalid 'to' argument");
    }
  }
View Full Code Here

          return Null.INSTANCE;
        }
      }

    } else if(!(className instanceof S4Object)) {
      throw new EvalException("Class should be either a character-string name or a class definition");
    } else {
      return className;
    }
  }
View Full Code Here

  public static SEXP R_getGeneric(@Current Context context, Symbol symbol, boolean mustFind, Environment rho, String pkg) {

    SEXP generic = getGeneric(context, symbol, rho, pkg);
    if(generic == Symbol.UNBOUND_VALUE) {
      if(mustFind) {
        throw new EvalException("No generic function definition found for '%s' in the supplied environment", symbol.getPrintName());
      }
      generic = Null.INSTANCE;
    }
    return generic;
  }
View Full Code Here

    } else if(code_string.equalsIgnoreCase("set")) {
      code = prim_methods_t.HAS_METHODS;
    } else if(code_string.equalsIgnoreCase("suppress")) {
      code = prim_methods_t.SUPPRESSED;
    else {
      throw new EvalException("invalid primitive methods code (\"%s\"): should be \"clear\", \"reset\", \"set\", or \"suppress\"", code_string);
    }
    return code;
  }
View Full Code Here

    //      ptr = R_get_standardGeneric_ptr();
    //      }

 
    if(Strings.isNullOrEmpty(fname)) {
      throw new EvalException("argument to 'standardGeneric' must be a non-empty character string");
   
    SEXP fdef = get_this_generic(context, fname);
    if(fdef == Null.INSTANCE) {
      throw new EvalException("call to standardGeneric(\"%s\") apparently not from the body of that generic function", fname);
    }

    return context.getSession().getSingleton(MethodDispatch.class)
    .standardGeneric(context, Symbol.get(fname), env, fdef);
   
View Full Code Here

TOP

Related Classes of org.renjin.eval.EvalException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.