Search in sources :

Example 61 with IASTAppendable

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

the class Convert method complexMatrix2List.

public static IASTAppendable complexMatrix2List(final FieldMatrix<Complex> matrix, boolean matrixFormat) {
    if (matrix == null) {
        return F.NIL;
    }
    final int rowSize = matrix.getRowDimension();
    final int colSize = matrix.getColumnDimension();
    final IASTAppendable out = F.ListAlloc(rowSize);
    IASTAppendable currOutRow;
    for (int i = 0; i < rowSize; i++) {
        currOutRow = F.ListAlloc(colSize);
        out.append(currOutRow);
        for (int j = 0; j < colSize; j++) {
            final Complex expr = matrix.getEntry(i, j);
            currOutRow.append(F.complexNum(expr));
        }
    }
    if (matrixFormat) {
        // because the rows can contain sub lists the IAST.IS_MATRIX flag cannot be set directly.
        // isMatrix() must be
        // used!
        out.isMatrix(true);
    }
    return out;
}
Also used : IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) Complex(org.hipparchus.complex.Complex)

Example 62 with IASTAppendable

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

the class Convert method polynomialFunction2Expr.

/**
 * Converts an array of coefficients into the (polynomial) expression representation.
 *
 * @param coefficients the coefficients of the polynomial function
 * @param sym the name of the polynomial functions variable
 * @return
 */
public static IExpr polynomialFunction2Expr(double[] coefficients, ISymbol sym) {
    if (coefficients[0] == 0.0) {
        if (coefficients.length == 1) {
            return F.C0;
        }
    }
    IASTAppendable sum = F.PlusAlloc(coefficients.length);
    sum.append(coefficients[0]);
    for (int i = 1; i < coefficients.length; ++i) {
        if (coefficients[i] != 0) {
            sum.append(F.Times(F.num(coefficients[i]), F.Power(sym, F.ZZ(i))));
        }
    }
    return sum;
}
Also used : IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable)

Example 63 with IASTAppendable

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

the class CreamConvert method integerSolve.

/**
 * Create a cream integer solver.
 *
 * @param list
 * @param equationVariables all variables which are defined in the equations
 * @param userDefinedVariables all variables which are defined by the user. May contain additional
 *        variables which aren't available in <code>equationVariables</code>
 * @param engine
 * @return a list of rules with the integer solutions
 */
public IAST integerSolve(final IAST list, final IAST equationVariables, final IAST userDefinedVariables, int maximumNumberOfResults, final EvalEngine engine) {
    IASTAppendable result = F.ListAlloc();
    Solver solver = new DefaultSolver(expr2Cream(list, equationVariables), Solver.DEFAULT);
    // call with timeout
    solver.findAll(new CreamSolutionHandler(result, equationVariables, userDefinedVariables, maximumNumberOfResults, engine), 10000);
    return result;
}
Also used : DefaultSolver(jp.ac.kobe_u.cs.cream.DefaultSolver) Solver(jp.ac.kobe_u.cs.cream.Solver) IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) DefaultSolver(jp.ac.kobe_u.cs.cream.DefaultSolver)

Example 64 with IASTAppendable

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

the class Pods method parseInput.

/**
 * package private
 */
