nthRoots method
Calculate all nth roots of the complex number.
Throw ArgumentError if n is not positive.
Implementation
List<Complex> nthRoots(int n) {
if (n <= 0) {
throw ArgumentError('n must be a positive integer.');
}
final roots = <Complex>[];
// 1. Calculates the nth root of the modulus r.
// Maintains maximum double precision.
final double rRoot = math.pow(modulus, 1 / n).toDouble();
for (int k = 0; k < n; k++) {
// 2. Calculate the angle of the k-th root.
// Maintains maximum double precision.
final angle = ((theta1 + (2 * math.pi * k)) / n);
// 3. Calculate the real and imaginary parts.
final real = rRoot * math.cos(angle);
final imag = rRoot * math.sin(angle);
// 4. Create the Complex object (using _service for efficiency).
// Note: ‘angle’ is the same for both theta1 and theta2 in this context.
roots.add(Complex._service(real, imag, rRoot, angle, angle));
}
return roots;
}