Exemplo n.º 1
0
  /**
   * Call this function to resolve variables and init the function. May throw MyError
   * (InvalidFunction).
   */
  public void initFunction() {
    // replace function variables in tree
    for (int i = 0; i < fVars.length; i++) {
      FunctionVariable fVar = fVars[i];

      // look for Variable objects with name of function variable and
      // replace them
      int replacements = expression.replaceVariables(fVar.getSetVarString(), fVar);
      isConstantFunction = isConstantFunction && replacements == 0;

      if (replacements == 0) {
        // x, y got polynomials while parsing
        replacements = expression.replacePolynomials(fVar);
        isConstantFunction = isConstantFunction && replacements == 0;
      }
    }

    // replace variable names by objects
    expression.resolveVariables();

    // the idea here was to allow something like: Derivative[f] + 3x
    // but wrapping the GeoFunction objects as ExpressionNodes of type
    // FUNCTION
    // leads to Derivative[f](x) + 3x
    // expression.wrapGeoFunctionsAsExpressionNode();

    // replace all polynomials in expression (they are all equal to "1x" if
    // we got this far)
    // by an instance of MyDouble

    // simplify constant parts in expression
    expression.simplifyConstantIntegers();

    // evaluate expression to find out about the type of function
    ExpressionValue ev;
    try {
      ev = expression.evaluate();
    } catch (MyError err) {
      // Evaluation failed: DESPERATE MODE
      try {
        // try to fix structure of expression and then try evaluation again
        fixStructure();
        ev = expression.evaluate();
      } catch (Throwable th) {
        // throw original error when desperate mode failed
        throw err;
      }
    }

    // initialize type as boolean or numeric function
    initType(ev);
  }