Complex.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.apache.commons.math3.complex;

  18. import java.io.Serializable;
  19. import java.util.ArrayList;
  20. import java.util.List;

  21. import org.apache.commons.math3.FieldElement;
  22. import org.apache.commons.math3.exception.NotPositiveException;
  23. import org.apache.commons.math3.exception.NullArgumentException;
  24. import org.apache.commons.math3.exception.util.LocalizedFormats;
  25. import org.apache.commons.math3.util.FastMath;
  26. import org.apache.commons.math3.util.MathUtils;
  27. import org.apache.commons.math3.util.Precision;

  28. /**
  29.  * Representation of a Complex number, i.e. a number which has both a
  30.  * real and imaginary part.
  31.  * <br/>
  32.  * Implementations of arithmetic operations handle {@code NaN} and
  33.  * infinite values according to the rules for {@link java.lang.Double}, i.e.
  34.  * {@link #equals} is an equivalence relation for all instances that have
  35.  * a {@code NaN} in either real or imaginary part, e.g. the following are
  36.  * considered equal:
  37.  * <ul>
  38.  *  <li>{@code 1 + NaNi}</li>
  39.  *  <li>{@code NaN + i}</li>
  40.  *  <li>{@code NaN + NaNi}</li>
  41.  * </ul>
  42.  * Note that this is in contradiction with the IEEE-754 standard for floating
  43.  * point numbers (according to which the test {@code x == x} must fail if
  44.  * {@code x} is {@code NaN}). The method
  45.  * {@link org.apache.commons.math3.util.Precision#equals(double,double,int)
  46.  * equals for primitive double} in {@link org.apache.commons.math3.util.Precision}
  47.  * conforms with IEEE-754 while this class conforms with the standard behavior
  48.  * for Java object types.
  49.  * <br/>
  50.  * Implements Serializable since 2.0
  51.  *
  52.  */
  53. public class Complex implements FieldElement<Complex>, Serializable  {
  54.     /** The square root of -1. A number representing "0.0 + 1.0i" */
  55.     public static final Complex I = new Complex(0.0, 1.0);
  56.     // CHECKSTYLE: stop ConstantName
  57.     /** A complex number representing "NaN + NaNi" */
  58.     public static final Complex NaN = new Complex(Double.NaN, Double.NaN);
  59.     // CHECKSTYLE: resume ConstantName
  60.     /** A complex number representing "+INF + INFi" */
  61.     public static final Complex INF = new Complex(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY);
  62.     /** A complex number representing "1.0 + 0.0i" */
  63.     public static final Complex ONE = new Complex(1.0, 0.0);
  64.     /** A complex number representing "0.0 + 0.0i" */
  65.     public static final Complex ZERO = new Complex(0.0, 0.0);

  66.     /** Serializable version identifier */
  67.     private static final long serialVersionUID = -6195664516687396620L;

  68.     /** The imaginary part. */
  69.     private final double imaginary;
  70.     /** The real part. */
  71.     private final double real;
  72.     /** Record whether this complex number is equal to NaN. */
  73.     private final transient boolean isNaN;
  74.     /** Record whether this complex number is infinite. */
  75.     private final transient boolean isInfinite;

  76.     /**
  77.      * Create a complex number given only the real part.
  78.      *
  79.      * @param real Real part.
  80.      */
  81.     public Complex(double real) {
  82.         this(real, 0.0);
  83.     }

  84.     /**
  85.      * Create a complex number given the real and imaginary parts.
  86.      *
  87.      * @param real Real part.
  88.      * @param imaginary Imaginary part.
  89.      */
  90.     public Complex(double real, double imaginary) {
  91.         this.real = real;
  92.         this.imaginary = imaginary;

  93.         isNaN = Double.isNaN(real) || Double.isNaN(imaginary);
  94.         isInfinite = !isNaN &&
  95.             (Double.isInfinite(real) || Double.isInfinite(imaginary));
  96.     }

  97.     /**
  98.      * Return the absolute value of this complex number.
  99.      * Returns {@code NaN} if either real or imaginary part is {@code NaN}
  100.      * and {@code Double.POSITIVE_INFINITY} if neither part is {@code NaN},
  101.      * but at least one part is infinite.
  102.      *
  103.      * @return the absolute value.
  104.      */
  105.     public double abs() {
  106.         if (isNaN) {
  107.             return Double.NaN;
  108.         }
  109.         if (isInfinite()) {
  110.             return Double.POSITIVE_INFINITY;
  111.         }
  112.         if (FastMath.abs(real) < FastMath.abs(imaginary)) {
  113.             if (imaginary == 0.0) {
  114.                 return FastMath.abs(real);
  115.             }
  116.             double q = real / imaginary;
  117.             return FastMath.abs(imaginary) * FastMath.sqrt(1 + q * q);
  118.         } else {
  119.             if (real == 0.0) {
  120.                 return FastMath.abs(imaginary);
  121.             }
  122.             double q = imaginary / real;
  123.             return FastMath.abs(real) * FastMath.sqrt(1 + q * q);
  124.         }
  125.     }

  126.     /**
  127.      * Returns a {@code Complex} whose value is
  128.      * {@code (this + addend)}.
  129.      * Uses the definitional formula
  130.      * <pre>
  131.      *  <code>
  132.      *   (a + bi) + (c + di) = (a+c) + (b+d)i
  133.      *  </code>
  134.      * </pre>
  135.      * <br/>
  136.      * If either {@code this} or {@code addend} has a {@code NaN} value in
  137.      * either part, {@link #NaN} is returned; otherwise {@code Infinite}
  138.      * and {@code NaN} values are returned in the parts of the result
  139.      * according to the rules for {@link java.lang.Double} arithmetic.
  140.      *
  141.      * @param  addend Value to be added to this {@code Complex}.
  142.      * @return {@code this + addend}.
  143.      * @throws NullArgumentException if {@code addend} is {@code null}.
  144.      */
  145.     public Complex add(Complex addend) throws NullArgumentException {
  146.         MathUtils.checkNotNull(addend);
  147.         if (isNaN || addend.isNaN) {
  148.             return NaN;
  149.         }

  150.         return createComplex(real + addend.getReal(),
  151.                              imaginary + addend.getImaginary());
  152.     }

  153.     /**
  154.      * Returns a {@code Complex} whose value is {@code (this + addend)},
  155.      * with {@code addend} interpreted as a real number.
  156.      *
  157.      * @param addend Value to be added to this {@code Complex}.
  158.      * @return {@code this + addend}.
  159.      * @see #add(Complex)
  160.      */
  161.     public Complex add(double addend) {
  162.         if (isNaN || Double.isNaN(addend)) {
  163.             return NaN;
  164.         }

  165.         return createComplex(real + addend, imaginary);
  166.     }

  167.      /**
  168.      * Return the conjugate of this complex number.
  169.      * The conjugate of {@code a + bi} is {@code a - bi}.
  170.      * <br/>
  171.      * {@link #NaN} is returned if either the real or imaginary
  172.      * part of this Complex number equals {@code Double.NaN}.
  173.      * <br/>
  174.      * If the imaginary part is infinite, and the real part is not
  175.      * {@code NaN}, the returned value has infinite imaginary part
  176.      * of the opposite sign, e.g. the conjugate of
  177.      * {@code 1 + POSITIVE_INFINITY i} is {@code 1 - NEGATIVE_INFINITY i}.
  178.      *
  179.      * @return the conjugate of this Complex object.
  180.      */
  181.     public Complex conjugate() {
  182.         if (isNaN) {
  183.             return NaN;
  184.         }

  185.         return createComplex(real, -imaginary);
  186.     }

  187.     /**
  188.      * Returns a {@code Complex} whose value is
  189.      * {@code (this / divisor)}.
  190.      * Implements the definitional formula
  191.      * <pre>
  192.      *  <code>
  193.      *    a + bi          ac + bd + (bc - ad)i
  194.      *    ----------- = -------------------------
  195.      *    c + di         c<sup>2</sup> + d<sup>2</sup>
  196.      *  </code>
  197.      * </pre>
  198.      * but uses
  199.      * <a href="http://doi.acm.org/10.1145/1039813.1039814">
  200.      * prescaling of operands</a> to limit the effects of overflows and
  201.      * underflows in the computation.
  202.      * <br/>
  203.      * {@code Infinite} and {@code NaN} values are handled according to the
  204.      * following rules, applied in the order presented:
  205.      * <ul>
  206.      *  <li>If either {@code this} or {@code divisor} has a {@code NaN} value
  207.      *   in either part, {@link #NaN} is returned.
  208.      *  </li>
  209.      *  <li>If {@code divisor} equals {@link #ZERO}, {@link #NaN} is returned.
  210.      *  </li>
  211.      *  <li>If {@code this} and {@code divisor} are both infinite,
  212.      *   {@link #NaN} is returned.
  213.      *  </li>
  214.      *  <li>If {@code this} is finite (i.e., has no {@code Infinite} or
  215.      *   {@code NaN} parts) and {@code divisor} is infinite (one or both parts
  216.      *   infinite), {@link #ZERO} is returned.
  217.      *  </li>
  218.      *  <li>If {@code this} is infinite and {@code divisor} is finite,
  219.      *   {@code NaN} values are returned in the parts of the result if the
  220.      *   {@link java.lang.Double} rules applied to the definitional formula
  221.      *   force {@code NaN} results.
  222.      *  </li>
  223.      * </ul>
  224.      *
  225.      * @param divisor Value by which this {@code Complex} is to be divided.
  226.      * @return {@code this / divisor}.
  227.      * @throws NullArgumentException if {@code divisor} is {@code null}.
  228.      */
  229.     public Complex divide(Complex divisor)
  230.         throws NullArgumentException {
  231.         MathUtils.checkNotNull(divisor);
  232.         if (isNaN || divisor.isNaN) {
  233.             return NaN;
  234.         }

  235.         final double c = divisor.getReal();
  236.         final double d = divisor.getImaginary();
  237.         if (c == 0.0 && d == 0.0) {
  238.             return NaN;
  239.         }

  240.         if (divisor.isInfinite() && !isInfinite()) {
  241.             return ZERO;
  242.         }

  243.         if (FastMath.abs(c) < FastMath.abs(d)) {
  244.             double q = c / d;
  245.             double denominator = c * q + d;
  246.             return createComplex((real * q + imaginary) / denominator,
  247.                 (imaginary * q - real) / denominator);
  248.         } else {
  249.             double q = d / c;
  250.             double denominator = d * q + c;
  251.             return createComplex((imaginary * q + real) / denominator,
  252.                 (imaginary - real * q) / denominator);
  253.         }
  254.     }

  255.     /**
  256.      * Returns a {@code Complex} whose value is {@code (this / divisor)},
  257.      * with {@code divisor} interpreted as a real number.
  258.      *
  259.      * @param  divisor Value by which this {@code Complex} is to be divided.
  260.      * @return {@code this / divisor}.
  261.      * @see #divide(Complex)
  262.      */
  263.     public Complex divide(double divisor) {
  264.         if (isNaN || Double.isNaN(divisor)) {
  265.             return NaN;
  266.         }
  267.         if (divisor == 0d) {
  268.             return NaN;
  269.         }
  270.         if (Double.isInfinite(divisor)) {
  271.             return !isInfinite() ? ZERO : NaN;
  272.         }
  273.         return createComplex(real / divisor,
  274.                              imaginary  / divisor);
  275.     }

  276.     /** {@inheritDoc} */
  277.     public Complex reciprocal() {
  278.         if (isNaN) {
  279.             return NaN;
  280.         }

  281.         if (real == 0.0 && imaginary == 0.0) {
  282.             return INF;
  283.         }

  284.         if (isInfinite) {
  285.             return ZERO;
  286.         }

  287.         if (FastMath.abs(real) < FastMath.abs(imaginary)) {
  288.             double q = real / imaginary;
  289.             double scale = 1. / (real * q + imaginary);
  290.             return createComplex(scale * q, -scale);
  291.         } else {
  292.             double q = imaginary / real;
  293.             double scale = 1. / (imaginary * q + real);
  294.             return createComplex(scale, -scale * q);
  295.         }
  296.     }

  297.     /**
  298.      * Test for equality with another object.
  299.      * If both the real and imaginary parts of two complex numbers
  300.      * are exactly the same, and neither is {@code Double.NaN}, the two
  301.      * Complex objects are considered to be equal.
  302.      * The behavior is the same as for JDK's {@link Double#equals(Object)
  303.      * Double}:
  304.      * <ul>
  305.      *  <li>All {@code NaN} values are considered to be equal,
  306.      *   i.e, if either (or both) real and imaginary parts of the complex
  307.      *   number are equal to {@code Double.NaN}, the complex number is equal
  308.      *   to {@code NaN}.
  309.      *  </li>
  310.      *  <li>
  311.      *   Instances constructed with different representations of zero (i.e.
  312.      *   either "0" or "-0") are <em>not</em> considered to be equal.
  313.      *  </li>
  314.      * </ul>
  315.      *
  316.      * @param other Object to test for equality with this instance.
  317.      * @return {@code true} if the objects are equal, {@code false} if object
  318.      * is {@code null}, not an instance of {@code Complex}, or not equal to
  319.      * this instance.
  320.      */
  321.     @Override
  322.     public boolean equals(Object other) {
  323.         if (this == other) {
  324.             return true;
  325.         }
  326.         if (other instanceof Complex){
  327.             Complex c = (Complex) other;
  328.             if (c.isNaN) {
  329.                 return isNaN;
  330.             } else {
  331.                 return MathUtils.equals(real, c.real) &&
  332.                     MathUtils.equals(imaginary, c.imaginary);
  333.             }
  334.         }
  335.         return false;
  336.     }

  337.     /**
  338.      * Test for the floating-point equality between Complex objects.
  339.      * It returns {@code true} if both arguments are equal or within the
  340.      * range of allowed error (inclusive).
  341.      *
  342.      * @param x First value (cannot be {@code null}).
  343.      * @param y Second value (cannot be {@code null}).
  344.      * @param maxUlps {@code (maxUlps - 1)} is the number of floating point
  345.      * values between the real (resp. imaginary) parts of {@code x} and
  346.      * {@code y}.
  347.      * @return {@code true} if there are fewer than {@code maxUlps} floating
  348.      * point values between the real (resp. imaginary) parts of {@code x}
  349.      * and {@code y}.
  350.      *
  351.      * @see Precision#equals(double,double,int)
  352.      * @since 3.3
  353.      */
  354.     public static boolean equals(Complex x, Complex y, int maxUlps) {
  355.         return Precision.equals(x.real, y.real, maxUlps) &&
  356.             Precision.equals(x.imaginary, y.imaginary, maxUlps);
  357.     }

  358.     /**
  359.      * Returns {@code true} iff the values are equal as defined by
  360.      * {@link #equals(Complex,Complex,int) equals(x, y, 1)}.
  361.      *
  362.      * @param x First value (cannot be {@code null}).
  363.      * @param y Second value (cannot be {@code null}).
  364.      * @return {@code true} if the values are equal.
  365.      *
  366.      * @since 3.3
  367.      */
  368.     public static boolean equals(Complex x, Complex y) {
  369.         return equals(x, y, 1);
  370.     }

  371.     /**
  372.      * Returns {@code true} if, both for the real part and for the imaginary
  373.      * part, there is no double value strictly between the arguments or the
  374.      * difference between them is within the range of allowed error
  375.      * (inclusive).
  376.      *
  377.      * @param x First value (cannot be {@code null}).
  378.      * @param y Second value (cannot be {@code null}).
  379.      * @param eps Amount of allowed absolute error.
  380.      * @return {@code true} if the values are two adjacent floating point
  381.      * numbers or they are within range of each other.
  382.      *
  383.      * @see Precision#equals(double,double,double)
  384.      * @since 3.3
  385.      */
  386.     public static boolean equals(Complex x, Complex y, double eps) {
  387.         return Precision.equals(x.real, y.real, eps) &&
  388.             Precision.equals(x.imaginary, y.imaginary, eps);
  389.     }

  390.     /**
  391.      * Returns {@code true} if, both for the real part and for the imaginary
  392.      * part, there is no double value strictly between the arguments or the
  393.      * relative difference between them is smaller or equal to the given
  394.      * tolerance.
  395.      *
  396.      * @param x First value (cannot be {@code null}).
  397.      * @param y Second value (cannot be {@code null}).
  398.      * @param eps Amount of allowed relative error.
  399.      * @return {@code true} if the values are two adjacent floating point
  400.      * numbers or they are within range of each other.
  401.      *
  402.      * @see Precision#equalsWithRelativeTolerance(double,double,double)
  403.      * @since 3.3
  404.      */
  405.     public static boolean equalsWithRelativeTolerance(Complex x, Complex y,
  406.                                                       double eps) {
  407.         return Precision.equalsWithRelativeTolerance(x.real, y.real, eps) &&
  408.             Precision.equalsWithRelativeTolerance(x.imaginary, y.imaginary, eps);
  409.     }

  410.     /**
  411.      * Get a hashCode for the complex number.
  412.      * Any {@code Double.NaN} value in real or imaginary part produces
  413.      * the same hash code {@code 7}.
  414.      *
  415.      * @return a hash code value for this object.
  416.      */
  417.     @Override
  418.     public int hashCode() {
  419.         if (isNaN) {
  420.             return 7;
  421.         }
  422.         return 37 * (17 * MathUtils.hash(imaginary) +
  423.             MathUtils.hash(real));
  424.     }

  425.     /**
  426.      * Access the imaginary part.
  427.      *
  428.      * @return the imaginary part.
  429.      */
  430.     public double getImaginary() {
  431.         return imaginary;
  432.     }

  433.     /**
  434.      * Access the real part.
  435.      *
  436.      * @return the real part.
  437.      */
  438.     public double getReal() {
  439.         return real;
  440.     }

  441.     /**
  442.      * Checks whether either or both parts of this complex number is
  443.      * {@code NaN}.
  444.      *
  445.      * @return true if either or both parts of this complex number is
  446.      * {@code NaN}; false otherwise.
  447.      */
  448.     public boolean isNaN() {
  449.         return isNaN;
  450.     }

  451.     /**
  452.      * Checks whether either the real or imaginary part of this complex number
  453.      * takes an infinite value (either {@code Double.POSITIVE_INFINITY} or
  454.      * {@code Double.NEGATIVE_INFINITY}) and neither part
  455.      * is {@code NaN}.
  456.      *
  457.      * @return true if one or both parts of this complex number are infinite
  458.      * and neither part is {@code NaN}.
  459.      */
  460.     public boolean isInfinite() {
  461.         return isInfinite;
  462.     }

  463.     /**
  464.      * Returns a {@code Complex} whose value is {@code this * factor}.
  465.      * Implements preliminary checks for {@code NaN} and infinity followed by
  466.      * the definitional formula:
  467.      * <pre>
  468.      *  <code>
  469.      *   (a + bi)(c + di) = (ac - bd) + (ad + bc)i
  470.      *  </code>
  471.      * </pre>
  472.      * Returns {@link #NaN} if either {@code this} or {@code factor} has one or
  473.      * more {@code NaN} parts.
  474.      * <br/>
  475.      * Returns {@link #INF} if neither {@code this} nor {@code factor} has one
  476.      * or more {@code NaN} parts and if either {@code this} or {@code factor}
  477.      * has one or more infinite parts (same result is returned regardless of
  478.      * the sign of the components).
  479.      * <br/>
  480.      * Returns finite values in components of the result per the definitional
  481.      * formula in all remaining cases.
  482.      *
  483.      * @param  factor value to be multiplied by this {@code Complex}.
  484.      * @return {@code this * factor}.
  485.      * @throws NullArgumentException if {@code factor} is {@code null}.
  486.      */
  487.     public Complex multiply(Complex factor)
  488.         throws NullArgumentException {
  489.         MathUtils.checkNotNull(factor);
  490.         if (isNaN || factor.isNaN) {
  491.             return NaN;
  492.         }
  493.         if (Double.isInfinite(real) ||
  494.             Double.isInfinite(imaginary) ||
  495.             Double.isInfinite(factor.real) ||
  496.             Double.isInfinite(factor.imaginary)) {
  497.             // we don't use isInfinite() to avoid testing for NaN again
  498.             return INF;
  499.         }
  500.         return createComplex(real * factor.real - imaginary * factor.imaginary,
  501.                              real * factor.imaginary + imaginary * factor.real);
  502.     }

  503.     /**
  504.      * Returns a {@code Complex} whose value is {@code this * factor}, with {@code factor}
  505.      * interpreted as a integer number.
  506.      *
  507.      * @param  factor value to be multiplied by this {@code Complex}.
  508.      * @return {@code this * factor}.
  509.      * @see #multiply(Complex)
  510.      */
  511.     public Complex multiply(final int factor) {
  512.         if (isNaN) {
  513.             return NaN;
  514.         }
  515.         if (Double.isInfinite(real) ||
  516.             Double.isInfinite(imaginary)) {
  517.             return INF;
  518.         }
  519.         return createComplex(real * factor, imaginary * factor);
  520.     }

  521.     /**
  522.      * Returns a {@code Complex} whose value is {@code this * factor}, with {@code factor}
  523.      * interpreted as a real number.
  524.      *
  525.      * @param  factor value to be multiplied by this {@code Complex}.
  526.      * @return {@code this * factor}.
  527.      * @see #multiply(Complex)
  528.      */
  529.     public Complex multiply(double factor) {
  530.         if (isNaN || Double.isNaN(factor)) {
  531.             return NaN;
  532.         }
  533.         if (Double.isInfinite(real) ||
  534.             Double.isInfinite(imaginary) ||
  535.             Double.isInfinite(factor)) {
  536.             // we don't use isInfinite() to avoid testing for NaN again
  537.             return INF;
  538.         }
  539.         return createComplex(real * factor, imaginary * factor);
  540.     }

  541.     /**
  542.      * Returns a {@code Complex} whose value is {@code (-this)}.
  543.      * Returns {@code NaN} if either real or imaginary
  544.      * part of this Complex number equals {@code Double.NaN}.
  545.      *
  546.      * @return {@code -this}.
  547.      */
  548.     public Complex negate() {
  549.         if (isNaN) {
  550.             return NaN;
  551.         }

  552.         return createComplex(-real, -imaginary);
  553.     }

  554.     /**
  555.      * Returns a {@code Complex} whose value is
  556.      * {@code (this - subtrahend)}.
  557.      * Uses the definitional formula
  558.      * <pre>
  559.      *  <code>
  560.      *   (a + bi) - (c + di) = (a-c) + (b-d)i
  561.      *  </code>
  562.      * </pre>
  563.      * If either {@code this} or {@code subtrahend} has a {@code NaN]} value in either part,
  564.      * {@link #NaN} is returned; otherwise infinite and {@code NaN} values are
  565.      * returned in the parts of the result according to the rules for
  566.      * {@link java.lang.Double} arithmetic.
  567.      *
  568.      * @param  subtrahend value to be subtracted from this {@code Complex}.
  569.      * @return {@code this - subtrahend}.
  570.      * @throws NullArgumentException if {@code subtrahend} is {@code null}.
  571.      */
  572.     public Complex subtract(Complex subtrahend)
  573.         throws NullArgumentException {
  574.         MathUtils.checkNotNull(subtrahend);
  575.         if (isNaN || subtrahend.isNaN) {
  576.             return NaN;
  577.         }

  578.         return createComplex(real - subtrahend.getReal(),
  579.                              imaginary - subtrahend.getImaginary());
  580.     }

  581.     /**
  582.      * Returns a {@code Complex} whose value is
  583.      * {@code (this - subtrahend)}.
  584.      *
  585.      * @param  subtrahend value to be subtracted from this {@code Complex}.
  586.      * @return {@code this - subtrahend}.
  587.      * @see #subtract(Complex)
  588.      */
  589.     public Complex subtract(double subtrahend) {
  590.         if (isNaN || Double.isNaN(subtrahend)) {
  591.             return NaN;
  592.         }
  593.         return createComplex(real - subtrahend, imaginary);
  594.     }

  595.     /**
  596.      * Compute the
  597.      * <a href="http://mathworld.wolfram.com/InverseCosine.html" TARGET="_top">
  598.      * inverse cosine</a> of this complex number.
  599.      * Implements the formula:
  600.      * <pre>
  601.      *  <code>
  602.      *   acos(z) = -i (log(z + i (sqrt(1 - z<sup>2</sup>))))
  603.      *  </code>
  604.      * </pre>
  605.      * Returns {@link Complex#NaN} if either real or imaginary part of the
  606.      * input argument is {@code NaN} or infinite.
  607.      *
  608.      * @return the inverse cosine of this complex number.
  609.      * @since 1.2
  610.      */
  611.     public Complex acos() {
  612.         if (isNaN) {
  613.             return NaN;
  614.         }

  615.         return this.add(this.sqrt1z().multiply(I)).log().multiply(I.negate());
  616.     }

  617.     /**
  618.      * Compute the
  619.      * <a href="http://mathworld.wolfram.com/InverseSine.html" TARGET="_top">
  620.      * inverse sine</a> of this complex number.
  621.      * Implements the formula:
  622.      * <pre>
  623.      *  <code>
  624.      *   asin(z) = -i (log(sqrt(1 - z<sup>2</sup>) + iz))
  625.      *  </code>
  626.      * </pre>
  627.      * Returns {@link Complex#NaN} if either real or imaginary part of the
  628.      * input argument is {@code NaN} or infinite.
  629.      *
  630.      * @return the inverse sine of this complex number.
  631.      * @since 1.2
  632.      */
  633.     public Complex asin() {
  634.         if (isNaN) {
  635.             return NaN;
  636.         }

  637.         return sqrt1z().add(this.multiply(I)).log().multiply(I.negate());
  638.     }

  639.     /**
  640.      * Compute the
  641.      * <a href="http://mathworld.wolfram.com/InverseTangent.html" TARGET="_top">
  642.      * inverse tangent</a> of this complex number.
  643.      * Implements the formula:
  644.      * <pre>
  645.      *  <code>
  646.      *   atan(z) = (i/2) log((i + z)/(i - z))
  647.      *  </code>
  648.      * </pre>
  649.      * Returns {@link Complex#NaN} if either real or imaginary part of the
  650.      * input argument is {@code NaN} or infinite.
  651.      *
  652.      * @return the inverse tangent of this complex number
  653.      * @since 1.2
  654.      */
  655.     public Complex atan() {
  656.         if (isNaN) {
  657.             return NaN;
  658.         }

  659.         return this.add(I).divide(I.subtract(this)).log()
  660.                 .multiply(I.divide(createComplex(2.0, 0.0)));
  661.     }

  662.     /**
  663.      * Compute the
  664.      * <a href="http://mathworld.wolfram.com/Cosine.html" TARGET="_top">
  665.      * cosine</a>
  666.      * of this complex number.
  667.      * Implements the formula:
  668.      * <pre>
  669.      *  <code>
  670.      *   cos(a + bi) = cos(a)cosh(b) - sin(a)sinh(b)i
  671.      *  </code>
  672.      * </pre>
  673.      * where the (real) functions on the right-hand side are
  674.      * {@link FastMath#sin}, {@link FastMath#cos},
  675.      * {@link FastMath#cosh} and {@link FastMath#sinh}.
  676.      * <br/>
  677.      * Returns {@link Complex#NaN} if either real or imaginary part of the
  678.      * input argument is {@code NaN}.
  679.      * <br/>
  680.      * Infinite values in real or imaginary parts of the input may result in
  681.      * infinite or NaN values returned in parts of the result.
  682.      * <pre>
  683.      *  Examples:
  684.      *  <code>
  685.      *   cos(1 &plusmn; INFINITY i) = 1 &#x2213; INFINITY i
  686.      *   cos(&plusmn;INFINITY + i) = NaN + NaN i
  687.      *   cos(&plusmn;INFINITY &plusmn; INFINITY i) = NaN + NaN i
  688.      *  </code>
  689.      * </pre>
  690.      *
  691.      * @return the cosine of this complex number.
  692.      * @since 1.2
  693.      */
  694.     public Complex cos() {
  695.         if (isNaN) {
  696.             return NaN;
  697.         }

  698.         return createComplex(FastMath.cos(real) * FastMath.cosh(imaginary),
  699.                              -FastMath.sin(real) * FastMath.sinh(imaginary));
  700.     }

  701.     /**
  702.      * Compute the
  703.      * <a href="http://mathworld.wolfram.com/HyperbolicCosine.html" TARGET="_top">
  704.      * hyperbolic cosine</a> of this complex number.
  705.      * Implements the formula:
  706.      * <pre>
  707.      *  <code>
  708.      *   cosh(a + bi) = cosh(a)cos(b) + sinh(a)sin(b)i}
  709.      *  </code>
  710.      * </pre>
  711.      * where the (real) functions on the right-hand side are
  712.      * {@link FastMath#sin}, {@link FastMath#cos},
  713.      * {@link FastMath#cosh} and {@link FastMath#sinh}.
  714.      * <br/>
  715.      * Returns {@link Complex#NaN} if either real or imaginary part of the
  716.      * input argument is {@code NaN}.
  717.      * <br/>
  718.      * Infinite values in real or imaginary parts of the input may result in
  719.      * infinite or NaN values returned in parts of the result.
  720.      * <pre>
  721.      *  Examples:
  722.      *  <code>
  723.      *   cosh(1 &plusmn; INFINITY i) = NaN + NaN i
  724.      *   cosh(&plusmn;INFINITY + i) = INFINITY &plusmn; INFINITY i
  725.      *   cosh(&plusmn;INFINITY &plusmn; INFINITY i) = NaN + NaN i
  726.      *  </code>
  727.      * </pre>
  728.      *
  729.      * @return the hyperbolic cosine of this complex number.
  730.      * @since 1.2
  731.      */
  732.     public Complex cosh() {
  733.         if (isNaN) {
  734.             return NaN;
  735.         }

  736.         return createComplex(FastMath.cosh(real) * FastMath.cos(imaginary),
  737.                              FastMath.sinh(real) * FastMath.sin(imaginary));
  738.     }

  739.     /**
  740.      * Compute the
  741.      * <a href="http://mathworld.wolfram.com/ExponentialFunction.html" TARGET="_top">
  742.      * exponential function</a> of this complex number.
  743.      * Implements the formula:
  744.      * <pre>
  745.      *  <code>
  746.      *   exp(a + bi) = exp(a)cos(b) + exp(a)sin(b)i
  747.      *  </code>
  748.      * </pre>
  749.      * where the (real) functions on the right-hand side are
  750.      * {@link FastMath#exp}, {@link FastMath#cos}, and
  751.      * {@link FastMath#sin}.
  752.      * <br/>
  753.      * Returns {@link Complex#NaN} if either real or imaginary part of the
  754.      * input argument is {@code NaN}.
  755.      * <br/>
  756.      * Infinite values in real or imaginary parts of the input may result in
  757.      * infinite or NaN values returned in parts of the result.
  758.      * <pre>
  759.      *  Examples:
  760.      *  <code>
  761.      *   exp(1 &plusmn; INFINITY i) = NaN + NaN i
  762.      *   exp(INFINITY + i) = INFINITY + INFINITY i
  763.      *   exp(-INFINITY + i) = 0 + 0i
  764.      *   exp(&plusmn;INFINITY &plusmn; INFINITY i) = NaN + NaN i
  765.      *  </code>
  766.      * </pre>
  767.      *
  768.      * @return <code><i>e</i><sup>this</sup></code>.
  769.      * @since 1.2
  770.      */
  771.     public Complex exp() {
  772.         if (isNaN) {
  773.             return NaN;
  774.         }

  775.         double expReal = FastMath.exp(real);
  776.         return createComplex(expReal *  FastMath.cos(imaginary),
  777.                              expReal * FastMath.sin(imaginary));
  778.     }

  779.     /**
  780.      * Compute the
  781.      * <a href="http://mathworld.wolfram.com/NaturalLogarithm.html" TARGET="_top">
  782.      * natural logarithm</a> of this complex number.
  783.      * Implements the formula:
  784.      * <pre>
  785.      *  <code>
  786.      *   log(a + bi) = ln(|a + bi|) + arg(a + bi)i
  787.      *  </code>
  788.      * </pre>
  789.      * where ln on the right hand side is {@link FastMath#log},
  790.      * {@code |a + bi|} is the modulus, {@link Complex#abs},  and
  791.      * {@code arg(a + bi) = }{@link FastMath#atan2}(b, a).
  792.      * <br/>
  793.      * Returns {@link Complex#NaN} if either real or imaginary part of the
  794.      * input argument is {@code NaN}.
  795.      * <br/>
  796.      * Infinite (or critical) values in real or imaginary parts of the input may
  797.      * result in infinite or NaN values returned in parts of the result.
  798.      * <pre>
  799.      *  Examples:
  800.      *  <code>
  801.      *   log(1 &plusmn; INFINITY i) = INFINITY &plusmn; (&pi;/2)i
  802.      *   log(INFINITY + i) = INFINITY + 0i
  803.      *   log(-INFINITY + i) = INFINITY + &pi;i
  804.      *   log(INFINITY &plusmn; INFINITY i) = INFINITY &plusmn; (&pi;/4)i
  805.      *   log(-INFINITY &plusmn; INFINITY i) = INFINITY &plusmn; (3&pi;/4)i
  806.      *   log(0 + 0i) = -INFINITY + 0i
  807.      *  </code>
  808.      * </pre>
  809.      *
  810.      * @return the value <code>ln &nbsp; this</code>, the natural logarithm
  811.      * of {@code this}.
  812.      * @since 1.2
  813.      */
  814.     public Complex log() {
  815.         if (isNaN) {
  816.             return NaN;
  817.         }

  818.         return createComplex(FastMath.log(abs()),
  819.                              FastMath.atan2(imaginary, real));
  820.     }

  821.     /**
  822.      * Returns of value of this complex number raised to the power of {@code x}.
  823.      * Implements the formula:
  824.      * <pre>
  825.      *  <code>
  826.      *   y<sup>x</sup> = exp(x&middot;log(y))
  827.      *  </code>
  828.      * </pre>
  829.      * where {@code exp} and {@code log} are {@link #exp} and
  830.      * {@link #log}, respectively.
  831.      * <br/>
  832.      * Returns {@link Complex#NaN} if either real or imaginary part of the
  833.      * input argument is {@code NaN} or infinite, or if {@code y}
  834.      * equals {@link Complex#ZERO}.
  835.      *
  836.      * @param  x exponent to which this {@code Complex} is to be raised.
  837.      * @return <code> this<sup>{@code x}</sup></code>.
  838.      * @throws NullArgumentException if x is {@code null}.
  839.      * @since 1.2
  840.      */
  841.     public Complex pow(Complex x)
  842.         throws NullArgumentException {
  843.         MathUtils.checkNotNull(x);
  844.         return this.log().multiply(x).exp();
  845.     }

  846.     /**
  847.      * Returns of value of this complex number raised to the power of {@code x}.
  848.      *
  849.      * @param  x exponent to which this {@code Complex} is to be raised.
  850.      * @return <code>this<sup>x</sup></code>.
  851.      * @see #pow(Complex)
  852.      */
  853.      public Complex pow(double x) {
  854.         return this.log().multiply(x).exp();
  855.     }

  856.     /**
  857.      * Compute the
  858.      * <a href="http://mathworld.wolfram.com/Sine.html" TARGET="_top">
  859.      * sine</a>
  860.      * of this complex number.
  861.      * Implements the formula:
  862.      * <pre>
  863.      *  <code>
  864.      *   sin(a + bi) = sin(a)cosh(b) - cos(a)sinh(b)i
  865.      *  </code>
  866.      * </pre>
  867.      * where the (real) functions on the right-hand side are
  868.      * {@link FastMath#sin}, {@link FastMath#cos},
  869.      * {@link FastMath#cosh} and {@link FastMath#sinh}.
  870.      * <br/>
  871.      * Returns {@link Complex#NaN} if either real or imaginary part of the
  872.      * input argument is {@code NaN}.
  873.      * <br/>
  874.      * Infinite values in real or imaginary parts of the input may result in
  875.      * infinite or {@code NaN} values returned in parts of the result.
  876.      * <pre>
  877.      *  Examples:
  878.      *  <code>
  879.      *   sin(1 &plusmn; INFINITY i) = 1 &plusmn; INFINITY i
  880.      *   sin(&plusmn;INFINITY + i) = NaN + NaN i
  881.      *   sin(&plusmn;INFINITY &plusmn; INFINITY i) = NaN + NaN i
  882.      *  </code>
  883.      * </pre>
  884.      *
  885.      * @return the sine of this complex number.
  886.      * @since 1.2
  887.      */
  888.     public Complex sin() {
  889.         if (isNaN) {
  890.             return NaN;
  891.         }

  892.         return createComplex(FastMath.sin(real) * FastMath.cosh(imaginary),
  893.                              FastMath.cos(real) * FastMath.sinh(imaginary));
  894.     }

  895.     /**
  896.      * Compute the
  897.      * <a href="http://mathworld.wolfram.com/HyperbolicSine.html" TARGET="_top">
  898.      * hyperbolic sine</a> of this complex number.
  899.      * Implements the formula:
  900.      * <pre>
  901.      *  <code>
  902.      *   sinh(a + bi) = sinh(a)cos(b)) + cosh(a)sin(b)i
  903.      *  </code>
  904.      * </pre>
  905.      * where the (real) functions on the right-hand side are
  906.      * {@link FastMath#sin}, {@link FastMath#cos},
  907.      * {@link FastMath#cosh} and {@link FastMath#sinh}.
  908.      * <br/>
  909.      * Returns {@link Complex#NaN} if either real or imaginary part of the
  910.      * input argument is {@code NaN}.
  911.      * <br/>
  912.      * Infinite values in real or imaginary parts of the input may result in
  913.      * infinite or NaN values returned in parts of the result.
  914.      * <pre>
  915.      *  Examples:
  916.      *  <code>
  917.      *   sinh(1 &plusmn; INFINITY i) = NaN + NaN i
  918.      *   sinh(&plusmn;INFINITY + i) = &plusmn; INFINITY + INFINITY i
  919.      *   sinh(&plusmn;INFINITY &plusmn; INFINITY i) = NaN + NaN i
  920.      *  </code>
  921.      * </pre>
  922.      *
  923.      * @return the hyperbolic sine of {@code this}.
  924.      * @since 1.2
  925.      */
  926.     public Complex sinh() {
  927.         if (isNaN) {
  928.             return NaN;
  929.         }

  930.         return createComplex(FastMath.sinh(real) * FastMath.cos(imaginary),
  931.             FastMath.cosh(real) * FastMath.sin(imaginary));
  932.     }

  933.     /**
  934.      * Compute the
  935.      * <a href="http://mathworld.wolfram.com/SquareRoot.html" TARGET="_top">
  936.      * square root</a> of this complex number.
  937.      * Implements the following algorithm to compute {@code sqrt(a + bi)}:
  938.      * <ol><li>Let {@code t = sqrt((|a| + |a + bi|) / 2)}</li>
  939.      * <li><pre>if {@code  a &#8805; 0} return {@code t + (b/2t)i}
  940.      *  else return {@code |b|/2t + sign(b)t i }</pre></li>
  941.      * </ol>
  942.      * where <ul>
  943.      * <li>{@code |a| = }{@link FastMath#abs}(a)</li>
  944.      * <li>{@code |a + bi| = }{@link Complex#abs}(a + bi)</li>
  945.      * <li>{@code sign(b) =  }{@link FastMath#copySign(double,double) copySign(1d, b)}
  946.      * </ul>
  947.      * <br/>
  948.      * Returns {@link Complex#NaN} if either real or imaginary part of the
  949.      * input argument is {@code NaN}.
  950.      * <br/>
  951.      * Infinite values in real or imaginary parts of the input may result in
  952.      * infinite or NaN values returned in parts of the result.
  953.      * <pre>
  954.      *  Examples:
  955.      *  <code>
  956.      *   sqrt(1 &plusmn; INFINITY i) = INFINITY + NaN i
  957.      *   sqrt(INFINITY + i) = INFINITY + 0i
  958.      *   sqrt(-INFINITY + i) = 0 + INFINITY i
  959.      *   sqrt(INFINITY &plusmn; INFINITY i) = INFINITY + NaN i
  960.      *   sqrt(-INFINITY &plusmn; INFINITY i) = NaN &plusmn; INFINITY i
  961.      *  </code>
  962.      * </pre>
  963.      *
  964.      * @return the square root of {@code this}.
  965.      * @since 1.2
  966.      */
  967.     public Complex sqrt() {
  968.         if (isNaN) {
  969.             return NaN;
  970.         }

  971.         if (real == 0.0 && imaginary == 0.0) {
  972.             return createComplex(0.0, 0.0);
  973.         }

  974.         double t = FastMath.sqrt((FastMath.abs(real) + abs()) / 2.0);
  975.         if (real >= 0.0) {
  976.             return createComplex(t, imaginary / (2.0 * t));
  977.         } else {
  978.             return createComplex(FastMath.abs(imaginary) / (2.0 * t),
  979.                                  FastMath.copySign(1d, imaginary) * t);
  980.         }
  981.     }

  982.     /**
  983.      * Compute the
  984.      * <a href="http://mathworld.wolfram.com/SquareRoot.html" TARGET="_top">
  985.      * square root</a> of <code>1 - this<sup>2</sup></code> for this complex
  986.      * number.
  987.      * Computes the result directly as
  988.      * {@code sqrt(ONE.subtract(z.multiply(z)))}.
  989.      * <br/>
  990.      * Returns {@link Complex#NaN} if either real or imaginary part of the
  991.      * input argument is {@code NaN}.
  992.      * <br/>
  993.      * Infinite values in real or imaginary parts of the input may result in
  994.      * infinite or NaN values returned in parts of the result.
  995.      *
  996.      * @return the square root of <code>1 - this<sup>2</sup></code>.
  997.      * @since 1.2
  998.      */
  999.     public Complex sqrt1z() {
  1000.         return createComplex(1.0, 0.0).subtract(this.multiply(this)).sqrt();
  1001.     }

  1002.     /**
  1003.      * Compute the
  1004.      * <a href="http://mathworld.wolfram.com/Tangent.html" TARGET="_top">
  1005.      * tangent</a> of this complex number.
  1006.      * Implements the formula:
  1007.      * <pre>
  1008.      *  <code>
  1009.      *   tan(a + bi) = sin(2a)/(cos(2a)+cosh(2b)) + [sinh(2b)/(cos(2a)+cosh(2b))]i
  1010.      *  </code>
  1011.      * </pre>
  1012.      * where the (real) functions on the right-hand side are
  1013.      * {@link FastMath#sin}, {@link FastMath#cos}, {@link FastMath#cosh} and
  1014.      * {@link FastMath#sinh}.
  1015.      * <br/>
  1016.      * Returns {@link Complex#NaN} if either real or imaginary part of the
  1017.      * input argument is {@code NaN}.
  1018.      * <br/>
  1019.      * Infinite (or critical) values in real or imaginary parts of the input may
  1020.      * result in infinite or NaN values returned in parts of the result.
  1021.      * <pre>
  1022.      *  Examples:
  1023.      *  <code>
  1024.      *   tan(a &plusmn; INFINITY i) = 0 &plusmn; i
  1025.      *   tan(&plusmn;INFINITY + bi) = NaN + NaN i
  1026.      *   tan(&plusmn;INFINITY &plusmn; INFINITY i) = NaN + NaN i
  1027.      *   tan(&plusmn;&pi;/2 + 0 i) = &plusmn;INFINITY + NaN i
  1028.      *  </code>
  1029.      * </pre>
  1030.      *
  1031.      * @return the tangent of {@code this}.
  1032.      * @since 1.2
  1033.      */
  1034.     public Complex tan() {
  1035.         if (isNaN || Double.isInfinite(real)) {
  1036.             return NaN;
  1037.         }
  1038.         if (imaginary > 20.0) {
  1039.             return createComplex(0.0, 1.0);
  1040.         }
  1041.         if (imaginary < -20.0) {
  1042.             return createComplex(0.0, -1.0);
  1043.         }

  1044.         double real2 = 2.0 * real;
  1045.         double imaginary2 = 2.0 * imaginary;
  1046.         double d = FastMath.cos(real2) + FastMath.cosh(imaginary2);

  1047.         return createComplex(FastMath.sin(real2) / d,
  1048.                              FastMath.sinh(imaginary2) / d);
  1049.     }

  1050.     /**
  1051.      * Compute the
  1052.      * <a href="http://mathworld.wolfram.com/HyperbolicTangent.html" TARGET="_top">
  1053.      * hyperbolic tangent</a> of this complex number.
  1054.      * Implements the formula:
  1055.      * <pre>
  1056.      *  <code>
  1057.      *   tan(a + bi) = sinh(2a)/(cosh(2a)+cos(2b)) + [sin(2b)/(cosh(2a)+cos(2b))]i
  1058.      *  </code>
  1059.      * </pre>
  1060.      * where the (real) functions on the right-hand side are
  1061.      * {@link FastMath#sin}, {@link FastMath#cos}, {@link FastMath#cosh} and
  1062.      * {@link FastMath#sinh}.
  1063.      * <br/>
  1064.      * Returns {@link Complex#NaN} if either real or imaginary part of the
  1065.      * input argument is {@code NaN}.
  1066.      * <br/>
  1067.      * Infinite values in real or imaginary parts of the input may result in
  1068.      * infinite or NaN values returned in parts of the result.
  1069.      * <pre>
  1070.      *  Examples:
  1071.      *  <code>
  1072.      *   tanh(a &plusmn; INFINITY i) = NaN + NaN i
  1073.      *   tanh(&plusmn;INFINITY + bi) = &plusmn;1 + 0 i
  1074.      *   tanh(&plusmn;INFINITY &plusmn; INFINITY i) = NaN + NaN i
  1075.      *   tanh(0 + (&pi;/2)i) = NaN + INFINITY i
  1076.      *  </code>
  1077.      * </pre>
  1078.      *
  1079.      * @return the hyperbolic tangent of {@code this}.
  1080.      * @since 1.2
  1081.      */
  1082.     public Complex tanh() {
  1083.         if (isNaN || Double.isInfinite(imaginary)) {
  1084.             return NaN;
  1085.         }
  1086.         if (real > 20.0) {
  1087.             return createComplex(1.0, 0.0);
  1088.         }
  1089.         if (real < -20.0) {
  1090.             return createComplex(-1.0, 0.0);
  1091.         }
  1092.         double real2 = 2.0 * real;
  1093.         double imaginary2 = 2.0 * imaginary;
  1094.         double d = FastMath.cosh(real2) + FastMath.cos(imaginary2);

  1095.         return createComplex(FastMath.sinh(real2) / d,
  1096.                              FastMath.sin(imaginary2) / d);
  1097.     }



  1098.     /**
  1099.      * Compute the argument of this complex number.
  1100.      * The argument is the angle phi between the positive real axis and
  1101.      * the point representing this number in the complex plane.
  1102.      * The value returned is between -PI (not inclusive)
  1103.      * and PI (inclusive), with negative values returned for numbers with
  1104.      * negative imaginary parts.
  1105.      * <br/>
  1106.      * If either real or imaginary part (or both) is NaN, NaN is returned.
  1107.      * Infinite parts are handled as {@code Math.atan2} handles them,
  1108.      * essentially treating finite parts as zero in the presence of an
  1109.      * infinite coordinate and returning a multiple of pi/4 depending on
  1110.      * the signs of the infinite parts.
  1111.      * See the javadoc for {@code Math.atan2} for full details.
  1112.      *
  1113.      * @return the argument of {@code this}.
  1114.      */
  1115.     public double getArgument() {
  1116.         return FastMath.atan2(getImaginary(), getReal());
  1117.     }

  1118.     /**
  1119.      * Computes the n-th roots of this complex number.
  1120.      * The nth roots are defined by the formula:
  1121.      * <pre>
  1122.      *  <code>
  1123.      *   z<sub>k</sub> = abs<sup>1/n</sup> (cos(phi + 2&pi;k/n) + i (sin(phi + 2&pi;k/n))
  1124.      *  </code>
  1125.      * </pre>
  1126.      * for <i>{@code k=0, 1, ..., n-1}</i>, where {@code abs} and {@code phi}
  1127.      * are respectively the {@link #abs() modulus} and
  1128.      * {@link #getArgument() argument} of this complex number.
  1129.      * <br/>
  1130.      * If one or both parts of this complex number is NaN, a list with just
  1131.      * one element, {@link #NaN} is returned.
  1132.      * if neither part is NaN, but at least one part is infinite, the result
  1133.      * is a one-element list containing {@link #INF}.
  1134.      *
  1135.      * @param n Degree of root.
  1136.      * @return a List<Complex> of all {@code n}-th roots of {@code this}.
  1137.      * @throws NotPositiveException if {@code n <= 0}.
  1138.      * @since 2.0
  1139.      */
  1140.     public List<Complex> nthRoot(int n) throws NotPositiveException {

  1141.         if (n <= 0) {
  1142.             throw new NotPositiveException(LocalizedFormats.CANNOT_COMPUTE_NTH_ROOT_FOR_NEGATIVE_N,
  1143.                                            n);
  1144.         }

  1145.         final List<Complex> result = new ArrayList<Complex>();

  1146.         if (isNaN) {
  1147.             result.add(NaN);
  1148.             return result;
  1149.         }
  1150.         if (isInfinite()) {
  1151.             result.add(INF);
  1152.             return result;
  1153.         }

  1154.         // nth root of abs -- faster / more accurate to use a solver here?
  1155.         final double nthRootOfAbs = FastMath.pow(abs(), 1.0 / n);

  1156.         // Compute nth roots of complex number with k = 0, 1, ... n-1
  1157.         final double nthPhi = getArgument() / n;
  1158.         final double slice = 2 * FastMath.PI / n;
  1159.         double innerPart = nthPhi;
  1160.         for (int k = 0; k < n ; k++) {
  1161.             // inner part
  1162.             final double realPart = nthRootOfAbs *  FastMath.cos(innerPart);
  1163.             final double imaginaryPart = nthRootOfAbs *  FastMath.sin(innerPart);
  1164.             result.add(createComplex(realPart, imaginaryPart));
  1165.             innerPart += slice;
  1166.         }

  1167.         return result;
  1168.     }

  1169.     /**
  1170.      * Create a complex number given the real and imaginary parts.
  1171.      *
  1172.      * @param realPart Real part.
  1173.      * @param imaginaryPart Imaginary part.
  1174.      * @return a new complex number instance.
  1175.      * @since 1.2
  1176.      * @see #valueOf(double, double)
  1177.      */
  1178.     protected Complex createComplex(double realPart,
  1179.                                     double imaginaryPart) {
  1180.         return new Complex(realPart, imaginaryPart);
  1181.     }

  1182.     /**
  1183.      * Create a complex number given the real and imaginary parts.
  1184.      *
  1185.      * @param realPart Real part.
  1186.      * @param imaginaryPart Imaginary part.
  1187.      * @return a Complex instance.
  1188.      */
  1189.     public static Complex valueOf(double realPart,
  1190.                                   double imaginaryPart) {
  1191.         if (Double.isNaN(realPart) ||
  1192.             Double.isNaN(imaginaryPart)) {
  1193.             return NaN;
  1194.         }
  1195.         return new Complex(realPart, imaginaryPart);
  1196.     }

  1197.     /**
  1198.      * Create a complex number given only the real part.
  1199.      *
  1200.      * @param realPart Real part.
  1201.      * @return a Complex instance.
  1202.      */
  1203.     public static Complex valueOf(double realPart) {
  1204.         if (Double.isNaN(realPart)) {
  1205.             return NaN;
  1206.         }
  1207.         return new Complex(realPart);
  1208.     }

  1209.     /**
  1210.      * Resolve the transient fields in a deserialized Complex Object.
  1211.      * Subclasses will need to override {@link #createComplex} to
  1212.      * deserialize properly.
  1213.      *
  1214.      * @return A Complex instance with all fields resolved.
  1215.      * @since 2.0
  1216.      */
  1217.     protected final Object readResolve() {
  1218.         return createComplex(real, imaginary);
  1219.     }

  1220.     /** {@inheritDoc} */
  1221.     public ComplexField getField() {
  1222.         return ComplexField.getInstance();
  1223.     }

  1224.     /** {@inheritDoc} */
  1225.     @Override
  1226.     public String toString() {
  1227.         return "(" + real + ", " + imaginary + ")";
  1228.     }

  1229. }