use of org.matheclipse.core.convert.AST2Expr in project symja_android_library by axkr.
the class PatternMatchingTestCase method comparePriority.
public void comparePriority(String patternString1, String patternString2, int result) {
try {
EvalEngine engine = EvalEngine.get();
ASTNode node = fParser.parse(patternString1);
IExpr pat1 = new AST2Expr(false, engine).convert(node);
node = fParser.parse(patternString1);
IExpr pat2 = new AST2Expr(false, engine).convert(node);
PatternMatcher matcher1 = new PatternMatcher(pat1);
PatternMatcher matcher2 = new PatternMatcher(pat2);
assertEquals(matcher1.equivalentTo(matcher2), result);
} catch (Exception e) {
e.printStackTrace();
assertEquals(Integer.MAX_VALUE, result);
}
}
use of org.matheclipse.core.convert.AST2Expr in project symja_android_library by axkr.
the class ConvertRubi method convert.
public static void convert(ASTNode node, StringBuffer buffer, boolean last, IASTAppendable listOfRules) {
try {
// convert ASTNode to an IExpr node
IExpr expr = new AST2Expr().convert(node);
if (expr.isAST(S.CompoundExpression, 3)) {
IAST compoundExpressionAST = (IAST) expr;
for (int i = 1; i < compoundExpressionAST.size(); i++) {
expr = compoundExpressionAST.get(i);
if (expr.isAST(S.SetDelayed, 3)) {
IAST ast = (IAST) expr;
appendSetDelayedToBuffer(ast, buffer, last, listOfRules);
} else if (expr.isAST(S.If, 4)) {
IAST ast = (IAST) expr;
if (ast.get(1).toString().equals("§showsteps")) {
expr = ast.get(3);
if (expr.isAST(S.SetDelayed, 3)) {
ast = (IAST) expr;
appendSetDelayedToBuffer(ast, buffer, last, listOfRules);
}
}
}
}
} else if (expr.isAST(S.SetDelayed, 3)) {
IAST ast = (IAST) expr;
appendSetDelayedToBuffer(ast, buffer, last, listOfRules);
} else if (expr.isAST(S.If, 4)) {
IAST ast = (IAST) expr;
if (ast.get(1).toString().equals("§showsteps")) {
expr = ast.get(3);
if (expr.isAST(S.SetDelayed, 3)) {
ast = (IAST) expr;
appendSetDelayedToBuffer(ast, buffer, last, listOfRules);
}
}
}
// }
} catch (UnsupportedOperationException uoe) {
System.out.println(uoe.getMessage());
System.out.println(node.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
use of org.matheclipse.core.convert.AST2Expr in project symja_android_library by axkr.
the class ConvertRubiUtilityFunctions method convert.
public static void convert(ASTNode node, StringBuffer buffer, boolean last, Set<String> functionSet, IASTAppendable listOfRules) {
try {
// convert ASTNode to an IExpr node
IExpr expr = new AST2Expr(false, EvalEngine.get()).convert(node);
if (expr.isAST(S.CompoundExpression)) {
IAST ast = (IAST) expr;
ast.forEach(x -> convertExpr(x, buffer, last, functionSet, listOfRules));
} else {
convertExpr(expr, buffer, last, functionSet, listOfRules);
}
} catch (UnsupportedOperationException uoe) {
System.out.println(uoe.getMessage());
System.out.println(node.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
use of org.matheclipse.core.convert.AST2Expr in project symja_android_library by axkr.
the class ExprEvaluatorTests method smartFuzz.
/**
* Fuzz testing - automated software testing that involves providing random arguments as inputs
* for the input expressions in file <code>./data/harvest.sym</code> harvested from existing JUnit
* tests of built-in functions.
*/
public static void smartFuzz() {
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;
boolean quietMode = true;
EvalEngine engine = new EvalEngine(true);
engine.setRecursionLimit(256);
engine.setIterationLimit(1000);
ExprEvaluator eval = new ExprEvaluator(engine, true, (short) 20);
List<ASTNode> node = parseFileToList();
IExpr temp;
OutputFormFactory fInputFactory = OutputFormFactory.get(true, false, 5, 7);
fInputFactory.setInputForm(true);
AST2Expr ast2Expr = new AST2Expr(engine.isRelaxedSyntax(), engine);
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);
IAST seedList = //
F.List(//
ba, //
b0a, //
nae, //
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.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.ZZ(Integer.MIN_VALUE), //
F.ZZ(Integer.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))), //
F.Graph(F.List(F.Rule(F.C1, F.C2), F.Rule(F.C2, F.C3), F.Rule(F.C3, F.C1))), //
F.Graph(F.List()), //
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();
SlowComputationThread thread = null;
for (int j = 1; j < 10000; j++) {
int i = 0;
while (i < node.size()) {
temp = ast2Expr.convert(node.get(i++));
if (temp.isAST() && temp.size() > 1) {
int seedIndex = random.nextInt(1, seedList.size());
IExpr seed = seedList.get(seedIndex);
String mutantStr = "initial";
IASTMutable mutant = ((IAST) temp).copy();
try {
ISymbol sym = mutant.topHead();
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.OptimizeExpression || sym == S.Share || sym == S.Set || sym == S.SetDelayed || sym == S.UpSet || sym == S.UpSetDelayed) {
continue;
}
int randomIndex = random.nextInt(1, mutant.size());
if (mutant.isAssociation()) {
mutant.set(randomIndex, F.Rule(F.ZZ(randomIndex), seed));
} else {
mutant.set(randomIndex, seed);
}
for (int k = 0; k < 1; k++) {
seedIndex = random.nextInt(1, seedList.size());
seed = seedList.get(seedIndex);
randomIndex = random.nextInt(1, mutant.size());
if (mutant.isAssociation()) {
mutant.set(randomIndex, F.Rule(F.ZZ(randomIndex), seed));
} else {
mutant.set(randomIndex, seed);
}
}
engine.init();
engine.setQuietMode(quietMode);
engine.setRecursionLimit(256);
engine.setIterationLimit(1000);
// mutantStr = fInputFactory.toString(mutant);
// System.out.println(">> " + mutantStr);
// if (counter++ > 80) {
// System.out.println("");
// counter = 0;
// System.out.flush();
// System.err.flush();
// }
thread = new SlowComputationThread(">> " + mutant.toString(), engine);
thread.start();
engine.evaluate(mutant);
} catch (FlowControlException mex) {
if (!quietMode) {
System.err.println(mutant.toString());
mex.printStackTrace();
System.err.println();
}
} catch (SyntaxError se) {
System.err.println(mutant.toString());
se.printStackTrace();
System.err.println();
// fail();
} catch (ValidateException ve) {
System.err.println(mutant.toString());
ve.printStackTrace();
System.err.println();
// fail();
} catch (MathException mex) {
System.err.println(mutant.toString());
mex.printStackTrace();
System.err.println();
fail();
} catch (RuntimeException rex) {
System.err.println(mutant.toString());
rex.printStackTrace();
fail();
} catch (Error rex) {
System.err.println(mutant.toString());
if (rex instanceof StackOverflowError) {
System.err.println("java.lang.StackOverflowError");
rex.printStackTrace();
fail();
} else {
System.err.println(mutantStr);
rex.printStackTrace();
fail();
}
} finally {
thread.terminate();
thread.interrupt();
}
}
}
}
// return result;
}
use of org.matheclipse.core.convert.AST2Expr in project symja_android_library by axkr.
the class TeXTests method testSmartFuzz.
public void testSmartFuzz() {
boolean quietMode = true;
EvalEngine engine = EvalEngine.get();
List<ASTNode> node = parseFileToList();
IExpr temp;
TeXFormFactory fTeXFactory = new TeXFormFactory();
OutputFormFactory fInputFactory = OutputFormFactory.get(true, false, 5, 7);
fInputFactory.setInputForm(true);
AST2Expr ast2Expr = new AST2Expr(engine.isRelaxedSyntax(), engine);
IAST seedList = //
F.List(//
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), //
S.True, //
S.False, //
F.assoc(F.CEmptyList), //
F.assoc(F.List(F.Rule(F.x, F.y))), //
F.CEmptyList, //
F.List(F.Rule(F.C1, F.C0)), //
F.List(F.Rule(F.x, F.CN1)), //
F.C0, //
F.C1, //
F.CN1, //
F.C2, //
F.CN2, //
F.CN10, //
F.CN1D2, //
F.C1D2, //
F.CNI, //
F.CI, //
F.CInfinity, //
F.CNInfinity, //
F.Null, //
F.Power(F.x, F.C2), //
F.ComplexInfinity, //
F.x_, //
F.y_, //
F.C1DSqrt5, //
F.Slot1, //
F.stringx(""), //
F.stringx("\uffff"), F.Subtract(F.C1, F.C1));
int counter = 0;
ThreadLocalRandom random = ThreadLocalRandom.current();
for (int j = 1; j < 10000; j++) {
int i = 0;
while (i < node.size()) {
temp = ast2Expr.convert(node.get(i++));
if (temp.isAST() && temp.size() > 1) {
final StringBuilder buf = new StringBuilder();
int seedIndex = random.nextInt(1, seedList.size());
IExpr seed = seedList.get(seedIndex);
IASTMutable mutant = ((IAST) temp).copy();
int randomIndex = random.nextInt(1, mutant.size());
mutant.set(randomIndex, seed);
for (int k = 0; k < 1; k++) {
seedIndex = random.nextInt(1, seedList.size());
seed = seedList.get(seedIndex);
randomIndex = random.nextInt(1, mutant.size());
mutant.set(randomIndex, seed);
}
engine.init();
engine.setQuietMode(quietMode);
engine.setRecursionLimit(256);
engine.setIterationLimit(1000);
final String mutantStr = fInputFactory.toString(mutant);
try {
// System.out.print(".");
if (counter++ > 80) {
// System.out.println("");
counter = 0;
System.out.flush();
System.err.flush();
}
// eval.eval(mutantStr);
fTeXFactory.convert(buf, mutant, 0);
System.out.println(buf.toString());
} catch (FlowControlException mex) {
if (!quietMode) {
System.err.println(mutantStr);
mex.printStackTrace();
System.err.println();
}
} catch (SyntaxError se) {
if (!quietMode) {
System.err.println(mutantStr);
se.printStackTrace();
System.err.println();
}
// fail();
} catch (MathException mex) {
System.err.println(mutantStr);
mex.printStackTrace();
System.err.println();
fail();
} catch (RuntimeException rex) {
System.err.println(mutantStr);
rex.printStackTrace();
fail();
} catch (Error rex) {
System.err.println(mutantStr);
if (rex instanceof StackOverflowError) {
System.err.println("java.lang.StackOverflowError");
rex.printStackTrace();
} else {
rex.printStackTrace();
fail();
}
}
}
}
}
// return result;
}
Aggregations