Search in sources :

Example 26 with IASTMutable

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

the class RootsFunctions method unitPolynomial.

/**
 * Solve polynomials of the form <code>a * x^varDegree + b == 0</code>
 *
 * @param varDegree
 * @param polynomial
 * @return
 */
private static IASTAppendable unitPolynomial(int varDegree, ExprPolynomial polynomial) {
    IExpr a = C0;
    IExpr b = C0;
    for (ExprMonomial monomial : polynomial) {
        IExpr coeff = monomial.coefficient();
        long lExp = monomial.exponent().getVal(0);
        if (lExp == varDegree) {
            a = coeff;
        } else if (lExp == 0) {
            b = coeff;
        } else {
            return F.NIL;
        }
    }
    if (a.isZero() || b.isZero()) {
        return F.NIL;
    }
    boolean isNegative = false;
    IExpr rhsNumerator = EvalEngine.get().evaluate(b.negate());
    // EvalEngine.get().evaluate(F.Divide(F.C1, a));
    IExpr rhsDenominator = a;
    if ((varDegree & 0x0001) == 0x0001) {
        // odd
        IExpr zNumerator;
        if (rhsNumerator.isTimes()) {
            IASTMutable temp = ((IAST) rhsNumerator).mapThread(F.Power(F.Slot1, F.fraction(1, varDegree)), 1);
            if (rhsNumerator.first().isNegative()) {
                isNegative = true;
                temp.set(1, rhsNumerator.first().negate());
            }
            zNumerator = EvalEngine.get().evaluate(temp);
        } else {
            if (rhsNumerator.isNegative()) {
                isNegative = true;
                rhsNumerator = rhsNumerator.negate();
            }
            zNumerator = EvalEngine.get().evaluate(F.Power(rhsNumerator, F.fraction(1, varDegree)));
        }
        IExpr zDenominator;
        if (rhsDenominator.isTimes()) {
            IASTMutable temp = ((IAST) rhsDenominator).mapThread(F.Power(F.Slot1, F.fraction(-1, varDegree)), 1);
            if (rhsDenominator.first().isNegative()) {
                isNegative = !isNegative;
                temp.set(1, rhsDenominator.first().negate());
            }
            zDenominator = EvalEngine.get().evaluate(temp);
        } else {
            if (rhsDenominator.isNegative()) {
                isNegative = !isNegative;
                rhsDenominator = rhsDenominator.negate();
            }
            zDenominator = EvalEngine.get().evaluate(F.Power(rhsDenominator, F.fraction(-1, varDegree)));
        }
        IASTAppendable result = F.ListAlloc(varDegree);
        for (int i = 0; i < varDegree; i++) {
            if (isNegative) {
                result.append(F.Times(F.Power(F.CN1, i + 1), F.Power(F.CN1, F.fraction(i, varDegree)), zNumerator, zDenominator));
            } else {
                result.append(F.Times(F.Power(F.CN1, i), F.Power(F.CN1, F.fraction(i, varDegree)), zNumerator, zDenominator));
            }
        }
        return result;
    } else {
        // even
        IExpr zNumerator;
        if (rhsNumerator.isTimes()) {
            IExpr temp = ((IAST) rhsNumerator).mapThread(F.Power(F.Slot1, F.fraction(1, varDegree)), 1);
            zNumerator = EvalEngine.get().evaluate(temp);
        } else {
            zNumerator = EvalEngine.get().evaluate(F.Power(rhsNumerator, F.fraction(1, varDegree)));
        }
        IExpr zDenominator;
        if (rhsDenominator.isTimes()) {
            IExpr temp = ((IAST) rhsDenominator).mapThread(F.Power(F.Slot1, F.fraction(-1, varDegree)), 1);
            zDenominator = EvalEngine.get().evaluate(temp);
        } else {
            zDenominator = EvalEngine.get().evaluate(F.Power(rhsDenominator, F.fraction(-1, varDegree)));
        }
        IASTAppendable result = F.ListAlloc(varDegree);
        long size = varDegree / 2;
        // isNegative?1:0;
        int k = 0;
        for (int i = 1; i <= size; i++) {
            result.append(F.Times(F.CN1, F.Power(F.CN1, F.fraction(k, varDegree)), zNumerator, zDenominator));
            result.append(F.Times(F.Power(F.CN1, F.fraction(k, varDegree)), zNumerator, zDenominator));
            k += 2;
        }
        return result;
    }
}
Also used : IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) IExpr(org.matheclipse.core.interfaces.IExpr) IASTMutable(org.matheclipse.core.interfaces.IASTMutable) IAST(org.matheclipse.core.interfaces.IAST) ExprMonomial(org.matheclipse.core.polynomials.longexponent.ExprMonomial)

