Пример #1
0
 /**
  * Create a differention rule for function with 2 arguments. The rules must be in terms of "x" and
  * "y"
  *
  * @param inName name of function
  * @param inPfmc PostfixMathCommandI for function
  * @param rule1 a string represention differation of function wrt "x"
  * @param rule2 a string represention differation of function wrt "y"
  * @throws ParseException
  */
 private MacroDiffRules(
     DJep djep, String inName, PostfixMathCommandI inPfmc, String rule1, String rule2)
     throws ParseException {
   name = inName;
   pfmc = inPfmc;
   if (pfmc != null) {
     int nParam = pfmc.getNumberOfParameters();
     if (nParam != 2) {
       throw new ParseException(
           "Number of rules must match number of parameters for "
               + inName
               + " which is "
               + nParam);
     }
   }
   XSymbolTable localSymTab =
       (XSymbolTable) ((XSymbolTable) djep.getSymbolTable()).newInstance(); // new
   // SymbolTable();
   localSymTab.copyConstants(djep.getSymbolTable());
   XJep localJep = djep.newInstance(localSymTab);
   Node node1 = localJep.parse(rule1);
   Node node2 = localJep.parse(rule2);
   rules = new Node[2];
   rules[0] = node1;
   rules[1] = node2;
 }
Пример #2
0
  /**
   * Evaluate a function at a specific point for one single variable. Evaluation is done between
   * xmin and xmax. It is assumed that there are no any other variable involved
   *
   * @param indvars Define independent variable, like 'x' Only one variable is allowed
   * @param xmin xmin value for independent varible
   * @param xmax xmax value for independent varible
   * @return true if no errors
   */
  public boolean eval(String indvars, double xmin, double xmax) {

    boolean suc = true;
    String name = proxy.getName();
    int points = proxy.getPoints();

    double min = xmin;
    double max = xmax;
    x = new double[points];
    y = new double[points];
    for (int i = 0; i < points; i++) {
      x[i] = min + i * (max - min) / (points - 1);
      jep.addVariable(indvars.trim(), x[i]);

      try {
        Object result = jep.evaluate(node);
        if (result instanceof Double) {
          y[i] = ((Double) jep.evaluate(node)).doubleValue();
        }
      } catch (ParseException e) {
        jhplot.utils.Util.ErrorMessage(
            "Failed to parse function " + name + " Error:" + e.toString());
        suc = false;
      }
    }

    if (suc) isEvaluated = true;

    return suc;
  } // end 1-D evaluation
Пример #3
0
  /**
   * Evaluate a function at a specific point in x
   *
   * @param vars Values for evaluation separated by commas, x=1,y=2,z=3
   * @return function value at x
   */
  public double eval(String vars) {

    // get all variables
    String[] tmp = vars.split(",");
    // double[] vd = new double[tmp.length];

    for (int i = 0; i < tmp.length; i++) {
      String[] vv = tmp[i].split("=");

      if (vv.length != 2) {
        ErrorMessage("Error in parsing list of input variablse. Did you use val=number? ");
      }

      try {
        double d = Double.valueOf(vv[1].trim()).doubleValue();
        jep.addVariable(vv[0].trim(), d);
      } catch (NumberFormatException nfe) {
        System.out.println("NumberFormatException: " + nfe.getMessage());
      }
    }

    try {
      Object result = jep.evaluate(node);
      if (result instanceof Double) {
        return ((Double) jep.evaluate(node)).doubleValue();
      }
    } catch (ParseException e) {
      return 0;
    }

    return 0;
  }
Пример #4
0
  /**
   * Create a function in any dimension evaluation.
   *
   * <p>The function may have many independent variables.
   *
   * <p>
   *
   * <h3>List of commands</h3>
   *
   * <ul>
   *   <li>( ) parenthesis , comma
   *   <li>+, -, unary -, unary +
   *   <li>*, /
   *   <li>^ (raise to a power)
   *   <li>pi, e, All the constants in class SpecialFunction
   *   <li>log
   *   <li>sin, cos, tan, sinh, cosh, tanh
   *   <li>asin, acos, atan, asinh, acosh, atanh
   *   <li>sqrt
   *   <li>rand
   *   <li>exp
   *   <li>remainder
   *   <li>atan2
   *   <li>Special functions and constants. Look at the book
   *   <li>All the functions in class SpecialFunction
   *   <li>Independent variables x
   *   <li>Scientific notation using "e", "E", "d", "D".
   * </ul>
   *
   * @prama title Title of the function
   * @param name String representing the function
   * @param vars String representing variables. Each variable should be separated by a comma.
   *     Example "x,y,z"
   */
  public FND(String title, String name, String vars) {

    this.title = title;
    proxy = new FProxy(3, title, name, null, new double[] {0, 0, 0, 0, 0, 0}, maxpoints, true);

    name = proxy.getName();
    proxy.setVariables(vars);
    setTitle(title);
    lpp.setType(LinePars.F1D);
    jep = new XJep();
    jep.addStandardConstants();
    jep.addStandardFunctions();
    jep.setAllowUndeclared(true);
    jep.setImplicitMul(true);
    jep.setAllowAssignment(true);

    // get all variables
    avars = vars.split(",");
    for (int i = 0; i < avars.length; i++) {
      jep.addVariable(avars[i].trim(), 0);
    }

    jep.addVariable("x", 0);

    try {
      node = jep.parse(name);
      processed = jep.preprocess(node);
    } catch (ParseException e) {
    } catch (Exception e) {
      ErrorMessage("Error in parsing " + name);
    }
  }
