Search in sources :

Example 16 with WrongArgumentType

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

the class Programming method assignPart.

public static IExpr assignPart(final IExpr assignedExpr, final IAST part, int partPosition, IAST rhs, int rhsPos, EvalEngine engine) {
    if (!assignedExpr.isAST() || partPosition >= part.size()) {
        return assignedExpr;
    }
    IAST assignedAST = (IAST) assignedExpr;
    final IExpr arg2 = part.get(partPosition);
    int partPositionPlus1 = partPosition + 1;
    int[] span = arg2.isSpan(assignedAST.size());
    if (span != null) {
        int start = span[0];
        int last = span[1];
        int step = span[2];
        IAST result = F.NIL;
        if (step < 0 && start >= last) {
            int rhsIndx = 1;
            for (int i = start; i >= last; i += step) {
                IExpr temp = rhs.get(rhsIndx++);
                if (!temp.isList()) {
                    temp = assignPart(assignedAST.get(i), part, partPositionPlus1, temp, engine);
                } else {
                    temp = assignPart(assignedAST.get(i), part, partPositionPlus1, (IAST) temp, 1, engine);
                }
                if (temp.isPresent()) {
                    if (!result.isPresent()) {
                        result = assignedAST.clone();
                    }
                    result.set(i, temp);
                }
            }
        } else if (step > 0 && (last != 1 || start <= last)) {
            int rhsIndx = 1;
            for (int i = start; i <= last; i += step) {
                IExpr temp = rhs.get(rhsIndx++);
                if (!temp.isList()) {
                    temp = assignPart(assignedAST.get(i), part, partPositionPlus1, temp, engine);
                } else {
                    temp = assignPart(assignedAST.get(i), part, partPositionPlus1, (IAST) temp, 1, engine);
                }
                if (temp.isPresent()) {
                    if (!result.isPresent()) {
                        result = assignedAST.clone();
                    }
                    result.set(i, temp);
                }
            }
        } else {
            throw new WrongArgumentType(part, arg2, partPosition, "Wrong argument for Part[] function: " + arg2.toString() + " selects no part expression.");
        }
        return result;
    } else if (arg2.isSignedNumber()) {
        final int indx = Validate.checkIntType(part, partPosition, Integer.MIN_VALUE);
        IExpr ires = null;
        ires = assignPartValue(assignedAST, indx, rhs.getAST(rhsPos++));
        if (partPositionPlus1 < part.size()) {
            if (ires.isAST()) {
                return assignPart((IAST) ires, part, partPositionPlus1, rhs, rhsPos++, engine);
            } else {
                throw new WrongArgumentType(part, assignedAST, partPosition, "Wrong argument for Part[] function. Function or list expected.");
            }
        }
        return ires;
    } else if (arg2.isList()) {
        IExpr temp = null;
        final IAST list = (IAST) arg2;
        final IAST result = F.ListAlloc(list.size());
        for (int i = 1; i < list.size(); i++) {
            final IExpr listArg = list.get(i);
            if (listArg.isInteger()) {
                IExpr ires = null;
                final int indx = Validate.checkIntType(list, i, Integer.MIN_VALUE);
                ires = assignPartValue(assignedAST, indx, list);
                if (ires == null) {
                    return F.NIL;
                }
                if (partPositionPlus1 < part.size()) {
                    if (ires.isAST()) {
                        temp = assignPart(ires, part, partPositionPlus1, rhs, rhsPos++, engine);
                        result.append(temp);
                    } else {
                        throw new WrongArgumentType(part, assignedAST, partPosition, "Wrong argument for Part[] function. Function or list expected.");
                    }
                } else {
                    result.append(ires);
                }
            }
        }
        return result;
    }
    throw new WrongArgumentType(part, assignedAST, partPosition, "Wrong argument for Part[] function: " + arg2.toString() + " selects no part expression.");
}
Also used : WrongArgumentType(org.matheclipse.core.eval.exception.WrongArgumentType) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 17 with WrongArgumentType

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

the class Replace method replaceExpr.

private static IExpr replaceExpr(final IAST ast, IExpr arg1, IExpr rules, final EvalEngine engine) {
    if (rules.isListOfLists()) {
        IAST result = F.List();
        for (IExpr list : (IAST) rules) {
            IAST subList = (IAST) list;
            IExpr temp = F.NIL;
            for (IExpr element : subList) {
                if (element.isRuleAST()) {
                    IAST rule = (IAST) element;
                    Function<IExpr, IExpr> function = Functors.rules(rule);
                    temp = function.apply(arg1);
                    if (temp.isPresent()) {
                        break;
                    }
                } else {
                    WrongArgumentType wat = new WrongArgumentType(ast, ast, -1, "Rule expression (x->y) expected: ");
                    throw wat;
                }
            }
            if (temp.isPresent()) {
                result.append(temp);
            } else {
                result.append(arg1);
            }
        }
        return result;
    } else if (rules.isList()) {
        for (IExpr element : (IAST) rules) {
            if (element.isRuleAST()) {
                IAST rule = (IAST) element;
                Function<IExpr, IExpr> function = Functors.rules(rule);
                IExpr temp = function.apply(arg1);
                if (temp.isPresent()) {
                    return temp;
                }
            } else {
                WrongArgumentType wat = new WrongArgumentType(ast, ast, -1, "Rule expression (x->y) expected: ");
                throw wat;
            }
        }
        return arg1;
    }
    if (rules.isRuleAST()) {
        return replaceRule(arg1, (IAST) rules);
    } else {
        WrongArgumentType wat = new WrongArgumentType(ast, ast, -1, "Rule expression (x->y) expected: ");
        engine.printMessage(wat.getMessage());
    }
    return F.NIL;
}
Also used : Function(java.util.function.Function) WrongArgumentType(org.matheclipse.core.eval.exception.WrongArgumentType) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 18 with WrongArgumentType

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

