sqrt method
Compute the square root of this complex number.
Implements the following algorithm to compute `sqrt(a + bi)}:
- Let
t = sqrt((|a| + |a + bi|) / 2)
- if
a ≥ 0
returnt + (b/2t)i
else return|b|/2t + sign(b)t i
where:
|a| = abs(a)
|a + bi| = abs(a + bi)
sign(b) = copySign(double, double) copySign(1d, b)
Returns nan if either real or imaginary part of the
input argument is NaN
.
Infinite values in real or imaginary parts of the input may result in infinite or NaN values returned in parts of the result.
Examples:
sqrt(1 ± INFINITY i) = INFINITY + NaN i
sqrt(INFINITY + i) = INFINITY + 0i
sqrt(-INFINITY + i) = 0 + INFINITY i
sqrt(INFINITY ± INFINITY i) = INFINITY + NaN i
sqrt(-INFINITY ± INFINITY i) = NaN ± INFINITY i
Implementation
@override
Cartesian sqrt() {
if (isNaN) return Complex.nan;
if (real == 0.0 && imaginary == 0.0) {
return Complex.zero;
}
final t = math.sqrt((real.abs() + abs()) / 2.0);
if (real >= 0.0) {
return Cartesian(t, imaginary / (2.0 * t));
} else {
return Cartesian(
imaginary.abs() / (2.0 * t), fastmath.copySign(1.0, imaginary) * t);
}
}