use of org.matheclipse.core.interfaces.IASTMutable in project symja_android_library by axkr.
the class ConvertRubi method appendSetDelayedToBuffer.
// public static IExpr replaceTrig(IExpr expr) {
// // IAST astRules = F.List();
// // astRules.add(F.Rule(F.$s("F"), F.$s("§f")));
// // astRules.add(F.Rule(F.$s("G"), F.$s("§g")));
// // astRules.add(F.Rule(F.$s("H"), F.$s("§h")));
// // astRules.add(F.Rule(F.$s("sin"), F.$s("§sin")));
// // astRules.add(F.Rule(F.$s("cos"), F.$s("§cos")));
// // astRules.add(F.Rule(F.$s("tan"), F.$s("§tan")));
// // astRules.add(F.Rule(F.$s("cot"), F.$s("§cot")));
// // astRules.add(F.Rule(F.$s("csc"), F.$s("§csc")));
// // astRules.add(F.Rule(F.$s("sec"), F.$s("§sec")));
// // IExpr temp = expr.replaceAll(astRules);
// // if (temp.isPresent()) {
// // return temp;
// // }
// return expr;
// }
public static void appendSetDelayedToBuffer(IAST ast, StringBuffer buffer, boolean last, IASTAppendable listOfRules) {
if (ast.get(1).topHead().toString().equalsIgnoreCase("Int")) {
IAST integrate = (IAST) ast.get(1);
if (integrate.get(1).isPlus()) {
System.out.println(ast.toString());
return;
}
}
IExpr leftHandSide = ast.get(1);
IExpr rightHandSide = ast.arg2();
// if (!rightHandSide.isFree(x -> x.topHead().toString().equalsIgnoreCase("unintegrable"),
// true)) {
// System.out.println("IGNORED: " + ast.toString());
// } else {
GLOBAL_COUNTER++;
// System.out.println(leftHandSide.toString());
if (ast.get(1).isAST()) {
// leftHandSide = PatternMatcher.evalLeftHandSide((IAST) leftHandSide, EvalEngine.get());
IExpr temp = leftHandSide;
if (leftHandSide.topHead().toString().equalsIgnoreCase("int")) {
if (temp.size() == 3) {
((IASTMutable) temp).set(0, S.Integrate);
}
buffer.append("IIntegrate(" + GLOBAL_COUNTER + ",");
} else {
try {
leftHandSide = EvalEngine.get().evalHoldPattern((IAST) temp);
} catch (Exception ex) {
System.out.println("GLOBAL_COUNTER: " + GLOBAL_COUNTER);
ex.printStackTrace();
leftHandSide = temp;
}
buffer.append("ISetDelayed(" + GLOBAL_COUNTER + ",");
}
} else {
buffer.append("ISetDelayed(" + GLOBAL_COUNTER + ",");
}
buffer.append(leftHandSide.internalFormString(true, 0));
buffer.append(",\n ");
// if (rightHandSide.topHead().toString().equalsIgnoreCase("CompoundExpression")) {
// System.out.println(rightHandSide.toString());
// }
ISymbol s = F.symbol("Int");
// F.$s("AbortRubi")));
IExpr temp = ast.arg2().replaceAll(F.Rule(s, S.Integrate));
if (temp.isPresent()) {
rightHandSide = temp;
}
// for Rubi patternExpression must be set to true in internalFormString() for right-hand-side
buffer.append(rightHandSide.internalFormString(true, 0));
if (last) {
buffer.append(")\n");
} else {
buffer.append("),\n");
}
// }
listOfRules.append(F.List(leftHandSide, rightHandSide));
}
use of org.matheclipse.core.interfaces.IASTMutable in project symja_android_library by axkr.
the class RulePreprocessor method convertToMatcher.
private static void convertToMatcher(IExpr expr, String rulePostfix, final PrintWriter out, String symbolName, EvalEngine engine) {
boolean last;
StringBuilder buffer = new StringBuilder();
StringBuilder symbolBuffer = new StringBuilder();
// ArraySet<ISymbol> headerSymbols = new ArraySet<ISymbol>();
if (expr.isAST()) {
IAST list = (IAST) expr;
for (int i = 1; i < list.size(); i++) {
last = i == (list.argSize());
expr = list.get(i);
if (expr.isSymbol()) {
String name = expr.toString();
symbolBuffer.append("\n public final static ISymbol " + name + " = Dummy(\"" + name + "\");\n");
} else if (expr.isAST(S.SetDelayed, 3)) {
IASTMutable ast = ((IAST) expr).copy();
if (ast.arg1().isAST()) {
ast.set(1, engine.evalHoldPattern((IAST) ast.arg1()));
}
buffer.append(" // " + ast.toString().replaceAll("\\n", "") + "\n");
buffer.append("matcher.caseOf(");
appendSetDelayedToMatcher(ast, buffer, false, last);
} else if (expr.isAST(S.Set, 3)) {
IASTMutable ast = ((IAST) expr).copy();
if (ast.arg1().isAST()) {
ast.set(1, engine.evalHoldPattern((IAST) ast.arg1()));
}
buffer.append(" // " + ast.toString().replaceAll("\\n", "") + "\n");
buffer.append("matcher.caseOf(");
appendSetDelayedToMatcher(ast, buffer, true, last);
} else if (expr.isAST(S.Rule, 3)) {
throw new UnsupportedOperationException();
}
}
} else {
if (expr.isSymbol()) {
String name = expr.toString();
symbolBuffer.append("\n public final static ISymbol " + name + " = Dummy(\"" + name + "\");\n");
} else if (expr.isAST(S.SetDelayed, 3)) {
IAST ast = (IAST) expr;
buffer.append("matcher.caseOf(");
appendSetDelayedToMatcher(ast, buffer, false, true);
} else if (expr.isAST(S.Set, 3)) {
IAST ast = (IAST) expr;
buffer.append("matcher.caseOf(");
appendSetDelayedToMatcher(ast, buffer, true, true);
} else if (expr.isAST(S.Rule, 3)) {
throw new UnsupportedOperationException();
}
}
out.print(symbolBuffer.toString());
out.print("public static Matcher init");
out.print(rulePostfix);
out.print("() {\n");
out.print(" Matcher matcher = new Matcher();");
out.print(buffer.toString());
out.print("return matcher;\n");
out.print("}\n");
}
use of org.matheclipse.core.interfaces.IASTMutable in project symja_android_library by axkr.
the class D method addDerivative.
private static IAST addDerivative(final int pos, IAST deriveHead, final IExpr header, final IAST args) {
IASTMutable derivativeHead1 = deriveHead.copyAppendable();
for (int i = 1; i < derivativeHead1.size(); i++) {
if (i == pos) {
derivativeHead1.set(i, derivativeHead1.get(i).inc());
}
}
IASTAppendable derivativeHead2 = F.ast(derivativeHead1);
derivativeHead2.append(header);
IASTAppendable derivativeAST = F.ast(derivativeHead2, args.size());
derivativeAST.appendArgs(args.size(), i -> args.get(i));
return derivativeAST;
}
use of org.matheclipse.core.interfaces.IASTMutable in project symja_android_library by axkr.
the class ExprPolynomial method coefficientList.
/**
* Get the coefficients of a univariate polynomial up to n degree. Throws WrongNumberOfArguments
* if the polynomial is not univariate.
*
* @return the coefficients of a univariate polynomial up to n degree
*/
public IAST coefficientList() {
final int argsSize = ring.getVars().size() - 1;
if (argsSize == 1) {
long exp;
if (ring.tord.getEvord() == ExprTermOrder.IGRLEX || ring.tord.getEvord() == ExprTermOrder.REVILEX) {
long lastDegree = degree();
IExpr[] exprs = new IExpr[(int) lastDegree + 1];
for (int i = 0; i < exprs.length; i++) {
exprs[i] = F.C0;
}
for (Map.Entry<ExpVectorLong, IExpr> entry : val.entrySet()) {
exp = entry.getKey().getVal(0);
exprs[(int) exp] = entry.getValue();
}
return F.function(S.List, exprs);
} else {
long lastDegree = 0L;
IASTAppendable result = F.ListAlloc(val.size());
for (Map.Entry<ExpVectorLong, IExpr> entry : val.entrySet()) {
exp = entry.getKey().getVal(0);
while (lastDegree < exp) {
result.append(F.C0);
lastDegree++;
}
if (lastDegree == exp) {
result.append(entry.getValue());
lastDegree++;
}
}
return result;
}
} else if (argsSize > 1) {
long exp;
int[] arr = new int[argsSize];
for (int j = 0; j < argsSize; j++) {
arr[j] = (int) degree(j) + 1;
}
IASTMutable constantArray = F.C0.constantArray(S.List, 0, arr);
for (ExpVectorLong expArray : val.keySet()) {
int[] positions = new int[argsSize];
for (int i = 0; i < expArray.length(); i++) {
exp = expArray.getVal(i);
positions[expArray.varIndex(i)] = (int) exp + 1;
}
constantArray.setPart(val.get(expArray), positions);
}
return constantArray;
}
return F.NIL;
}
use of org.matheclipse.core.interfaces.IASTMutable 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;
}
Aggregations