use of org.matheclipse.core.interfaces.IASTAppendable in project symja_android_library by axkr.
the class ImportString method evaluate.
@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
if (!(ast.arg1() instanceof IStringX)) {
return F.NIL;
}
String str1 = ((IStringX) ast.arg1()).toString();
Extension format = Extension.TXT;
if (ast.size() > 2) {
if (!(ast.arg2() instanceof IStringX)) {
return F.NIL;
}
format = Extension.importExtension(((IStringX) ast.arg2()).toString());
}
try {
switch(format) {
case JSON:
return JSONConvert.importJSON(str1);
case EXPRESSIONJSON:
return ExpressionJSONConvert.importExpressionJSON(str1);
case TABLE:
AST2Expr ast2Expr = new AST2Expr(engine.isRelaxedSyntax(), engine);
final Parser parser = new Parser(engine.isRelaxedSyntax(), true);
CSVFormat csvFormat = CSVFormat.RFC4180.withDelimiter(',');
Iterable<CSVRecord> records = csvFormat.parse(new StringReader(str1));
IASTAppendable rowList = F.ListAlloc(256);
for (CSVRecord record : records) {
IASTAppendable columnList = F.ListAlloc(record.size());
for (String string : record) {
final ASTNode node = parser.parse(string);
IExpr temp = ast2Expr.convert(node);
columnList.append(temp);
}
rowList.append(columnList);
}
return rowList;
case STRING:
return ofString(str1, engine);
case TXT:
return ofText(str1, engine);
default:
}
} catch (SyntaxError se) {
LOGGER.log(engine.getLogLevel(), "ImportString: syntax error!", se);
} catch (Exception ex) {
LOGGER.log(engine.getLogLevel(), "ImportString", ex);
}
return F.NIL;
}
use of org.matheclipse.core.interfaces.IASTAppendable in project symja_android_library by axkr.
the class Integrate method integratePolynomialByParts.
/**
* See <a href="http://en.wikipedia.org/wiki/Integration_by_parts">Wikipedia- Integration by
* parts</a>
*
* @param ast TODO - not used
* @param arg1
* @param symbol
* @return
*/
private static IExpr integratePolynomialByParts(IAST ast, final IAST arg1, IExpr symbol, EvalEngine engine) {
IASTAppendable fTimes = F.TimesAlloc(arg1.size());
IASTAppendable gTimes = F.TimesAlloc(arg1.size());
collectPolynomialTerms(arg1, symbol, gTimes, fTimes);
IExpr g = gTimes.oneIdentity1();
IExpr f = fTimes.oneIdentity1();
// only call integrateByParts for simple Times() expressions
if (f.isOne() || g.isOne()) {
return F.NIL;
}
return integrateByParts(f, g, symbol, engine);
}
use of org.matheclipse.core.interfaces.IASTAppendable in project symja_android_library by axkr.
the class Integrate method evaluate.
@Override
public IExpr evaluate(IAST holdallAST, EvalEngine engine) {
if (Config.JAS_NO_THREADS) {
// Android changed: call static initializer in evaluate() method.
new IntegrateInitializer().run();
} else {
// see #setUp() method
}
try {
// wait for initializer run is completed, no matter how many threads call evaluate() method
await();
} catch (InterruptedException ignored) {
}
IAssumptions oldAssumptions = engine.getAssumptions();
boolean numericMode = engine.isNumericMode();
try {
OptionArgs options = null;
if (holdallAST.size() > 3) {
options = new OptionArgs(S.Integrate, holdallAST, holdallAST.size() - 1, engine);
if (!options.isInvalidPosition()) {
holdallAST = holdallAST.most();
}
}
IExpr assumptionExpr = OptionArgs.determineAssumptions(holdallAST, -1, options);
if (assumptionExpr.isPresent() && assumptionExpr.isAST()) {
IAssumptions assumptions = org.matheclipse.core.eval.util.Assumptions.getInstance(assumptionExpr);
if (assumptions != null) {
engine.setAssumptions(assumptions);
}
}
boolean evaled = false;
IExpr result;
engine.setNumericMode(false);
if (holdallAST.size() < 3 || holdallAST.isEvalFlagOn(IAST.BUILT_IN_EVALED)) {
return F.NIL;
}
final IExpr arg1Holdall = holdallAST.arg1();
final IExpr a1 = NumberTheory.rationalize(arg1Holdall, false).orElse(arg1Holdall);
IExpr arg1 = engine.evaluateNIL(a1);
if (arg1.isPresent()) {
evaled = true;
} else {
arg1 = a1;
}
if (arg1.isIndeterminate()) {
return S.Indeterminate;
}
if (holdallAST.size() > 3) {
// Integrate[Integrate[fxy, y], x] ...
return holdallAST.foldRight((x, y) -> engine.evaluateNIL(F.Integrate(x, y)), arg1, 2);
}
IExpr arg2 = engine.evaluateNIL(holdallAST.arg2());
if (arg2.isPresent()) {
evaled = true;
} else {
arg2 = holdallAST.arg2();
}
if (arg2.isList()) {
IAST xList = (IAST) arg2;
if (xList.isVector() == 3) {
// Integrate[f[x], {x,a,b}]
IAST copy = holdallAST.setAtCopy(2, xList.arg1());
IExpr temp = engine.evaluate(copy);
if (temp.isFreeAST(S.Integrate)) {
return definiteIntegral(temp, xList, engine);
}
}
return F.NIL;
}
if (arg1.isList() && arg2.isSymbol()) {
return mapIntegrate((IAST) arg1, arg2);
}
final IASTAppendable ast = holdallAST.setAtClone(1, arg1);
ast.set(2, arg2);
final IExpr x = ast.arg2();
if (!x.isVariable()) {
// `1` is not a valid variable.
return IOFunctions.printMessage(ast.topHead(), "ivar", F.list(x), engine);
}
if (arg1.isNumber()) {
// Integrate[x_?NumberQ,y_Symbol] -> x*y
return Times(arg1, x);
}
if (arg1 instanceof ASTSeriesData) {
ASTSeriesData series = ((ASTSeriesData) arg1);
if (series.getX().equals(x)) {
final IExpr temp = ((ASTSeriesData) arg1).integrate(x);
if (temp != null) {
return temp;
}
}
return F.NIL;
}
if (arg1.isFree(x, true)) {
// Integrate[x_,y_Symbol] -> x*y /; FreeQ[x,y]
return Times(arg1, x);
}
if (arg1.equals(x)) {
// Integrate[x_,x_Symbol] -> x^2 / 2
return Times(F.C1D2, Power(arg1, F.C2));
}
boolean showSteps = false;
if (showSteps) {
LOGGER.info(arg1);
if (DEBUG_EXPR.contains(arg1)) {
// System.exit(-1);
}
DEBUG_EXPR.add(arg1);
}
if (arg1.isAST()) {
final IAST fx = (IAST) arg1;
if (fx.topHead().equals(x)) {
// issue #91
return F.NIL;
}
int[] dim = fx.isPiecewise();
if (dim != null) {
return integratePiecewise(dim, fx, ast);
}
result = integrateAbs(fx, x);
if (result.isPresent()) {
if (result == S.Undefined) {
return F.NIL;
}
return result;
}
result = integrateByRubiRules(fx, x, ast, engine);
if (result.isPresent()) {
IExpr temp = result.replaceAll(f -> {
if (f.isAST(UtilityFunctionCtors.Unintegrable, 3)) {
IAST integrate = F.Integrate(f.first(), f.second());
integrate.addEvalFlags(IAST.BUILT_IN_EVALED);
return integrate;
} else if (f.isAST(F.$rubi("CannotIntegrate"), 3)) {
IAST integrate = F.Integrate(f.first(), f.second());
integrate.addEvalFlags(IAST.BUILT_IN_EVALED);
return integrate;
}
return F.NIL;
});
return temp.orElse(result);
}
if (fx.isTimes()) {
IAST[] temp = fx.filter(arg -> arg.isFree(x));
IExpr free = temp[0].oneIdentity1();
if (!free.isOne()) {
IExpr rest = temp[1].oneIdentity1();
// Integrate[free_ * rest_,x_Symbol] -> free*Integrate[rest, x] /; FreeQ[free,x]
return Times(free, Integrate(rest, x));
}
}
if (fx.isPower()) {
// base ^ exponent
IExpr base = fx.base();
IExpr exponent = fx.exponent();
if (base.equals(x) && exponent.isFree(x)) {
if (exponent.isMinusOne()) {
// Integrate[ 1 / x_ , x_ ] -> Log[x]
return Log(x);
}
// Integrate[ x_ ^n_ , x_ ] -> x^(n+1)/(n+1) /; FreeQ[n, x]
IExpr temp = Plus(F.C1, exponent);
return Divide(Power(x, temp), temp);
}
if (exponent.equals(x) && base.isFree(x)) {
if (base.isE()) {
// E^x
return fx;
}
// a^x / Log(a)
return F.Divide(fx, F.Log(base));
}
}
result = callRestIntegrate(fx, x, engine);
if (result.isPresent()) {
return result;
}
}
return evaled ? ast : F.NIL;
} finally {
engine.setAssumptions(oldAssumptions);
engine.setNumericMode(numericMode);
}
}
use of org.matheclipse.core.interfaces.IASTAppendable in project symja_android_library by axkr.
the class Integrate method integratePiecewise.
private static IExpr integratePiecewise(int[] dim, final IAST piecewiseFunction, final IAST integrateFunction) {
IAST list = (IAST) piecewiseFunction.arg1();
if (list.size() > 1) {
IASTAppendable pwResult = F.ListAlloc(list.size());
for (int i = 1; i < list.size(); i++) {
IASTMutable integrate = integrateFunction.copy();
integrate.set(1, list.get(i).first());
pwResult.append(F.list(integrate, list.get(i).second()));
}
IASTMutable piecewise = piecewiseFunction.copy();
piecewise.set(1, pwResult);
if (piecewiseFunction.size() > 2) {
IASTMutable integrate = integrateFunction.copy();
integrate.set(1, piecewiseFunction.arg2());
piecewise.set(2, integrate);
}
return piecewise;
}
return F.NIL;
}
use of org.matheclipse.core.interfaces.IASTAppendable in project symja_android_library by axkr.
the class VisitorReplaceArgs method visit.
@Override
public IExpr visit(IASTMutable ast) {
IExpr temp;
IASTAppendable result = F.NIL;
int size = ast.size();
boolean evaled = false;
for (int i = 1; i < size; i++) {
evaled = false;
temp = ast.get(i);
for (int j = 1; i < astSlots.size(); i++) {
if (astSlots.get(j).equals(temp)) {
if (!result.isPresent()) {
result = ast.copyAppendable();
}
result.set(i, F.Slot(F.ZZ(j)));
evaled = true;
break;
}
}
if (!evaled) {
temp = temp.accept(this);
if (temp.isPresent()) {
if (!result.isPresent()) {
// something was evaluated - return a new IAST:
result = ast.copyAppendable();
}
result.set(i, temp);
}
}
}
return result;
}
Aggregations