Search in sources :

Example 6 with IRational

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

the class ComplexSym method normalize.

@Override
public INumber normalize() {
    if (fImaginary.isZero()) {
        if (fReal instanceof IFraction) {
            if (fReal.getDenominator().isOne()) {
                return fReal.getNumerator();
            }
            if (fReal.getNumerator().isZero()) {
                return F.C0;
            }
        }
        return fReal;
    }
    boolean evaled = false;
    IRational newRe = fReal;
    IRational newIm = fImaginary;
    if (fReal instanceof IFraction) {
        if (fReal.getDenominator().isOne()) {
            newRe = fReal.getNumerator();
            evaled = true;
        }
        if (fReal.getNumerator().isZero()) {
            newRe = F.C0;
            evaled = true;
        }
    }
    if (fImaginary instanceof IFraction && fImaginary.getDenominator().isOne()) {
        newIm = fImaginary.getNumerator();
        evaled = true;
    }
    return evaled ? valueOf(newRe, newIm) : this;
}
Also used : IFraction(org.matheclipse.core.interfaces.IFraction) IRational(org.matheclipse.core.interfaces.IRational)

Example 7 with IRational

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

the class F method complexNum.

public static IComplexNum complexNum(final IComplex value) {
    final IRational realFraction = value.getRealPart();
    final IRational imagFraction = value.getImaginaryPart();
    final EvalEngine engine = EvalEngine.get();
    if (engine.isApfloat()) {
        return ApcomplexNum.valueOf(realFraction.toBigNumerator(), realFraction.toBigDenominator(), imagFraction.toBigNumerator(), imagFraction.toBigDenominator(), engine.getNumericPrecision());
    }
    // double precision complex number
    double nr = realFraction.getNumerator().doubleValue();
    double dr = realFraction.getDenominator().doubleValue();
    double ni = imagFraction.getNumerator().doubleValue();
    double di = imagFraction.getDenominator().doubleValue();
    return complexNum(nr / dr, ni / di);
}
Also used : EvalEngine(org.matheclipse.core.eval.EvalEngine) IRational(org.matheclipse.core.interfaces.IRational)

Example 8 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 9 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)

Aggregations

IRational (org.matheclipse.core.interfaces.IRational)9 IExpr (org.matheclipse.core.interfaces.IExpr)4 EvalEngine (org.matheclipse.core.eval.EvalEngine)3 IInteger (org.matheclipse.core.interfaces.IInteger)3 INum (org.matheclipse.core.interfaces.INum)3 ISymbol (org.matheclipse.core.interfaces.ISymbol)3 Num (org.matheclipse.core.expression.Num)2 IAST (org.matheclipse.core.interfaces.IAST)2 IFraction (org.matheclipse.core.interfaces.IFraction)2 ISignedNumber (org.matheclipse.core.interfaces.ISignedNumber)2 MultisetPartitionsIterator (org.matheclipse.combinatoric.MultisetPartitionsIterator)1 IComplex (org.matheclipse.core.interfaces.IComplex)1 IPatternSequence (org.matheclipse.core.interfaces.IPatternSequence)1