Search in sources :

Example 11 with OptionArgs

use of org.matheclipse.core.eval.util.OptionArgs in project symja_android_library by axkr.

the class ListLinePlot3D method evaluate.

@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
    IOFunctions.printExperimental(S.ListLinePlot3D);
    if (ast.argSize() > 0) {
        IAST plotStyle = F.NIL;
        if (ast.argSize() > 1) {
            final OptionArgs options = new OptionArgs(ast.topHead(), ast, 2, engine);
            if (options.isInvalidPosition(1)) {
                return options.printNonopt(ast, 1, engine);
            }
            IExpr temp = options.getOption(S.PlotStyle);
            if (temp.isAST()) {
                plotStyle = (IAST) temp;
            }
        }
        // e.g.: ListLinePlot3D[{1, 2, 3, 4, 5}]
        if (ast.arg1().isASTSizeGE(S.List, 2)) {
            try {
                double d = ((IAST) ast.arg1()).arg1().evalDouble();
                IExpr heightLinePlot = heightLinePlot(F.list(ast.arg1()), plotStyle, engine);
                if (heightLinePlot.isPresent()) {
                    IASTAppendable result = F.Graphics3D(heightLinePlot);
                    if (ast.argSize() > 1) {
                        // add same options to Graphics3D
                        result.appendAll(ast, 2, ast.size());
                    }
                    return result;
                }
                return F.NIL;
            } catch (ArgumentTypeException ate) {
            // fall through
            }
        }
        // try if arg1 is a matrix
        int[] dimension = ast.arg1().isMatrix(false);
        // e.g.: ListLinePlot3D[{{x_1, y_1, z_1}, {x_2, y_2, z_2}}]
        if (dimension != null && dimension.length == 2 && dimension[1] == 3) {
            IASTAppendable result = F.Graphics3D(coordinateLinePlot(F.list(ast.arg1()), plotStyle, engine));
            if (ast.argSize() > 1) {
                // add same options to Graphics3D
                result.appendAll(ast, 2, ast.size());
            }
            return result;
        }
        // e.g.: ListLinePlot3D[{{1, 2, 3, 4}, {-1, -2, -3, -4}}]
        if (ast.arg1().isASTSizeGE(S.List, 2) && ((IAST) ast.arg1()).arg1().isASTSizeGE(S.List, 2)) {
            try {
                double d = ((IAST) ((IAST) ast.arg1()).arg1()).arg1().evalDouble();
                IExpr heightLinePlot = heightLinePlot((IAST) ast.arg1(), plotStyle, engine);
                if (heightLinePlot.isPresent()) {
                    IASTAppendable result = F.Graphics3D(heightLinePlot);
                    if (ast.argSize() > 1) {
                        // add same options to Graphics3D
                        result.appendAll(ast, 2, ast.size());
                    }
                    return result;
                }
            } catch (ArgumentTypeException ate) {
            // fall through
            }
        }
        if (ast.arg1().isASTSizeGE(S.List, 2)) {
            dimension = ((IAST) ast.arg1()).arg1().isMatrix(false);
            // e.g.: ListLinePlot3D[{{coord1, coord2}, {coord3, coord4}}]
            if (dimension != null && dimension.length == 2 && dimension[1] == 3) {
                IASTAppendable result = F.Graphics3D(coordinateLinePlot((IAST) ast.arg1(), plotStyle, engine));
                if (ast.argSize() > 1) {
                    // add same options to Graphics3D
                    result.appendAll(ast, 2, ast.size());
                }
                return result;
            }
        }
    }
    // `1` is not a valid dataset or a list of datasets.
    return IOFunctions.printMessage(ast.topHead(), "ldata", F.list(ast.arg1()), engine);
}
Also used : IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) OptionArgs(org.matheclipse.core.eval.util.OptionArgs) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr) ArgumentTypeException(org.matheclipse.core.eval.exception.ArgumentTypeException)

Example 12 with OptionArgs

use of org.matheclipse.core.eval.util.OptionArgs in project symja_android_library by axkr.

the class ListPlot3D method evaluate.

