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
This commit is contained in:
Gilles Sadowski 2012-07-29 12:05:48 +00:00
parent 0ed64edde5
commit 72779b2300
7 changed files with 94 additions and 86 deletions

View File

@ -52,6 +52,9 @@ If the output is not quite correct, check for invisible trailing spaces!
<body>
<release version="3.1" date="TBD" description="
">
<action dev="erans" type="fix" issue="MATH-622">
Raised (to 10) the default number of fractional digits to print out.
</action>
<action dev="erans" type="fix" issue="MATH-762">
Removed duplicate code.
</action>

View File

@ -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;

View File

@ -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;
}

View File

@ -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

View File

@ -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);

View File

@ -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);

View File

@ -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);