Search in sources :

Example 16 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.isArbitraryMode()) {
        return ApcomplexNum.valueOf(realFraction.toBigNumerator(), realFraction.toBigDenominator(), imagFraction.toBigNumerator(), imagFraction.toBigDenominator());
    }
    // double precision complex number
    double nr = realFraction.numerator().doubleValue();
    double dr = realFraction.denominator().doubleValue();
    double ni = imagFraction.numerator().doubleValue();
    double di = imagFraction.denominator().doubleValue();
    return complexNum(nr / dr, ni / di);
}
Also used : EvalEngine(org.matheclipse.core.eval.EvalEngine) IRational(org.matheclipse.core.interfaces.IRational)

Example 17 with IRational

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

the class ComplexSym method complexArg.

@Override
public IExpr complexArg() {
    // ic == ( x + I * y )
    IRational x = getRealPart();
    IRational y = getImaginaryPart();
    int xi = x.compareTo(F.C0);
    int yi = y.compareTo(F.C0);
    if (xi < 0) {
        // x < 0
        if (yi < 0) {
            // -Pi + ArcTan(y/x)
            return Plus(Negate(Pi), ArcTan(Divide(y, x)));
        } else {
            // Pi + ArcTan(y/x)
            return Plus(Pi, ArcTan(Divide(y, x)));
        }
    }
    if (xi > 0) {
        // ArcTan(y/x)
        return ArcTan(Divide(y, x));
    }
    if (yi < 0) {
        // -Pi/2 + ArcTan(x/y)
        return Plus(Times(CN1D2, Pi), ArcTan(Divide(x, y)));
    } else {
        if (yi > 0) {
            // Pi/2 + ArcTan(x/y)
            return Plus(Times(C1D2, Pi), ArcTan(Divide(x, y)));
        }
    }
    return F.C0;
}
Also used : IRational(org.matheclipse.core.interfaces.IRational)

Example 18 with IRational

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

the class ComplexSym method quotientRemainder.

/**
 * Return the quotient and remainder as an array <code>[quotient, remainder]</code> of the
 * division of <code>IComplex</code> numbers <code>this / c2</code>.
 *
 * <p>
 * See
 *
 * <ul>
 * <li><a href="https://en.wikipedia.org/wiki/Gaussian_integer">Wikipedia - Gaussian integer</a>
 * <li><a href=
 * "http://fermatslasttheorem.blogspot.com/2005/06/division-algorithm-for-gaussian.html">Division
 * Algorithm for Gaussian Integers </a>
 * </ul>
 *
 * @param c2
 * @return the quotient and remainder as an array <code>[quotient, remainder]</code>
 */
@Override
public IComplex[] quotientRemainder(final IComplex c2) {
    final IRational re = c2.re();
    final IRational im = c2.im();
    IRational numeratorReal = // 
    fReal.multiply(re).subtract(fImaginary.multiply(im.negate()));
    IRational numeratorImaginary = // 
    fReal.multiply(im.negate()).add(re.multiply(fImaginary));
    IRational denominator = // 
    re.multiply(re).add(im.multiply(im));
    if (denominator.isZero()) {
        throw new IllegalArgumentException("Denominator can not be zero.");
    }
    IInteger divisionReal = numeratorReal.divideBy(denominator).roundExpr();
    IInteger divisionImaginary = numeratorImaginary.divideBy(denominator).roundExpr();
    IRational remainderReal = fReal.subtract(re.multiply(divisionReal)).subtract(im.multiply(divisionImaginary).negate());
    IRational remainderImaginary = fImaginary.subtract(re.multiply(divisionImaginary)).subtract(im.multiply(divisionReal));
    return new ComplexSym[] { // 
    valueOf(divisionReal, divisionImaginary), valueOf(remainderReal, remainderImaginary) };
}
Also used : IInteger(org.matheclipse.core.interfaces.IInteger) IRational(org.matheclipse.core.interfaces.IRational)

Example 19 with IRational

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

the class ComplexSym method sqrtCC.

@Override
public IComplex sqrtCC() {
    // https://math.stackexchange.com/a/44414
    // this == c + d*I
    IRational c = fReal;
    IRational d = fImaginary;
    IExpr val1 = c.multiply(c).add(d.multiply(d)).sqrt();
    if (val1.isRational()) {
        IExpr a = c.add((IRational) val1).divide(F.C2).sqrt();
        if (a.isRational()) {
            IExpr val2 = ((IRational) val1).subtract(c).divide(F.C2).sqrt();
            if (val2.isRational()) {
                // Sqrt(c + d*I) -> a + b*I
                IRational b = ((IRational) val2);
                return valueOf(// 
                (IRational) a, // 
                (d.complexSign() >= 0) ? b : b.negate());
            }
        }
    }
    return null;
}
Also used : IRational(org.matheclipse.core.interfaces.IRational) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 20 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.denominator().isOne()) {
                return fReal.numerator();
            }
            if (fReal.numerator().isZero()) {
                return F.C0;
            }
        }
        return fReal;
    }
    boolean evaled = false;
    IRational newRe = fReal;
    IRational newIm = fImaginary;
    if (fReal instanceof IFraction) {
        if (fReal.denominator().isOne()) {
            newRe = fReal.numerator();
            evaled = true;
        }
        if (fReal.numerator().isZero()) {
            newRe = F.C0;
            evaled = true;
        }
    }
    if (fImaginary instanceof IFraction && fImaginary.denominator().isOne()) {
        newIm = fImaginary.numerator();
        evaled = true;
    }
    return evaled ? valueOf(newRe, newIm) : this;
}
Also used : IFraction(org.matheclipse.core.interfaces.IFraction) IRational(org.matheclipse.core.interfaces.IRational)

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