@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
    IOFunctions.printExperimental(S.ListPlot3D);
    if (ast.argSize() > 0) {
        if (ast.argSize() > 1) {
            final OptionArgs options = new OptionArgs(ast.topHead(), ast, 2, engine);
            if (options.isInvalidPosition(1)) {
                return options.printNonopt(ast, 1, engine);
            }
        // IExpr colorFunction = options.getOption(S.ColorFunction);
        // if (colorFunction.isPresent()) {
        // // ... color function is set...
        // }
        }
        // }
        int[] dimension = ast.arg1().isMatrix(false);
        if (dimension != null && dimension.length == 2) {
            // convert possible sparse array expression:
            IAST values = (IAST) ast.arg1().normal(false);
            if (dimension[0] == 3 && dimension[1] == 3) {
                // Draw a triangle with x, y and z coordinates.
                IAST polygons = F.Polygon(F.list(// 
                F.list(((IAST) values.arg1()).arg1(), ((IAST) values.arg2()).arg1(), ((IAST) values.arg3()).arg1()), // 
                F.list(((IAST) values.arg1()).arg2(), ((IAST) values.arg2()).arg2(), ((IAST) values.arg3()).arg2()), // 
                F.list(((IAST) values.arg1()).arg3(), ((IAST) values.arg2()).arg3(), ((IAST) values.arg3()).arg3())));
                IASTAppendable result = F.Graphics3D(polygons);
                if (ast.argSize() > 1) {
                    // add same options to Graphics3D
                    result.appendAll(ast, 2, ast.size());
                }
                return result;
            } else if (dimension[1] == 4) {
                // Draw polygons given just the z value in a grid of size
                // (heights.argSize() - 1) * (heights.arg1().argSize() - 1)
                // 2 polygons per square
                IASTAppendable polygonList = F.ListAlloc((values.argSize() - 1) * (values.arg1().argSize() - 1) * 2);
                double minHeight = (((IAST) values.arg1()).arg1()).evalDouble();
                double maxHeight = minHeight;
                ArrayList<double[]> heightRows = new ArrayList<double[]>();
                for (int i = 1; i <= values.argSize(); i++) {
                    IAST row = ((IAST) values.get(i));
                    try {
                        heightRows.add(new double[0]);
                        double[] heights = new double[row.argSize()];
                        for (int j = 1; j <= row.argSize(); j++) {
                            // evalDouble may throw ArgumentTypeException
                            heights[j - 1] = row.get(j).evalDouble();
                            double height = heights[j - 1];
                            if (height < minHeight)
                                minHeight = height;
                            if (height > maxHeight)
                                maxHeight = height;
                        }
                        heightRows.set(i - 1, heights);
                    } catch (ArgumentTypeException ate) {
                    // ignore this row
                    }
                }
                // deltaHeight is used to normalize the heights.
                double deltaHeight = maxHeight - minHeight;
                // As i and j start at 0, the last element is ignored (as it should be).
                for (int i = 0; i < heightRows.size() - 1; i++) {
                    double[] heights = heightRows.get(i);
                    double[] nextHeights = heightRows.get(i + 1);
                    if (heights.length > 0 && nextHeights.length == heights.length) {
                        for (int j = 0; j < heights.length - 1; j++) {
                            polygonList.append(F.Polygon(// 
                            F.list(F.list(F.num(i + 1), F.num(j + 1), F.num(heights[j] / deltaHeight)), F.list(F.num(i + 2), F.num(j + 2), F.num(nextHeights[j + 1] / deltaHeight)), F.list(F.num(i + 1), F.num(j + 2), F.num(heights[j + 1] / deltaHeight)))));
                            polygonList.append(F.Polygon(F.list(F.list(F.num(i + 1), F.num(j + 1), F.num(heights[j] / deltaHeight)), F.list(F.num(i + 2), F.num(j + 2), F.num(nextHeights[j + 1] / deltaHeight)), F.list(F.num(i + 2), F.num(j + 1), F.num(nextHeights[j] / deltaHeight)))));
                        }
                    }
                }
                IASTAppendable result = F.Graphics3D(polygonList);
                if (ast.argSize() > 1) {
                    // add same options to Graphics3D
                    result.appendAll(ast, 2, ast.size());
                }
                return result;
            }
        }
    }
    return F.NIL;
}
Also used : IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) ArrayList(java.util.ArrayList) OptionArgs(org.matheclipse.core.eval.util.OptionArgs) IAST(org.matheclipse.core.interfaces.IAST) ArgumentTypeException(org.matheclipse.core.eval.exception.ArgumentTypeException)

Example 13 with OptionArgs

use of org.matheclipse.core.eval.util.OptionArgs in project symja_android_library by axkr.

the class FunctionExpand method evaluate.

@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
    IExpr result = F.REMEMBER_AST_CACHE.getIfPresent(ast);
    if (result != null) {
        return result;
    }
    IExpr arg1 = ast.arg1();
    IExpr assumptionExpr = F.NIL;
    if (ast.size() > 2) {
        IExpr arg2 = ast.arg2();
        if (!arg2.isRule()) {
            assumptionExpr = arg2;
        }
        final OptionArgs options = new OptionArgs(ast.topHead(), ast, 2, engine);
        assumptionExpr = options.getOption(S.Assumptions).orElse(assumptionExpr);
    }
    if (assumptionExpr.isPresent()) {
        if (assumptionExpr.isAST()) {
            IAssumptions oldAssumptions = engine.getAssumptions();
            IAssumptions assumptions;
            if (oldAssumptions == null) {
                assumptions = org.matheclipse.core.eval.util.Assumptions.getInstance(assumptionExpr);
            } else {
                assumptions = oldAssumptions.copy();
                assumptions = assumptions.addAssumption(assumptionExpr);
            }
            if (assumptions != null) {
                try {
                    engine.setAssumptions(assumptions);
                    return callMatcher(ast, arg1);
                } finally {
                    engine.setAssumptions(oldAssumptions);
                }
            }
        }
    }
    // don't call PowerExpand
    return callMatcher(ast, arg1);
}
Also used : IAssumptions(org.matheclipse.core.eval.util.IAssumptions) OptionArgs(org.matheclipse.core.eval.util.OptionArgs) IExpr(org.matheclipse.core.interfaces.IExpr)

Aggregations

OptionArgs (org.matheclipse.core.eval.util.OptionArgs)13 IExpr (org.matheclipse.core.interfaces.IExpr)10 IAST (org.matheclipse.core.interfaces.IAST)8 IASTAppendable (org.matheclipse.core.interfaces.IASTAppendable)4 ArgumentTypeException (org.matheclipse.core.eval.exception.ArgumentTypeException)3 MathIllegalStateException (org.hipparchus.exception.MathIllegalStateException)2 MathRuntimeException (org.hipparchus.exception.MathRuntimeException)2 EvalEngine (org.matheclipse.core.eval.EvalEngine)2 IAssumptions (org.matheclipse.core.eval.util.IAssumptions)2 ISignedNumber (org.matheclipse.core.interfaces.ISignedNumber)2 Escaper (com.google.common.escape.Escaper)1 ArrayList (java.util.ArrayList)1 MathIllegalArgumentException (org.hipparchus.exception.MathIllegalArgumentException)1 ASTSeriesData (org.matheclipse.core.expression.ASTSeriesData)1 Dimensions2D (org.matheclipse.core.graphics.Dimensions2D)1