Search in sources :

Example 31 with IASTAppendable

use of org.matheclipse.core.interfaces.IASTAppendable in project symja_android_library by axkr.

the class Combinatoric method permutationCycles.

/**
 * Create a <code>Cycles({...})</code> expression from the permutation list.
 *
 * @param permList the permutation list
 * @param engine
 * @param head define the head of the resulting expression (can be different from {@link S#Cycles}
 * @return
 */
public static IExpr permutationCycles(IAST permList, EvalEngine engine, IExpr head) {
    if (permList.isEmptyList()) {
        return F.Cycles(F.CEmptyList);
    }
    int[] positions = isPermutationList(permList, false, engine);
    if (positions == null) {
        // not a valid permutation list
        return F.NIL;
    }
    // 
    // permList should be valid here
    // 
    IASTAppendable mainList = F.ListAlloc(F.allocMin16(permList));
    IASTAppendable cycleList = F.NIL;
    int oldPosition = -1;
    int newPosition = -1;
    for (int i = 0; i < positions.length; i++) {
        newPosition = positions[i];
        if (newPosition < 0) {
            cycleList = F.NIL;
        } else {
            if (newPosition == i + 1) {
                positions[i] = -1;
                cycleList = F.NIL;
                if (head != S.Cycles) {
                    // ignore singletons for S.Cycles; otherwise keep singletons:
                    IAST singleton = F.list(F.ZZ(newPosition));
                    mainList.append(singleton);
                }
            } else {
                positions[i] = -1;
                if (!cycleList.isPresent()) {
                    cycleList = F.ListAlloc(permList.argSize() < 16 ? permList.size() : 16);
                    mainList.append(cycleList);
                }
                cycleList.append(newPosition);
                oldPosition = newPosition;
                while (positions[oldPosition - 1] > 0) {
                    newPosition = positions[oldPosition - 1];
                    if (newPosition < 0) {
                        break;
                    }
                    positions[oldPosition - 1] = -1;
                    if (positions[newPosition - 1] < 0) {
                        cycleList.append(1, newPosition);
                    } else {
                        cycleList.append(newPosition);
                    }
                    oldPosition = newPosition;
                }
                cycleList = F.NIL;
            }
        }
    }
    return F.unaryAST1(head, mainList);
}
Also used : IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) IAST(org.matheclipse.core.interfaces.IAST)

Example 32 with IASTAppendable

use of org.matheclipse.core.interfaces.IASTAppendable in project symja_android_library by axkr.

the class BooleanFunctions method solveInstances.

/**
 * Example: Create a list of rules in the form <code>
 * {{a->False,b->True,c->False,d->False},{a->True,b->False,c->False,d->False},...}</code> for the
 * variables <code>{a,b,c,d}</code>
 *
 * @param booleanExpression an expression build from symbols and boolean operators like <code>
 *     And, Or, Not, Xor, Nand, Nor, Implies, Equivalent,...</code>
 * @param variables the possible variables. Example: <code>{a,b,c,d}</code>
 * @param maximumNumberOfResults
 * @return
 */
public static IAST solveInstances(IExpr booleanExpression, IAST variables, int maximumNumberOfResults) {
    LogicFormula lf = new LogicFormula();
    Variable[] vars = lf.ast2Variable(variables);
    List<Assignment> assignments = logicNGSatisfiabilityInstances(booleanExpression, vars, lf, maximumNumberOfResults);
    Object2IntMap<String> map = LogicFormula.name2Position(vars);
    IASTAppendable list = F.ListAlloc(assignments.size());
    for (int i = 0; i < assignments.size(); i++) {
        if (i >= maximumNumberOfResults) {
            break;
        }
        list.append(lf.literals2VariableList(assignments.get(i).literals(), map));
    }
    EvalAttributes.sort(list, Comparators.REVERSE_CANONICAL_COMPARATOR);
    return list;
}
Also used : Assignment(org.logicng.datastructures.Assignment) Variable(org.logicng.formulas.Variable) IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable)

Example 33 with IASTAppendable

use of org.matheclipse.core.interfaces.IASTAppendable in project symja_android_library by axkr.

the class Algebra method factorRational.

