use of org.matheclipse.core.interfaces.IBuiltInSymbol 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.IBuiltInSymbol 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();
return engine.evalAttributes(symbol, this).orElseGet(() -> engine.evalRules(symbol, this));
}
use of org.matheclipse.core.interfaces.IBuiltInSymbol in project symja_android_library by axkr.
the class AbstractAST method isListOfEdges.
/**
* {@inheritDoc}
*/
@Override
public GraphType isListOfEdges() {
if (head().equals(S.List)) {
boolean directed = true;
for (int i = 1; i < size(); i++) {
IExpr temp = get(i);
if (temp.isAST2() && temp.head().isBuiltInSymbol()) {
IBuiltInSymbol symbol = (IBuiltInSymbol) temp.head();
if (symbol == S.DirectedEdge || symbol == S.Rule) {
continue;
}
if (!(symbol == S.UndirectedEdge || symbol == S.TwoWayRule)) {
// the row is no list of edges
return null;
}
directed = false;
} else {
return null;
}
}
Builder builder = new DefaultGraphType.Builder();
if (directed) {
return builder.directed().build();
}
return builder.undirected().build();
}
return null;
}
use of org.matheclipse.core.interfaces.IBuiltInSymbol in project symja_android_library by axkr.
the class KryoUtil method main.
public static void main(String[] args) throws Exception {
// List<Class<?>> asList = Arrays.asList(B3.class.getDeclaredClasses());
// for (int i = 0; i < asList.size(); i++) {
// System.out.println(
// "kryo.register(B3." + asList.get(i).getSimpleName() + ".class);");
// }
// List<Class<?>> asList = Arrays.asList(UtilityFunctionCtors.class.getDeclaredClasses());
// for (int i = 0; i < asList.size(); i++) {
// System.out.println("kryo.register(Class.forName(\"" + asList.get(i).toString().substring(6)
// + "\"), new IASTSerializer());");
// }
F.initSymbols();
Kryo kryo = initKryo();
// IBuiltInSymbol object = S.Im;
// IAST object = F.Sin(F.x);
// IAST object =F.ternaryAST3(F.f, F.x, F.Dummy("$dummy"), F.stringx("string"));
Integrate.CONST.await();
Context rubiContext = Context.RUBI;
Output rubiOutput = new Output(new FileOutputStream("rubi_context.bin"));
kryo.writeClassAndObject(rubiOutput, rubiContext);
rubiOutput.close();
Input rubInput = new Input(new FileInputStream("rubi_context.bin"));
rubiContext = (Context) kryo.readClassAndObject(rubInput);
System.out.println(rubiContext.toString());
rubInput.close();
IBuiltInSymbol symbol = S.Integrate;
RulesData rulesData = symbol.getRulesData();
AbstractVisitor visitor = Share.createVisitor();
rulesData.accept(visitor);
Output output = new Output(new FileOutputStream("integrate.bin"));
kryo.writeClassAndObject(output, rulesData);
output.close();
Input input = new Input(new FileInputStream("integrate.bin"));
RulesData object2 = (RulesData) kryo.readClassAndObject(input);
System.out.println(object2.definition());
input.close();
}
use of org.matheclipse.core.interfaces.IBuiltInSymbol in project symja_android_library by axkr.
the class S method initFinalSymbol.
/**
* Convert the symbolName to lowercase (if <code>Config.PARSER_USE_LOWERCASE_SYMBOLS</code> is
* set) and insert a new Symbol in the <code>PREDEFINED_SYMBOLS_MAP</code>. The symbol is created
* using the given upper case string to use it as associated class name in package
* org.matheclipse.core.reflection.system.
*
* @param symbolName the predefined symbol name in upper-case form
* @param ordinal
* @return
*/
public static IBuiltInSymbol initFinalSymbol(final String symbolName, int ordinal) {
String str;
if (ParserConfig.PARSER_USE_LOWERCASE_SYMBOLS) {
str = (symbolName.length() == 1) ? symbolName : symbolName.toLowerCase();
} else {
str = symbolName;
}
final IBuiltInSymbol temp = new BuiltInSymbol(str, ordinal);
BUILT_IN_SYMBOLS[ordinal] = temp;
org.matheclipse.core.expression.Context.SYSTEM.put(str, temp);
GLOBAL_IDS_MAP.put(temp, (short) ordinal);
return temp;
}
Aggregations