use of org.matheclipse.core.interfaces.ISignedNumber in project symja_android_library by axkr.
the class ExprEvaluator method evaluateDoube.
/**
* Parse the given <code>expression String</code> and evaluate it to a double value
*
* @param expression
* @return
* @throws SyntaxError
*/
public double evaluateDoube(final String inputExpression) {
if (inputExpression != null) {
EvalEngine.set(engine);
engine.reset();
fExpr = engine.parse(inputExpression);
if (fExpr != null) {
IExpr temp = evaluate(F.N(fExpr));
if (temp.isSignedNumber()) {
return ((ISignedNumber) temp).doubleValue();
}
}
}
return Double.NaN;
}
use of org.matheclipse.core.interfaces.ISignedNumber in project symja_android_library by axkr.
the class NIntegrate method evaluate.
@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
Validate.checkRange(ast, 3);
ISymbol method = LegendreGauss;
int maxPoints = DEFAULT_MAX_POINTS;
int maxIterations = DEFAULT_MAX_ITERATIONS;
// automatic scale value
int precisionGoal = 16;
if (ast.size() >= 4) {
final Options options = new Options(ast.topHead(), ast, 3, engine);
IExpr option = options.getOption("Method");
if (option.isSymbol()) {
method = (ISymbol) option;
}
option = options.getOption("MaxPoints");
if (option.isSignedNumber()) {
try {
maxPoints = ((ISignedNumber) option).toInt();
} catch (ArithmeticException ae) {
engine.printMessage("Error in option MaxPoints. Using default value: " + maxPoints);
}
}
option = options.getOption("MaxIterations");
if (option.isSignedNumber()) {
try {
maxIterations = ((ISignedNumber) option).toInt();
} catch (ArithmeticException ae) {
engine.printMessage("Error in option MaxIterations. Using default value: " + maxIterations);
}
}
option = options.getOption("PrecisionGoal");
if (option.isSignedNumber()) {
try {
precisionGoal = ((ISignedNumber) option).toInt();
} catch (ArithmeticException ae) {
engine.printMessage("Error in option PrecisionGoal. Using default value: " + precisionGoal);
}
}
}
if (ast.arg2().isList()) {
IAST list = (IAST) ast.arg2();
IExpr function = ast.arg1();
if (list.isAST3() && list.arg1().isSymbol()) {
ISignedNumber min = list.arg2().evalSignedNumber();
ISignedNumber max = list.arg3().evalSignedNumber();
if (min != null && max != null) {
if (function.isAST(F.Equal, 3)) {
function = F.Plus(((IAST) function).arg1(), F.Negate(((IAST) function).arg2()));
}
try {
double result = integrate(method.getSymbolName(), list, min.doubleValue(), max.doubleValue(), function, maxPoints, maxIterations);
result = Precision.round(result, precisionGoal);
return Num.valueOf(result);
} catch (Exception e) {
throw new WrappedException(e);
}
}
}
}
return F.NIL;
}
use of org.matheclipse.core.interfaces.ISignedNumber in project symja_android_library by axkr.
the class NDSolve method evaluate.
@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
if (!ToggleFeature.DSOLVE) {
return F.NIL;
}
Validate.checkSize(ast, 4);
if (ast.arg3().isAST(F.List, 4)) {
try {
IAST uFunctionSymbols = Validate.checkSymbolOrSymbolList(ast, 2);
int dimension = uFunctionSymbols.size() - 1;
IAST xVarList = (IAST) ast.arg3();
ISymbol xVar = (ISymbol) xVarList.arg1();
IExpr xMinExpr = xVarList.arg2();
IExpr xMaxExpr = xVarList.arg3();
if (xMinExpr.isSignedNumber() && xMaxExpr.isSignedNumber()) {
double xMin = ((ISignedNumber) xMinExpr).doubleValue();
double xMax = ((ISignedNumber) xMaxExpr).doubleValue();
double xStep = 0.1;
IAST listOfEquations = Validate.checkEquations(ast, 1).clone();
IExpr[][] boundaryCondition = new IExpr[2][dimension];
int i = 1;
while (i < listOfEquations.size()) {
IExpr equation = listOfEquations.get(i);
if (equation.isFree(xVar)) {
if (determineSingleBoundary(equation, uFunctionSymbols, xVar, boundaryCondition, engine)) {
listOfEquations.remove(i);
continue;
}
}
i++;
}
IExpr[] dotEquations = new IExpr[dimension];
i = 1;
while (i < listOfEquations.size()) {
IExpr equation = listOfEquations.get(i);
if (!equation.isFree(xVar)) {
if (determineSingleDotEquation(equation, uFunctionSymbols, xVar, dotEquations, engine)) {
listOfEquations.remove(i);
continue;
}
}
i++;
}
if (uFunctionSymbols.isList()) {
AbstractIntegrator dp853 = new DormandPrince853Integrator(1.0e-8, 100.0, 1.0e-10, 1.0e-10);
double[] xyz = new double[dimension];
for (int j = 0; j < dimension; j++) {
xyz[j] = ((INum) engine.evalN(boundaryCondition[1][j])).doubleValue();
}
OrdinaryDifferentialEquation ode = new FirstODE(engine, dotEquations, uFunctionSymbols, xVar);
IAST resultList = F.List();
IAST result = F.Interpolation(resultList);
IAST list;
for (double tDouble = xMin; tDouble < xMax; tDouble += xStep) {
list = F.List(F.num(tDouble));
dp853.integrate(ode, tDouble, xyz, tDouble + xStep, xyz);
for (int j = 0; j < xyz.length; j++) {
list.append(F.num(xyz[j]));
}
resultList.append(list);
}
resultList.setEvalFlags(IAST.IS_MATRIX);
return result;
}
}
} catch (RuntimeException rex) {
if (Config.SHOW_STACKTRACE) {
rex.printStackTrace();
}
}
}
return F.NIL;
}
use of org.matheclipse.core.interfaces.ISignedNumber in project symja_android_library by axkr.
the class Min method evaluate.
@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
Validate.checkRange(ast, 1);
if (ast.isAST0()) {
return F.CInfinity;
}
if (ast.arg1().isInterval1()) {
IAST list = (IAST) ast.arg1().getAt(1);
try {
return (ISignedNumber) list.arg1();
} catch (ClassCastException cca) {
// do nothing
}
}
IAST resultList = EvalAttributes.flatten(F.List, ast);
if (resultList.isPresent()) {
return minimum(resultList, true);
}
return minimum(ast, false);
}
use of org.matheclipse.core.interfaces.ISignedNumber in project symja_android_library by axkr.
the class CoreCallbackFunction method evaluate.
@Override
public double evaluate(DoubleEvaluator doubleEngine, FunctionNode functionNode, double[] args) {
ASTNode node = functionNode.getNode(0);
if (node instanceof SymbolNode) {
AST2Expr ast2Expr = new AST2Expr();
IExpr head = ast2Expr.convert(node);
IAST fun = F.ast(head);
for (int i = 0; i < args.length; i++) {
fun.append(F.num(args[i]));
}
final IExpr result = F.evaln(fun);
if (result.isSignedNumber()) {
return ((ISignedNumber) result).doubleValue();
}
} else if (node instanceof FunctionNode) {
AST2Expr ast2Expr = new AST2Expr();
IExpr head = ast2Expr.convert(node);
IAST fun = F.ast(head);
for (int i = 0; i < args.length; i++) {
fun.append(F.num(args[i]));
}
final IExpr result = F.evaln(fun);
if (result.isSignedNumber()) {
return ((ISignedNumber) result).doubleValue();
}
}
throw new MathException("CoreCallbackFunction#evaluate() not possible for: " + functionNode.toString());
}
Aggregations