use of org.matheclipse.core.interfaces.IEvaluator in project symja_android_library by axkr.
the class ExpressionJSONConvert method exportGraphics3DJSON.
private static JsonNode exportGraphics3DJSON(IExpr data3D) {
if (data3D.isList()) {
IAST list = (IAST) data3D;
ArrayNode temp = JSON_OBJECT_MAPPER.createArrayNode();
for (int i = 1; i < list.size(); i++) {
IExpr arg = list.getAST(i);
if (arg.isAST()) {
IAST ast = (IAST) arg;
if (ast.head().isBuiltInSymbol()) {
StringBuilder buf = new StringBuilder();
IBuiltInSymbol symbol = (IBuiltInSymbol) ast.head();
IEvaluator evaluator = symbol.getEvaluator();
if (evaluator instanceof IGraphics3D) {
// JsonNode n = ((IGraphics3D) evaluator).graphics3D(buf, (IAST) ast);
// temp.add(n);
}
}
}
}
return temp;
}
ArrayNode temp = JSON_OBJECT_MAPPER.createArrayNode();
temp.add(temp.toString());
return temp;
}
use of org.matheclipse.core.interfaces.IEvaluator in project symja_android_library by axkr.
the class DoubleStackEvaluator method evalAST.
public static double evalAST(double[] stack, final int top, final IAST ast) {
if (ast.head().isBuiltInSymbol()) {
final IBuiltInSymbol symbol = (IBuiltInSymbol) ast.head();
final IEvaluator module = symbol.getEvaluator();
if (module instanceof INumeric) {
int newTop = top;
// fast evaluation path
if (top + ast.size() >= stack.length) {
stack = new double[ast.size() + 50];
}
for (int i = 1; i < ast.size(); i++) {
++newTop;
stack[newTop] = DoubleStackEvaluator.eval(stack, newTop, ast.get(i));
}
return ((INumeric) module).evalReal(stack, newTop, ast.argSize());
}
}
// slow evaluation path
final IExpr result = F.evaln(ast);
if (result.isReal()) {
return ((ISignedNumber) result).doubleValue();
}
if (result instanceof IComplexNum && F.isZero(((IComplexNum) result).imDoubleValue())) {
return ((IComplexNum) result).reDoubleValue();
}
throw new UnsupportedOperationException("EvalDouble#evalAST(): " + ast);
}
use of org.matheclipse.core.interfaces.IEvaluator in project symja_android_library by axkr.
the class AbstractAST method evaluate.
/**
* {@inheritDoc}
*/
@Override
public IExpr evaluate(EvalEngine engine) {
LOGGER.debug("Evaluate {}", this);
final IExpr head = head();
final int argSize = argSize();
if (head instanceof ISymbol) {
ISymbol headSymbol = (ISymbol) head;
Class<?> clazz = headSymbol.getContext().getJavaClass();
if (clazz != null) {
String staticMethodName = headSymbol.getSymbolName();
// try {
// Method method = clazz.getMethod(staticMethodName);
// if (Modifier.isStatic(method.getModifiers())) {
// Parameter[] parameters = method.getParameters();
// if (parameters.length == argSize()) {
// Object[] params = JavaFunctions.determineParameters(this, parameters, 1);
// if (params != null) {
// Object result;
// try {
// result = method.invoke(null, params);
// if (result instanceof String) {
// return F.stringx((String) result);
// }
// return Object2Expr.convert(result);
// } catch (IllegalAccessException
// | IllegalArgumentException
// | InvocationTargetException e) {
// // fall through?
// }
// }
// }
// }
//
// } catch (IllegalArgumentException | NoSuchMethodException | SecurityException e) {
// // fall through?
// }
Method[] methods = clazz.getMethods();
for (int i = 0; i < methods.length; i++) {
if (Modifier.isStatic(methods[i].getModifiers())) {
if (staticMethodName.equals(methods[i].getName())) {
Parameter[] parameters = methods[i].getParameters();
if (parameters.length == argSize()) {
Object[] params = JavaFunctions.determineParameters(this, parameters, 1);
if (params != null) {
Object result;
try {
result = methods[i].invoke(null, params);
if (result instanceof String) {
return F.stringx((String) result);
}
return Object2Expr.convert(result, false, true);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
// fall through?
}
}
}
}
}
}
}
if (head instanceof IBuiltInSymbol) {
final IEvaluator evaluator = ((IBuiltInSymbol) head).getEvaluator();
if (evaluator instanceof ICoreFunctionEvaluator) {
try {
ICoreFunctionEvaluator functionEvaluator = (ICoreFunctionEvaluator) evaluator;
EvalEngine.OptionsResult opres = engine.checkBuiltinArguments(this, functionEvaluator);
if (opres == null) {
return F.NIL;
}
IAST ast = opres.result;
IBuiltInSymbol header = ((IBuiltInSymbol) head);
if ((header.getAttributes() & ISymbol.SEQUENCEHOLD) != ISymbol.SEQUENCEHOLD) {
IExpr temp;
if ((temp = F.flattenSequence(this)).isPresent()) {
return temp;
}
}
if (isBooleanFunction()) {
IExpr temp = extractConditionalExpression(false);
if (temp.isPresent()) {
return temp;
}
}
IExpr evaluateTemp = evalEvaluate(engine);
if (evaluateTemp.isPresent()) {
return evaluateTemp;
}
return functionEvaluator.evaluate(ast, engine);
} catch (ValidateException ve) {
return IOFunctions.printMessage(topHead(), ve, engine);
} catch (FlowControlException e) {
throw e;
} catch (SymjaMathException ve) {
LOGGER.log(engine.getLogLevel(), topHead(), ve);
return F.NIL;
}
}
}
}
if (head.isAssociation() && argSize == 1) {
return ((IAssociation) head).getValue(arg1());
}
final ISymbol symbol = topHead();
IExpr temp = engine.evalAttributes(symbol, this);
if (temp.isPresent()) {
return temp;
}
return engine.evalRules(symbol, this);
}
use of org.matheclipse.core.interfaces.IEvaluator in project symja_android_library by axkr.
the class FunctionIDGenerator method printGithubSymjaFunctionLineNumber.
public static void printGithubSymjaFunctionLineNumber() {
try {
ParserConfig.EXPLICIT_TIMES_OPERATOR = true;
F.initSymbols();
ArrayList<String> list = new ArrayList<String>();
for (int i = 0; i < AST2Expr.UPPERCASE_SYMBOL_STRINGS.length; i++) {
list.add(AST2Expr.UPPERCASE_SYMBOL_STRINGS[i]);
}
for (int i = 0; i < AST2Expr.SYMBOL_STRINGS.length; i++) {
list.add(AST2Expr.SYMBOL_STRINGS[i]);
}
for (int i = 0; i < AST2Expr.FUNCTION_STRINGS.length; i++) {
list.add(AST2Expr.FUNCTION_STRINGS[i]);
}
for (int i = 0; i < AST2Expr.DOLLAR_STRINGS.length; i++) {
list.add(AST2Expr.DOLLAR_STRINGS[i]);
}
Collections.sort(list);
int counter = 0;
System.out.println("/**\n" + " * Generated by class: <code>org.matheclipse.core.preprocessor.FunctionIDGenerator\n" + " * </code>\n" + " */\n" + "public final static int[] LINE_NUMBER_OF_JAVA_CLASS = new int[] { //");
for (int i = 0; i < list.size(); i++) {
counter++;
if (counter > 10) {
System.out.println(" //");
counter = 0;
}
ISymbol sym = F.symbol(list.get(i));
if (sym instanceof IBuiltInSymbol) {
IBuiltInSymbol builtin = (IBuiltInSymbol) sym;
IEvaluator eval = builtin.getEvaluator();
if (eval != null && eval != BuiltInSymbol.DUMMY_EVALUATOR) {
Class<? extends IEvaluator> clazz = eval.getClass();
if (//
!clazz.isAnonymousClass() && !clazz.isSynthetic() && !clazz.isInterface() && !clazz.isPrimitive()) {
//
String fileName = buildFileNameL(clazz);
File sourceLocation = new File(fileName);
int lineCounter = lineNumberOfClass(sourceLocation, list.get(i));
System.out.print(lineCounter);
} else {
System.out.print("0");
}
} else {
System.out.print("0");
}
} else {
System.out.print("0");
}
if (i < list.size() - 1) {
System.out.print(",");
}
}
System.out.print("};");
} catch (RuntimeException ex) {
System.out.println();
ex.printStackTrace();
}
}
use of org.matheclipse.core.interfaces.IEvaluator in project symja_android_library by axkr.
the class GraphicsFunctions method exportGraphics3DRecursive.
public static boolean exportGraphics3DRecursive(StringBuilder buf, IAST data3D) {
if (data3D.isList()) {
boolean first = true;
IAST rgbColor = F.NIL;
IExpr opacity = F.NIL;
IAST list = data3D;
for (int i = 1; i < list.size(); i++) {
IExpr arg = list.get(i);
if (arg.isAST()) {
IAST ast = (IAST) arg;
if (ast.isList()) {
StringBuilder primitivesBuffer = new StringBuilder();
if (exportGraphics3DRecursive(primitivesBuffer, ast)) {
if (!first) {
buf.append(",");
}
first = false;
buf.append(primitivesBuffer);
}
} else if (ast.isRGBColor()) {
rgbColor = ast;
} else if (ast.isAST(S.Opacity, 2)) {
opacity = ast.arg1();
} else if (ast.head().isBuiltInSymbol()) {
IBuiltInSymbol symbol = (IBuiltInSymbol) ast.head();
IEvaluator evaluator = symbol.getEvaluator();
if (evaluator instanceof IGraphics3D) {
StringBuilder primitivesBuffer = new StringBuilder();
if (((IGraphics3D) evaluator).graphics3D(primitivesBuffer, ast, rgbColor, opacity)) {
if (!first) {
buf.append(",");
}
first = false;
buf.append(primitivesBuffer);
}
}
}
}
}
return true;
}
return false;
}
Aggregations