Return-Path: X-Original-To: apmail-commons-issues-archive@minotaur.apache.org Delivered-To: apmail-commons-issues-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 408D664D8 for ; Mon, 16 May 2011 07:42:31 +0000 (UTC) Received: (qmail 50937 invoked by uid 500); 16 May 2011 07:42:31 -0000 Delivered-To: apmail-commons-issues-archive@commons.apache.org Received: (qmail 50648 invoked by uid 500); 16 May 2011 07:42:29 -0000 Mailing-List: contact issues-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: issues@commons.apache.org Delivered-To: mailing list issues@commons.apache.org Received: (qmail 50638 invoked by uid 99); 16 May 2011 07:42:28 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 16 May 2011 07:42:28 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED,T_RP_MATCHES_RCVD X-Spam-Check-By: apache.org Received: from [140.211.11.116] (HELO hel.zones.apache.org) (140.211.11.116) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 16 May 2011 07:42:26 +0000 Received: from hel.zones.apache.org (hel.zones.apache.org [140.211.11.116]) by hel.zones.apache.org (Postfix) with ESMTP id 57D55CC1BA for ; Mon, 16 May 2011 07:41:47 +0000 (UTC) Date: Mon, 16 May 2011 07:41:47 +0000 (UTC) From: =?utf-8?Q?Arne_Pl=C3=B6se_=28JIRA=29?= To: issues@commons.apache.org Message-ID: <880300261.14789.1305531707356.JavaMail.tomcat@hel.zones.apache.org> Subject: [jira] [Created] (MATH-577) Enhance Complex.java MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-JIRA-FingerPrint: 30527f35849b9dde25b450d4833f0394 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=C3=B6se Priority: Minor Add some double shorthand methods to Complex fix different NaN checks in ad= d 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 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- src/main/java/org/apache/commons/math/complex/Complex.java=09(Revision = 1103615) +++ src/main/java/org/apache/commons/math/complex/Complex.java=09(Arbeitsko= pie) @@ -80,6 +80,15 @@ private final transient boolean isInfinite; =20 /** + * 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 rhs is null */ public Complex add(Complex rhs) { + if (isAnyNaN(rhs)) { + return Complex.NaN; + } return createComplex(real + rhs.getReal(), imaginary + rhs.getImaginary()); } =20 + 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 rhs is null */ public Complex divide(Complex rhs) { - if (isNaN() || rhs.isNaN()) { - return NaN; + if (isAnyNaN(rhs)) { + return Complex.NaN; } =20 double c =3D rhs.getReal(); double d =3D rhs.getImaginary(); if (c =3D=3D 0.0 && d =3D=3D 0.0) { - return NaN; + return Complex.NaN; } =20 if (rhs.isInfinite() && !isInfinite()) { - return ZERO; + return Complex.ZERO; } =20 if (FastMath.abs(c) < FastMath.abs(d)) { @@ -236,6 +255,28 @@ } =20 /** + * Just a condensed variant of divide with rhs.imaginary =3D=3D 0... + * @param rhs + * @return + */ + public Complex divide(double rhs) { + if (isAnyNaN(rhs)) { + return Complex.NaN; + } + + double d0 =3D 0; + if (rhs =3D=3D 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. *

* If both the real and imaginary parts of two Complex numbers @@ -269,6 +310,13 @@ return false; } =20 + /** {@inheritDoc} */ + @Override + public String toString() { + return String.format("%s %si", Double.toString(real), Double.toStr= ing(imaginary)); + } + + /** * Get a hashCode for the complex number. *

@@ -355,8 +403,8 @@ * @throws NullPointerException if rhs 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; } =20 return createComplex(-real, -imaginary); @@ -440,14 +488,28 @@ * @throws NullPointerException if rhs 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()); } =20 + 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 * @@ -683,6 +745,14 @@ return this.log().multiply(x).exp(); } =20 + public Complex pow(double x) { + return this.log().multiply(x).exp(); + } + + public Complex pow(int x) { + return this.log().multiply(x).exp(); + } + /** * Compute the * -- This message is automatically generated by JIRA. For more information on JIRA, see: http://www.atlassian.com/software/jira