Search in sources :

Example 1 with List

use of org.matheclipse.core.expression.F.List in project symja_android_library by axkr.

the class Algebra method partialFractionDecompositionRational.

/**
	 * Returns an AST with head <code>Plus</code>, which contains the partial fraction decomposition of the numerator
	 * and denominator parts.
	 * 
	 * @param pf
	 *            partial fraction generator
	 * @param parts
	 * @param variable
	 *            a variable
	 * @return <code>F.NIL</code> if the partial fraction decomposition wasn't constructed
	 */
public static IExpr partialFractionDecompositionRational(IPartialFractionGenerator pf, IExpr[] parts, ISymbol variable) {
    try {
        IAST variableList = F.List(variable);
        IExpr exprNumerator = F.evalExpandAll(parts[0]);
        IExpr exprDenominator = F.evalExpandAll(parts[1]);
        ASTRange r = new ASTRange(variableList, 1);
        List<IExpr> varList = r;
        String[] varListStr = new String[1];
        varListStr[0] = variableList.arg1().toString();
        JASConvert<BigRational> jas = new JASConvert<BigRational>(varList, BigRational.ZERO);
        GenPolynomial<BigRational> numerator = jas.expr2JAS(exprNumerator, false);
        GenPolynomial<BigRational> denominator = jas.expr2JAS(exprDenominator, false);
        // get factors
        FactorAbstract<BigRational> factorAbstract = FactorFactory.getImplementation(BigRational.ZERO);
        SortedMap<GenPolynomial<BigRational>, Long> sfactors = factorAbstract.baseFactors(denominator);
        List<GenPolynomial<BigRational>> D = new ArrayList<GenPolynomial<BigRational>>(sfactors.keySet());
        SquarefreeAbstract<BigRational> sqf = SquarefreeFactory.getImplementation(BigRational.ZERO);
        List<List<GenPolynomial<BigRational>>> Ai = sqf.basePartialFraction(numerator, sfactors);
        if (Ai.size() > 0) {
            // IAST result = F.Plus();
            pf.allocPlus(Ai.size() * 2);
            pf.setJAS(jas);
            if (!Ai.get(0).get(0).isZERO()) {
                pf.addNonFractionalPart(Ai.get(0).get(0));
            }
            for (int i = 1; i < Ai.size(); i++) {
                List<GenPolynomial<BigRational>> list = Ai.get(i);
                int j = 0;
                for (GenPolynomial<BigRational> genPolynomial : list) {
                    if (!genPolynomial.isZERO()) {
                        GenPolynomial<BigRational> Di_1 = D.get(i - 1);
                        pf.addSinglePartialFraction(genPolynomial, Di_1, j);
                    }
                    j++;
                }
            }
            return pf.getResult();
        }
    } catch (JASConversionException e) {
        if (Config.DEBUG) {
            e.printStackTrace();
        }
    }
    return F.NIL;
}
Also used : ASTRange(org.matheclipse.core.expression.ASTRange) GenPolynomial(edu.jas.poly.GenPolynomial) BigRational(edu.jas.arith.BigRational) ArrayList(java.util.ArrayList) JASConversionException(org.matheclipse.core.eval.exception.JASConversionException) ModLong(edu.jas.arith.ModLong) JASConvert(org.matheclipse.core.convert.JASConvert) List(org.matheclipse.core.expression.F.List) List(java.util.List) ArrayList(java.util.ArrayList) IAST(org.matheclipse.core.interfaces.IAST) JASIExpr(org.matheclipse.core.convert.JASIExpr) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 2 with List

use of org.matheclipse.core.expression.F.List in project symja_android_library by axkr.

the class Object2Expr method convert.

/**
	 * Converts the following J<va objects into an IExpr expression
	 * 
	 * <pre>
	 * Java Object     -&gt; MathEclipse object
	 * -------------------------------------
	 * null object          Null symbol
	 * IExpr                IExpr type
	 * Boolean              True or False symbol
	 * BigInteger           Integer value  
	 * java.math.BigInteger Integer value  
	 * BigDecimal           Double with doubleValue() value
	 * Double               Double with doubleValue() value
	 * Float                Double with doubleValue() value
	 * Number               Integer with longValue() value
	 * java.util.List       0-th element of the list gives the head of the function 
	 *                      1..nth element of the list give the arguments of the function
	 * Object[]             a list of converted objects  
	 * int[]                a list of <code>IntegerSym</code>Integer values
	 * double[]             a list of <code>Num</code> values
	 * double[][]           a matrix (i.e. nested lists) of Double values
	 * Complex[]            a list of <code>ComplexNum</code> values
	 * boolean[]            a list of True or False symbols
	 * 
	 * </pre>
	 * 
	 */