Пример #5
0
  /**
   * Initialize function from proxy.
   *
   * @param f
   */
  public FND(FProxy f) {
    proxy = f;
    String name = proxy.getName();
    if (proxy.getType() != 4) {
      ErrorMessage("Error in parsing FND. Wrong type! " + name);
      return;
    }

    setTitle(proxy.getTitle());
    lpp.setType(LinePars.F1D);
    jep = new XJep();
    jep.addStandardConstants();
    jep.addStandardFunctions();
    jep.setAllowUndeclared(true);
    jep.setImplicitMul(true);
    jep.setAllowAssignment(true);

    // get all variables
    String vars = proxy.getVariables();
    avars = vars.split(",");
    for (int i = 0; i < avars.length; i++) {
      jep.addVariable(avars[i].trim(), 0);
    }

    jep.addVariable("x", 0);

    try {
      node = jep.parse(name);
      processed = jep.preprocess(node);
    } catch (ParseException e) {
    } catch (Exception e) {
      ErrorMessage("Error in parsing " + name);
    }
  }
Пример #6
0
  public void simplifyTestString(String expr, String expected) throws ParseException {
    XJep xj = (XJep) j;

    Node node = xj.parse(expr);
    Node processed = xj.preprocess(node);
    Node simp = xj.simplify(processed);
    String res = xj.toString(simp);

    if (!expected.equals(res))
      System.out.println(
          "Error: Value of \"" + expr + "\" is \"" + res + "\" should be \"" + expected + "\"");
    assertEquals("<" + expr + ">", expected, res);
    System.out.println("Success: Value of \"" + expr + "\" is \"" + res + "\"");
  }
Пример #7
0
  /**
   * Evaluate a function at a specific point for one single variable. Evaluation is done between
   * xmin and xmax
   *
   * @param indvars Define independent variable, like 'x' Only one variable is allowed
   * @param xmin xmin value for independent varible
   * @param xmax xmax value for independent varible
   * @param vars define values for other variables, like 'y=1,z=3'
   * @return true if no errors
   */
  public boolean eval(String indvars, double xmin, double xmax, String vars) {
    String name = proxy.getName();
    int points = proxy.getPoints();
    boolean suc = true;
    String[] tmp = vars.split(",");
    // double[] vd = new double[tmp.length];
    fixedVars = vars;

    for (int i = 0; i < tmp.length; i++) {
      String[] vv = tmp[i].split("=");

      if (vv.length != 2) {
        ErrorMessage("Error in parsing list of input variablse. Did you use val=number? ");
      }

      try {
        double d = Double.valueOf(vv[1].trim()).doubleValue();
        jep.addVariable(vv[0].trim(), d);
      } catch (NumberFormatException nfe) {
        System.out.println("NumberFormatException: " + nfe.getMessage());
        suc = false;
      }
    }

    double min = xmin;
    double max = xmax;
    x = new double[points];
    y = new double[points];
    for (int i = 0; i < points; i++) {
      x[i] = min + i * (max - min) / (points - 1);
      jep.addVariable(indvars.trim(), x[i]);

      try {
        Object result = jep.evaluate(node);
        if (result instanceof Double) {
          y[i] = ((Double) jep.evaluate(node)).doubleValue();
        }
      } catch (ParseException e) {
        jhplot.utils.Util.ErrorMessage(
            "Failed to parse function " + name + " Error:" + e.toString());
        suc = false;
      }
    }

    if (suc) isEvaluated = true;

    return suc;
  } // end 1-D evaluation
Пример #8
0
  /** Treat the function as complex. */
  public void simplify() {

    String name = proxy.getName();

    try {
      simp = jep.simplify(processed);
    } catch (ParseException e) {
    } catch (Exception e) {
      ErrorMessage("Error in simplification of " + name);
    }
  }
Пример #9
0
  /**
   * Create a differentation rule for function with n arguments. The rules must be in terms of "x1",
   * "x2", ... "xn"
   *
   * @param inName name of function
   * @param inPfmc PostfixMathCommandI for function
   * @throws ParseException
   */
  private MacroDiffRules(DJep djep, String inName, PostfixMathCommandI inPfmc, String[] inRules)
      throws ParseException {
    name = inName;
    pfmc = inPfmc;
    if (pfmc != null) {
      int nParam = pfmc.getNumberOfParameters();
      if (nParam != inRules.length) {
        throw new ParseException(
            "Number of rules must match number of parameters for "
                + inName
                + " which is "
                + nParam);
      }
    }

    XSymbolTable localSymTab = (XSymbolTable) ((XSymbolTable) djep.getSymbolTable()).newInstance();
    localSymTab.copyConstants(djep.getSymbolTable());
    XJep localJep = djep.newInstance(localSymTab);

    rules = new Node[inRules.length];
    for (int i = 0; i < inRules.length; ++i) {
      rules[i] = localJep.parse(inRules[i]);
    }
  }
Пример #10
0
 /** Convert to string */
 public String toString() {
   if (simp != null) {
     return jep.toString(simp);
   }
   return jep.toString(node);
 }
Пример #11
0
 /** Treat the function as complex. */
 public void setComplex() {
   jep.addComplex();
 }