public static IntegrationFunction parse(String string) throws InvalidIntegralException {
    variablesNames = new ArrayList<String>();
    countVariables(string);
    try {
      if (variablesNames.size() == 0) {
        return new ConstantFunction(Double.parseDouble(string));
      }

      ExpressionBuilder builder = new ExpressionBuilder(string);

      builder.withVariableNames("x", "y", "z");

      final Calculable calc = builder.build();

      IntegrationFunction function =
          new IntegrationFunction() {
            @Override
            public double evaluate(double... args) {
              if (args.length == 1) {
                return calc.calculate(args[0], 0, 0);
              } else if (args.length == 2) {
                return calc.calculate(args[0], args[1], 0);
              } else {
                return calc.calculate(args);
              }
            }
          };

      return function;
    } catch (UnknownFunctionException e) {
      throw new InvalidIntegralException(e);
    } catch (UnparsableExpressionException e) {
      throw new InvalidIntegralException(e);
    }
  }
Пример #2
0
  /*
   * Sets the actual expression string for this expression
   */
  private void setExpressions(String start, String end) {

    startBuilder = new ExpressionBuilder(start);
    endBuilder = new ExpressionBuilder(end);
    for (String n : getAllSymbols()) {
      startBuilder.withVariable(new Variable(n));
      endBuilder.withVariable(new Variable(n));
    }
  }