Complex[] reference, x;
// Fifth root of a positive real number
reference =
new Complex[] {
new Complex(-1.11622474376531575, -0.81098474715738870),
new Complex(-1.11622474376531575, 0.81098474715738870),
new Complex(0.42635991303470834, -1.31220088525839459),
new Complex(0.42635991303470834, 1.31220088525839459),
new Complex(1.3797296614612148) };
x = ComplexMath.root(5, new Complex(5));
assertTrue(x.length == 5);
for (int i = 0; i < x.length; i++) {
boolean temp = false;
for (int j = 0; j < reference.length; j++) {
temp |= approximatelyEqual(x[i], reference[j]);
}
assertTrue(temp);
}
// Eighth root of a negative real number
reference =
new Complex[] {
new Complex(-1.84775906502257351, -0.76536686473017954),
new Complex(-1.84775906502257351, 0.76536686473017954),
new Complex(-0.76536686473017954, -1.84775906502257351),
new Complex(-0.76536686473017954, 1.84775906502257351),
new Complex(0.76536686473017954, -1.84775906502257351),
new Complex(0.76536686473017954, 1.84775906502257351),
new Complex(1.84775906502257351, -0.76536686473017954),
new Complex(1.84775906502257351, 0.76536686473017954) };
x = ComplexMath.root(8, new Complex(-256));
assertTrue(x.length == 8);
for (int i = 0; i < x.length; i++) {
boolean temp = false;
for (int j = 0; j < reference.length; j++) {
temp |= approximatelyEqual(x[i], reference[j]);
}
assertTrue(temp);
}
// Fifth root of a complex number
reference =
new Complex[] { new Complex(-1.49595075945776575, 0.12140512225626765),
new Complex(-0.57773734005399185, -1.38521747185763709),
new Complex(-0.34681107478712560, 1.46024996382034682),
new Complex(1.13888944673444278, -0.97751660167448831),
new Complex(1.28160972756444042, 0.78107898745551094) };
x = ComplexMath.root(5, new Complex(-7, 3));
assertTrue(x.length == 5);
for (int i = 0; i < x.length; i++) {
boolean temp = false;
for (int j = 0; j < reference.length; j++) {
temp |= approximatelyEqual(x[i], reference[j]);
}
assertTrue(temp);
}
// negative square root of a complex number
reference =
new Complex[] { new Complex(-0.07285869092739988, 0.35496203179504731),
new Complex(0.07285869092739988, -0.35496203179504731) };
x = ComplexMath.root(-2, new Complex(-7, 3));
assertTrue(x.length == 2);
for (int i = 0; i < x.length; i++) {
boolean temp = false;
for (int j = 0; j < reference.length; j++) {
temp |= approximatelyEqual(x[i], reference[j]);
}
assertTrue(temp);
}
/* Edge cases */
// root(0, Complex) -- should yield no results.
x = ComplexMath.root(0, new Complex(23, 42));
assertTrue(x.length == 0);
// root(0, 0) -- should yield no results.
x = ComplexMath.root(0, new Complex(0, 0));
assertTrue(x.length == 0);
// root(3, 0) -- should yield zero, three times
x = ComplexMath.root(3, new Complex(0, 0));
assertTrue(x.length == 3);
for (int i = 0; i < x.length; i++) {
assertTrue(x[i].equals(new Complex(0, 0)));
}
// root(-2, 0) -- should yield no results.
x = ComplexMath.root(-2, new Complex(0, 0));
assertTrue(x.length == 0);
}