public static IExpr convert(Object obj) throws ConversionException {
    if (obj == null) {
        return F.Null;
    }
    if (obj instanceof IExpr) {
        return (IExpr) obj;
    }
    if (obj instanceof Boolean) {
        if (((Boolean) obj).booleanValue()) {
            return F.True;
        }
        return F.False;
    }
    if (obj instanceof BigInteger) {
        return F.integer((BigInteger) obj);
    }
    if (obj instanceof java.math.BigInteger) {
        return F.integer((java.math.BigInteger) obj);
    }
    if (obj instanceof Number) {
        if (obj instanceof BigDecimal) {
            return F.num(((BigDecimal) obj).doubleValue());
        }
        if (obj instanceof Double) {
            return F.num(((Double) obj).doubleValue());
        }
        if (obj instanceof Float) {
            return F.num(((Float) obj).doubleValue());
        }
        return F.integer(((Number) obj).longValue());
    }
    if (obj instanceof java.util.List) {
        final java.util.List<?> lst = (java.util.List<?>) obj;
        IAST list = F.NIL;
        if (lst.size() == 0) {
            list = List();
        } else {
            final ISymbol head = F.userSymbol(lst.get(0).toString());
            int size = lst.size();
            list = F.ast(head, size, false);
            for (int i = 1; i < size; i++) {
                list.append(convert(lst.get(i)));
            }
        }
        return list;
    }
    if (obj instanceof Object[]) {
        final Object[] array = (Object[]) obj;
        final IAST list = F.ListAlloc(array.length);
        for (int i = 0; i < array.length; i++) {
            list.append(convert(array[i]));
        }
        return list;
    }
    if (obj instanceof int[]) {
        return AST.newInstance(F.List, (int[]) obj);
    }
    if (obj instanceof double[]) {
        return AST.newInstance(F.List, (double[]) obj);
    }
    if (obj instanceof double[][]) {
        final double[][] dd = (double[][]) obj;
        final IAST list = F.ListAlloc(dd.length);
        for (int i = 0; i < dd.length; i++) {
            final IAST row = F.ListAlloc(dd[i].length);
            for (int j = 0; j < dd[i].length; j++) {
                row.append(F.num(dd[i][j]));
            }
            list.append(row);
        }
        return list;
    }
    if (obj instanceof org.hipparchus.complex.Complex[]) {
        return AST.newInstance(F.List, (org.hipparchus.complex.Complex[]) obj);
    }
    if (obj instanceof boolean[]) {
        final boolean[] array = (boolean[]) obj;
        final IAST list = F.ListAlloc(array.length);
        for (int i = 0; i < array.length; i++) {
            if (array[i]) {
                list.append(F.True);
            } else {
                list.append(F.False);
            }
        }
        return list;
    }
    return F.$str(obj.toString());
}
Also used : ISymbol(org.matheclipse.core.interfaces.ISymbol) BigDecimal(java.math.BigDecimal) BigInteger(java.math.BigInteger) List(org.matheclipse.core.expression.F.List) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST)

Aggregations

List (org.matheclipse.core.expression.F.List)2 IAST (org.matheclipse.core.interfaces.IAST)2 IExpr (org.matheclipse.core.interfaces.IExpr)2 BigRational (edu.jas.arith.BigRational)1 ModLong (edu.jas.arith.ModLong)1 GenPolynomial (edu.jas.poly.GenPolynomial)1 BigDecimal (java.math.BigDecimal)1 BigInteger (java.math.BigInteger)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 JASConvert (org.matheclipse.core.convert.JASConvert)1 JASIExpr (org.matheclipse.core.convert.JASIExpr)1 JASConversionException (org.matheclipse.core.eval.exception.JASConversionException)1 ASTRange (org.matheclipse.core.expression.ASTRange)1 ISymbol (org.matheclipse.core.interfaces.ISymbol)1