use of org.matheclipse.core.eval.util.OptionArgs in project symja_android_library by axkr.
the class AbstractMatrix1Expr method optionZeroTest.
public static Predicate<IExpr> optionZeroTest(final IAST ast, int start, EvalEngine engine) {
Predicate<IExpr> zeroChecker = POSSIBLE_ZEROQ_TEST;
if (ast.size() > 1) {
final OptionArgs options = new OptionArgs(ast.topHead(), ast, start, ast.size(), engine);
IExpr zeroTest = options.getOption(S.ZeroTest);
if (zeroTest.isPresent()) {
if (!zeroTest.equals(S.Automatic)) {
zeroChecker = Predicates.isTrue(engine, zeroTest);
}
}
}
return zeroChecker;
}
use of org.matheclipse.core.eval.util.OptionArgs in project symja_android_library by axkr.
the class GraphicsFunctions method graphicsToSVG.
public static void graphicsToSVG(IAST ast, StringBuilder buf) {
EvalEngine engine = EvalEngine.get();
IAST numericAST = (IAST) engine.evalN(ast);
Dimensions2D dim = new Dimensions2D(350, 350);
// set a default value
dim.color = RGBColor.BLUE;
if (numericAST.size() > 2) {
final OptionArgs options = new OptionArgs(numericAST.topHead(), numericAST, 2, engine);
IExpr option = options.getOption(S.PlotRange);
if (option.isListOfLists() && option.size() == 3) {
IAST list = (IAST) option;
dim.setPlotRange(list.getAST(1), list.getAST(2));
}
option = options.getOption(S.Axes);
if (option.isTrue()) {
dim.setAxes(true);
}
}
try {
int width = dim.width;
int height = dim.height;
if (ast.size() > 1) {
IExpr arg1 = ast.arg1();
if (!arg1.isList()) {
arg1 = F.list(arg1);
}
primitivesDimension((IAST) arg1, dim);
exportGraphicsSVG(buf, (IAST) arg1, dim);
}
if (dim.isAxes()) {
double xScale = width / (dim.xMax - dim.xMin);
double yScale = height / (dim.yMax - dim.yMin);
double x1 = 0;
// vertical axe
// + "0.000000,233.333333 6.666667,233.333333");
buf.append("<polyline points=\"");
buf.append(Show2SVG.FORMATTER.format((x1 - dim.xMin) * xScale));
buf.append(",");
buf.append(Show2SVG.FORMATTER.format(0.0));
buf.append(" ");
buf.append(Show2SVG.FORMATTER.format((x1 - dim.xMin) * xScale));
buf.append(",");
buf.append(Show2SVG.FORMATTER.format(height));
buf.append("\" style=\"stroke: rgb(0.000000%, 0.000000%, 0.000000%); stroke-opacity: 1; stroke-width: 0.666667px; fill: none\"/>\n");
// horizontals axe
double y1 = (-dim.yMin) * yScale;
buf.append("<polyline points=\"");
buf.append(Show2SVG.FORMATTER.format(0));
buf.append(",");
buf.append(Show2SVG.FORMATTER.format(y1));
buf.append(" ");
buf.append(Show2SVG.FORMATTER.format(width));
buf.append(",");
buf.append(Show2SVG.FORMATTER.format(y1));
buf.append("\" style=\"stroke: rgb(0.000000%, 0.000000%, 0.000000%); stroke-opacity: 1; stroke-width: 0.666667px; fill: none\"/>\n");
}
} finally {
}
}
use of org.matheclipse.core.eval.util.OptionArgs in project symja_android_library by axkr.
the class Show3D2ThreeJS method graphics3dToSVG.
/**
* A 3D Graphics command like
*
* <pre>
* Graphics3D(Polygon({{0,0,0}, {0,1,1}, {1,0,0}}))
* </pre>
*
* will be converted to:
*
* <pre>
* <graphics3d data="{"viewpoint": [1.3, -2.4, 2.0], "elements": [{"coords":......
* </pre>
*
* <p>
* It's a bit messy because of all the HTML escaping. What we are interested in is the data field.
* It's a JSON dict describing the 3D graphics in terms of graphics primitives. This JSON can be
* used in <a href="http://threejs.org/">threejs.org</a> to construct a 3D div.
*
* @param ast
* @param buf
* @throws IOException
* @deprecated
*/
@Deprecated
private static void graphics3dToSVG(IAST ast, StringBuilder buf) {
EvalEngine engine = EvalEngine.get();
IAST numericAST = (IAST) engine.evalN(ast);
double[] viewpoints = new double[] { 1.3, -2.4, 2.0 };
if (numericAST.size() > 2) {
final OptionArgs options = new OptionArgs(numericAST.topHead(), numericAST, 2, engine);
optionViewPoint(options, viewpoints);
}
int width = 400;
int height = 200;
Dimensions2D dim = new Dimensions2D(width, height);
buf.append("<graphics3d data=\"{");
StringBuilder builder = new StringBuilder(1024);
appendDoubleArray(builder, "viewpoint", viewpoints);
try {
for (int i = 1; i < numericAST.size(); i++) {
// } else
if (numericAST.get(i).isSameHeadSizeGE(S.Polygon, 2)) {
elements("polygon", numericAST.getAST(i), builder, dim);
} else if (numericAST.get(i).isSameHeadSizeGE(S.Point, 2)) {
elements("point", numericAST.getAST(i), builder, dim);
}
}
} finally {
builder.append("\"lighting\": [{\"color\": [0.3, 0.2, 0.4], \"type\": \"Ambient\"}, " + "{\"color\": [0.8, 0.0, 0.0], \"position\": [2.0, 0.0, 2.0], \"type\": \"Directional\"}, " + "{\"color\": [0.0, 0.8, 0.0], \"position\": [2.0, 2.0, 2.0], \"type\": \"Directional\"}, " + "{\"color\": [0.0, 0.0, 0.8], \"position\": [0.0, 2.0, 2.0], \"type\": \"Directional\"}], " + "\"axes\": {\"hasaxes\": [false, false, false], " + "\"ticks\": [[[0.0, 0.2, 0.4, 0.6000000000000001, 0.8, 1.0], [0.05, 0.1, 0.15000000000000002, 0.25, 0.30000000000000004, 0.35000000000000003, 0.45, 0.5, 0.55, 0.65, 0.7000000000000001, 0.75, 0.8500000000000001, 0.9, 0.9500000000000001], [\"0.0\", \"0.2\", \"0.4\", \"0.6\", \"0.8\", \"1.0\"]], [[0.0, 0.2, 0.4, 0.6000000000000001, 0.8, 1.0], [0.05, 0.1, 0.15000000000000002, 0.25, 0.30000000000000004, 0.35000000000000003, 0.45, 0.5, 0.55, 0.65, 0.7000000000000001, 0.75, 0.8500000000000001, 0.9, 0.9500000000000001], [\"0.0\", \"0.2\", \"0.4\", \"0.6\", \"0.8\", \"1.0\"]], [[0.0, 0.2, 0.4, 0.6000000000000001, 0.8, 1.0], [0.05, 0.1, 0.15000000000000002, 0.25, 0.30000000000000004, 0.35000000000000003, 0.45, 0.5, 0.55, 0.65, 0.7000000000000001, 0.75, 0.8500000000000001, 0.9, 0.9500000000000001], [\"0.0\", \"0.2\", \"0.4\", \"0.6\", \"0.8\", \"1.0\"]]]}, " + "\"extent\": {\"zmax\": 1.0, \"ymax\": 1.0, \"zmin\": 0.0, \"xmax\": 1.0, \"xmin\": 0.0, \"ymin\": 0.0}");
Escaper escaper = HtmlEscapers.htmlEscaper();
buf.append(escaper.escape(builder.toString()));
buf.append("}\" />");
}
}
use of org.matheclipse.core.eval.util.OptionArgs 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.eval.util.OptionArgs in project symja_android_library by axkr.
the class Interpolation method evaluate.
@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
int[] dims = ast.arg1().isMatrix();
if (dims != null && dims[0] > 2 && dims[1] >= 2) {
String method = "";
final OptionArgs options = new OptionArgs(ast.topHead(), ast, 2, engine);
IExpr option = options.getOption(S.Method);
if (option.isPresent()) {
method = option.toString();
}
if (!method.isEmpty()) {
// TODO: if ("Spline".equals(method)) {
if ("Hermite".equals(method)) {
return hermiteInterpolate((IAST) ast.arg1(), dims, engine);
}
return IOFunctions.printMessage(ast.topHead(), "optx", F.list(S.Method, ast), engine);
}
if (ast.isAST1()) {
if (dims[1] >= 2) {
int rowsSize = dims[0];
if (rowsSize >= 4) {
return piecewisePolynomialInterpolate(ast, rowsSize, engine);
}
}
return F.NIL;
}
}
return F.NIL;
}
Aggregations