Search in sources :

Example 11 with IRational

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

the class PolynomialHomogenization method determineLCM.

private void determineLCM(final IExpr expression) {
    if (expression instanceof IAST) {
        final IAST ast = (IAST) expression;
        if (ast.isPlus() || ast.isTimes()) {
            for (int i = 1; i < ast.size(); i++) {
                determineLCM(ast.get(i));
            }
            return;
        } else if (ast.isPower()) {
            IExpr exp = ast.exponent();
            IExpr base = ast.base();
            if (exp.isReal()) {
                IInteger lcm = F.C1;
                IRational rat = ((ISignedNumber) exp).rationalFactor();
                if (rat == null) {
                    return;
                }
                if (!rat.isInteger()) {
                    IInteger denominator = rat.denominator();
                    if (denominator.isNegative()) {
                        denominator = denominator.negate();
                    }
                    lcm = denominator;
                }
                // if (base.isTimes()) {
                // 
                // }
                replaceExpressionLCM(base, lcm);
                return;
            }
            if (exp.isTimes()) {
                determineTimes(ast, base, (IAST) exp);
                // ((IAST) exp).forEach(x -> determineLCM(F.Power(base, x)));
                return;
            } else if (exp.isPlus()) {
                // && base.isExactNumber()) {
                // ex: 4^(2*x+3)
                IAST plusAST = (IAST) exp;
                if (plusAST.first().isInteger()) {
                    determineLCM(S.Power.of(base, plusAST.first()));
                    determineLCM(S.Power.of(base, plusAST.rest().oneIdentity0()));
                    return;
                }
            }
            replaceExpressionLCM(ast, F.C1);
            return;
        }
        replaceExpressionLCM(expression, F.C1);
        return;
    }
    if (expression instanceof ISymbol) {
        replaceExpressionLCM(expression, F.C1);
    }
}
Also used : ISymbol(org.matheclipse.core.interfaces.ISymbol) IInteger(org.matheclipse.core.interfaces.IInteger) IRational(org.matheclipse.core.interfaces.IRational) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 12 with IRational

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

the class D method binaryD.

/**
 * Evaluate <code>D(functionO>fX, x)</code> for some general cases.
 *
 * @param functionOfX the function of <code>x</code>
 * @param x derive w.r.t this variable
 * @param ast
 * @param engine
 * @return
 */