the class Replace method replaceExprWithLevelSpecification.

private static IExpr replaceExprWithLevelSpecification(final IAST ast, IExpr arg1, IExpr rules, IExpr exprLevelSpecification, EvalEngine engine) {
    // use replaceFunction#setRule() method to set the current rules which
    // are initialized with null
    ReplaceFunction replaceFunction = new ReplaceFunction(ast, null, engine);
    VisitorLevelSpecification level = new VisitorLevelSpecification(replaceFunction, exprLevelSpecification, false);
    if (rules.isListOfLists()) {
        IAST result = F.List();
        for (IExpr list : (IAST) rules) {
            IExpr temp = F.NIL;
            IAST subList = (IAST) list;
            for (IExpr element : subList) {
                if (element.isRuleAST()) {
                    IAST rule = (IAST) element;
                    replaceFunction.setRule(rule);
                    temp = arg1.accept(level);
                    if (temp.isPresent()) {
                        break;
                    }
                } else {
                    WrongArgumentType wat = new WrongArgumentType(ast, ast, -1, "Rule expression (x->y) expected: ");
                    throw wat;
                }
            }
            result.append(temp.orElse(arg1));
        }
        return result;
    }
    replaceFunction.setRule(rules);
    return arg1.accept(level).orElse(arg1);
}
Also used : WrongArgumentType(org.matheclipse.core.eval.exception.WrongArgumentType) VisitorLevelSpecification(org.matheclipse.core.visit.VisitorLevelSpecification) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 19 with WrongArgumentType

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

the class ReplaceList method replaceExpr.

private static IAST replaceExpr(final IAST ast, IExpr arg1, IExpr rules, IAST result, int maxNumberOfResults, final EvalEngine engine) {
    if (rules.isList()) {
        for (IExpr element : (IAST) rules) {
            if (element.isRuleAST()) {
                IAST rule = (IAST) element;
                Function<IExpr, IExpr> function = Functors.rules(rule);
                IExpr temp = function.apply(arg1);
                if (temp.isPresent()) {
                    if (maxNumberOfResults <= result.size()) {
                        return result;
                    }
                    result.append(temp);
                }
            } else {
                WrongArgumentType wat = new WrongArgumentType(ast, ast, -1, "Rule expression (x->y) expected: ");
                throw wat;
            }
        }
        return result;
    }
    if (rules.isRuleAST()) {
        Function<IExpr, IExpr> function = Functors.rules((IAST) rules);
        IExpr temp = function.apply(arg1);
        if (temp.isPresent()) {
            if (maxNumberOfResults <= result.size()) {
                return result;
            }
            result.append(temp);
        }
    } else {
        WrongArgumentType wat = new WrongArgumentType(ast, ast, -1, "Rule expression (x->y) expected: ");
        engine.printMessage(wat.getMessage());
    }
    return result;
}
Also used : WrongArgumentType(org.matheclipse.core.eval.exception.WrongArgumentType) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST)

Example 20 with WrongArgumentType

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

the class Resultant method evaluate.

@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
    Validate.checkSize(ast, 4);
    // TODO allow multinomials
    IExpr arg3 = Validate.checkSymbolType(ast, 3);
    ISymbol x = (ISymbol) arg3;
    IExpr a = F.evalExpandAll(ast.arg1());
    IExpr b = F.evalExpandAll(ast.arg2());
    ExprPolynomialRing ring = new ExprPolynomialRing(F.List(x));
    try {
        // check if a is a polynomial otherwise check ArithmeticException,
        // ClassCastException
        ring.create(a);
    } catch (RuntimeException ex) {
        throw new WrongArgumentType(ast, a, 1, "Polynomial expected!");
    }
    try {
        // check if b is a polynomial otherwise check ArithmeticException,
        // ClassCastException
        ring.create(b);
        return F.Together(resultant(a, b, x, engine));
    } catch (RuntimeException ex) {
        throw new WrongArgumentType(ast, b, 2, "Polynomial expected!");
    }
}
Also used : ExprPolynomialRing(org.matheclipse.core.polynomials.ExprPolynomialRing) ISymbol(org.matheclipse.core.interfaces.ISymbol) WrongArgumentType(org.matheclipse.core.eval.exception.WrongArgumentType) IExpr(org.matheclipse.core.interfaces.IExpr)

Aggregations

WrongArgumentType (org.matheclipse.core.eval.exception.WrongArgumentType)37 IAST (org.matheclipse.core.interfaces.IAST)30 IExpr (org.matheclipse.core.interfaces.IExpr)29 ISymbol (org.matheclipse.core.interfaces.ISymbol)12 ISignedNumber (org.matheclipse.core.interfaces.ISignedNumber)7 IInteger (org.matheclipse.core.interfaces.IInteger)6 ExprPolynomialRing (org.matheclipse.core.polynomials.ExprPolynomialRing)5 ExprPolynomial (org.matheclipse.core.polynomials.ExprPolynomial)4 ExpVector (edu.jas.poly.ExpVector)3 LinearConstraint (org.hipparchus.optim.linear.LinearConstraint)3 IFraction (org.matheclipse.core.interfaces.IFraction)3 ModLong (edu.jas.arith.ModLong)2 GenPolynomial (edu.jas.poly.GenPolynomial)2 ArrayList (java.util.ArrayList)2 Apint (org.apfloat.Apint)2 PrettyPrint (edu.jas.kern.PrettyPrint)1 Function (java.util.function.Function)1 IntVariable (jp.ac.kobe_u.cs.cream.IntVariable)1 Apfloat (org.apfloat.Apfloat)1 MathIllegalStateException (org.hipparchus.exception.MathIllegalStateException)1