@Test public void testNull() { try { ComplexMathUtils.add(null, Z1); } catch (final IllegalArgumentException e) { assertStackTraceElement(e.getStackTrace()); ComplexMathUtils.add(Z1, null); } catch (final IllegalArgumentException e) { assertStackTraceElement(e.getStackTrace()); ComplexMathUtils.add(X, null); } catch (final IllegalArgumentException e) { assertStackTraceElement(e.getStackTrace()); ComplexMathUtils.add(null, X); } catch (final IllegalArgumentException e) { assertStackTraceElement(e.getStackTrace()); ComplexMathUtils.arg(null); } catch (final IllegalArgumentException e) { assertStackTraceElement(e.getStackTrace()); ComplexMathUtils.conjugate(null); } catch (final IllegalArgumentException e) { assertStackTraceElement(e.getStackTrace()); ComplexMathUtils.divide(null, Z1); } catch (final IllegalArgumentException e) { assertStackTraceElement(e.getStackTrace());
@Test public void testPower() { assertComplexEquals(ComplexMathUtils.pow(Z1, 0), new ComplexNumber(1, 0)); assertComplexEquals(ComplexMathUtils.pow(X, new ComplexNumber(0, 0)), new ComplexNumber(1, 0)); assertComplexEquals(ComplexMathUtils.sqrt(ComplexMathUtils.pow(Z1, 2)), Z1); assertComplexEquals(ComplexMathUtils.sqrt(ComplexMathUtils.pow(Z2, 2)), Z2); assertComplexEquals(ComplexMathUtils.pow(ComplexMathUtils.pow(Z1, 1. / 3), 3), Z1); assertComplexEquals(ComplexMathUtils.pow(ComplexMathUtils.pow(X, ComplexMathUtils.inverse(Z2)), Z2), new ComplexNumber(X, 0)); assertComplexEquals(ComplexMathUtils.pow(ComplexMathUtils.pow(Z1, ComplexMathUtils.inverse(Z2)), Z2), Z1); }
@Test public void testSqrt() { ComplexNumber z1 = new ComplexNumber(3, -2); ComplexNumber z2 = new ComplexNumber(-3, 4); ComplexNumber z3 = new ComplexNumber(-3, -4); ComplexNumber rZ1 = ComplexMathUtils.sqrt(z1); ComplexNumber rZ2 = ComplexMathUtils.sqrt(z2); ComplexNumber rZ3 = ComplexMathUtils.sqrt(z3); assertComplexEquals(ComplexMathUtils.pow(z1, 0.5), rZ1); assertComplexEquals(ComplexMathUtils.pow(z2, 0.5), rZ2); assertComplexEquals(ComplexMathUtils.pow(z3, 0.5), rZ3); assertComplexEquals(z1, ComplexMathUtils.square(rZ1)); assertComplexEquals(z2, ComplexMathUtils.square(rZ2)); assertComplexEquals(z3, ComplexMathUtils.square(rZ3)); }
@Test public void testDivideMultiply() { assertComplexEquals(ComplexMathUtils.multiply(ComplexMathUtils.divide(Z1, Z2), Z2), Z1); assertComplexEquals(ComplexMathUtils.multiply(ComplexMathUtils.divide(Z1, X), X), Z1); assertComplexEquals(ComplexMathUtils.multiply(ComplexMathUtils.divide(X, Z1), Z1), X_C); assertComplexEquals(ComplexMathUtils.multiply(X, Z1), ComplexMathUtils.multiply(Z1, X)); }
public static ComplexNumber pow(double x, ComplexNumber z) { ArgChecker.notNull(z, "z"); return pow(new ComplexNumber(x, 0), z); }
@Test public void testArg() { assertEquals(Math.atan2(W, V), ComplexMathUtils.arg(Z1), EPS); }
@Test public void testModulus() { assertEquals(Math.sqrt(V * V + W * W), ComplexMathUtils.mod(Z1), EPS); }
public static ComplexNumber atan(ComplexNumber z) { ArgChecker.notNull(z, "z"); ComplexNumber iZ = ComplexMathUtils.multiply(z, I); ComplexNumber half = new ComplexNumber(0, 0.5); return ComplexMathUtils.multiply( half, ComplexMathUtils.log(ComplexMathUtils.divide(ComplexMathUtils.subtract(1, iZ), ComplexMathUtils.add(1, iZ)))); }
public static ComplexNumber multiply(ComplexNumber... z) { ArgChecker.notNull(z, "z"); int n = z.length; ArgChecker.isTrue(n > 0, "nothing to multiply"); if (n == 1) { return z[0]; } else if (n == 2) { return multiply(z[0], z[1]); } else { ComplexNumber product = multiply(z[0], z[1]); for (int i = 2; i < n; i++) { product = multiply(product, z[i]); } return product; } }
public static ComplexNumber pow(ComplexNumber z1, ComplexNumber z2) { ArgChecker.notNull(z1, "z1"); ArgChecker.notNull(z2, "z2"); double mod = mod(z1); double arg = arg(z1); double mult = Math.pow(mod, z2.getReal()) * Math.exp(-z2.getImaginary() * arg); double theta = z2.getReal() * arg + z2.getImaginary() * Math.log(mod); return new ComplexNumber(mult * Math.cos(theta), mult * Math.sin(theta)); }