Search in sources :

Example 36 with ArgumentTypeException

use of org.matheclipse.core.eval.exception.ArgumentTypeException in project symja_android_library by axkr.

the class JavaFunctions method determineParameters.

public static Object[] determineParameters(final IAST ast, Parameter[] parameters, int offset) {
    try {
        Object[] params = new Object[parameters.length];
        for (int j = 0; j < parameters.length; j++) {
            Parameter p = parameters[j];
            IExpr arg = ast.get(j + offset);
            Class<?> clazz = p.getType();
            if (arg instanceof DataExpr<?>) {
                Object obj = ((DataExpr) arg).toData();
                if (clazz.isInstance(obj)) {
                    params[j] = obj;
                    continue;
                }
            }
            if (clazz.isInstance(arg)) {
                params[j] = arg;
            } else if (clazz.equals(boolean.class)) {
                if (arg.isTrue()) {
                    params[j] = Boolean.TRUE;
                } else if (arg.isFalse()) {
                    params[j] = Boolean.FALSE;
                } else {
                    return null;
                }
            } else if (clazz.equals(double.class)) {
                params[j] = Double.valueOf(arg.evalDouble());
            } else if (clazz.equals(float.class)) {
                params[j] = Float.valueOf((float) arg.evalDouble());
            } else if (clazz.equals(int.class)) {
                int n = arg.toIntDefault();
                if (n == Integer.MIN_VALUE) {
                    return null;
                }
                params[j] = Integer.valueOf(n);
            } else if (clazz.equals(long.class)) {
                long l = arg.toLongDefault();
                if (l == Long.MIN_VALUE) {
                    return null;
                }
                params[j] = Long.valueOf(l);
            } else if (clazz.equals(short.class)) {
                int s = arg.toIntDefault();
                if (s < Short.MIN_VALUE || s > Short.MAX_VALUE) {
                    return null;
                }
                params[j] = Short.valueOf((short) s);
            } else if (clazz.equals(byte.class)) {
                int b = arg.toIntDefault();
                if (b < Byte.MIN_VALUE || b > Byte.MAX_VALUE) {
                    return null;
                }
                params[j] = Byte.valueOf((byte) b);
            } else if (clazz.equals(char.class)) {
                if (!arg.isString()) {
                    return null;
                }
                String str = arg.toString();
                if (str.length() != 1) {
                    return null;
                }
                params[j] = Character.valueOf(str.charAt(0));
            } else if (clazz.equals(String.class)) {
                if (!arg.isString()) {
                    return null;
                }
                params[j] = arg.toString();
            } else if (clazz.equals(org.hipparchus.complex.Complex.class)) {
                org.hipparchus.complex.Complex complex = arg.evalComplex();
                if (complex == null) {
                    return null;
                }
                params[j] = complex;
            } else if (clazz.equals(Class.class) && arg instanceof JavaClassExpr) {
                params[j] = ((JavaClassExpr) arg).toData();
            } else {
                params[j] = arg;
            }
        }
        return params;
    } catch (ArgumentTypeException atex) {
    }
    return null;
}
Also used : DataExpr(org.matheclipse.core.expression.DataExpr) JavaClassExpr(org.matheclipse.core.expression.data.JavaClassExpr) Parameter(java.lang.reflect.Parameter) IExpr(org.matheclipse.core.interfaces.IExpr) ArgumentTypeException(org.matheclipse.core.eval.exception.ArgumentTypeException)

Example 37 with ArgumentTypeException

use of org.matheclipse.core.eval.exception.ArgumentTypeException in project symja_android_library by axkr.

the class NIntegrate method integrate.

// public final static ISymbol LegendreGauss = F
// .initFinalSymbol(Config.PARSER_USE_LOWERCASE_SYMBOLS ? "legendregauss" : "LegendreGauss");
/**
 * Integrate a function numerically.
 *
 * @param method the following methods are possible: LegendreGauss, Simpson, Romberg, Trapezoid
 * @param list a list of the form <code>{x, lowerBound, upperBound}</code>, where <code>lowerBound
 *     </code> and <code>upperBound</code> are numbers which could be converted to a Java double
 *        value.
 * @param min Lower bound of the integration interval.
 * @param max Upper bound of the integration interval.
 * @param function the function which should be integrated.
 * @param maxPoints maximum number of points
 * @param maxIterations maximum number of iterations
 * @return
 * @throws MathIllegalStateException
 */
