nthRoot method
Get the nth root of the number
Implementation
dynamic nthRoot(double nth, {bool allRoots = false}) {
Complex complex = Complex.zero();
if (this is Integer ||
this is Precision ||
this is Double ||
this is Real) {
complex = Complex.fromReal(numberToNum(this));
}
if (this is Imaginary) {
complex = Complex.num(Integer.zero, this as Imaginary);
}
if (this is Complex) {
complex = this as Complex;
}
//Return all the complex numbers of the nth root
if (allRoots) {
var roots = <Complex>[];
for (var k = 0; k < nth; k++) {
var r = math.pow(abs().toDouble(), 1 / nth);
var theta = (complex.phase.value + 2 * math.pi * k) / nth;
roots.add(Complex(r * math.cos(theta), r * math.sin(theta)));
}
return roots;
}
// return the ony smallest positive complex number
var isNegativeNth = nth < 0;
var n = isNegativeNth ? -nth : nth;
var a = 0.0;
var b = 0.0;
if (n == 0) {
a = 1;
} else {
var length = math.pow(abs().toDouble(), 1.0 / n) as double;
var angle =
complex.phase < 0 ? complex.phase + (2 * math.pi) : complex.phase;
angle /= n;
a = length * math.cos(angle.toDouble());
b = length * math.sin(angle.toDouble());
}
if (isNegativeNth) {
final den = a * a + b * b;
a /= den;
b = -b / den;
}
return Complex(a, b);
}