static IExpr parseInput(String inputStr, EvalEngine engine) {
    engine.setPackageMode(false);
    IExpr inExpr = F.NIL;
    // }
    if (!inExpr.isPresent()) {
        final FuzzyParser parser = new FuzzyParser(engine);
        try {
            inExpr = parser.parseFuzzyList(inputStr);
        } catch (SyntaxError serr) {
            // this includes syntax errors
            LOGGER.debug("Pods: FuzzyParser.parseFuzzyList() failed", serr);
            TeXParser texConverter = new TeXParser(engine);
            inExpr = texConverter.toExpression(inputStr);
        }
    }
    if (inExpr == S.$Aborted) {
        return F.NIL;
    }
    if (inExpr.isList() && inExpr.size() == 2) {
        inExpr = inExpr.first();
    }
    if (inExpr.isTimes() && !inExpr.isNumericFunction(true) && inExpr.argSize() <= 4) {
        if (((IAST) inExpr).isEvalFlagOn(IAST.TIMES_PARSED_IMPLICIT)) {
            inExpr = flattenTimes((IAST) inExpr).orElse(inExpr);
            IAST rest = ((IAST) inExpr).setAtClone(0, S.List);
            IASTAppendable specialFunction = F.NIL;
            String stemForm = getStemForm(rest.arg1().toString().toLowerCase());
            IExpr head = rest.head();
            if (stemForm != null) {
                head = STEM.getSymbol(stemForm);
                if (head != null) {
                    specialFunction = rest.setAtClone(0, head);
                    specialFunction.remove(1);
                }
            }
            if (!specialFunction.isPresent()) {
                stemForm = getStemForm(rest.last().toString().toLowerCase());
                if (stemForm != null) {
                    head = STEM.getSymbol(stemForm);
                    if (head != null) {
                        specialFunction = rest.setAtClone(0, head);
                        specialFunction.remove(rest.size() - 1);
                    }
                }
            }
            if (specialFunction.isPresent()) {
                if (head != null) {
                    if (head == S.UnitConvert) {
                        IExpr temp = unitConvert(engine, rest.rest());
                        if (temp.isPresent()) {
                            return temp;
                        }
                    } else {
                        int i = 1;
                        while (i < specialFunction.size()) {
                            String argStr = specialFunction.get(i).toString().toLowerCase();
                            if (argStr.equalsIgnoreCase("by") || argStr.equalsIgnoreCase("for")) {
                                specialFunction.remove(i);
                                continue;
                            }
                            i++;
                        }
                        return specialFunction;
                    }
                }
            }
            if (rest.arg1().toString().equalsIgnoreCase("convert")) {
                rest = inExpr.rest();
            }
            if (rest.argSize() > 2) {
                rest = rest.removeIf(x -> x.toString().equals("in"));
            }
            IExpr temp = unitConvert(engine, rest);
            if (temp.isPresent()) {
                return temp;
            }
        }
    }
    return inExpr;
}
Also used : IStringX(org.matheclipse.core.interfaces.IStringX) Level(org.apache.logging.log4j.Level) IInteger(org.matheclipse.core.interfaces.IInteger) StringUtils(org.apache.commons.lang3.StringUtils) ThreadLocalNotifyingAppender(org.matheclipse.logging.ThreadLocalNotifyingAppender) TeXUtilities(org.matheclipse.core.eval.TeXUtilities) IDistribution(org.matheclipse.core.interfaces.IDistribution) Trie(org.matheclipse.parser.trie.Trie) VariablesSet(org.matheclipse.core.convert.VariablesSet) Map(java.util.Map) EvalEngine(org.matheclipse.core.eval.EvalEngine) ID(org.matheclipse.core.expression.ID) IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) Set(java.util.Set) LevenshteinDistance(org.apache.commons.text.similarity.LevenshteinDistance) JSBuilder(org.matheclipse.core.form.output.JSBuilder) ISymbol(org.matheclipse.core.interfaces.ISymbol) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) MathMLUtilities(org.matheclipse.core.eval.MathMLUtilities) Logger(org.apache.logging.log4j.Logger) StringFunctions(org.matheclipse.core.builtin.StringFunctions) ExprParser(org.matheclipse.core.parser.ExprParser) GraphExpr(org.matheclipse.core.expression.data.GraphExpr) StandardTokenizer(org.apache.lucene.analysis.standard.StandardTokenizer) TrieBuilder(org.matheclipse.parser.trie.TrieBuilder) IEvaluator(org.matheclipse.core.interfaces.IEvaluator) IFraction(org.matheclipse.core.interfaces.IFraction) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Encode(org.owasp.encoder.Encode) Message(org.apache.logging.log4j.message.Message) WriterOutputStream(org.matheclipse.core.eval.util.WriterOutputStream) Documentation(org.matheclipse.core.form.Documentation) TrieMatch(org.matheclipse.parser.trie.TrieMatch) Suppliers(com.google.common.base.Suppliers) FuzzyParser(org.matheclipse.api.parser.FuzzyParser) ThreadLocalNotifierClosable(org.matheclipse.logging.ThreadLocalNotifyingAppender.ThreadLocalNotifierClosable) SyntaxError(org.matheclipse.parser.client.SyntaxError) Soundex(org.apache.commons.codec.language.Soundex) PrintStream(java.io.PrintStream) CharTermAttribute(org.apache.lucene.analysis.tokenattributes.CharTermAttribute) TokenStream(org.apache.lucene.analysis.TokenStream) F(org.matheclipse.core.expression.F) IAST(org.matheclipse.core.interfaces.IAST) IBuiltInSymbol(org.matheclipse.core.interfaces.IBuiltInSymbol) StringWriter(java.io.StringWriter) PorterStemFilter(org.apache.lucene.analysis.en.PorterStemFilter) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Config(org.matheclipse.core.basic.Config) GraphFunctions(org.matheclipse.core.builtin.GraphFunctions) IOException(java.io.IOException) StringEscapeUtils(org.apache.commons.text.StringEscapeUtils) S(org.matheclipse.core.expression.S) StringReader(java.io.StringReader) ElementData1(org.matheclipse.core.data.ElementData1) TeXParser(org.matheclipse.core.form.tex.TeXParser) IExpr(org.matheclipse.core.interfaces.IExpr) Comparator(java.util.Comparator) Collections(java.util.Collections) LogManager(org.apache.logging.log4j.LogManager) RomanArabicConverter(com.baeldung.algorithms.romannumerals.RomanArabicConverter) FuzzyParser(org.matheclipse.api.parser.FuzzyParser) IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) SyntaxError(org.matheclipse.parser.client.SyntaxError) TeXParser(org.matheclipse.core.form.tex.TeXParser) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST)

Example 65 with IASTAppendable

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

the class Pods method flattenTimesRecursive.

public static IASTAppendable flattenTimesRecursive(final IAST ast) {
    int[] newSize = new int[1];
    newSize[0] = 0;
    boolean[] flattened = new boolean[] { false };
    ast.forEach(expr -> {
        if (ast.isTimes() && ast.isEvalFlagOn(IAST.TIMES_PARSED_IMPLICIT)) {
            flattened[0] = true;
            newSize[0] += ast.size();
        } else {
            newSize[0]++;
        }
    });
    if (flattened[0]) {
        IASTAppendable result = F.ast(ast.head(), newSize[0], false);
        ast.forEach(expr -> {
            if (expr.isTimes() && ((IAST) expr).isEvalFlagOn(IAST.TIMES_PARSED_IMPLICIT)) {
                result.appendArgs(flattenTimesRecursive((IAST) expr).orElse((IAST) expr));
            } else {
                result.append(expr);
            }
        });
        return result;
    }
    return F.NIL;
}
Also used : IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) IAST(org.matheclipse.core.interfaces.IAST)

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