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);
}
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;
}
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) };
}
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;
}
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;
}
Aggregations