use of org.matheclipse.core.convert.VariablesSet in project symja_android_library by axkr.
the class HornerForm method evaluate.
@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
Validate.checkRange(ast, 2, 3);
IExpr arg1 = ast.arg1();
if (arg1.isAST()) {
IAST poly = (IAST) arg1;
VariablesSet eVar;
IAST variables;
if (ast.isAST2()) {
variables = Validate.checkSymbolOrSymbolList(ast, 2);
} else {
eVar = new VariablesSet(ast.arg1());
variables = eVar.getVarList();
}
if (variables.size() >= 2) {
ISymbol sym = (ISymbol) variables.arg1();
if (poly.isASTSizeGE(F.Plus, 2)) {
HornerScheme scheme = new HornerScheme();
return scheme.generate(engine.isNumericMode(), poly, sym);
}
}
}
return arg1;
}
use of org.matheclipse.core.convert.VariablesSet in project symja_android_library by axkr.
the class NMinimize method numericEval.
@Override
public IExpr numericEval(final IAST ast, EvalEngine engine) {
Validate.checkSize(ast, 3);
if (ast.arg1().isList() && ast.arg2().isList()) {
IAST list1 = (IAST) ast.arg1();
IAST list2 = (IAST) ast.arg2();
VariablesSet vars = new VariablesSet(list2);
if (list1.isAST2()) {
IExpr function = list1.arg1();
IExpr listOfconstraints = list1.arg2();
if (listOfconstraints.isAnd()) {
// lc1 && lc2 && lc3...
LinearObjectiveFunction objectiveFunction = getObjectiveFunction(vars, function);
List<LinearConstraint> constraints = getConstraints(vars, listOfconstraints);
return simplexSolver(vars, objectiveFunction, objectiveFunction, new LinearConstraintSet(constraints), GoalType.MINIMIZE, new NonNegativeConstraint(true), PivotSelectionRule.BLAND);
}
}
}
return F.NIL;
}
use of org.matheclipse.core.convert.VariablesSet in project symja_android_library by axkr.
the class NRoots method evaluate.
@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
Validate.checkRange(ast, 2, 3);
IAST variables;
if (ast.size() == 2) {
VariablesSet eVar = new VariablesSet(ast.arg1());
if (!eVar.isSize(1)) {
// factor only possible for univariate polynomials
engine.printMessage("NRoots: factorization only possible for univariate polynomials");
return F.NIL;
}
variables = eVar.getVarList();
} else {
if (ast.arg2().isList()) {
variables = (IAST) ast.arg2();
} else {
variables = F.List(ast.arg2());
}
}
IExpr temp = roots(ast.arg1(), variables, engine);
if (!temp.isList()) {
return F.NIL;
}
IAST list = (IAST) temp;
IAST result = F.List();
for (int i = 1; i < list.size(); i++) {
result.append(engine.evalN(list.get(i)));
}
return result;
}
use of org.matheclipse.core.convert.VariablesSet in project symja_android_library by axkr.
the class QuineMcCluskyFormula method read.
public static QuineMcCluskyFormula read(IAST orAST) throws BooleanFunctionConversionException {
VariablesSet exVar = new VariablesSet(orAST);
IAST vars = exVar.getVarList();
if (vars.isAST0()) {
throw new BooleanFunctionConversionException();
}
ArrayList<QuineMcCluskyTerm> terms = QuineMcCluskyTerm.convertToTerms(orAST, vars);
return new QuineMcCluskyFormula(terms, vars);
}
use of org.matheclipse.core.convert.VariablesSet 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;
}
Aggregations