Return-Path: X-Original-To: apmail-commons-commits-archive@minotaur.apache.org Delivered-To: apmail-commons-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id E5FE274B5 for ; Thu, 14 Jul 2011 15:25:37 +0000 (UTC) Received: (qmail 40496 invoked by uid 500); 14 Jul 2011 15:25:37 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 40304 invoked by uid 500); 14 Jul 2011 15:25:36 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 40293 invoked by uid 99); 14 Jul 2011 15:25:36 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 14 Jul 2011 15:25:36 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 14 Jul 2011 15:25:33 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 5B64B23888EA for ; Thu, 14 Jul 2011 15:25:12 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1146755 - in /commons/proper/math/trunk/src: main/java/org/apache/commons/math/complex/ComplexFormat.java test/java/org/apache/commons/math/complex/ComplexFormatAbstractTest.java Date: Thu, 14 Jul 2011 15:25:12 -0000 To: commits@commons.apache.org From: erans@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20110714152512.5B64B23888EA@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: erans Date: Thu Jul 14 15:25:11 2011 New Revision: 1146755 URL: http://svn.apache.org/viewvc?rev=1146755&view=rev Log: MATH-617 Make "1 + 1i" appear as "1 + i" on formatted output. Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/complex/ComplexFormat.java commons/proper/math/trunk/src/test/java/org/apache/commons/math/complex/ComplexFormatAbstractTest.java Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/complex/ComplexFormat.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/complex/ComplexFormat.java?rev=1146755&r1=1146754&r2=1146755&view=diff ============================================================================== --- commons/proper/math/trunk/src/main/java/org/apache/commons/math/complex/ComplexFormat.java (original) +++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/complex/ComplexFormat.java Thu Jul 14 15:25:11 2011 @@ -26,6 +26,7 @@ import org.apache.commons.math.util.Comp import org.apache.commons.math.exception.util.LocalizedFormats; import org.apache.commons.math.exception.MathParseException; import org.apache.commons.math.exception.MathIllegalArgumentException; +import org.apache.commons.math.exception.MathInternalError; import org.apache.commons.math.exception.NullArgumentException; import org.apache.commons.math.exception.NoDataException; @@ -178,13 +179,16 @@ public class ComplexFormat { // format sign and imaginary double im = complex.getImaginary(); + StringBuffer imAppendTo = new StringBuffer(); if (im < 0.0) { toAppendTo.append(" - "); - CompositeFormat.formatDouble(-im, getImaginaryFormat(), toAppendTo, pos); + imAppendTo = formatImaginary(-im, new StringBuffer(), pos); + toAppendTo.append(imAppendTo); toAppendTo.append(getImaginaryCharacter()); } else if (im > 0.0 || Double.isNaN(im)) { toAppendTo.append(" + "); - CompositeFormat.formatDouble(im, getImaginaryFormat(), toAppendTo, pos); + imAppendTo = formatImaginary(im, new StringBuffer(), pos); + toAppendTo.append(imAppendTo); toAppendTo.append(getImaginaryCharacter()); } @@ -192,6 +196,35 @@ public class ComplexFormat { } /** + * Format the absolute value of the imaginary part. + * + * @param absIm Absolute value of the imaginary part of a complex number. + * @param toAppendTo where the text is to be appended. + * @param pos On input: an alignment field, if desired. On output: the + * offsets of the alignment field. + * @return the value passed in as toAppendTo. + * @throws MathInternalError if {@code absIm} is not positive. + */ + private StringBuffer formatImaginary(double absIm, + StringBuffer toAppendTo, + FieldPosition pos) { + if (absIm < 0) { + throw new MathInternalError(); + } + + pos.setBeginIndex(0); + pos.setEndIndex(0); + + CompositeFormat.formatDouble(absIm, getImaginaryFormat(), toAppendTo, pos); + if (toAppendTo.toString().equals("1")) { + // Remove the character "1" if it is the only one. + toAppendTo.setLength(0); + } + + return toAppendTo; + } + + /** * Formats a object to produce a string. {@code obj} must be either a * {@link Complex} object or a {@link Number} object. Any other type of * object will result in an {@link IllegalArgumentException} being thrown. Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/complex/ComplexFormatAbstractTest.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/complex/ComplexFormatAbstractTest.java?rev=1146755&r1=1146754&r2=1146755&view=diff ============================================================================== --- commons/proper/math/trunk/src/test/java/org/apache/commons/math/complex/ComplexFormatAbstractTest.java (original) +++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/complex/ComplexFormatAbstractTest.java Thu Jul 14 15:25:11 2011 @@ -42,13 +42,39 @@ public abstract class ComplexFormatAbstr @Test public void testSimpleNoDecimals() { - Complex c = new Complex(1, 1); - String expected = "1 + 1i"; + Complex c = new Complex(1, 2); + String expected = "1 + 2i"; String actual = complexFormat.format(c); Assert.assertEquals(expected, actual); } @Test + public void testTrimOneImaginary() { + final ComplexFormat fmt = ComplexFormat.getInstance(getLocale()); + fmt.getImaginaryFormat().setMaximumFractionDigits(1); + + Complex c = new Complex(1, 1.04); + String expected = "1 + i"; + String actual = fmt.format(c); + Assert.assertEquals(expected, actual); + + c = new Complex(1, 1.09); + expected = "1 + 1" + getDecimalCharacter() + "1i"; + actual = fmt.format(c); + Assert.assertEquals(expected, actual); + + c = new Complex(1, -1.09); + expected = "1 - 1" + getDecimalCharacter() + "1i"; + actual = fmt.format(c); + Assert.assertEquals(expected, actual); + + c = new Complex(1, -1.04); + expected = "1 - i"; + actual = fmt.format(c); + Assert.assertEquals(expected, actual); + } + + @Test public void testSimpleWithDecimals() { Complex c = new Complex(1.23, 1.43); String expected = "1" + getDecimalCharacter() + "23 + 1" + getDecimalCharacter() + "43i"; @@ -107,7 +133,7 @@ public abstract class ComplexFormatAbstr @Test public void testDifferentImaginaryChar() { Complex c = new Complex(1, 1); - String expected = "1 + 1j"; + String expected = "1 + j"; String actual = complexFormatJ.format(c); Assert.assertEquals(expected, actual); }