Search in sources :

Example 1 with ASTElementLimitExceeded

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

the class FrobeniusSolve method evaluate.

/**
 * {@inheritDoc}
 */
@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
    if (ast.arg1().isList() && ast.arg2().isInteger()) {
        IAST list = ast.getAST(1);
        try {
            int[] listInt = Validate.checkListOfInts(ast, list, true, false, engine);
            if (listInt != null) {
                for (int i = 0; i < listInt.length; i++) {
                    if (listInt[i] < 0 && ast.size() < 4) {
                    }
                }
                IInteger[] solution;
                IASTAppendable result = F.ListAlloc(8);
                FrobeniusSolver solver = getSolver(listInt, (IInteger) ast.arg2());
                // all solutions
                int numberOfSolutions = -1;
                if (ast.size() == 4) {
                    numberOfSolutions = ast.arg3().toIntDefault(-1);
                }
                while ((solution = solver.take()) != null) {
                    if (result.size() >= Config.MAX_AST_SIZE) {
                        throw new ASTElementLimitExceeded(result.size());
                    }
                    result.append(F.List(solution));
                }
                return result;
            }
        } catch (LimitException le) {
            throw le;
        } catch (RuntimeException rex) {
            LOGGER.debug("FrobeniusSolve.evaluate() failed", rex);
        }
    }
    return F.NIL;
}
Also used : IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) FrobeniusSolver(org.matheclipse.core.frobenius.FrobeniusSolver) IInteger(org.matheclipse.core.interfaces.IInteger) IAST(org.matheclipse.core.interfaces.IAST) LimitException(org.matheclipse.core.eval.exception.LimitException) ASTElementLimitExceeded(org.matheclipse.core.eval.exception.ASTElementLimitExceeded)

Example 2 with ASTElementLimitExceeded

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

the class AbstractIntegerSym method primitiveRootList.

/**
 * The primitive roots of this integer number
 *
 * @return the primitive roots
 * @throws ArithmeticException
 */
@Override
public IInteger[] primitiveRootList() throws ArithmeticException {
    IInteger phi = eulerPhi();
    int size = phi.eulerPhi().toIntDefault();
    if (size <= 0) {
        return null;
    }
    if (isEven() && !equals(F.C2) && !equals(F.C4)) {
        if (quotient(F.C2).isEven()) {
            return new IInteger[0];
        }
    }
    IAST ast = phi.factorInteger();
    IInteger[] d = new IInteger[ast.argSize()];
    for (int i = 1; i < ast.size(); i++) {
        IAST element = (IAST) ast.get(i);
        IInteger q = (IInteger) element.arg1();
        d[i - 1] = phi.quotient(q);
    }
    int k = 0;
    IInteger n = this;
    IInteger m = F.C1;
    if (Config.MAX_AST_SIZE < size) {
        throw new ASTElementLimitExceeded(size);
    }
    IInteger[] resultArray = new IInteger[size];
    boolean b;
    while (m.compareTo(n) < 0) {
        b = m.gcd(n).isOne();
        for (int i = 0; i < d.length; i++) {
            b = b && m.modPow(d[i], n).isGT(F.C1);
        }
        if (b) {
            resultArray[k++] = m;
        }
        m = m.add(F.C1);
    }
    if (resultArray[0] == null) {
        return new IInteger[0];
    }
    return resultArray;
}
Also used : IInteger(org.matheclipse.core.interfaces.IInteger) IAST(org.matheclipse.core.interfaces.IAST) ASTElementLimitExceeded(org.matheclipse.core.eval.exception.ASTElementLimitExceeded)

Example 3 with ASTElementLimitExceeded

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

the class BooleanFunctions method xorToDNF.

/**
 * Convert the XOR expression into DNF format.
 *
 * @param xorForm the <code>Xor(...)</code> expression
 * @return
 */
private static IAST xorToDNF(IAST xorForm) {
    int size = xorForm.argSize();
    if (size > 2) {
        if (size <= 15) {
            IASTAppendable orAST = F.Or();
            // allBits filled with '1' up to size of bits
            final int allBits = FULL_BITSETS[size - 1];
            for (int i = allBits; i >= 0; i--) {
                int singleBit = 0b1;
                int count = 0;
                for (int j = 0; j < size; j++) {
                    if ((singleBit & i) != 0) {
                        count++;
                    }
                    singleBit <<= 1;
                }
                if ((count & 1) == 1) {
                    IASTMutable andAST = F.astMutable(S.And, size);
                    singleBit = 0b1;
                    int startPos = 1;
                    int startNotPos = count + 1;
                    for (int j = 0; j < size; j++) {
                        if ((singleBit & i) == 0) {
                            andAST.set(startNotPos++, F.Not(xorForm.get(j + 1)));
                        } else {
                            andAST.set(startPos++, xorForm.get(j + 1));
                        }
                        singleBit <<= 1;
                    }
                    orAST.append(andAST);
                }
            }
            return orAST;
        }
        throw new ASTElementLimitExceeded(Short.MAX_VALUE);
    }
    IExpr arg1 = xorForm.arg1();
    IExpr arg2 = xorForm.arg2();
    return F.Or(F.And(arg1, F.Not(arg2)), F.And(F.Not(arg1), arg2));
}
Also used : IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) IASTMutable(org.matheclipse.core.interfaces.IASTMutable) IExpr(org.matheclipse.core.interfaces.IExpr) ASTElementLimitExceeded(org.matheclipse.core.eval.exception.ASTElementLimitExceeded)

Aggregations

ASTElementLimitExceeded (org.matheclipse.core.eval.exception.ASTElementLimitExceeded)3 IAST (org.matheclipse.core.interfaces.IAST)2 IASTAppendable (org.matheclipse.core.interfaces.IASTAppendable)2 IInteger (org.matheclipse.core.interfaces.IInteger)2 LimitException (org.matheclipse.core.eval.exception.LimitException)1 FrobeniusSolver (org.matheclipse.core.frobenius.FrobeniusSolver)1 IASTMutable (org.matheclipse.core.interfaces.IASTMutable)1 IExpr (org.matheclipse.core.interfaces.IExpr)1