private static IExpr binaryD(final IExpr functionOfX, IExpr x, final IAST ast, EvalEngine engine) {
    int[] dim = functionOfX.isPiecewise();
    if (dim != null) {
        return dPiecewise(dim, (IAST) functionOfX, ast, engine);
    }
    if (functionOfX instanceof ASTSeriesData) {
        ASTSeriesData series = ((ASTSeriesData) functionOfX);
        if (series.getX().equals(x)) {
            final IExpr temp = ((ASTSeriesData) functionOfX).derive(x);
            if (temp != null) {
                return temp;
            }
            return F.NIL;
        }
        return F.C0;
    }
    if (functionOfX.isFree(x, true)) {
        return F.C0;
    }
    if (functionOfX.isNumber()) {
        // D[x_?NumberQ,y_] -> 0
        return F.C0;
    }
    if (functionOfX.equals(x)) {
        // D[x_,x_] -> 1
        return F.C1;
    }
    if (functionOfX.isAST()) {
        final IAST function = (IAST) functionOfX;
        final IExpr header = function.head();
        if (function.isPlus()) {
            // D(a_+b_+c_,x_) -> D(a,x)+D(b,x)+D(c,x)
            IExpr result = function.mapThread(F.D(F.Slot1, x), 1);
            return engine.evaluate(result);
        } else if (function.isTimes()) {
            return function.map(F.PlusAlloc(16), new BinaryBindIth1st(function, F.D(S.Null, x)));
        } else if (function.isPower()) {
            // f ^ g
            final IExpr f = function.base();
            final IExpr g = function.exponent();
            if (g.isFree(x)) {
                // g*D(f,y)*f^(g-1)
                return F.Times(g, F.D(f, x), F.Power(f, g.dec()));
            }
            if (f.isFree(x)) {
                if (f.isE()) {
                    return F.Times(F.D(g, x), F.Exp(g));
                }
                // D(g,y)*Log(f)*f^g
                return F.Times(F.D(g, x), F.Log(f), F.Power(f, g));
            }
            // D[f_^g_,y_]:= f^g*(((g*D[f,y])/f)+Log[f]*D[g,y])
            final IASTAppendable resultList = F.TimesAlloc(2);
            resultList.append(F.Power(f, g));
            resultList.append(F.Plus(F.Times(g, F.D(f, x), F.Power(f, F.CN1)), F.Times(F.Log(f), F.D(g, x))));
            return resultList;
        } else if (function.isAST(S.Surd, 3)) {
            // Surd[f,g]
            final IExpr f = function.base();
            if (function.exponent().isInteger()) {
                final IInteger g = (IInteger) function.exponent();
                if (g.isMinusOne()) {
                    return F.Times(F.CN1, F.D(f, x), F.Power(f, F.CN2));
                }
                final IRational gInverse = g.inverse();
                if (g.isNegative()) {
                    if (g.isEven()) {
                        return F.Times(gInverse, F.D(f, x), F.Power(F.Surd(f, g.negate()), g.dec()));
                    }
                    return F.Times(gInverse, F.D(f, x), F.Power(f, F.CN1), F.Power(F.Surd(f, g.negate()), F.CN1));
                }
                return F.Times(gInverse, F.D(f, x), F.Power(F.Surd(f, g), g.dec().negate()));
            }
        } else if ((header == S.Log) && (function.isAST2())) {
            if (function.isFreeAt(1, x)) {
                // D[Log[i_FreeQ(x), x_], z_]:= (x*Log[a])^(-1)*D[x,z];
                return F.Times(F.Power(F.Times(function.arg2(), F.Log(function.arg1())), F.CN1), F.D(function.arg2(), x));
            }
        // } else if (header == F.LaplaceTransform && (listArg1.size()
        // == 4)) {
        // if (listArg1.arg3().equals(x) && listArg1.arg1().isFree(x,
        // true)) {
        // // D(LaplaceTransform(c,t,s), s) -> -c / s^2
        // return F.Times(-1L, listArg1.arg2(), F.Power(x, -2L));
        // } else if (listArg1.arg1().equals(x)) {
        // // D(LaplaceTransform(c,t,s), c) -> 1/s
        // return F.Power(x, -1L);
        // } else if (listArg1.arg1().isFree(x, true) &&
        // listArg1.arg2().isFree(x, true) && listArg1.arg3().isFree(x,
        // true))
        // {
        // // D(LaplaceTransform(c,t,s), w) -> 0
        // return F.C0;
        // } else if (listArg1.arg2().equals(x)) {
        // // D(LaplaceTransform(c,t,s), t) -> 0
        // return F.C0;
        // }
        } else if (function.isAST1() && ast.isEvalFlagOff(IAST.IS_DERIVATIVE_EVALED)) {
            IAST[] derivStruct = function.isDerivativeAST1();
            if (derivStruct != null && derivStruct[2] != null) {
                IAST headAST = derivStruct[1];
                IAST a1Head = derivStruct[0];
                if (a1Head.isAST1() && a1Head.arg1().isInteger()) {
                    try {
                        int n = ((IInteger) a1Head.arg1()).toInt();
                        IExpr arg1 = function.arg1();
                        if (n > 0) {
                            IAST fDerivParam = Derivative.createDerivative(n + 1, headAST.arg1(), arg1);
                            if (x.equals(arg1)) {
                                return fDerivParam;
                            }
                            return F.Times(F.D(arg1, x), fDerivParam);
                        }
                    } catch (ArithmeticException ae) {
                    }
                }
                return F.NIL;
            }
            return getDerivativeArg1(x, function.arg1(), header, engine);
        } else if (function.isAST() && ast.isEvalFlagOff(IAST.IS_DERIVATIVE_EVALED)) {
            return getDerivativeArgN(x, function, header);
        }
    }
    return F.NIL;
}
Also used : IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) BinaryBindIth1st(org.matheclipse.core.generic.BinaryBindIth1st) IInteger(org.matheclipse.core.interfaces.IInteger) ASTSeriesData(org.matheclipse.core.expression.ASTSeriesData) IRational(org.matheclipse.core.interfaces.IRational) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST)

Example 13 with IRational

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

the class TeXFormFactory method convertComplex.

public void convertComplex(final StringBuffer buf, final IComplex c, final int precedence) {
    if (c.isImaginaryUnit()) {
        buf.append("i ");
        return;
    }
    if (precedence > plusPrec) {
        buf.append("\\left( ");
    }
    IRational re = c.getRealPart();
    IRational im = c.getImaginaryPart();
    if (!re.isZero()) {
        convert(buf, c.getRealPart(), 0);
        if (im.compareInt(0) >= 0) {
            buf.append(" + ");
        } else {
            buf.append(" - ");
            im = im.negate();
        }
    }
    convert(buf, im, 0);
    // InvisibleTimes
    buf.append("\\,");
    buf.append("i ");
    if (precedence > plusPrec) {
        buf.append("\\right) ");
    }
}
Also used : IRational(org.matheclipse.core.interfaces.IRational)

Example 14 with IRational

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

the class ContinuedFraction method evaluate.