public static IAST factorRational(GenPolynomial<BigRational> polyRat, JASConvert<BigRational> jas, ISymbol head) {
    Object[] objects = jas.factorTerms(polyRat);
    GenPolynomial<edu.jas.arith.BigInteger> poly = (GenPolynomial<edu.jas.arith.BigInteger>) objects[2];
    FactorAbstract<edu.jas.arith.BigInteger> factorAbstract = FactorFactory.getImplementation(edu.jas.arith.BigInteger.ONE);
    SortedMap<GenPolynomial<edu.jas.arith.BigInteger>, Long> map;
    map = factorAbstract.factors(poly);
    IASTAppendable result = F.ast(head, map.size() + 1);
    java.math.BigInteger gcd = (java.math.BigInteger) objects[0];
    java.math.BigInteger lcm = (java.math.BigInteger) objects[1];
    if (!gcd.equals(java.math.BigInteger.ONE) || !lcm.equals(java.math.BigInteger.ONE)) {
        result.append(F.fraction(gcd, lcm));
    }
    for (SortedMap.Entry<GenPolynomial<edu.jas.arith.BigInteger>, Long> entry : map.entrySet()) {
        final GenPolynomial<BigInteger> key = entry.getKey();
        final Long value = entry.getValue();
        if (key.isONE() && value.equals(1L)) {
            continue;
        }
        if (value == 1L) {
            result.append(jas.integerPoly2Expr(key));
        } else {
            result.append(F.Power(jas.integerPoly2Expr(key), F.ZZ(value)));
        }
    }
    return result;
}
Also used : GenPolynomial(edu.jas.poly.GenPolynomial) IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) SortedMap(java.util.SortedMap) ModLong(edu.jas.arith.ModLong) BigInteger(edu.jas.arith.BigInteger)

Example 34 with IASTAppendable

use of org.matheclipse.core.interfaces.IASTAppendable in project symja_android_library by axkr.

the class Algebra method factorComplex.

/**
 * @param polynomial the complex-rational polynomial which should be factored
 * @param jas
 * @param head the head of the factorization result AST (typically <code>F.Times</code> or <code>
 *     F.List</code>)
 * @param cfac
 * @return
 */
private static IExpr factorComplex(IExpr expr, GenPolynomial<Complex<BigRational>> polynomial, JASConvert<? extends RingElem<?>> jas, ISymbol head, ComplexRing<BigRational> cfac) {
    FactorComplex<BigRational> factorAbstract = new FactorComplex<BigRational>(cfac);
    SortedMap<GenPolynomial<Complex<BigRational>>, Long> map = factorAbstract.factors(polynomial);
    IASTAppendable result = F.ast(head, map.size());
    for (SortedMap.Entry<GenPolynomial<Complex<BigRational>>, Long> entry : map.entrySet()) {
        if (entry.getKey().isONE() && entry.getValue().equals(1L)) {
            continue;
        }
        final IExpr key = jas.complexPoly2Expr(entry.getKey());
        if (entry.getValue().equals(1L) && map.size() <= 2 && (key.equals(F.CNI) || key.equals(F.CI))) {
            // hack: factoring -I and I out of an expression should give no new factorized expression
            return expr;
        }
        result.append(F.Power(jas.complexPoly2Expr(entry.getKey()), F.ZZ(entry.getValue())));
    }
    return result;
}
Also used : GenPolynomial(edu.jas.poly.GenPolynomial) IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) BigRational(edu.jas.arith.BigRational) SortedMap(java.util.SortedMap) ModLong(edu.jas.arith.ModLong) JASIExpr(org.matheclipse.core.convert.JASIExpr) IExpr(org.matheclipse.core.interfaces.IExpr) FactorComplex(edu.jas.ufd.FactorComplex)

Example 35 with IASTAppendable

use of org.matheclipse.core.interfaces.IASTAppendable in project symja_android_library by axkr.

the class F method eval.

/**
 * Create a function with 1 argument and evaluate it.
 *
 * @param head
 * @param a0
 * @return the evaluated object
 * @deprecated
 */
@Deprecated
private static IExpr eval(final ISymbol head, final IExpr a0) {
    final IASTAppendable ast = ast(head);
    ast.append(a0);
    return EvalEngine.get().evaluate(ast);
}
Also used : IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable)

Aggregations

IASTAppendable (org.matheclipse.core.interfaces.IASTAppendable)363 IExpr (org.matheclipse.core.interfaces.IExpr)219 IAST (org.matheclipse.core.interfaces.IAST)130 ISymbol (org.matheclipse.core.interfaces.ISymbol)36 IASTMutable (org.matheclipse.core.interfaces.IASTMutable)30 IInteger (org.matheclipse.core.interfaces.IInteger)29 Map (java.util.Map)28 EvalEngine (org.matheclipse.core.eval.EvalEngine)20 PrettyPrint (edu.jas.kern.PrettyPrint)13 SortedMap (java.util.SortedMap)13 ArrayList (java.util.ArrayList)12 F (org.matheclipse.core.expression.F)12 BigRational (edu.jas.arith.BigRational)10 LogManager (org.apache.logging.log4j.LogManager)10 Logger (org.apache.logging.log4j.Logger)10 ExpVector (edu.jas.poly.ExpVector)9 HashMap (java.util.HashMap)9 IBuiltInSymbol (org.matheclipse.core.interfaces.IBuiltInSymbol)8 IStringX (org.matheclipse.core.interfaces.IStringX)8 ASTNode (org.matheclipse.parser.client.ast.ASTNode)8