Complex implements FieldElement<Complex>, Serializable { public List<Complex> nthRoot(int n) throws NotPositiveException { if (n <= 0) { throw new NotPositiveException(LocalizedFormats.CANNOT_COMPUTE_NTH_ROOT_FOR_NEGATIVE_N, n); } final List<Complex> result = new ArrayList<Complex>(); if (isNaN) { result.add(NaN); return result; } if (isInfinite()) { result.add(INF); return result; } final double nthRootOfAbs = FastMath.pow(abs(), 1.0 / n); final double nthPhi = getArgument() / n; final double slice = 2 * FastMath.PI / n; double innerPart = nthPhi; for (int k = 0; k < n ; k++) { final double realPart = nthRootOfAbs * FastMath.cos(innerPart); final double imaginaryPart = nthRootOfAbs * FastMath.sin(innerPart); result.add(createComplex(realPart, imaginaryPart)); innerPart += slice; } return result; } Complex(double real);  Complex(double real, double imaginary); double abs(); Complex add(Complex addend); Complex add(double addend); Complex conjugate(); Complex divide(Complex divisor); Complex divide(double divisor); Complex reciprocal(); @Override boolean equals(Object other); static boolean equals(Complex x, Complex y, int maxUlps); static boolean equals(Complex x, Complex y); static boolean equals(Complex x, Complex y, double eps); static boolean equalsWithRelativeTolerance(Complex x, Complex y,
                                                      double eps); @Override int hashCode(); double getImaginary(); double getReal(); boolean isNaN(); boolean isInfinite(); Complex multiply(Complex factor); Complex multiply(final int factor); Complex multiply(double factor); Complex negate(); Complex subtract(Complex subtrahend); Complex subtract(double subtrahend); Complex acos(); Complex asin(); Complex atan(); Complex cos(); Complex cosh(); Complex exp(); Complex log(); Complex pow(Complex x); Complex pow(double x); Complex sin(); Complex sinh(); Complex sqrt(); Complex sqrt1z(); Complex tan(); Complex tanh(); double getArgument(); List<Complex> nthRoot(int n); static Complex valueOf(double realPart,
                                  double imaginaryPart); static Complex valueOf(double realPart); ComplexField getField(); @Override String toString(); static final Complex I; static final Complex NaN; static final Complex INF; static final Complex ONE; static final Complex ZERO; }
@Test public void testNthRoot_cornercase_NAN_Inf() { List<Complex> roots = oneNaN.nthRoot(3); Assert.assertEquals(1,roots.size()); Assert.assertEquals(Complex.NaN, roots.get(0)); roots = nanZero.nthRoot(3); Assert.assertEquals(1,roots.size()); Assert.assertEquals(Complex.NaN, roots.get(0)); roots = nanInf.nthRoot(3); Assert.assertEquals(1,roots.size()); Assert.assertEquals(Complex.NaN, roots.get(0)); roots = oneInf.nthRoot(3); Assert.assertEquals(1,roots.size()); Assert.assertEquals(Complex.INF, roots.get(0)); roots = negInfInf.nthRoot(3); Assert.assertEquals(1,roots.size()); Assert.assertEquals(Complex.INF, roots.get(0)); }