commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Arne Plöse (JIRA) <j...@apache.org>
Subject [jira] [Updated] (MATH-577) Enhance Complex.java
Date Mon, 16 May 2011 07:43:47 GMT

     [ https://issues.apache.org/jira/browse/MATH-577?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]

Arne Plöse updated MATH-577:
----------------------------

    Attachment: Complex.diff

OK her is the diff

> Enhance Complex.java
> --------------------
>
>                 Key: MATH-577
>                 URL: https://issues.apache.org/jira/browse/MATH-577
>             Project: Commons Math
>          Issue Type: Improvement
>    Affects Versions: 3.0
>            Reporter: Arne Plöse
>            Priority: Minor
>         Attachments: Complex.diff
>
>
> Add some double shorthand methods to Complex fix different NaN checks in add and subtract
! Testcase  testAddNaN will fail (what should be the result ?)
> What is missing JavaDoc and testcases.
> Sorry, I can't find anything to attach a file?
> Index: src/main/java/org/apache/commons/math/complex/Complex.java
> ===================================================================
> --- src/main/java/org/apache/commons/math/complex/Complex.java	(Revision 1103615)
> +++ src/main/java/org/apache/commons/math/complex/Complex.java	(Arbeitskopie)
> @@ -80,6 +80,15 @@
>      private final transient boolean isInfinite;
>  
>      /**
> +     * Create a complex number given the real part.
> +     *
> +     * @param real the real part
> +     */
> +    public Complex(double real) {
> +        this(real, 0);
> +    }
> +
> +    /**
>       * Create a complex number given the real and imaginary parts.
>       *
>       * @param real the real part
> @@ -147,10 +156,20 @@
>       * @throws NullPointerException if <code>rhs</code> is null
>       */
>      public Complex add(Complex rhs) {
> +        if (isAnyNaN(rhs)) {
> +            return Complex.NaN;
> +        }
>          return createComplex(real + rhs.getReal(),
>              imaginary + rhs.getImaginary());
>      }
>  
> +    public Complex add(double rhs) {
> +        if (isAnyNaN(rhs)) {
> +            return Complex.NaN;
> +        }
> +        return createComplex(real + rhs, imaginary);
> +    }
> +
>      /**
>       * Return the conjugate of this complex number. The conjugate of
>       * "A + Bi" is "A - Bi".
> @@ -167,7 +186,7 @@
>       */
>      public Complex conjugate() {
>          if (isNaN()) {
> -            return NaN;
> +            return Complex.NaN;
>          }
>          return createComplex(real, -imaginary);
>      }
> @@ -208,18 +227,18 @@
>       * @throws NullPointerException if <code>rhs</code> is null
>       */
>      public Complex divide(Complex rhs) {
> -        if (isNaN() || rhs.isNaN()) {
> -            return NaN;
> +        if (isAnyNaN(rhs)) {
> +            return Complex.NaN;
>          }
>  
>          double c = rhs.getReal();
>          double d = rhs.getImaginary();
>          if (c == 0.0 && d == 0.0) {
> -            return NaN;
> +            return Complex.NaN;
>          }
>  
>          if (rhs.isInfinite() && !isInfinite()) {
> -            return ZERO;
> +            return Complex.ZERO;
>          }
>  
>          if (FastMath.abs(c) < FastMath.abs(d)) {
> @@ -236,6 +255,28 @@
>      }
>  
>      /**
> +     * Just a condensed variant of divide with rhs.imaginary == 0...
> +     * @param rhs
> +     * @return
> +     */
> +    public Complex divide(double rhs) {
> +        if (isAnyNaN(rhs)) {
> +            return Complex.NaN;
> +        }
> +
> +        double d0 = 0;
> +        if (rhs == 0.0) {
> +            return Complex.NaN;
> +        }
> +
> +        if (Double.isInfinite(rhs) && !isInfinite()) {
> +            return Complex.ZERO;
> +        }
> +
> +        return createComplex(real / rhs, imaginary / rhs);
> +    }
> +
> +    /**
>       * Test for the equality of two Complex objects.
>       * <p>
>       * If both the real and imaginary parts of two Complex numbers
> @@ -269,6 +310,13 @@
>          return false;
>      }
>  
> +    /** {@inheritDoc} */
> +    @Override
> +    public String toString() {
> +        return String.format("%s %si", Double.toString(real), Double.toString(imaginary));
> +    }
> +
> +
>      /**
>       * Get a hashCode for the complex number.
>       * <p>
> @@ -355,8 +403,8 @@
>       * @throws NullPointerException if <code>rhs</code> is null
>       */
>      public Complex multiply(Complex rhs) {
> -        if (isNaN() || rhs.isNaN()) {
> -            return NaN;
> +        if (isAnyNaN(rhs)) {
> +            return Complex.NaN;
>          }
>          if (Double.isInfinite(real) || Double.isInfinite(imaginary) ||
>              Double.isInfinite(rhs.real)|| Double.isInfinite(rhs.imaginary)) {
> @@ -394,8 +442,8 @@
>       * @return the complex number product
>       */
>      public Complex multiply(double rhs) {
> -        if (isNaN() || Double.isNaN(rhs)) {
> -            return NaN;
> +        if (isAnyNaN(rhs)) {
> +            return Complex.NaN;
>          }
>          if (Double.isInfinite(real) || Double.isInfinite(imaginary) ||
>              Double.isInfinite(rhs)) {
> @@ -415,7 +463,7 @@
>       */
>      public Complex negate() {
>          if (isNaN()) {
> -            return NaN;
> +            return Complex.NaN;
>          }
>  
>          return createComplex(-real, -imaginary);
> @@ -440,14 +488,28 @@
>       * @throws NullPointerException if <code>rhs</code> is null
>       */
>      public Complex subtract(Complex rhs) {
> -        if (isNaN() || rhs.isNaN()) {
> -            return NaN;
> +        if (isAnyNaN(rhs)) {
> +            return Complex.NaN;
>          }
> -
>          return createComplex(real - rhs.getReal(),
>              imaginary - rhs.getImaginary());
>      }
>  
> +    protected boolean isAnyNaN(double rhs) {
> +        return isNaN() || Double.isNaN(rhs);
> +    }
> +
> +    protected boolean isAnyNaN(Complex rhs) {
> +        return isNaN() || rhs.isNaN();
> +    }
> +
> +    public Complex subtract(double rhs) {
> +        if (isAnyNaN(rhs)) {
> +            return Complex.NaN;
> +        }
> +        return createComplex(real - rhs, imaginary);
> +    }
> +
>      /**
>       * Compute the
>       * <a href="http://mathworld.wolfram.com/InverseCosine.html" TARGET="_top">
> @@ -683,6 +745,14 @@
>          return this.log().multiply(x).exp();
>      }
>  
> +    public Complex pow(double x) {
> +        return this.log().multiply(x).exp();
> +    }
> +
> +    public Complex pow(int x) {
> +        return this.log().multiply(x).exp();
> +    }
> +
>      /**
>       * Compute the
>       * <a href="http://mathworld.wolfram.com/Sine.html" TARGET="_top">

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

Mime
View raw message