From 72779b230040d8dcc0c39d9791fc6c9d8fd2d142 Mon Sep 17 00:00:00 2001 From: Gilles Sadowski Date: Sun, 29 Jul 2012 12:05:48 +0000 Subject: [PATCH] MATH-622 Default is now to print 10 fractional digits. git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1366821 13f79535-47bb-0310-9956-ffa450edef68 --- src/changes/changes.xml | 3 ++ .../commons/math3/linear/LUDecomposition.java | 19 ++++--- .../commons/math3/util/CompositeFormat.java | 2 +- .../complex/ComplexFormatAbstractTest.java | 46 ++++++++-------- .../threed/Vector3DFormatAbstractTest.java | 28 +++++----- .../linear/RealMatrixFormatAbstractTest.java | 54 +++++++++---------- .../linear/RealVectorFormatAbstractTest.java | 28 +++++----- 7 files changed, 94 insertions(+), 86 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 88c94badc..2ae0029df 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -52,6 +52,9 @@ If the output is not quite correct, check for invisible trailing spaces! + + Raised (to 10) the default number of fractional digits to print out. + Removed duplicate code. diff --git a/src/main/java/org/apache/commons/math3/linear/LUDecomposition.java b/src/main/java/org/apache/commons/math3/linear/LUDecomposition.java index 65185d356..a2a4be90c 100644 --- a/src/main/java/org/apache/commons/math3/linear/LUDecomposition.java +++ b/src/main/java/org/apache/commons/math3/linear/LUDecomposition.java @@ -89,8 +89,8 @@ public class LUDecomposition { matrix.getColumnDimension()); } - final int m = matrix.getColumnDimension(); lu = matrix.getData(); + final int m = lu.length; pivot = new int[m]; cachedL = null; cachedU = null; @@ -105,15 +105,20 @@ public class LUDecomposition { // Loop over columns for (int col = 0; col < m; col++) { + final double[] luColumnCol = new double[m]; + for (int i = 0; i < m; i++) { + luColumnCol[i] = lu[i][col]; + } // upper for (int row = 0; row < col; row++) { final double[] luRow = lu[row]; double sum = luRow[col]; for (int i = 0; i < row; i++) { - sum -= luRow[i] * lu[i][col]; + sum -= luRow[i] * luColumnCol[i]; } luRow[col] = sum; + luColumnCol[row] = sum; } // lower @@ -123,9 +128,10 @@ public class LUDecomposition { final double[] luRow = lu[row]; double sum = luRow[col]; for (int i = 0; i < col; i++) { - sum -= luRow[i] * lu[i][col]; + sum -= luRow[i] * luColumnCol[i]; } luRow[col] = sum; + luColumnCol[row] = sum; // maintain best permutation choice if (FastMath.abs(sum) > largest) { @@ -135,22 +141,21 @@ public class LUDecomposition { } // Singularity check - if (FastMath.abs(lu[max][col]) < singularityThreshold) { + if (FastMath.abs(luColumnCol[max]) < singularityThreshold) { singular = true; return; } // Pivot if necessary if (max != col) { - double tmp = 0; final double[] luMax = lu[max]; final double[] luCol = lu[col]; for (int i = 0; i < m; i++) { - tmp = luMax[i]; + final double tmp = luMax[i]; luMax[i] = luCol[i]; luCol[i] = tmp; } - int temp = pivot[max]; + final int temp = pivot[max]; pivot[max] = pivot[col]; pivot[col] = temp; even = !even; diff --git a/src/main/java/org/apache/commons/math3/util/CompositeFormat.java b/src/main/java/org/apache/commons/math3/util/CompositeFormat.java index 491bc9ce9..46b74349e 100644 --- a/src/main/java/org/apache/commons/math3/util/CompositeFormat.java +++ b/src/main/java/org/apache/commons/math3/util/CompositeFormat.java @@ -52,7 +52,7 @@ public class CompositeFormat { */ public static NumberFormat getDefaultNumberFormat(final Locale locale) { final NumberFormat nf = NumberFormat.getInstance(locale); - nf.setMaximumFractionDigits(2); + nf.setMaximumFractionDigits(10); return nf; } diff --git a/src/test/java/org/apache/commons/math3/complex/ComplexFormatAbstractTest.java b/src/test/java/org/apache/commons/math3/complex/ComplexFormatAbstractTest.java index a7ca4f891..b57c26ce8 100644 --- a/src/test/java/org/apache/commons/math3/complex/ComplexFormatAbstractTest.java +++ b/src/test/java/org/apache/commons/math3/complex/ComplexFormatAbstractTest.java @@ -84,48 +84,48 @@ public abstract class ComplexFormatAbstractTest { @Test public void testSimpleWithDecimalsTrunc() { - Complex c = new Complex(1.2323, 1.4343); - String expected = "1" + getDecimalCharacter() + "23 + 1" + getDecimalCharacter() + "43i"; + Complex c = new Complex(1.232323232323, 1.434343434343); + String expected = "1" + getDecimalCharacter() + "2323232323 + 1" + getDecimalCharacter() + "4343434343i"; String actual = complexFormat.format(c); Assert.assertEquals(expected, actual); } @Test public void testNegativeReal() { - Complex c = new Complex(-1.2323, 1.4343); - String expected = "-1" + getDecimalCharacter() + "23 + 1" + getDecimalCharacter() + "43i"; + Complex c = new Complex(-1.232323232323, 1.43); + String expected = "-1" + getDecimalCharacter() + "2323232323 + 1" + getDecimalCharacter() + "43i"; String actual = complexFormat.format(c); Assert.assertEquals(expected, actual); } @Test public void testNegativeImaginary() { - Complex c = new Complex(1.2323, -1.4343); - String expected = "1" + getDecimalCharacter() + "23 - 1" + getDecimalCharacter() + "43i"; + Complex c = new Complex(1.23, -1.434343434343); + String expected = "1" + getDecimalCharacter() + "23 - 1" + getDecimalCharacter() + "4343434343i"; String actual = complexFormat.format(c); Assert.assertEquals(expected, actual); } @Test public void testNegativeBoth() { - Complex c = new Complex(-1.2323, -1.4343); - String expected = "-1" + getDecimalCharacter() + "23 - 1" + getDecimalCharacter() + "43i"; + Complex c = new Complex(-1.232323232323, -1.434343434343); + String expected = "-1" + getDecimalCharacter() + "2323232323 - 1" + getDecimalCharacter() + "4343434343i"; String actual = complexFormat.format(c); Assert.assertEquals(expected, actual); } @Test public void testZeroReal() { - Complex c = new Complex(0.0, -1.4343); - String expected = "0 - 1" + getDecimalCharacter() + "43i"; + Complex c = new Complex(0.0, -1.434343434343); + String expected = "0 - 1" + getDecimalCharacter() + "4343434343i"; String actual = complexFormat.format(c); Assert.assertEquals(expected, actual); } @Test public void testZeroImaginary() { - Complex c = new Complex(30.233, 0); - String expected = "30" + getDecimalCharacter() + "23"; + Complex c = new Complex(30.23333333333, 0); + String expected = "30" + getDecimalCharacter() + "2333333333"; String actual = complexFormat.format(c); Assert.assertEquals(expected, actual); } @@ -143,8 +143,8 @@ public abstract class ComplexFormatAbstractTest { Locale defaultLocal = Locale.getDefault(); Locale.setDefault(getLocale()); - Complex c = new Complex(232.222, -342.33); - String expected = "232" + getDecimalCharacter() + "22 - 342" + getDecimalCharacter() + "33i"; + Complex c = new Complex(232.22222222222, -342.3333333333); + String expected = "232" + getDecimalCharacter() + "2222222222 - 342" + getDecimalCharacter() + "3333333333i"; String actual = (new ComplexFormat()).format(c); Assert.assertEquals(expected, actual); @@ -193,32 +193,32 @@ public abstract class ComplexFormatAbstractTest { @Test public void testParseSimpleWithDecimalsTrunc() { - String source = "1" + getDecimalCharacter() + "2323 + 1" + getDecimalCharacter() + "4343i"; - Complex expected = new Complex(1.2323, 1.4343); + String source = "1" + getDecimalCharacter() + "232323232323 + 1" + getDecimalCharacter() + "434343434343i"; + Complex expected = new Complex(1.232323232323, 1.434343434343); Complex actual = complexFormat.parse(source); Assert.assertEquals(expected, actual); } @Test public void testParseNegativeReal() { - String source = "-1" + getDecimalCharacter() + "2323 + 1" + getDecimalCharacter() + "4343i"; - Complex expected = new Complex(-1.2323, 1.4343); + String source = "-1" + getDecimalCharacter() + "232323232323 + 1" + getDecimalCharacter() + "4343i"; + Complex expected = new Complex(-1.232323232323, 1.4343); Complex actual = complexFormat.parse(source); Assert.assertEquals(expected, actual); } @Test public void testParseNegativeImaginary() { - String source = "1" + getDecimalCharacter() + "2323 - 1" + getDecimalCharacter() + "4343i"; - Complex expected = new Complex(1.2323, -1.4343); + String source = "1" + getDecimalCharacter() + "2323 - 1" + getDecimalCharacter() + "434343434343i"; + Complex expected = new Complex(1.2323, -1.434343434343); Complex actual = complexFormat.parse(source); Assert.assertEquals(expected, actual); } @Test public void testParseNegativeBoth() { - String source = "-1" + getDecimalCharacter() + "2323 - 1" + getDecimalCharacter() + "4343i"; - Complex expected = new Complex(-1.2323, -1.4343); + String source = "-1" + getDecimalCharacter() + "232323232323 - 1" + getDecimalCharacter() + "434343434343i"; + Complex expected = new Complex(-1.232323232323, -1.434343434343); Complex actual = complexFormat.parse(source); Assert.assertEquals(expected, actual); } @@ -298,7 +298,7 @@ public abstract class ComplexFormatAbstractTest { ComplexFormat cf = ComplexFormat.getInstance(getLocale()); Double pi = Double.valueOf(FastMath.PI); String text = cf.format(pi); - Assert.assertEquals("3" + getDecimalCharacter() + "14", text); + Assert.assertEquals("3" + getDecimalCharacter() + "1415926536", text); } @Test diff --git a/src/test/java/org/apache/commons/math3/geometry/euclidean/threed/Vector3DFormatAbstractTest.java b/src/test/java/org/apache/commons/math3/geometry/euclidean/threed/Vector3DFormatAbstractTest.java index 5dfb9635c..1d48a6d1a 100644 --- a/src/test/java/org/apache/commons/math3/geometry/euclidean/threed/Vector3DFormatAbstractTest.java +++ b/src/test/java/org/apache/commons/math3/geometry/euclidean/threed/Vector3DFormatAbstractTest.java @@ -64,22 +64,22 @@ public abstract class Vector3DFormatAbstractTest { @Test public void testSimpleWithDecimalsTrunc() { - Vector3D c = new Vector3D(1.2323, 1.4343, 1.6333); + Vector3D c = new Vector3D(1.232323232323, 1.434343434343, 1.633333333333); String expected = "{1" + getDecimalCharacter() + - "23; 1" + getDecimalCharacter() + - "43; 1" + getDecimalCharacter() + - "63}"; + "2323232323; 1" + getDecimalCharacter() + + "4343434343; 1" + getDecimalCharacter() + + "6333333333}"; String actual = vector3DFormat.format(c); Assert.assertEquals(expected, actual); } @Test public void testNegativeX() { - Vector3D c = new Vector3D(-1.2323, 1.4343, 1.6333); + Vector3D c = new Vector3D(-1.232323232323, 1.43, 1.63); String expected = "{-1" + getDecimalCharacter() + - "23; 1" + getDecimalCharacter() + + "2323232323; 1" + getDecimalCharacter() + "43; 1" + getDecimalCharacter() + "63}"; String actual = vector3DFormat.format(c); @@ -88,11 +88,11 @@ public abstract class Vector3DFormatAbstractTest { @Test public void testNegativeY() { - Vector3D c = new Vector3D(1.2323, -1.4343, 1.6333); + Vector3D c = new Vector3D(1.23, -1.434343434343, 1.63); String expected = "{1" + getDecimalCharacter() + "23; -1" + getDecimalCharacter() + - "43; 1" + getDecimalCharacter() + + "4343434343; 1" + getDecimalCharacter() + "63}"; String actual = vector3DFormat.format(c); Assert.assertEquals(expected, actual); @@ -100,12 +100,12 @@ public abstract class Vector3DFormatAbstractTest { @Test public void testNegativeZ() { - Vector3D c = new Vector3D(1.2323, 1.4343, -1.6333); + Vector3D c = new Vector3D(1.23, 1.43, -1.633333333333); String expected = "{1" + getDecimalCharacter() + "23; 1" + getDecimalCharacter() + "43; -1" + getDecimalCharacter() + - "63}"; + "6333333333}"; String actual = vector3DFormat.format(c); Assert.assertEquals(expected, actual); } @@ -123,12 +123,12 @@ public abstract class Vector3DFormatAbstractTest { Locale defaultLocal = Locale.getDefault(); Locale.setDefault(getLocale()); - Vector3D c = new Vector3D(232.222, -342.33, 432.444); + Vector3D c = new Vector3D(232.22222222222, -342.3333333333, 432.44444444444); String expected = "{232" + getDecimalCharacter() + - "22; -342" + getDecimalCharacter() + - "33; 432" + getDecimalCharacter() + - "44}"; + "2222222222; -342" + getDecimalCharacter() + + "3333333333; 432" + getDecimalCharacter() + + "4444444444}"; String actual = (new Vector3DFormat()).format(c); Assert.assertEquals(expected, actual); diff --git a/src/test/java/org/apache/commons/math3/linear/RealMatrixFormatAbstractTest.java b/src/test/java/org/apache/commons/math3/linear/RealMatrixFormatAbstractTest.java index b39c37795..a66827cbe 100644 --- a/src/test/java/org/apache/commons/math3/linear/RealMatrixFormatAbstractTest.java +++ b/src/test/java/org/apache/commons/math3/linear/RealMatrixFormatAbstractTest.java @@ -68,64 +68,64 @@ public abstract class RealMatrixFormatAbstractTest { @Test public void testSimpleWithDecimalsTrunc() { - RealMatrix m = MatrixUtils.createRealMatrix(new double[][] {{1.2323, 1.4343, 1.6333}, - {2.4666, 2.4666, 2.6666}}); + RealMatrix m = MatrixUtils.createRealMatrix(new double[][] {{1.232323232323, 1.43, 1.63}, + {2.46, 2.46, 2.666666666666}}); String expected = "{{1" + getDecimalCharacter() + - "23,1" + getDecimalCharacter() + + "2323232323,1" + getDecimalCharacter() + "43,1" + getDecimalCharacter() + "63},{2" + getDecimalCharacter() + - "47,2" + getDecimalCharacter() + - "47,2" + getDecimalCharacter() + - "67}}"; + "46,2" + getDecimalCharacter() + + "46,2" + getDecimalCharacter() + + "6666666667}}"; String actual = realMatrixFormat.format(m); Assert.assertEquals(expected, actual); } @Test public void testNegativeComponent() { - RealMatrix m = MatrixUtils.createRealMatrix(new double[][] {{-1.2323, 1.4343, 1.6333}, - {2.4666, 2.4666, 2.6666}}); + RealMatrix m = MatrixUtils.createRealMatrix(new double[][] {{-1.232323232323, 1.43, 1.63}, + {2.46, 2.46, 2.66}}); String expected = "{{-1" + getDecimalCharacter() + - "23,1" + getDecimalCharacter() + + "2323232323,1" + getDecimalCharacter() + "43,1" + getDecimalCharacter() + "63},{2" + getDecimalCharacter() + - "47,2" + getDecimalCharacter() + - "47,2" + getDecimalCharacter() + - "67}}"; + "46,2" + getDecimalCharacter() + + "46,2" + getDecimalCharacter() + + "66}}"; String actual = realMatrixFormat.format(m); Assert.assertEquals(expected, actual); } @Test public void testNegativeComponent2() { - RealMatrix m = MatrixUtils.createRealMatrix(new double[][] {{1.2323, -1.4343, 1.6333}, - {2.4666, 2.4666, 2.6666}}); + RealMatrix m = MatrixUtils.createRealMatrix(new double[][] {{1.23, -1.434343434343, 1.63}, + {2.46, 2.46, 2.66}}); String expected = "{{1" + getDecimalCharacter() + "23,-1" + getDecimalCharacter() + - "43,1" + getDecimalCharacter() + + "4343434343,1" + getDecimalCharacter() + "63},{2" + getDecimalCharacter() + - "47,2" + getDecimalCharacter() + - "47,2" + getDecimalCharacter() + - "67}}"; + "46,2" + getDecimalCharacter() + + "46,2" + getDecimalCharacter() + + "66}}"; String actual = realMatrixFormat.format(m); Assert.assertEquals(expected, actual); } @Test public void testNegativeSecondRow() { - RealMatrix m = MatrixUtils.createRealMatrix(new double[][] {{1.2323, 1.4343, 1.6333}, - {-2.4666, 2.4666, 2.6666}}); + RealMatrix m = MatrixUtils.createRealMatrix(new double[][] {{1.23, 1.43, 1.63}, + {-2.66666666666, 2.46, 2.66}}); String expected = "{{1" + getDecimalCharacter() + "23,1" + getDecimalCharacter() + "43,1" + getDecimalCharacter() + "63},{-2" + getDecimalCharacter() + - "47,2" + getDecimalCharacter() + - "47,2" + getDecimalCharacter() + - "67}}"; + "6666666667,2" + getDecimalCharacter() + + "46,2" + getDecimalCharacter() + + "66}}"; String actual = realMatrixFormat.format(m); Assert.assertEquals(expected, actual); } @@ -143,12 +143,12 @@ public abstract class RealMatrixFormatAbstractTest { Locale defaultLocale = Locale.getDefault(); Locale.setDefault(getLocale()); - RealMatrix m = MatrixUtils.createRealMatrix(new double[][] {{232.222, -342.33, 432.444}}); + RealMatrix m = MatrixUtils.createRealMatrix(new double[][] {{232.2222222222, -342.33333333333, 432.44444444444}}); String expected = "{{232" + getDecimalCharacter() + - "22,-342" + getDecimalCharacter() + - "33,432" + getDecimalCharacter() + - "44}}"; + "2222222222,-342" + getDecimalCharacter() + + "3333333333,432" + getDecimalCharacter() + + "4444444444}}"; String actual = (new RealMatrixFormat()).format(m); Assert.assertEquals(expected, actual); diff --git a/src/test/java/org/apache/commons/math3/linear/RealVectorFormatAbstractTest.java b/src/test/java/org/apache/commons/math3/linear/RealVectorFormatAbstractTest.java index 929ccde0c..c1a3d741b 100644 --- a/src/test/java/org/apache/commons/math3/linear/RealVectorFormatAbstractTest.java +++ b/src/test/java/org/apache/commons/math3/linear/RealVectorFormatAbstractTest.java @@ -64,22 +64,22 @@ public abstract class RealVectorFormatAbstractTest { @Test public void testSimpleWithDecimalsTrunc() { - ArrayRealVector c = new ArrayRealVector(new double[] {1.2323, 1.4343, 1.6333}); + ArrayRealVector c = new ArrayRealVector(new double[] {1.232323232323, 1.43434343434343, 1.633333333333}); String expected = "{1" + getDecimalCharacter() + - "23; 1" + getDecimalCharacter() + - "43; 1" + getDecimalCharacter() + - "63}"; + "2323232323; 1" + getDecimalCharacter() + + "4343434343; 1" + getDecimalCharacter() + + "6333333333}"; String actual = realVectorFormat.format(c); Assert.assertEquals(expected, actual); } @Test public void testNegativeX() { - ArrayRealVector c = new ArrayRealVector(new double[] {-1.2323, 1.4343, 1.6333}); + ArrayRealVector c = new ArrayRealVector(new double[] {-1.232323232323, 1.43, 1.63}); String expected = "{-1" + getDecimalCharacter() + - "23; 1" + getDecimalCharacter() + + "2323232323; 1" + getDecimalCharacter() + "43; 1" + getDecimalCharacter() + "63}"; String actual = realVectorFormat.format(c); @@ -88,11 +88,11 @@ public abstract class RealVectorFormatAbstractTest { @Test public void testNegativeY() { - ArrayRealVector c = new ArrayRealVector(new double[] {1.2323, -1.4343, 1.6333}); + ArrayRealVector c = new ArrayRealVector(new double[] {1.23, -1.434343434343, 1.63}); String expected = "{1" + getDecimalCharacter() + "23; -1" + getDecimalCharacter() + - "43; 1" + getDecimalCharacter() + + "4343434343; 1" + getDecimalCharacter() + "63}"; String actual = realVectorFormat.format(c); Assert.assertEquals(expected, actual); @@ -100,12 +100,12 @@ public abstract class RealVectorFormatAbstractTest { @Test public void testNegativeZ() { - ArrayRealVector c = new ArrayRealVector(new double[] {1.2323, 1.4343, -1.6333}); + ArrayRealVector c = new ArrayRealVector(new double[] {1.23, 1.43, -1.633333333333}); String expected = "{1" + getDecimalCharacter() + "23; 1" + getDecimalCharacter() + "43; -1" + getDecimalCharacter() + - "63}"; + "6333333333}"; String actual = realVectorFormat.format(c); Assert.assertEquals(expected, actual); } @@ -123,12 +123,12 @@ public abstract class RealVectorFormatAbstractTest { Locale defaultLocal = Locale.getDefault(); Locale.setDefault(getLocale()); - ArrayRealVector c = new ArrayRealVector(new double[] {232.222, -342.33, 432.444}); + ArrayRealVector c = new ArrayRealVector(new double[] {232.22222222222, -342.3333333333, 432.44444444444}); String expected = "{232" + getDecimalCharacter() + - "22; -342" + getDecimalCharacter() + - "33; 432" + getDecimalCharacter() + - "44}"; + "2222222222; -342" + getDecimalCharacter() + + "3333333333; 432" + getDecimalCharacter() + + "4444444444}"; String actual = (new RealVectorFormat()).format(c); Assert.assertEquals(expected, actual);