Search in sources :

Example 11 with IASTAppendable

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

the class AbstractAST method linearPower.

@Override
public IExpr[] linearPower(IExpr variable) {
    int size = size();
    int counter = 0;
    if (isPlus()) {
        // a + b + c....
        IASTAppendable plusClone = copyAppendable();
        IExpr[] subLinear = null;
        int j = 1;
        for (int i = 1; i < size; i++) {
            if (get(i).isFree(variable, true)) {
                j++;
            } else {
                if (counter > 0 || get(i).isPlus()) {
                    return null;
                }
                subLinear = get(i).linearPower(variable);
                if (subLinear != null) {
                    counter++;
                    plusClone.remove(j);
                } else {
                    return null;
                }
            }
        }
        if (subLinear != null) {
            return new IExpr[] { plusClone.oneIdentity0(), subLinear[1], subLinear[2] };
        }
        return new IExpr[] { plusClone.oneIdentity0(), F.C0, F.C1 };
    } else if (isTimes()) {
        IInteger exp = F.C1;
        // a * b * c....
        IASTAppendable timesClone = copyAppendable();
        int j = 1;
        for (int i = 1; i < size; i++) {
            if (get(i).isFree(variable, true)) {
                j++;
            } else {
                if (get(i).equals(variable)) {
                    if (counter > 0) {
                        return null;
                    }
                    counter++;
                    timesClone.remove(j);
                    continue;
                } else if (get(i).isPower()) {
                    if (counter > 0) {
                        return null;
                    }
                    IAST power = (IAST) get(i);
                    if (power.base().equals(variable) && power.exponent().isInteger()) {
                        exp = (IInteger) power.exponent();
                        counter++;
                        timesClone.remove(j);
                        continue;
                    }
                }
                return null;
            }
        }
        return new IExpr[] { F.C0, timesClone.oneIdentity1(), exp };
    } else if (isPower() && base().equals(variable) && exponent().isInteger()) {
        return new IExpr[] { F.C0, F.C1, exponent() };
    } else if (this.equals(variable)) {
        return new IExpr[] { F.C0, F.C1, F.C1 };
    } else if (isFree(variable, true)) {
        return new IExpr[] { this, F.C0, F.C1 };
    }
    return null;
}
Also used : IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) IInteger(org.matheclipse.core.interfaces.IInteger) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST)

Example 12 with IASTAppendable

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

the class ASTRealMatrix method setAtCopy.

@Override
public IASTMutable setAtCopy(int i, IExpr expr) {
    if (expr instanceof ASTRealVector) {
        IASTMutable ast = copy();
        ast.set(i, expr);
        return ast;
    }
    IASTAppendable ast = copyAppendable();
    ast.set(i, expr);
    return ast;
}
Also used : IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) IASTMutable(org.matheclipse.core.interfaces.IASTMutable)

Example 13 with IASTAppendable

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

the class ASTRealMatrix method mapMatrixColumns.

/**
 * {@inheritDoc}
 */
@Override
public IExpr mapMatrixColumns(int[] dim, Function<IExpr, IExpr> f) {
    final int columnSize = dim[1];
    IASTAppendable result = F.ListAlloc(columnSize);
    return result.appendArgs(0, columnSize, j -> f.apply(new ASTRealVector(matrix.getColumnVector(j), false)));
}
Also used : IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable)

Example 14 with IASTAppendable

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

the class B2 method copyAppendable.

@Override
public IASTAppendable copyAppendable(int additionalCapacity) {
    IASTAppendable result = F.ast(head(), additionalCapacity + 2);
    result.append(arg1);
    result.append(arg2);
    return result;
}
Also used : IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable)

Example 15 with IASTAppendable

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

the class AST method parse.

/**
 * simple parser to simplify unit tests. The parser assumes that the String contains no syntax
 * errors.
 *
 * <p>
 * Example &quot;List[x,List[y]]&quot;
 *
 * @param inputString
 * @return
 */
public static IAST parse(final String inputString) {
    final StringTokenizer tokenizer = new StringTokenizer(inputString, "[],", true);
    String token = tokenizer.nextToken();
    final IASTAppendable list = newInstance(StringX.valueOf(token));
    token = tokenizer.nextToken();
    if ("[".equals(token)) {
        parseList(tokenizer, list);
        return list;
    }
    // syntax fError occured
    return null;
}
Also used : StringTokenizer(java.util.StringTokenizer) 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