Example 27 with IASTMutable

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

the class RootsFunctions method rootsOfExprPolynomial.

public static IASTMutable rootsOfExprPolynomial(final IExpr expr, IAST varList, boolean rootsOfQuartic) {
    IASTMutable result = F.NIL;
    try {
        // try to generate a common expression polynomial
        ExprPolynomialRing ring = new ExprPolynomialRing(ExprRingFactory.CONST, varList);
        ExprPolynomial ePoly = ring.create(expr, false, false, false);
        ePoly = ePoly.multiplyByMinimumNegativeExponents();
        if (ePoly.degree(0) >= Integer.MAX_VALUE) {
            return F.NIL;
        }
        if (ePoly.degree(0) >= 3) {
            result = unitPolynomial((int) ePoly.degree(0), ePoly);
            if (result.isPresent()) {
                result = QuarticSolver.sortASTArguments(result);
                return result;
            }
        }
        if (!rootsOfQuartic && ePoly.degree(0) > 2) {
            return F.NIL;
        }
        result = rootsOfQuarticPolynomial(ePoly);
        if (result.isPresent()) {
            if (expr.isNumericMode()) {
                for (int i = 1; i < result.size(); i++) {
                    result.set(i, F.chopExpr(result.get(i), Config.DEFAULT_ROOTS_CHOP_DELTA));
                }
            }
            result = QuarticSolver.sortASTArguments(result);
            return result;
        }
    } catch (JASConversionException e2) {
        LOGGER.debug("RootsFunctions.rootsOfExprPolynomial() failed", e2);
    }
    return F.NIL;
}
Also used : ExprPolynomialRing(org.matheclipse.core.polynomials.longexponent.ExprPolynomialRing) IASTMutable(org.matheclipse.core.interfaces.IASTMutable) JASConversionException(org.matheclipse.core.eval.exception.JASConversionException) ExprPolynomial(org.matheclipse.core.polynomials.longexponent.ExprPolynomial)

Example 28 with IASTMutable

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

the class NumericArrayExpr method normalRecursive.

private static void normalRecursive(byte[] byteArray, boolean unsigned, IASTMutable list, int[] dimension, int position, int[] index) {
    int size = dimension[position];
    if (dimension.length - 1 == position) {
        for (int i = 1; i <= size; i++) {
            IInteger intValue = unsigned ? F.ZZ(Byte.toUnsignedInt(byteArray[index[0]++])) : F.ZZ(byteArray[index[0]++]);
            list.set(i, intValue);
        }
        return;
    }
    int size2 = dimension[position + 1];
    for (int i = 1; i <= size; i++) {
        IASTMutable currentList = F.astMutable(S.List, size2);
        list.set(i, currentList);
        normalRecursive(byteArray, unsigned, currentList, dimension, position + 1, index);
    }
}
Also used : IInteger(org.matheclipse.core.interfaces.IInteger) IASTMutable(org.matheclipse.core.interfaces.IASTMutable)

Example 29 with IASTMutable

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

the class NumericArrayExpr method normalAppendable.

private IASTMutable normalAppendable(IExpr head, int[] dims) {
    IASTMutable list = F.astMutable(head, dims[0]);
    int[] index = new int[1];
    switch(fType) {
        case Integer8:
            normalRecursive((byte[]) fData, false, list, dims, 0, index);
            break;
        case Integer16:
            normalRecursive((short[]) fData, false, list, dims, 0, index);
            break;
        case Integer32:
            normalRecursive((int[]) fData, false, list, dims, 0, index);
            break;
        case Integer64:
            normalRecursive((long[]) fData, false, list, dims, 0, index);
            break;
        case UnsignedInteger8:
            normalRecursive((byte[]) fData, true, list, dims, 0, index);
            break;
        case UnsignedInteger16:
            normalRecursive((short[]) fData, true, list, dims, 0, index);
            break;
        case UnsignedInteger32:
            normalRecursive((int[]) fData, true, list, dims, 0, index);
            break;
        case UnsignedInteger64:
            normalRecursive((long[]) fData, true, list, dims, 0, index);
            break;
        case Real32:
            normalRecursive((float[]) fData, list, dims, 0, index);
            break;
        case Real64:
            normalRecursive((double[]) fData, list, dims, 0, index);
            break;
        case ComplexReal32:
            normalRecursiveComplex((float[]) fData, list, dims, 0, index);
            break;
        case ComplexReal64:
            normalRecursiveComplex((double[]) fData, list, dims, 0, index);
            break;
    }
    return list;
}
Also used : IASTMutable(org.matheclipse.core.interfaces.IASTMutable)

Example 30 with IASTMutable

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

the class SparseArrayExpr method arrayRules.