@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
    Validate.checkRange(ast, 2, 3);
    IExpr arg1 = ast.arg1();
    int maxIterations = Integer.MAX_VALUE;
    if (ast.isAST2() && ast.arg2().isInteger()) {
        maxIterations = Validate.checkIntType(ast, 2);
    }
    if (arg1 instanceof INum) {
        arg1 = F.fraction(((INum) arg1).getRealPart());
    } else if (arg1.isAST() || arg1.isSymbol() && arg1.isNumericFunction()) {
        IExpr num = engine.evalN(arg1);
        if (num instanceof INum) {
            arg1 = F.fraction(((INum) num).getRealPart());
        }
    }
    if (arg1.isRational()) {
        IRational rat = (IRational) arg1;
        IAST continuedFractionList;
        if (rat.getDenominator().isOne()) {
            continuedFractionList = F.List(rat.getNumerator());
        } else if (rat.getNumerator().isOne()) {
            continuedFractionList = F.ListAlloc(2);
            continuedFractionList.append(F.C0);
            continuedFractionList.append(rat.getDenominator());
        } else {
            IFraction temp = F.fraction(rat.getNumerator(), rat.getDenominator());
            IInteger quotient;
            IInteger remainder;
            continuedFractionList = F.ListAlloc(10);
            while (temp.getDenominator().compareInt(1) > 0 && (0 < maxIterations--)) {
                quotient = temp.getNumerator().div(temp.getDenominator());
                remainder = temp.getNumerator().mod(temp.getDenominator());
                continuedFractionList.append(quotient);
                temp = F.fraction(temp.getDenominator(), remainder);
                if (temp.getDenominator().isOne()) {
                    continuedFractionList.append(temp.getNumerator());
                }
            }
        }
        return continuedFractionList;
    }
    return F.NIL;
}
Also used : IFraction(org.matheclipse.core.interfaces.IFraction) IInteger(org.matheclipse.core.interfaces.IInteger) IRational(org.matheclipse.core.interfaces.IRational) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST) INum(org.matheclipse.core.interfaces.INum)

Example 15 with IRational

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

the class JASConvert method rationalPoly2Expr.

/**
 * Converts a <a href="http://krum.rz.uni-mannheim.de/jas/">JAS</a> polynomial to a Symja AST with
 * head <code>Plus</code>
 *
 * @param poly a JAS polynomial
 * @param factorTerms
 * @return
 * @throws ArithmeticException
 * @throws JASConversionException
 */
public IAST rationalPoly2Expr(final GenPolynomial<BigRational> poly, boolean factorTerms) throws ArithmeticException, JASConversionException {
    if (poly.length() == 0) {
        return F.Plus(F.C0);
    }
    if (factorTerms) {
        // pull out overall numerical factor in poly.
        Object[] objects = factorTerms(poly);
        GenPolynomial<edu.jas.arith.BigInteger> p2 = (GenPolynomial<edu.jas.arith.BigInteger>) objects[2];
        java.math.BigInteger gcd = (java.math.BigInteger) objects[0];
        java.math.BigInteger lcm = (java.math.BigInteger) objects[1];
        IRational factor = F.fraction(gcd, lcm).normalize();
        IASTAppendable result = F.PlusAlloc(p2.length());
        for (Monomial<edu.jas.arith.BigInteger> monomial : p2) {
            edu.jas.arith.BigInteger coeff = monomial.coefficient();
            ExpVector exp = monomial.exponent();
            IASTAppendable monomTimes = F.TimesAlloc(exp.length() + 1);
            monomialToExpr(coeff, exp, monomTimes);
            result.append(monomTimes.oneIdentity1());
        }
        if (factor.isOne()) {
            return result;
        }
        return F.Times(F.fraction(gcd, lcm), result);
    }
    IASTAppendable result = F.PlusAlloc(poly.length());
    for (Monomial<BigRational> monomial : poly) {
        BigRational coeff = monomial.coefficient();
        ExpVector exp = monomial.exponent();
        IASTAppendable monomTimes = F.TimesAlloc(exp.length() + 1);
        monomialToExpr(coeff, exp, monomTimes);
        result.append(monomTimes.oneIdentity1());
    }
    return result;
}
Also used : GenPolynomial(edu.jas.poly.GenPolynomial) BigRational(edu.jas.arith.BigRational) IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) ExpVector(edu.jas.poly.ExpVector) BigInteger(java.math.BigInteger) IRational(org.matheclipse.core.interfaces.IRational) BigInteger(java.math.BigInteger)

Aggregations

IRational (org.matheclipse.core.interfaces.IRational)30 IExpr (org.matheclipse.core.interfaces.IExpr)17 IInteger (org.matheclipse.core.interfaces.IInteger)11 ISymbol (org.matheclipse.core.interfaces.ISymbol)8 IAST (org.matheclipse.core.interfaces.IAST)6 IComplex (org.matheclipse.core.interfaces.IComplex)6 EvalEngine (org.matheclipse.core.eval.EvalEngine)5 IASTMutable (org.matheclipse.core.interfaces.IASTMutable)5 IFraction (org.matheclipse.core.interfaces.IFraction)4 INum (org.matheclipse.core.interfaces.INum)4 ISignedNumber (org.matheclipse.core.interfaces.ISignedNumber)4 Num (org.matheclipse.core.expression.Num)3 IASTAppendable (org.matheclipse.core.interfaces.IASTAppendable)3 BigRational (edu.jas.arith.BigRational)2 GenPolynomial (edu.jas.poly.GenPolynomial)2 BigInteger (java.math.BigInteger)2 JASIExpr (org.matheclipse.core.convert.JASIExpr)2 ArgumentTypeException (org.matheclipse.core.eval.exception.ArgumentTypeException)2 LimitException (org.matheclipse.core.eval.exception.LimitException)2 Complex (edu.jas.poly.Complex)1