public static double integrate(String method, IAST list, double min, double max, IExpr function, int maxPoints, int maxIterations) throws MathIllegalStateException {
    GaussIntegratorFactory factory = new GaussIntegratorFactory();
    if (!list.arg1().isSymbol()) {
        // `1` is not a valid variable.
        String str = IOFunctions.getMessage("ivar", F.list(list.arg1()), EvalEngine.get());
        throw new ArgumentTypeException(str);
    }
    ISymbol xVar = (ISymbol) list.arg1();
    final EvalEngine engine = EvalEngine.get();
    IExpr tempFunction = F.eval(function);
    UnivariateFunction f = new UnaryNumerical(tempFunction, xVar, engine);
    UnivariateIntegrator integrator;
    if ("Simpson".equalsIgnoreCase(method)) {
        integrator = new SimpsonIntegrator();
    } else if ("Romberg".equalsIgnoreCase(method)) {
        integrator = new RombergIntegrator();
    } else if ("Trapezoid".equalsIgnoreCase(method)) {
        integrator = new TrapezoidIntegrator();
    } else {
        if (maxPoints > 1000) {
            // see also https://github.com/Hipparchus-Math/hipparchus/issues/61
            throw new MathIllegalArgumentException(LocalizedCoreFormats.NUMBER_TOO_LARGE, maxPoints, 1000);
        }
        // default: LegendreGauss
        GaussIntegrator integ = factory.legendre(maxPoints, min, max);
        return integ.integrate(f);
    }
    return integrator.integrate(maxIterations, f, min, max);
}
Also used : SimpsonIntegrator(org.hipparchus.analysis.integration.SimpsonIntegrator) UnaryNumerical(org.matheclipse.core.generic.UnaryNumerical) ISymbol(org.matheclipse.core.interfaces.ISymbol) UnivariateFunction(org.hipparchus.analysis.UnivariateFunction) UnivariateIntegrator(org.hipparchus.analysis.integration.UnivariateIntegrator) RombergIntegrator(org.hipparchus.analysis.integration.RombergIntegrator) GaussIntegratorFactory(org.hipparchus.analysis.integration.gauss.GaussIntegratorFactory) GaussIntegrator(org.hipparchus.analysis.integration.gauss.GaussIntegrator) MathIllegalArgumentException(org.hipparchus.exception.MathIllegalArgumentException) EvalEngine(org.matheclipse.core.eval.EvalEngine) IExpr(org.matheclipse.core.interfaces.IExpr) ArgumentTypeException(org.matheclipse.core.eval.exception.ArgumentTypeException) TrapezoidIntegrator(org.hipparchus.analysis.integration.TrapezoidIntegrator)

Example 38 with ArgumentTypeException

use of org.matheclipse.core.eval.exception.ArgumentTypeException 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 39 with ArgumentTypeException

use of org.matheclipse.core.eval.exception.ArgumentTypeException in project symja_android_library by axkr.

the class ListLinePlot3D method coordinateLinePlot.

private IExpr coordinateLinePlot(IAST coordinates, IAST plotStyle, EvalEngine engine) {
    double minX = ((IAST) (((IAST) coordinates.arg1()).arg1())).arg1().evalDouble();
    double maxX = minX;
    double minY = ((IAST) (((IAST) coordinates.arg1()).arg1())).arg2().evalDouble();
    double maxY = minY;
    double minZ = ((IAST) (((IAST) coordinates.arg1()).arg1())).arg3().evalDouble();
    double maxZ = minZ;
    for (int i = 1; i <= coordinates.argSize(); i++) {
        IAST line = (IAST) coordinates.get(i);
        for (int j = 1; j <= line.argSize(); j++) {
            try {
                IAST coordinate = (IAST) line.get(j);
                // evalDouble may throw ArgumentTypeException
                double x = coordinate.arg1().evalDouble();
                if (x < minX)
                    minX = x;
                if (x > maxX)
                    maxX = x;
                double y = coordinate.arg2().evalDouble();
                if (y < minY)
                    minY = y;
                if (y > maxY)
                    maxY = y;
                double z = coordinate.arg3().evalDouble();
                if (z < minZ)
                    minZ = z;
                if (z > maxZ)
                    maxZ = z;
            } catch (ArgumentTypeException ate) {
            // ignore this row
            }
        }
    }
    // ListLinePlot3D size is 2.5 × 2.5 × 1 independently from its coordinates
    IAST deltaXYZ = F.List((maxX - minX) / 2.5, (maxY - minY) / 2.5, maxZ - minZ);
    // the color number with which the line will be printed
    int lineColorNumber = 1;
    IASTAppendable lineList = F.ListAlloc(coordinates.size() * 2);
    for (int i = 1; i <= coordinates.argSize(); i++) {
        final IAST color = GraphicsFunctions.plotStyleColorExpr(lineColorNumber++, plotStyle);
        lineList.append(color);
        // (# / deltaXYZ)& /@ line
        lineList.append(F.Line(S.Map.of(engine, F.Function(F.Divide(F.Slot1, deltaXYZ)), coordinates.get(i))));
    }
    return lineList;
}
Also used : IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) IAST(org.matheclipse.core.interfaces.IAST) ArgumentTypeException(org.matheclipse.core.eval.exception.ArgumentTypeException)

Example 40 with ArgumentTypeException

use of org.matheclipse.core.eval.exception.ArgumentTypeException 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)

Aggregations

ArgumentTypeException (org.matheclipse.core.eval.exception.ArgumentTypeException)44 IExpr (org.matheclipse.core.interfaces.IExpr)24 IAST (org.matheclipse.core.interfaces.IAST)16 Complex (org.hipparchus.complex.Complex)11 ISymbol (org.matheclipse.core.interfaces.ISymbol)10 EvalEngine (org.matheclipse.core.eval.EvalEngine)6 UnaryNumerical (org.matheclipse.core.generic.UnaryNumerical)6 UnivariateDifferentiableFunction (org.hipparchus.analysis.differentiation.UnivariateDifferentiableFunction)5 ISignedNumber (org.matheclipse.core.interfaces.ISignedNumber)5 ArrayList (java.util.ArrayList)4 IASTAppendable (org.matheclipse.core.interfaces.IASTAppendable)4 DSFactory (org.hipparchus.analysis.differentiation.DSFactory)3 FiniteDifferencesDifferentiator (org.hipparchus.analysis.differentiation.FiniteDifferencesDifferentiator)3 Config (org.matheclipse.core.basic.Config)3 F (org.matheclipse.core.expression.F)3 IInteger (org.matheclipse.core.interfaces.IInteger)3 INum (org.matheclipse.core.interfaces.INum)3 BisectionSolver (org.hipparchus.analysis.solvers.BisectionSolver)2 LinearConstraint (org.hipparchus.optim.linear.LinearConstraint)2 Gamma (org.hipparchus.special.Gamma)2