use of org.matheclipse.core.interfaces.IEvaluator in project symja_android_library by axkr.
the class EvalComplex method evalSymbol.
public static double[] evalSymbol(final ISymbol symbol) {
IExpr expr = symbol.assignedValue();
if (expr != null) {
// final IExpr expr = symbol.get();
if (expr instanceof ISignedNumber) {
final double[] result = new double[2];
result[0] = ((ISignedNumber) expr).doubleValue();
result[1] = 0.0;
return result;
}
if (expr instanceof ComplexNum) {
final double[] result = new double[2];
result[0] = ((ComplexNum) expr).getRealPart();
result[1] = ((ComplexNum) expr).getImaginaryPart();
return result;
}
}
if (symbol.isRealConstant()) {
// fast evaluation path
final double[] result = new double[2];
result[0] = ((ISignedNumberConstant) ((IBuiltInSymbol) symbol).getEvaluator()).evalReal();
result[1] = 0.0;
return result;
}
if (symbol.isBuiltInSymbol()) {
final IEvaluator module = ((IBuiltInSymbol) symbol).getEvaluator();
if (module instanceof INumericComplexConstant) {
// fast evaluation path
return ((INumericComplexConstant) module).evalComplex();
}
}
// slow evaluation path
final IExpr result = F.evaln(symbol);
if (result instanceof ComplexNum) {
final double[] res = new double[2];
res[0] = ((ComplexNum) result).getRealPart();
res[1] = ((ComplexNum) result).getImaginaryPart();
return res;
}
if (result instanceof Num) {
final double[] res = new double[2];
res[0] = ((Num) result).doubleValue();
res[1] = 0.0;
return res;
}
throw new UnsupportedOperationException();
}
use of org.matheclipse.core.interfaces.IEvaluator in project symja_android_library by axkr.
the class Pods method createResult.
public static ObjectNode createResult(String inputStr, int formats, boolean strictSymja) {
ObjectNode messageJSON = JSON_OBJECT_MAPPER.createObjectNode();
ObjectNode queryresult = JSON_OBJECT_MAPPER.createObjectNode();
messageJSON.putPOJO("queryresult", queryresult);
queryresult.put("success", "false");
queryresult.put("error", "false");
queryresult.put("numpods", 0);
queryresult.put("version", "0.1");
boolean error = false;
int numpods = 0;
IExpr inExpr = S.Null;
IExpr outExpr = S.Null;
EvalEngine engine = EvalEngine.get();
ArrayNode podsArray = JSON_OBJECT_MAPPER.createArrayNode();
if (strictSymja) {
engine.setPackageMode(false);
final ExprParser parser = new ExprParser(engine, true);
try {
inExpr = parser.parse(inputStr);
if (inExpr.isPresent()) {
long numberOfLeaves = inExpr.leafCount();
if (numberOfLeaves < Config.MAX_INPUT_LEAVES) {
outExpr = inExpr;
final StringWriter errorWriter = new StringWriter();
WriterOutputStream werrors = new WriterOutputStream(errorWriter);
PrintStream errors = new PrintStream(werrors);
IExpr firstEval = F.NIL;
try (ThreadLocalNotifierClosable c = setLogEventNotifier(errors)) {
engine.setErrorPrintStream(errors);
firstEval = engine.evaluateNIL(inExpr);
} finally {
engine.setErrorPrintStream(null);
}
addSymjaPod(podsArray, inExpr, inExpr, "Input", "Identity", formats, engine);
numpods++;
String errorString = "";
if (firstEval.isPresent()) {
outExpr = firstEval;
} else {
errorString = errorWriter.toString().trim();
}
outExpr = engine.evaluate(inExpr);
if (outExpr instanceof GraphExpr) {
String javaScriptStr = GraphFunctions.graphToJSForm((GraphExpr) outExpr);
if (javaScriptStr != null) {
String html = VISJS_IFRAME;
html = StringUtils.replace(html, "`1`", javaScriptStr);
html = //
StringUtils.replace(//
html, //
"`2`", //
" var options = { };\n");
// html = StringEscapeUtils.escapeHtml4(html);
int form = internFormat(SYMJA, "visjs");
addPod(podsArray, inExpr, outExpr, html, "Graph data", "Graph", form, engine);
numpods++;
} else {
addSymjaPod(podsArray, inExpr, outExpr, errorString, "Evaluated result", "Expression", formats, engine, true);
numpods++;
}
} else {
addSymjaPod(podsArray, inExpr, outExpr, errorString, "Evaluated result", "Expression", formats, engine, true);
numpods++;
}
resultStatistics(queryresult, error, numpods, podsArray);
return messageJSON;
}
}
} catch (SyntaxError serr) {
// this includes syntax errors
LOGGER.debug("Pods.createResult() failed", serr);
return errorJSON("0", serr.getMessage());
}
queryresult.put("error", error ? "true" : "false");
return messageJSON;
}
inExpr = parseInput(inputStr, engine);
if (inExpr.isPresent()) {
long numberOfLeaves = inExpr.leafCount();
if (numberOfLeaves < Config.MAX_INPUT_LEAVES) {
outExpr = inExpr;
final StringWriter errorWriter = new StringWriter();
WriterOutputStream werrors = new WriterOutputStream(errorWriter);
PrintStream errors = new PrintStream(werrors);
IExpr firstEval = F.NIL;
try (ThreadLocalNotifierClosable c = setLogEventNotifier(errors)) {
engine.setErrorPrintStream(errors);
firstEval = engine.evaluateNIL(inExpr);
} finally {
engine.setErrorPrintStream(null);
}
addSymjaPod(podsArray, inExpr, inExpr, "Input", "Identity", formats, engine);
numpods++;
String errorString = "";
if (firstEval.isPresent()) {
outExpr = firstEval;
} else {
errorString = errorWriter.toString().trim();
}
IExpr podOut = outExpr;
IExpr numExpr = F.NIL;
IExpr evaledNumExpr = F.NIL;
if (outExpr.isNumericFunction(true)) {
numExpr = inExpr.isAST(S.N) ? inExpr : F.N(inExpr);
evaledNumExpr = engine.evaluate(F.N(outExpr));
}
if (outExpr.isNumber() || outExpr.isQuantity()) {
if (outExpr.isInteger()) {
numpods += integerPods(podsArray, inExpr, (IInteger) outExpr, formats, engine);
resultStatistics(queryresult, error, numpods, podsArray);
return messageJSON;
} else {
podOut = outExpr;
if (outExpr.isRational()) {
addSymjaPod(podsArray, inExpr, podOut, "Exact result", "Rational", formats, engine);
numpods++;
}
if (//
numExpr.isPresent() && (evaledNumExpr.isInexactNumber() || evaledNumExpr.isQuantity())) {
addSymjaPod(podsArray, numExpr, evaledNumExpr, "Decimal form", "Numeric", formats, engine);
numpods++;
if (!outExpr.isRational()) {
if (evaledNumExpr.isInexactNumber()) {
inExpr = F.Rationalize(evaledNumExpr);
podOut = engine.evaluate(inExpr);
addSymjaPod(podsArray, inExpr, podOut, "Rational form", "Numeric", formats, engine);
numpods++;
}
}
}
if (outExpr.isFraction()) {
IFraction frac = (IFraction) outExpr;
if (!frac.integerPart().equals(F.C0)) {
inExpr = F.List(F.IntegerPart(outExpr), F.FractionalPart(outExpr));
podOut = engine.evaluate(inExpr);
String plaintext = podOut.first().toString() + " " + podOut.second().toString();
addSymjaPod(podsArray, inExpr, podOut, plaintext, "Mixed fraction", "Rational", formats, engine);
numpods++;
inExpr = F.ContinuedFraction(outExpr);
podOut = engine.evaluate(inExpr);
StringBuilder plainBuf = new StringBuilder();
if (podOut.isNonEmptyList()) {
IAST list = (IAST) podOut;
plainBuf.append('[');
plainBuf.append(list.arg1().toString());
plainBuf.append(';');
for (int i = 2; i < list.size(); i++) {
plainBuf.append(' ');
plainBuf.append(list.get(i).toString());
if (i < list.size() - 1) {
plainBuf.append(',');
}
}
plainBuf.append(']');
}
addSymjaPod(podsArray, inExpr, podOut, plainBuf.toString(), "Continued fraction", "ContinuedFraction", formats, engine);
numpods++;
}
}
resultStatistics(queryresult, error, numpods, podsArray);
return messageJSON;
}
} else {
if (outExpr.isAST(S.Plot, 2) && outExpr.first().isList()) {
outExpr = outExpr.first();
}
if (outExpr.isList()) {
IAST list = (IAST) outExpr;
ListPod listPod = new ListPod(list);
numpods += listPod.addJSON(podsArray, formats, engine);
}
if (//
numExpr.isPresent() && (evaledNumExpr.isInexactNumber() || evaledNumExpr.isQuantity())) {
addSymjaPod(podsArray, numExpr, evaledNumExpr, "Decimal form", "Numeric", formats, engine);
numpods++;
}
if (outExpr.isSymbol() || outExpr.isString()) {
String inputWord = outExpr.toString();
StringBuilder buf = new StringBuilder();
// }
if (outExpr.isSymbol() && Documentation.getMarkdown(buf, inputWord)) {
numpods += DocumentationPod.addDocumentationPod(new DocumentationPod((ISymbol) outExpr), podsArray, buf, formats);
resultStatistics(queryresult, error, numpods, podsArray);
return messageJSON;
} else {
if (outExpr.isString()) {
int mimeTyp = ((IStringX) outExpr).getMimeType();
if (mimeTyp == IStringX.APPLICATION_SYMJA || mimeTyp == IStringX.APPLICATION_JAVA || mimeTyp == IStringX.APPLICATION_JAVASCRIPT) {
String html = toHighligthedCode(outExpr.toString());
addSymjaPod(podsArray, inExpr, F.NIL, html, "Result", "String form", HTML, engine);
numpods++;
resultStatistics(queryresult, error, numpods, podsArray);
return messageJSON;
} else if (outExpr.isString()) {
podOut = outExpr;
addSymjaPod(podsArray, inExpr, podOut, "String form", "String", formats, engine);
numpods++;
}
}
ArrayList<IPod> soundsLike = listOfPods(inputWord);
if (soundsLike != null) {
boolean evaled = false;
for (int i = 0; i < soundsLike.size(); i++) {
IPod pod = soundsLike.get(i);
if (pod.keyWord().equalsIgnoreCase(inputWord)) {
int numberOfEntries = pod.addJSON(podsArray, formats, engine);
if (numberOfEntries > 0) {
numpods += numberOfEntries;
evaled = true;
break;
}
}
}
if (!evaled) {
for (int i = 0; i < soundsLike.size(); i++) {
IPod pod = soundsLike.get(i);
int numberOfEntries = pod.addJSON(podsArray, formats, engine);
if (numberOfEntries > 0) {
numpods += numberOfEntries;
}
}
}
resultStatistics(queryresult, error, numpods, podsArray);
return messageJSON;
}
}
} else {
if (inExpr.isAST(S.D, 2, 3)) {
if (inExpr.isAST1()) {
VariablesSet varSet = new VariablesSet(inExpr.first());
IAST variables = varSet.getVarList();
IASTAppendable result = ((IAST) inExpr).copyAppendable();
result.appendArgs(variables);
inExpr = result;
}
outExpr = engine.evaluate(inExpr);
podOut = outExpr;
addSymjaPod(podsArray, inExpr, podOut, "Derivative", "Derivative", formats, engine);
numpods++;
if (!outExpr.isFreeAST(x -> x.isTrigFunction())) {
inExpr = F.TrigToExp(outExpr);
podOut = engine.evaluate(inExpr);
// if (!S.PossibleZeroQ.ofQ(engine, F.Subtract(podOut, outExpr))) {
if (!podOut.equals(outExpr)) {
addSymjaPod(//
podsArray, inExpr, podOut, "Alternate form", "Simplification", formats, engine);
numpods++;
}
}
resultStatistics(queryresult, error, numpods, podsArray);
return messageJSON;
} else if (inExpr.isAST(S.Integrate, 2, 3)) {
if (inExpr.isAST1()) {
VariablesSet varSet = new VariablesSet(inExpr.first());
IAST variables = varSet.getVarList();
IASTAppendable result = ((IAST) inExpr).copyAppendable();
result.appendArgs(variables);
inExpr = result;
}
outExpr = engine.evaluate(inExpr);
podOut = outExpr;
addSymjaPod(podsArray, inExpr, podOut, "Integration", "Integral", formats, engine);
numpods++;
if (!outExpr.isFreeAST(x -> x.isTrigFunction())) {
inExpr = F.TrigToExp(outExpr);
podOut = engine.evaluate(inExpr);
if (!podOut.equals(outExpr)) {
addSymjaPod(podsArray, inExpr, podOut, "Alternate form", "Simplification", formats, engine);
numpods++;
}
}
resultStatistics(queryresult, error, numpods, podsArray);
return messageJSON;
} else if (inExpr.isAST(S.Solve, 2, 4)) {
if (inExpr.isAST1()) {
VariablesSet varSet = new VariablesSet(inExpr.first());
IAST variables = varSet.getVarList();
IASTAppendable result = ((IAST) inExpr).copyAppendable();
result.append(variables);
inExpr = result;
}
outExpr = engine.evaluate(inExpr);
podOut = outExpr;
addSymjaPod(podsArray, inExpr, podOut, "Solve equation", "Solver", formats, engine);
numpods++;
if (!outExpr.isFreeAST(x -> x.isTrigFunction())) {
inExpr = F.TrigToExp(outExpr);
podOut = engine.evaluate(inExpr);
// if (!S.PossibleZeroQ.ofQ(engine, F.Subtract(podOut, outExpr))) {
if (!podOut.equals(outExpr)) {
addSymjaPod(//
podsArray, inExpr, podOut, "Alternate form", "Simplification", formats, engine);
numpods++;
}
}
resultStatistics(queryresult, error, numpods, podsArray);
return messageJSON;
} else {
IExpr expr = inExpr;
if (outExpr.isAST(S.JSFormData, 3)) {
podOut = outExpr;
int form = internFormat(SYMJA, podOut.second().toString());
addPod(podsArray, inExpr, podOut, podOut.first().toString(), StringFunctions.inputForm(inExpr), "Function", "Plotter", form, engine);
numpods++;
} else if (outExpr instanceof GraphExpr) {
String javaScriptStr = GraphFunctions.graphToJSForm((GraphExpr) outExpr);
if (javaScriptStr != null) {
String html = VISJS_IFRAME;
html = StringUtils.replace(html, "`1`", javaScriptStr);
html = //
StringUtils.replace(//
html, //
"`2`", //
" var options = { };\n");
// html = StringEscapeUtils.escapeHtml4(html);
int form = internFormat(SYMJA, "visjs");
addPod(podsArray, inExpr, podOut, html, "Graph data", "Graph", form, engine);
numpods++;
}
} else {
IExpr head = outExpr.head();
if (head instanceof IBuiltInSymbol && outExpr.size() > 1) {
IEvaluator evaluator = ((IBuiltInSymbol) head).getEvaluator();
if (evaluator instanceof IDistribution) {
// if (evaluator instanceof IDiscreteDistribution) {
int snumpods = statisticsPods(podsArray, (IAST) outExpr, podOut, formats, engine);
numpods += snumpods;
}
}
VariablesSet varSet = new VariablesSet(outExpr);
IAST variables = varSet.getVarList();
if (outExpr.isBooleanFormula()) {
numpods += booleanPods(podsArray, outExpr, variables, formats, engine);
}
if (outExpr.isAST(S.Equal, 3)) {
IExpr arg1 = outExpr.first();
IExpr arg2 = outExpr.second();
if (//
arg1.isNumericFunction(varSet) && arg2.isNumericFunction(varSet)) {
if (variables.argSize() == 1) {
IExpr plot2D = F.Plot(F.List(arg1, arg2), F.List(variables.arg1(), F.num(-20), F.num(20)));
podOut = engine.evaluate(plot2D);
if (podOut.isAST(S.JSFormData, 3)) {
int form = internFormat(SYMJA, podOut.second().toString());
addPod(podsArray, inExpr, podOut, podOut.first().toString(), StringFunctions.inputForm(plot2D), "Function", "Plotter", form, engine);
numpods++;
}
}
if (!arg1.isZero() && !arg2.isZero()) {
inExpr = F.Equal(engine.evaluate(F.Subtract(arg1, arg2)), F.C0);
podOut = inExpr;
addSymjaPod(podsArray, inExpr, podOut, "Alternate form", "Simplification", formats, engine);
numpods++;
}
inExpr = F.Solve(F.binaryAST2(S.Equal, arg1, arg2), variables);
podOut = engine.evaluate(inExpr);
addSymjaPod(podsArray, inExpr, podOut, "Solution", "Reduce", formats, engine);
numpods++;
}
resultStatistics(queryresult, error, numpods, podsArray);
return messageJSON;
} else {
if (!inExpr.equals(outExpr)) {
addSymjaPod(podsArray, inExpr, outExpr, "Result", "Identity", formats, engine);
numpods++;
}
}
boolean isNumericFunction = outExpr.isNumericFunction(varSet);
if (isNumericFunction) {
if (variables.argSize() == 1) {
IExpr plot2D = F.Plot(outExpr, F.List(variables.arg1(), F.num(-7), F.num(7)));
podOut = engine.evaluate(plot2D);
if (podOut.isAST(S.JSFormData, 3)) {
int form = internFormat(SYMJA, podOut.second().toString());
addPod(podsArray, inExpr, podOut, podOut.first().toString(), StringFunctions.inputForm(plot2D), "Function", "Plotter", form, engine);
numpods++;
}
} else if (variables.argSize() == 2) {
IExpr plot3D = F.Plot3D(outExpr, F.List(variables.arg1(), F.num(-3.5), F.num(3.5)), F.List(variables.arg2(), F.num(-3.5), F.num(3.5)));
podOut = engine.evaluate(plot3D);
if (podOut.isAST(S.JSFormData, 3)) {
int form = internFormat(SYMJA, podOut.second().toString());
addPod(podsArray, inExpr, podOut, podOut.first().toString(), StringFunctions.inputForm(plot3D), "3D plot", "Plot", form, engine);
numpods++;
}
}
}
if (!outExpr.isFreeAST(x -> x.isTrigFunction())) {
inExpr = F.TrigToExp(outExpr);
podOut = engine.evaluate(inExpr);
// {
if (!podOut.equals(outExpr)) {
addSymjaPod(podsArray, inExpr, podOut, "Alternate form", "Simplification", formats, engine);
numpods++;
}
}
if (isNumericFunction && variables.argSize() == 1) {
if (outExpr.isPolynomial(variables) && !outExpr.isAtom()) {
inExpr = F.Factor(outExpr);
podOut = engine.evaluate(inExpr);
addSymjaPod(podsArray, inExpr, podOut, "Factor", "Polynomial", formats, engine);
numpods++;
IExpr x = variables.first();
inExpr = F.Minimize(outExpr, x);
podOut = engine.evaluate(inExpr);
if (podOut.isAST(S.List, 3) && podOut.first().isNumber() && podOut.second().isAST(S.List, 2)) {
IExpr rule = podOut.second().first();
if (rule.isRule()) {
StringBuilder buf = new StringBuilder();
buf.append("min{");
buf.append(outExpr.toString());
buf.append("} = ");
buf.append(podOut.first());
buf.append(" at ");
buf.append(rule.first().toString());
buf.append(" = ");
buf.append(rule.second().toString());
addSymjaPod(podsArray, inExpr, podOut, buf.toString(), "GlobalExtrema", "GlobalMinimum", formats, engine);
numpods++;
}
}
inExpr = F.Maximize(outExpr, x);
podOut = engine.evaluate(inExpr);
if (podOut.isAST(S.List, 3) && podOut.first().isNumber() && podOut.second().isAST(S.List, 2)) {
IExpr rule = podOut.second().first();
if (rule.isRule()) {
StringBuilder buf = new StringBuilder();
buf.append("max{");
buf.append(outExpr.toString());
buf.append("} = ");
buf.append(podOut.first());
buf.append(" at ");
buf.append(rule.first().toString());
buf.append(" = ");
buf.append(rule.second().toString());
addSymjaPod(podsArray, inExpr, podOut, buf.toString(), "GlobalExtrema", "GlobalMaximum", formats, engine);
numpods++;
}
}
}
inExpr = F.D(outExpr, variables.arg1());
podOut = engine.evaluate(inExpr);
addSymjaPod(podsArray, inExpr, podOut, "Derivative", "Derivative", formats, engine);
numpods++;
inExpr = F.Integrate(outExpr, variables.arg1());
podOut = engine.evaluate(inExpr);
addSymjaPod(podsArray, inExpr, podOut, "Indefinite integral", "Integral", formats, engine);
numpods++;
}
}
if (numpods == 1) {
// only Identity pod was appended
if (//
errorString.length() == 0 && !firstEval.isPresent()) {
addSymjaPod(podsArray, expr, outExpr, "Evaluated result", "Expression", formats, engine);
numpods++;
} else {
addSymjaPod(podsArray, expr, outExpr, errorString, "Evaluated result", "Expression", formats, engine, true);
numpods++;
}
}
resultStatistics(queryresult, error, numpods, podsArray);
return messageJSON;
}
}
}
if (numpods > 0) {
resultStatistics(queryresult, error, numpods, podsArray);
return messageJSON;
}
}
}
queryresult.put("error", error ? "true" : "false");
return messageJSON;
}
use of org.matheclipse.core.interfaces.IEvaluator in project symja_android_library by axkr.
the class GraphicsFunctions method exportGraphicsSVG.
public static boolean exportGraphicsSVG(StringBuilder buf, IAST data2D, Dimensions2D dim) {
if (data2D.isList()) {
boolean first = true;
IAST rgbColor = F.NIL;
IExpr opacity = F.num(0.75);
for (int i = 1; i < data2D.size(); i++) {
IExpr arg = data2D.get(i);
if (arg.isAST()) {
IAST primitive = (IAST) arg;
if (primitive.isAST(S.RGBColor, 4)) {
rgbColor = primitive;
continue;
}
if (primitive.isAST(S.Opacity, 2)) {
opacity = primitive.arg1();
continue;
}
if (primitive.head().isBuiltInSymbol()) {
IBuiltInSymbol symbol = (IBuiltInSymbol) primitive.head();
IEvaluator evaluator = symbol.getEvaluator();
if (evaluator instanceof IGraphics3D) {
if (!first) {
buf.append(",");
}
first = false;
if (!((IGraphics3D) evaluator).graphics2D(buf, primitive, dim, rgbColor, opacity)) {
return false;
}
continue;
}
}
}
}
return true;
}
return false;
}
use of org.matheclipse.core.interfaces.IEvaluator in project symja_android_library by axkr.
the class ExprEvaluatorTests method builtinFunctionFuzz.
/**
* Fuzz testing - automated software testing that involves providing random arguments as inputs
* for the Symja built-in functions.
*/
public static void builtinFunctionFuzz() {
Config.FILESYSTEM_ENABLED = false;
EvalEngine engine = new EvalEngine(true);
engine.setRecursionLimit(256);
engine.setIterationLimit(1000);
ExprEvaluator eval = new ExprEvaluator(engine, true, (short) 20);
byte[] bArray = new byte[0];
ByteArrayExpr ba = ByteArrayExpr.newInstance(bArray);
byte[] b0Array = new byte[] { 0 };
ByteArrayExpr b0a = ByteArrayExpr.newInstance(b0Array);
F.x.setAttributes(ISymbol.PROTECTED);
F.y.setAttributes(ISymbol.PROTECTED);
double[] doubleArr = new double[] { 1.0, -1.0, 0.0, 2.0, 100.0, 200.0 };
int[] dims = new int[] { 2, 3 };
NumericArrayExpr nae = new NumericArrayExpr(doubleArr, dims, NumericArrayExpr.Real64);
Config.MAX_AST_SIZE = 10000;
Config.MAX_OUTPUT_SIZE = 10000;
Config.MAX_INPUT_LEAVES = 100L;
Config.MAX_MATRIX_DIMENSION_SIZE = 100;
Config.MAX_PRECISION_APFLOAT = 100;
Config.MAX_BIT_LENGTH = 20000;
Config.MAX_POLYNOMIAL_DEGREE = 100;
IAST seedList = //
F.List(//
ba, //
b0a, //
nae, //
S.$Aborted, //
S.False, //
S.True, //
S.E, //
S.Pi, //
S.Indeterminate, //
F.Missing("test"), //
F.complex(-0.5, 0.5), //
F.complex(0.0, 0.5), //
F.complex(0.0, -1.0), //
F.complex(0.0, 1.0), //
F.complex(2.0, -1.0), //
F.complex(2.0, 1.0), //
F.complex(-2.0, -2.0), //
F.complex(-2.0, 2.0), //
F.complexNum("-0.8", "1.2", 30), //
F.num(0.5), //
F.num(-0.5), //
F.num(Math.PI * (-0.5)), //
F.num(Math.PI * 0.5), //
F.num(-Math.PI), //
F.num(Math.PI), //
F.num(-Math.E), //
F.num(Math.E), //
F.num("-0.8", 30), //
F.C0, //
F.C1, //
F.CN1, //
F.CN1D2, //
F.C1D2, //
F.CNI, //
F.CI, //
F.ZZ(42), //
F.CC(Long.MAX_VALUE, Long.MIN_VALUE, Long.MIN_VALUE, Long.MAX_VALUE), //
F.QQ(Long.MAX_VALUE, Long.MIN_VALUE), //
F.QQ(Long.MIN_VALUE, Long.MAX_VALUE), //
F.Slot2, // some primes
F.C2, F.C3, F.C5, F.C7, F.ZZ(11), F.ZZ(13), F.ZZ(17), F.ZZ(19), F.ZZ(101), F.ZZ(1009), //
F.ZZ(10007), //
F.CN2, //
F.CN3, //
F.CN5, //
F.CN7, //
F.ZZ(-11), //
F.ZZ(-13), //
F.ZZ(-17), //
F.ZZ(-19), //
F.ZZ(-101), //
F.ZZ(-1009), //
F.ZZ(-10007), //
F.ZZ(Integer.MIN_VALUE), //
F.ZZ(Integer.MAX_VALUE), //
F.ZZ(Byte.MIN_VALUE), //
F.ZZ(Byte.MAX_VALUE), //
F.CInfinity, //
F.CNInfinity, //
F.Null, //
F.Power(F.x, F.C2), //
F.Indeterminate, //
F.ComplexInfinity, //
F.x_, //
F.y_, // any sequence of one or more expressions
F.x__, // any sequence of one or more expressions
F.y__, // any sequence of zero or more expressions
F.x___, // any sequence of zero or more expressions
F.y___, //
F.CEmptyList, //
F.assoc(F.List(F.Rule(F.a, F.C0), F.RuleDelayed(F.b, F.C1))), //
F.assoc(F.List()), //
F.assoc(F.List(F.Rule(F.stringx("s1"), F.C0), F.RuleDelayed(F.stringx("s2"), F.C1))), F.assoc(F.List(F.Rule(F.stringx("s1"), F.assoc(F.List(F.Rule(F.a, F.C0), F.RuleDelayed(F.b, F.C1)))), F.RuleDelayed(F.stringx("s2"), //
F.assoc(F.List(F.Rule(F.a, F.C0), F.RuleDelayed(F.b, F.C1)))))), //
SparseArrayExpr.newDenseList(F.List(F.C0, F.C0), F.C0), //
SparseArrayExpr.newDenseList(F.List(F.C0, F.C1, F.C0, F.C2), F.C0), //
SparseArrayExpr.newDenseList(F.List(F.List(F.C0, F.C0), F.List(F.C0, F.C0)), F.C0), //
SparseArrayExpr.newDenseList(F.List(F.List(F.C1, F.C0), F.List(F.C0, F.C1)), F.C0), //
F.Function(F.EvenQ(F.Slot1)), //
F.Function(F.Expand(F.Power(F.Plus(F.C2, F.Slot1), F.C3))), //
S.Graph.of(F.List(F.Rule(F.C1, F.C2), F.Rule(F.C2, F.C3), F.Rule(F.C3, F.C1))), //
S.Graph.of(F.List()), S.Graph.of(F.List(F.Rule(F.C1, F.C2), F.Rule(F.C2, F.C3), F.Rule(F.C3, F.C1)), //
F.List(F.Rule(S.EdgeWeight, F.List(F.CD0, F.CD1, F.CD1)))), //
F.CEmptySequence, //
F.CEmptyList, //
F.List(F.List(F.C0)), //
F.List(F.List(F.C1)), //
F.List(F.List(F.CN1)), //
F.List(F.List(F.C1, F.C0), F.List(F.C0, F.C1)), //
F.List(F.List(F.C0, F.C0), F.List(F.C0, F.C0)), //
F.List(F.List(F.C1, F.C0), F.List(F.C0, F.C1), F.C0), //
F.List(F.List(F.C0, F.C0), F.List(F.C0, F.C0), F.C0), F.List(F.num("-3.1415", 30), F.num("2.987", 30), F.num("-1", 30), F.num("0.0", 30), //
F.num("1", 30)), //
F.List(F.CN1, F.CN2, F.C3), //
F.List(F.CN1D2, F.CN2, F.C3), //
F.List(F.x, F.CN2, F.C3), //
F.List(F.x, F.C5, F.CN3), //
F.List(F.x, F.CN3, F.CN1D2), //
F.List(F.x, F.CN1D2, F.C1D2, F.C1D4), //
F.List(F.C0, F.C0), //
F.List(F.C0, F.C0, F.C0), //
F.List(F.C1, F.C2, F.C3), //
F.List(F.C1, F.C1, F.C1), //
F.List(F.C1, F.C2, F.C3, F.a), //
F.List(F.C0, F.C0, F.C0, F.C0), //
F.List(F.C1, F.C1, F.C1, F.C1), //
F.List(F.x, F.CN1, F.C1, F.C1), //
F.List(F.x, F.C0, F.C0, F.C0), //
F.List(F.x, F.C1, F.CN1, F.CN1), //
F.List(F.CN1), //
F.List(F.C0), //
F.List(F.C1), // simulate level spec
F.List(F.CN5), // simulate level spec
F.List(F.C7), //
F.List(F.complex(0.0, -1.0)), //
F.List(F.complex(0.0, 1.0)), //
F.List(F.x), //
F.List(F.CN3D2), //
F.List(F.C3D2), //
F.List(F.C3D4), //
F.Part(F.x, F.C1), //
F.Part(F.x, F.C2), //
F.Part(F.x, F.ZZ(Integer.MAX_VALUE)), //
F.Part(F.x, F.CN1, F.C1, F.C1), //
F.Part(F.x, F.C1, F.C1, F.C1, F.C1), //
F.C1DSqrt5, // GoldenRatio
F.Divide(F.Plus(F.C1, F.Sqrt(5)), F.C2), // 1/GoldenRatio
F.Divide(F.C2, F.Plus(F.C1, F.Sqrt(5))), //
F.Negate(F.Sqrt(2)), //
F.Divide(F.Sqrt(2), F.C2), //
F.Negate(F.Divide(F.Sqrt(2), F.C2)), //
F.Plus(F.Sqrt(2), F.C1), //
F.Plus(F.Sqrt(2), F.CN1), //
F.Exp(F.Times(F.Pi, F.CI, F.C1D3)), //
F.Plus(F.C1, F.CI), //
F.Plus(F.CN1, F.CI), //
F.Times(F.Sqrt(2), F.C7), //
F.Times(F.Sqrt(2), F.Sqrt(5)), //
F.CSqrt2, //
F.C2Pi, //
F.CN3D2, //
F.C3D2, //
F.C3D4, //
F.QQ(Long.MAX_VALUE, 7L), //
F.QQ(Long.MIN_VALUE, 11L), //
F.QQ(7, Long.MAX_VALUE), //
F.QQ(11, Long.MAX_VALUE), //
F.QQ(Long.MAX_VALUE, Long.MAX_VALUE), //
F.QQ(Long.MIN_VALUE, Long.MAX_VALUE), //
F.Slot2, //
F.Slot(Integer.MAX_VALUE), //
IQuantity.of(1.2, "m"), //
F.RegularExpression("?i)"), //
F.CEmptyString, //
F.stringx("\\"), //
F.stringx("\r"), //
F.stringx("\t"), //
F.stringx("\n"), //
F.stringx("\r\n"), //
F.stringx("\n "), //
F.stringx("\uffff"), // division by zero problem
F.Power(F.C0, F.CN1), //
F.Subtract(F.C1, F.C1), //
F.Rule(S.Modulus, F.C2), //
F.Rule(S.Modulus, F.C10), //
F.Rule(S.Heads, S.True), //
F.Rule(S.Heads, S.False), //
F.$OptionsPattern(), //
F.OptionValue(F.a), //
F.OptionValue(F.b), //
F.OptionValue(F.x), F.OptionValue(F.y));
ThreadLocalRandom random = ThreadLocalRandom.current();
String[] functionStrs = AST2Expr.FUNCTION_STRINGS;
int[] counter = new int[] { 0 };
for (int loop = 0; loop < 20000; loop++) {
for (int i = 0; i < functionStrs.length; i++) {
IBuiltInSymbol sym = (IBuiltInSymbol) F.symbol(functionStrs[i]);
if (sym == S.PolynomialGCD || sym == S.TestReport || sym == S.VerificationTest || sym == S.On || sym == S.Off || sym == S.Compile || sym == S.CompiledFunction || sym == S.FactorialPower || sym == S.Pause || sym == S.Power || sym == S.OptimizeExpression || sym == S.Share || sym == S.Set || sym == S.SetDelayed || sym == S.UpSet || sym == S.UpSetDelayed) {
continue;
}
IEvaluator evaluator = sym.getEvaluator();
if (evaluator instanceof IFunctionEvaluator) {
int[] argSize = ((IFunctionEvaluator) evaluator).expectedArgSize(null);
if (argSize != null) {
int end = argSize[1];
if (end <= 10) {
int start = argSize[0];
if (start == 0) {
start = 1;
}
generateASTs(sym, start, end, seedList, random, counter, (IFunctionEvaluator) evaluator, engine, false, false);
generateASTs(sym, start, end, seedList, random, counter, (IFunctionEvaluator) evaluator, engine, true, false);
if (argSize.length > 2) {
generateASTs(sym, start, end, seedList, random, counter, (IFunctionEvaluator) evaluator, engine, false, true);
generateASTs(sym, start, end, seedList, random, counter, (IFunctionEvaluator) evaluator, engine, true, true);
}
continue;
} else {
int start = random.nextInt(argSize[0], 10);
generateASTs(sym, start, start + 4, seedList, random, counter, (IFunctionEvaluator) evaluator, engine, false, false);
generateASTs(sym, start, start + 4, seedList, random, counter, (IFunctionEvaluator) evaluator, engine, true, false);
if (argSize.length > 2) {
generateASTs(sym, start, start + 4, seedList, random, counter, (IFunctionEvaluator) evaluator, engine, false, true);
generateASTs(sym, start, start + 4, seedList, random, counter, (IFunctionEvaluator) evaluator, engine, true, true);
}
}
} else {
int start = random.nextInt(1, 7);
generateASTs(sym, start, start + 4, seedList, random, counter, (IFunctionEvaluator) evaluator, engine, false, false);
generateASTs(sym, start, start + 4, seedList, random, counter, (IFunctionEvaluator) evaluator, engine, false, true);
generateASTs(sym, start, start + 4, seedList, random, counter, (IFunctionEvaluator) evaluator, engine, true, false);
generateASTs(sym, start, start + 4, seedList, random, counter, (IFunctionEvaluator) evaluator, engine, true, true);
}
}
}
}
}
use of org.matheclipse.core.interfaces.IEvaluator in project symja_android_library by axkr.
the class ExprEvaluatorTests method nonBuiltinFunctionFuzz.
public static void nonBuiltinFunctionFuzz() {
Config.MAX_AST_SIZE = 10000;
Config.MAX_OUTPUT_SIZE = 10000;
Config.MAX_INPUT_LEAVES = 100L;
Config.MAX_MATRIX_DIMENSION_SIZE = 100;
Config.MAX_PRECISION_APFLOAT = 100;
Config.MAX_BIT_LENGTH = 200000;
Config.MAX_POLYNOMIAL_DEGREE = 100;
Config.FILESYSTEM_ENABLED = false;
EvalEngine engine = new EvalEngine(true);
engine.setRecursionLimit(256);
engine.setIterationLimit(1000);
ExprEvaluator eval = new ExprEvaluator(engine, true, (short) 20);
byte[] bArray = new byte[0];
ByteArrayExpr ba = ByteArrayExpr.newInstance(bArray);
byte[] b0Array = new byte[] { 0 };
ByteArrayExpr b0a = ByteArrayExpr.newInstance(b0Array);
F.x.setAttributes(ISymbol.PROTECTED);
F.y.setAttributes(ISymbol.PROTECTED);
IAST seedList = //
F.List(//
ba, //
b0a, //
F.complex(-0.5, 0.5), //
F.complex(0.0, 0.5), //
F.complex(0.0, -1.0), //
F.complex(0.0, 1.0), //
F.num(0.5), //
F.num(-0.5), //
F.num(Math.PI * (-0.5)), //
F.num(Math.PI * 0.5), //
F.num(-Math.PI), //
F.num(Math.PI), //
F.num(-Math.E), //
F.num(Math.E), //
F.C0, //
F.C1, //
F.CN1, //
F.CN1D2, //
F.C1D2, //
F.CNI, //
F.CI, //
F.CInfinity, //
F.CNInfinity, //
F.Null, //
F.Power(F.x, F.C2), //
F.Indeterminate, //
F.ComplexInfinity, //
F.x_, //
F.y_, //
F.CEmptyList, //
F.assoc(F.List(F.Rule(F.a, F.C0), F.RuleDelayed(F.b, F.C1))), //
F.assoc(F.List()), //
F.assoc(F.List(F.Rule(F.stringx("s1"), F.C0), F.RuleDelayed(F.stringx("s2"), F.C1))), //
SparseArrayExpr.newDenseList(F.List(F.C0, F.C0), F.C0), //
SparseArrayExpr.newDenseList(F.List(F.C0, F.C1, F.C0, F.C2), F.C0), //
SparseArrayExpr.newDenseList(F.List(F.List(F.C0, F.C0), F.List(F.C0, F.C0)), F.C0), //
SparseArrayExpr.newDenseList(F.List(F.List(F.C1, F.C0), F.List(F.C0, F.C1)), F.C0), //
F.List(F.List(F.C0)), //
F.List(F.List(F.C1)), //
F.List(F.List(F.CN1)), //
F.List(F.List(F.C1, F.C0), F.List(F.C0, F.C1)), //
F.List(F.List(F.C0, F.C0), F.List(F.C0, F.C0)), //
F.List(F.List(F.C1, F.C0), F.List(F.C0, F.C1), F.C0), //
F.List(F.List(F.C0, F.C0), F.List(F.C0, F.C0), F.C0), //
F.List(F.CN1, F.CN2, F.C3), //
F.List(F.CN1D2, F.CN2, F.C3), //
F.List(F.x, F.CN2, F.C3), //
F.List(F.x, F.C5, F.CN3), //
F.List(F.x, F.CN3, F.CN1D2), // simulate level spec
F.List(F.CN5), // simulate level spec
F.List(F.C7), //
F.C1DSqrt5, //
F.C2Pi, //
F.CN3D2, //
F.C3D2, //
F.C3D4, //
F.Slot2, //
F.stringx(""), //
F.stringx("\\"), //
F.stringx("\r"), //
F.stringx("\t"), //
F.stringx("\n"), //
F.stringx("\r\n"), //
F.stringx("\n "), //
F.stringx("\uffff"), F.Subtract(F.C1, F.C1));
String[] functionStrs = AST2Expr.FUNCTION_STRINGS;
ThreadLocalRandom random = ThreadLocalRandom.current();
int[] counter = new int[] { 0 };
for (int i = 0; i < functionStrs.length; i++) {
IBuiltInSymbol sym = (IBuiltInSymbol) F.symbol(functionStrs[i]);
IEvaluator evaluator = sym.getEvaluator();
if (evaluator instanceof IFunctionEvaluator) {
continue;
}
generateASTs(sym, 1, 5, seedList, random, counter, null, engine, false, false);
generateASTs(sym, 1, 5, seedList, random, counter, null, engine, false, true);
generateASTs(sym, 1, 5, seedList, random, counter, null, engine, true, false);
generateASTs(sym, 1, 5, seedList, random, counter, null, engine, true, true);
}
}
Aggregations