public String deriveAt(double y) { String first = ""; String second = ""; for (String s : fn) { first += s; first += (DELTA_X + y); } for (String s : fn) { second += s; second += y; } try { Calculable evalFirst = new ExpressionBuilder(first).build(); Calculable evalSec = new ExpressionBuilder(second).build(); // double evalFirst = (Double)se.eval(first); // double evalSec = (Double)se.eval(second); DecimalFormat dm = new DecimalFormat("#.###"); double ans = ((evalFirst.calculate() - evalSec.calculate()) / DELTA_X); return dm.format(ans); } catch (Exception e) { } return "Error"; }
public double evaluar(double valor, boolean error) throws UnknownFunctionException, UnparsableExpressionException { double resultado = 0; Calculable calc = this.parser.withVariable("x", valor).build(); resultado = calc.calculate(); return resultado; }
public double evaulateAt(double x) { String fnStr = ""; for (String s : fn) { fnStr += s; fnStr += x; } try { Calculable eval = new ExpressionBuilder(fnStr).build(); return eval.calculate(); } catch (Exception e) { } return 0; }
@Override public Variable evaluate(SimulationStatus status) { Calculable startCalc = buildExpression(startBuilder); Calculable endCalc = buildExpression(endBuilder); if (startCalc == null || endCalc == null) { return new Variable("Unknown"); } // Set the variables in the start and end calculators for (FlightDataType type : status.getFlightData().getTypes()) { double value = status.getFlightData().getLast(type); startCalc.setVariable(new Variable(type.getSymbol(), value)); endCalc.setVariable(new Variable(type.getSymbol(), value)); } // From the given datatype, get the time and function values and make an interpolator // Note: must get in a way that flight data system will figure out units. Otherwise there will // be a type conflict when we get the new data. FlightDataType type = FlightDataType.getType(null, getSymbol(), null); List<Double> data = status.getFlightData().get(type); List<Double> time = status.getFlightData().get(FlightDataType.TYPE_TIME); LinearInterpolator interp = new LinearInterpolator(time, data); // Evaluate the expression to get the start and end of the range double startTime, endTime; try { startTime = startCalc.calculate().getDoubleValue(); startTime = MathUtil.clamp(startTime, 0, Double.MAX_VALUE); endTime = endCalc.calculate().getDoubleValue(); endTime = MathUtil.clamp(endTime, 0, time.get(time.size() - 1)); } catch (java.util.EmptyStackException e) { log.info( Markers.USER_MARKER, "Unable to calculate time index for range expression " + getSymbol() + " due to empty stack exception"); return new Variable("Unknown"); } // generate an array representing the range double step = status.getSimulationConditions().getSimulation().getOptions().getTimeStep(); double[] t = ArrayUtils.range(startTime, endTime, step); double[] y = new double[t.length]; int i = 0; for (double tval : t) { y[i] = interp.getValue(tval); i++; } Variable result; if (y.length == 0) { result = new Variable("Unknown"); } else { result = new Variable(hash(), y, startTime, step); } return result; }