/**
 * Create array rules from the nested lists. From array rules a sparse array can be created.
 *
 * @param nestedListsOfValues
 * @return
 */
public static IAST arrayRules(IAST nestedListsOfValues, IExpr defaultValue) {
    int depth = SparseArrayExpr.depth(nestedListsOfValues, 1);
    if (depth < 0) {
        return F.NIL;
    }
    // default value rule is additionally appended at the end!
    IASTAppendable result = F.ListAlloc(F.allocMin32(F.allocLevel1(nestedListsOfValues, x -> x.isList()) + 2));
    IASTMutable positions = F.constantArray(F.C1, depth);
    if (SparseArrayExpr.arrayRulesRecursive(nestedListsOfValues, depth + 1, depth, positions, defaultValue, result)) {
        result.append(F.Rule(F.constantArray(F.$b(), depth), defaultValue));
        return result;
    }
    return F.NIL;
}
Also used : Arrays(java.util.Arrays) LinearAlgebra(org.matheclipse.core.builtin.LinearAlgebra) FieldElement(org.hipparchus.FieldElement) OpenMapRealMatrix(org.hipparchus.linear.OpenMapRealMatrix) FieldVector(org.hipparchus.linear.FieldVector) ObjectOutput(java.io.ObjectOutput) MathRuntimeException(org.hipparchus.exception.MathRuntimeException) OpenMapRealVector(org.hipparchus.linear.OpenMapRealVector) NullArgumentException(org.hipparchus.exception.NullArgumentException) DataExpr(org.matheclipse.core.expression.DataExpr) IOFunctions(org.matheclipse.core.builtin.IOFunctions) Function(java.util.function.Function) RealMatrix(org.hipparchus.linear.RealMatrix) Trie(org.matheclipse.parser.trie.Trie) ISparseArray(org.matheclipse.core.interfaces.ISparseArray) Tensors(org.matheclipse.core.generic.Tensors) EvalEngine(org.matheclipse.core.eval.EvalEngine) IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) F(org.matheclipse.core.expression.F) IAST(org.matheclipse.core.interfaces.IAST) Externalizable(java.io.Externalizable) IPatternMap(org.matheclipse.core.patternmatching.IPatternMap) Config(org.matheclipse.core.basic.Config) IOException(java.io.IOException) Field(org.hipparchus.Field) MathUtils(org.hipparchus.util.MathUtils) S(org.matheclipse.core.expression.S) IASTMutable(org.matheclipse.core.interfaces.IASTMutable) PatternMatcherAndEvaluator(org.matheclipse.core.patternmatching.PatternMatcherAndEvaluator) Logger(org.apache.logging.log4j.Logger) IntList(it.unimi.dsi.fastutil.ints.IntList) MathIllegalArgumentException(org.hipparchus.exception.MathIllegalArgumentException) AbstractFieldMatrix(org.hipparchus.linear.AbstractFieldMatrix) FieldMatrix(org.hipparchus.linear.FieldMatrix) IExpr(org.matheclipse.core.interfaces.IExpr) ObjectInput(java.io.ObjectInput) LocalizedCoreFormats(org.hipparchus.exception.LocalizedCoreFormats) RealVector(org.hipparchus.linear.RealVector) LogManager(org.apache.logging.log4j.LogManager) TrieNode(org.matheclipse.parser.trie.TrieNode) ArgumentTypeException(org.matheclipse.core.eval.exception.ArgumentTypeException) IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) IASTMutable(org.matheclipse.core.interfaces.IASTMutable)

Aggregations

IASTMutable (org.matheclipse.core.interfaces.IASTMutable)92 IExpr (org.matheclipse.core.interfaces.IExpr)60 IAST (org.matheclipse.core.interfaces.IAST)34 IASTAppendable (org.matheclipse.core.interfaces.IASTAppendable)26 ISymbol (org.matheclipse.core.interfaces.ISymbol)12 IInteger (org.matheclipse.core.interfaces.IInteger)5 Map (java.util.Map)4 IComplex (org.matheclipse.core.interfaces.IComplex)4 IRational (org.matheclipse.core.interfaces.IRational)4 ArrayList (java.util.ArrayList)3 TreeMap (java.util.TreeMap)3 EvalEngine (org.matheclipse.core.eval.EvalEngine)3 JASConversionException (org.matheclipse.core.eval.exception.JASConversionException)3 ValidateException (org.matheclipse.core.eval.exception.ValidateException)3 INumber (org.matheclipse.core.interfaces.INumber)3 IPatternObject (org.matheclipse.core.interfaces.IPatternObject)3 ISparseArray (org.matheclipse.core.interfaces.ISparseArray)3 ASTNode (org.matheclipse.parser.client.ast.ASTNode)3 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)2 AST2Expr (org.matheclipse.core.convert.AST2Expr)2