[MATH-831] Change default format, add static fields in MatrixUtils, replace toString() implementation in AbstractRealMatrix with the default RealMatrixFormat.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1364775 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
303993e923
commit
5553c97842
|
@ -38,6 +38,14 @@ import org.apache.commons.math3.util.FastMath;
|
|||
public abstract class AbstractRealMatrix
|
||||
extends RealLinearOperator
|
||||
implements RealMatrix {
|
||||
|
||||
/** Default format. */
|
||||
private static final RealMatrixFormat DEFAULT_FORMAT = RealMatrixFormat.getInstance();
|
||||
static {
|
||||
// set the minimum fraction digits to 1 to keep compatibility
|
||||
DEFAULT_FORMAT.getFormat().setMinimumFractionDigits(1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a matrix with no data
|
||||
*/
|
||||
|
@ -871,28 +879,11 @@ public abstract class AbstractRealMatrix
|
|||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
final int nRows = getRowDimension();
|
||||
final int nCols = getColumnDimension();
|
||||
final StringBuffer res = new StringBuffer();
|
||||
final StringBuilder res = new StringBuilder();
|
||||
String fullClassName = getClass().getName();
|
||||
String shortClassName = fullClassName.substring(fullClassName.lastIndexOf('.') + 1);
|
||||
res.append(shortClassName).append("{");
|
||||
|
||||
for (int i = 0; i < nRows; ++i) {
|
||||
if (i > 0) {
|
||||
res.append(",");
|
||||
}
|
||||
res.append("{");
|
||||
for (int j = 0; j < nCols; ++j) {
|
||||
if (j > 0) {
|
||||
res.append(",");
|
||||
}
|
||||
res.append(getEntry(i, j));
|
||||
}
|
||||
res.append("}");
|
||||
}
|
||||
|
||||
res.append("}");
|
||||
res.append(shortClassName);
|
||||
res.append(DEFAULT_FORMAT.format(this));
|
||||
return res.toString();
|
||||
}
|
||||
|
||||
|
|
|
@ -46,6 +46,18 @@ import org.apache.commons.math3.util.Precision;
|
|||
*/
|
||||
public class MatrixUtils {
|
||||
|
||||
/**
|
||||
* The default format for {@link RealMatrix} objects.
|
||||
* @since 3.1
|
||||
*/
|
||||
public static final RealMatrixFormat DEFAULT_FORMAT = RealMatrixFormat.getInstance();
|
||||
|
||||
/**
|
||||
* A format for {@link RealMatrix} objects compatible with octave.
|
||||
* @since 3.1
|
||||
*/
|
||||
public static final RealMatrixFormat OCTAVE_FORMAT = new RealMatrixFormat("[", "]", "", "", "; ", ", ");
|
||||
|
||||
/**
|
||||
* Private constructor.
|
||||
*/
|
||||
|
|
|
@ -29,19 +29,19 @@ import org.apache.commons.math3.util.CompositeFormat;
|
|||
|
||||
/**
|
||||
* Formats a {@code nxm} matrix in components list format
|
||||
* "[a<sub>0</sub><sub>0</sub>, a<sub>0</sub><sub>1</sub>, ...,
|
||||
* a<sub>0</sub><sub>m-1</sub>; a<sub>1</sub><sub>0</sub>,
|
||||
* a<sub>1</sub><sub>1</sub>, ..., a<sub>1</sub><sub>m-1</sub>; ...;
|
||||
* "{{a<sub>0</sub><sub>0</sub>,a<sub>0</sub><sub>1</sub>, ...,
|
||||
* a<sub>0</sub><sub>m-1</sub>},{a<sub>1</sub><sub>0</sub>,
|
||||
* a<sub>1</sub><sub>1</sub>, ..., a<sub>1</sub><sub>m-1</sub>},{...},{
|
||||
* a<sub>n-1</sub><sub>0</sub>, a<sub>n-1</sub><sub>1</sub>, ...,
|
||||
* a<sub>n-1</sub><sub>m-1</sub>}".
|
||||
* <p>The prefix and suffix "[" and "]", the row separator "; " and the column
|
||||
* separator ", " can be replaced by any user-defined strings. The number format
|
||||
* for components can be configured.</p>
|
||||
* a<sub>n-1</sub><sub>m-1</sub>}}".
|
||||
* <p>The prefix and suffix "{" and "}", the row prefix and suffix "{" and "}",
|
||||
* the row separator "," and the column separator "," can be replaced by any
|
||||
* user-defined strings. The number format for components can be configured.</p>
|
||||
*
|
||||
* <p>White space is ignored at parse time, even if it is in the prefix, suffix
|
||||
* or separator specifications. So even if the default separator does include a space
|
||||
* character that is used at format time, both input string "[1,1,1]" and
|
||||
* " [ 1 , 1 , 1 ] " will be parsed without error and the same vector will be
|
||||
* character that is used at format time, both input string "{{1,1,1}}" and
|
||||
* " { { 1 , 1 , 1 } } " will be parsed without error and the same matrix will be
|
||||
* returned. In the second case, however, the parse position after parsing will be
|
||||
* just after the closing curly brace, i.e. just before the trailing space.</p>
|
||||
*
|
||||
|
@ -55,29 +55,29 @@ import org.apache.commons.math3.util.CompositeFormat;
|
|||
public class RealMatrixFormat {
|
||||
|
||||
/** The default prefix: "{". */
|
||||
private static final String DEFAULT_PREFIX = "[";
|
||||
private static final String DEFAULT_PREFIX = "{";
|
||||
/** The default suffix: "}". */
|
||||
private static final String DEFAULT_SUFFIX = "]";
|
||||
/** The default row separator: ";". */
|
||||
private static final String DEFAULT_ROW_SEPARATOR = "; ";
|
||||
private static final String DEFAULT_SUFFIX = "}";
|
||||
/** The default row prefix: "{". */
|
||||
private static final String DEFAULT_ROW_PREFIX = "{";
|
||||
/** The default row suffix: "}". */
|
||||
private static final String DEFAULT_ROW_SUFFIX = "}";
|
||||
/** The default row separator: ",". */
|
||||
private static final String DEFAULT_ROW_SEPARATOR = ",";
|
||||
/** The default column separator: ",". */
|
||||
private static final String DEFAULT_COLUMN_SEPARATOR = ",";
|
||||
/** Prefix. */
|
||||
private final String prefix;
|
||||
/** Suffix. */
|
||||
private final String suffix;
|
||||
/** Row prefix. */
|
||||
private final String rowPrefix;
|
||||
/** Row suffix. */
|
||||
private final String rowSuffix;
|
||||
/** Row separator. */
|
||||
private final String rowSeparator;
|
||||
/** Column separator. */
|
||||
private final String columnSeparator;
|
||||
/** Trimmed prefix. */
|
||||
private final String trimmedPrefix;
|
||||
/** Trimmed suffix. */
|
||||
private final String trimmedSuffix;
|
||||
/** Trimmed row separator. */
|
||||
private final String trimmedRowSeparator;
|
||||
/** Trimmed column separator. */
|
||||
private final String trimmedColumnSeparator;
|
||||
/** The format used for components. */
|
||||
private final NumberFormat format;
|
||||
|
||||
|
@ -87,8 +87,8 @@ public class RealMatrixFormat {
|
|||
* "[", "]", ";" and ", " and the default number format for components.</p>
|
||||
*/
|
||||
public RealMatrixFormat() {
|
||||
this(DEFAULT_PREFIX, DEFAULT_SUFFIX, DEFAULT_ROW_SEPARATOR, DEFAULT_COLUMN_SEPARATOR,
|
||||
CompositeFormat.getDefaultNumberFormat());
|
||||
this(DEFAULT_PREFIX, DEFAULT_SUFFIX, DEFAULT_ROW_PREFIX, DEFAULT_ROW_SUFFIX,
|
||||
DEFAULT_ROW_SEPARATOR, DEFAULT_COLUMN_SEPARATOR, CompositeFormat.getDefaultNumberFormat());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -96,19 +96,24 @@ public class RealMatrixFormat {
|
|||
* @param format the custom format for components.
|
||||
*/
|
||||
public RealMatrixFormat(final NumberFormat format) {
|
||||
this(DEFAULT_PREFIX, DEFAULT_SUFFIX, DEFAULT_ROW_SEPARATOR, DEFAULT_COLUMN_SEPARATOR, format);
|
||||
this(DEFAULT_PREFIX, DEFAULT_SUFFIX, DEFAULT_ROW_PREFIX, DEFAULT_ROW_SUFFIX,
|
||||
DEFAULT_ROW_SEPARATOR, DEFAULT_COLUMN_SEPARATOR, format);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an instance with custom prefix, suffix and separator.
|
||||
* @param prefix prefix to use instead of the default "{"
|
||||
* @param suffix suffix to use instead of the default "}"
|
||||
* @param rowPrefix row prefix to use instead of the default "{"
|
||||
* @param rowSuffix row suffix to use instead of the default "}"
|
||||
* @param rowSeparator tow separator to use instead of the default ";"
|
||||
* @param columnSeparator column separator to use instead of the default ", "
|
||||
*/
|
||||
public RealMatrixFormat(final String prefix, final String suffix,
|
||||
final String rowPrefix, final String rowSuffix,
|
||||
final String rowSeparator, final String columnSeparator) {
|
||||
this(prefix, suffix, rowSeparator, columnSeparator, CompositeFormat.getDefaultNumberFormat());
|
||||
this(prefix, suffix, rowPrefix, rowSuffix, rowSeparator, columnSeparator,
|
||||
CompositeFormat.getDefaultNumberFormat());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -116,21 +121,22 @@ public class RealMatrixFormat {
|
|||
* for components.
|
||||
* @param prefix prefix to use instead of the default "{"
|
||||
* @param suffix suffix to use instead of the default "}"
|
||||
* @param rowPrefix row prefix to use instead of the default "{"
|
||||
* @param rowSuffix row suffix to use instead of the default "}"
|
||||
* @param rowSeparator tow separator to use instead of the default ";"
|
||||
* @param columnSeparator column separator to use instead of the default ", "
|
||||
* @param format the custom format for components.
|
||||
*/
|
||||
public RealMatrixFormat(final String prefix, final String suffix,
|
||||
final String rowPrefix, final String rowSuffix,
|
||||
final String rowSeparator, final String columnSeparator,
|
||||
final NumberFormat format) {
|
||||
this.prefix = prefix;
|
||||
this.suffix = suffix;
|
||||
this.rowPrefix = rowPrefix;
|
||||
this.rowSuffix = rowSuffix;
|
||||
this.rowSeparator = rowSeparator;
|
||||
this.columnSeparator = columnSeparator;
|
||||
trimmedPrefix = prefix.trim();
|
||||
trimmedSuffix = suffix.trim();
|
||||
trimmedRowSeparator = rowSeparator.trim();
|
||||
trimmedColumnSeparator = columnSeparator.trim();
|
||||
this.format = format;
|
||||
// disable grouping to prevent parsing problems
|
||||
this.format.setGroupingUsed(false);
|
||||
|
@ -161,6 +167,22 @@ public class RealMatrixFormat {
|
|||
return suffix;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the format prefix.
|
||||
* @return format prefix.
|
||||
*/
|
||||
public String getRowPrefix() {
|
||||
return rowPrefix;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the format suffix.
|
||||
* @return format suffix.
|
||||
*/
|
||||
public String getRowSuffix() {
|
||||
return rowSuffix;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the format separator between rows of the matrix.
|
||||
* @return format separator for rows.
|
||||
|
@ -232,12 +254,14 @@ public class RealMatrixFormat {
|
|||
// format rows
|
||||
final int rows = matrix.getRowDimension();
|
||||
for (int i = 0; i < rows; ++i) {
|
||||
toAppendTo.append(rowPrefix);
|
||||
for (int j = 0; j < matrix.getColumnDimension(); ++j) {
|
||||
if (j > 0) {
|
||||
toAppendTo.append(columnSeparator);
|
||||
}
|
||||
CompositeFormat.formatDouble(matrix.getEntry(i, j), format, toAppendTo, pos);
|
||||
}
|
||||
toAppendTo.append(rowSuffix);
|
||||
if (i < rows - 1) {
|
||||
toAppendTo.append(rowSeparator);
|
||||
}
|
||||
|
@ -278,6 +302,13 @@ public class RealMatrixFormat {
|
|||
public RealMatrix parse(String source, ParsePosition pos) {
|
||||
int initialIndex = pos.getIndex();
|
||||
|
||||
final String trimmedPrefix = prefix.trim();
|
||||
final String trimmedSuffix = suffix.trim();
|
||||
final String trimmedRowPrefix = rowPrefix.trim();
|
||||
final String trimmedRowSuffix = rowSuffix.trim();
|
||||
final String trimmedColumnSeparator = columnSeparator.trim();
|
||||
final String trimmedRowSeparator = rowSeparator.trim();
|
||||
|
||||
// parse prefix
|
||||
CompositeFormat.parseAndIgnoreWhitespace(source, pos);
|
||||
if (!CompositeFormat.parseFixedstring(source, trimmedPrefix, pos)) {
|
||||
|
@ -292,14 +323,25 @@ public class RealMatrixFormat {
|
|||
if (!rowComponents.isEmpty()) {
|
||||
CompositeFormat.parseAndIgnoreWhitespace(source, pos);
|
||||
if (!CompositeFormat.parseFixedstring(source, trimmedColumnSeparator, pos)) {
|
||||
if (!trimmedRowSuffix.isEmpty() && !CompositeFormat.parseFixedstring(source, trimmedRowSuffix, pos)) {
|
||||
return null;
|
||||
} else {
|
||||
CompositeFormat.parseAndIgnoreWhitespace(source, pos);
|
||||
if (CompositeFormat.parseFixedstring(source, trimmedRowSeparator, pos)) {
|
||||
matrix.add(rowComponents);
|
||||
rowComponents = new ArrayList<Number>();
|
||||
continue;
|
||||
} else {
|
||||
loop = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
CompositeFormat.parseAndIgnoreWhitespace(source, pos);
|
||||
if (!trimmedRowPrefix.isEmpty() && !CompositeFormat.parseFixedstring(source, trimmedRowPrefix, pos)) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
if (loop) {
|
||||
CompositeFormat.parseAndIgnoreWhitespace(source, pos);
|
||||
|
|
|
@ -21,6 +21,7 @@ import java.text.NumberFormat;
|
|||
import java.text.ParsePosition;
|
||||
import java.util.Locale;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.junit.Assert;
|
||||
|
||||
|
@ -29,7 +30,7 @@ import org.apache.commons.math3.exception.MathParseException;
|
|||
public abstract class RealMatrixFormatAbstractTest {
|
||||
|
||||
RealMatrixFormat realMatrixFormat = null;
|
||||
RealMatrixFormat realMatrixFormatOther = null;
|
||||
RealMatrixFormat realMatrixFormatOctave = null;
|
||||
|
||||
protected abstract Locale getLocale();
|
||||
|
||||
|
@ -39,13 +40,13 @@ public abstract class RealMatrixFormatAbstractTest {
|
|||
realMatrixFormat = RealMatrixFormat.getInstance(getLocale());
|
||||
final NumberFormat nf = NumberFormat.getInstance(getLocale());
|
||||
nf.setMaximumFractionDigits(2);
|
||||
realMatrixFormatOther = new RealMatrixFormat("{", "}", ", ", " : ", nf);
|
||||
realMatrixFormatOctave = new RealMatrixFormat("[", "]", "", "", "; ", ", ", nf);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSimpleNoDecimals() {
|
||||
RealMatrix m = MatrixUtils.createRealMatrix(new double[][] {{1, 1, 1}, {1, 1, 1}});
|
||||
String expected = "[1, 1, 1; 1, 1, 1]";
|
||||
String expected = "{{1,1,1},{1,1,1}}";
|
||||
String actual = realMatrixFormat.format(m);
|
||||
Assert.assertEquals(expected, actual);
|
||||
}
|
||||
|
@ -54,13 +55,13 @@ public abstract class RealMatrixFormatAbstractTest {
|
|||
public void testSimpleWithDecimals() {
|
||||
RealMatrix m = MatrixUtils.createRealMatrix(new double[][] {{1.23, 1.43, 1.63}, {2.46, 2.46, 2.66}});
|
||||
String expected =
|
||||
"[1" + getDecimalCharacter() +
|
||||
"{{1" + getDecimalCharacter() +
|
||||
"23,1" + getDecimalCharacter() +
|
||||
"43,1" + getDecimalCharacter() +
|
||||
"63; 2" + getDecimalCharacter() +
|
||||
"63},{2" + getDecimalCharacter() +
|
||||
"46,2" + getDecimalCharacter() +
|
||||
"46,2" + getDecimalCharacter() +
|
||||
"66]";
|
||||
"66}}";
|
||||
String actual = realMatrixFormat.format(m);
|
||||
Assert.assertEquals(expected, actual);
|
||||
}
|
||||
|
@ -70,45 +71,45 @@ public abstract class RealMatrixFormatAbstractTest {
|
|||
RealMatrix m = MatrixUtils.createRealMatrix(new double[][] {{1.2323, 1.4343, 1.6333},
|
||||
{2.4666, 2.4666, 2.6666}});
|
||||
String expected =
|
||||
"[1" + getDecimalCharacter() +
|
||||
"{{1" + getDecimalCharacter() +
|
||||
"23,1" + getDecimalCharacter() +
|
||||
"43,1" + getDecimalCharacter() +
|
||||
"63; 2" + getDecimalCharacter() +
|
||||
"63},{2" + getDecimalCharacter() +
|
||||
"47,2" + getDecimalCharacter() +
|
||||
"47,2" + getDecimalCharacter() +
|
||||
"67]";
|
||||
"67}}";
|
||||
String actual = realMatrixFormat.format(m);
|
||||
Assert.assertEquals(expected, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNegativeX() {
|
||||
public void testNegativeComponent() {
|
||||
RealMatrix m = MatrixUtils.createRealMatrix(new double[][] {{-1.2323, 1.4343, 1.6333},
|
||||
{2.4666, 2.4666, 2.6666}});
|
||||
String expected =
|
||||
"[-1" + getDecimalCharacter() +
|
||||
"{{-1" + getDecimalCharacter() +
|
||||
"23,1" + getDecimalCharacter() +
|
||||
"43,1" + getDecimalCharacter() +
|
||||
"63; 2" + getDecimalCharacter() +
|
||||
"63},{2" + getDecimalCharacter() +
|
||||
"47,2" + getDecimalCharacter() +
|
||||
"47,2" + getDecimalCharacter() +
|
||||
"67]";
|
||||
"67}}";
|
||||
String actual = realMatrixFormat.format(m);
|
||||
Assert.assertEquals(expected, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNegativeY() {
|
||||
public void testNegativeComponent2() {
|
||||
RealMatrix m = MatrixUtils.createRealMatrix(new double[][] {{1.2323, -1.4343, 1.6333},
|
||||
{2.4666, 2.4666, 2.6666}});
|
||||
String expected =
|
||||
"[1" + getDecimalCharacter() +
|
||||
"{{1" + getDecimalCharacter() +
|
||||
"23,-1" + getDecimalCharacter() +
|
||||
"43,1" + getDecimalCharacter() +
|
||||
"63; 2" + getDecimalCharacter() +
|
||||
"63},{2" + getDecimalCharacter() +
|
||||
"47,2" + getDecimalCharacter() +
|
||||
"47,2" + getDecimalCharacter() +
|
||||
"67]";
|
||||
"67}}";
|
||||
String actual = realMatrixFormat.format(m);
|
||||
Assert.assertEquals(expected, actual);
|
||||
}
|
||||
|
@ -118,13 +119,13 @@ public abstract class RealMatrixFormatAbstractTest {
|
|||
RealMatrix m = MatrixUtils.createRealMatrix(new double[][] {{1.2323, 1.4343, 1.6333},
|
||||
{-2.4666, 2.4666, 2.6666}});
|
||||
String expected =
|
||||
"[1" + getDecimalCharacter() +
|
||||
"{{1" + getDecimalCharacter() +
|
||||
"23,1" + getDecimalCharacter() +
|
||||
"43,1" + getDecimalCharacter() +
|
||||
"63; -2" + getDecimalCharacter() +
|
||||
"63},{-2" + getDecimalCharacter() +
|
||||
"47,2" + getDecimalCharacter() +
|
||||
"47,2" + getDecimalCharacter() +
|
||||
"67]";
|
||||
"67}}";
|
||||
String actual = realMatrixFormat.format(m);
|
||||
Assert.assertEquals(expected, actual);
|
||||
}
|
||||
|
@ -132,32 +133,32 @@ public abstract class RealMatrixFormatAbstractTest {
|
|||
@Test
|
||||
public void testNonDefaultSetting() {
|
||||
RealMatrix m = MatrixUtils.createRealMatrix(new double[][] {{1, 1, 1}, {1, 1, 1}});
|
||||
String expected = "{1 : 1 : 1, 1 : 1 : 1}";
|
||||
String actual = realMatrixFormatOther.format(m);
|
||||
String expected = "[1, 1, 1; 1, 1, 1]";
|
||||
String actual = realMatrixFormatOctave.format(m);
|
||||
Assert.assertEquals(expected, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultFormatRealVectorImpl() {
|
||||
Locale defaultLocal = Locale.getDefault();
|
||||
public void testDefaultFormat() {
|
||||
Locale defaultLocale = Locale.getDefault();
|
||||
Locale.setDefault(getLocale());
|
||||
|
||||
RealMatrix m = MatrixUtils.createRealMatrix(new double[][] {{232.222, -342.33, 432.444}});
|
||||
String expected =
|
||||
"[232" + getDecimalCharacter() +
|
||||
"{{232" + getDecimalCharacter() +
|
||||
"22,-342" + getDecimalCharacter() +
|
||||
"33,432" + getDecimalCharacter() +
|
||||
"44]";
|
||||
"44}}";
|
||||
String actual = (new RealMatrixFormat()).format(m);
|
||||
Assert.assertEquals(expected, actual);
|
||||
|
||||
Locale.setDefault(defaultLocal);
|
||||
Locale.setDefault(defaultLocale);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNan() {
|
||||
RealMatrix m = MatrixUtils.createRealMatrix(new double[][] {{Double.NaN, Double.NaN, Double.NaN}});
|
||||
String expected = "[(NaN), (NaN), (NaN)]";
|
||||
String expected = "{{(NaN),(NaN),(NaN)}}";
|
||||
String actual = realMatrixFormat.format(m);
|
||||
Assert.assertEquals(expected, actual);
|
||||
}
|
||||
|
@ -166,7 +167,7 @@ public abstract class RealMatrixFormatAbstractTest {
|
|||
public void testPositiveInfinity() {
|
||||
RealMatrix m = MatrixUtils.createRealMatrix(
|
||||
new double[][] {{Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY}});
|
||||
String expected = "[(Infinity), (Infinity), (Infinity)]";
|
||||
String expected = "{{(Infinity),(Infinity),(Infinity)}}";
|
||||
String actual = realMatrixFormat.format(m);
|
||||
Assert.assertEquals(expected, actual);
|
||||
}
|
||||
|
@ -175,22 +176,23 @@ public abstract class RealMatrixFormatAbstractTest {
|
|||
public void tesNegativeInfinity() {
|
||||
RealMatrix m = MatrixUtils.createRealMatrix(
|
||||
new double[][] {{Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY}});
|
||||
String expected = "[(-Infinity), (-Infinity), (-Infinity)]";
|
||||
String expected = "{{(-Infinity),(-Infinity),(-Infinity)}}";
|
||||
String actual = realMatrixFormat.format(m);
|
||||
Assert.assertEquals(expected, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseSimpleNoDecimals() {
|
||||
String source = "[1, 1, 1; 1, 1, 1]";
|
||||
String source = "{{1, 1, 1}, {1, 1, 1}}";
|
||||
RealMatrix expected = MatrixUtils.createRealMatrix(new double[][] {{1, 1, 1}, {1, 1, 1}});
|
||||
RealMatrix actual = realMatrixFormat.parse(source);
|
||||
Assert.assertEquals(expected, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void testParseSimpleWithClosingRowSeparator() {
|
||||
String source = "[1, 1, 1; 1, 1, 1 ;]";
|
||||
String source = "{{1, 1, 1},{1, 1, 1}, }}";
|
||||
RealMatrix expected = MatrixUtils.createRealMatrix(new double[][] {{1, 1, 1}, {1, 1, 1}});
|
||||
RealMatrix actual = realMatrixFormat.parse(source);
|
||||
Assert.assertEquals(expected, actual);
|
||||
|
@ -200,11 +202,11 @@ public abstract class RealMatrixFormatAbstractTest {
|
|||
public void testParseIgnoredWhitespace() {
|
||||
RealMatrix expected = MatrixUtils.createRealMatrix(new double[][] {{1, 1, 1}, {1, 1, 1}});
|
||||
ParsePosition pos1 = new ParsePosition(0);
|
||||
String source1 = "[1,1,1;1,1,1]";
|
||||
String source1 = "{{1,1,1},{1,1,1}}";
|
||||
Assert.assertEquals(expected, realMatrixFormat.parse(source1, pos1));
|
||||
Assert.assertEquals(source1.length(), pos1.getIndex());
|
||||
ParsePosition pos2 = new ParsePosition(0);
|
||||
String source2 = " [ 1 , 1 , 1 ; 1 , 1 , 1 ] ";
|
||||
String source2 = " { { 1 , 1 , 1 } , { 1 , 1 , 1 } } ";
|
||||
Assert.assertEquals(expected, realMatrixFormat.parse(source2, pos2));
|
||||
Assert.assertEquals(source2.length() - 1, pos2.getIndex());
|
||||
}
|
||||
|
@ -212,10 +214,10 @@ public abstract class RealMatrixFormatAbstractTest {
|
|||
@Test
|
||||
public void testParseSimpleWithDecimals() {
|
||||
String source =
|
||||
"[1" + getDecimalCharacter() +
|
||||
"{{1" + getDecimalCharacter() +
|
||||
"23,1" + getDecimalCharacter() +
|
||||
"43,1" + getDecimalCharacter() +
|
||||
"63]";
|
||||
"63}}";
|
||||
RealMatrix expected = MatrixUtils.createRealMatrix(new double[][] {{1.23, 1.43, 1.63}});
|
||||
RealMatrix actual = realMatrixFormat.parse(source);
|
||||
Assert.assertEquals(expected, actual);
|
||||
|
@ -224,10 +226,10 @@ public abstract class RealMatrixFormatAbstractTest {
|
|||
@Test
|
||||
public void testParseSimpleWithDecimalsTrunc() {
|
||||
String source =
|
||||
"[1" + getDecimalCharacter() +
|
||||
"{{1" + getDecimalCharacter() +
|
||||
"2323,1" + getDecimalCharacter() +
|
||||
"4343,1" + getDecimalCharacter() +
|
||||
"6333]";
|
||||
"6333}}";
|
||||
RealMatrix expected = MatrixUtils.createRealMatrix(new double[][] {{1.2323, 1.4343, 1.6333}});
|
||||
RealMatrix actual = realMatrixFormat.parse(source);
|
||||
Assert.assertEquals(expected, actual);
|
||||
|
@ -236,10 +238,10 @@ public abstract class RealMatrixFormatAbstractTest {
|
|||
@Test
|
||||
public void testParseNegativeComponent() {
|
||||
String source =
|
||||
"[-1" + getDecimalCharacter() +
|
||||
"{{-1" + getDecimalCharacter() +
|
||||
"2323,1" + getDecimalCharacter() +
|
||||
"4343,1" + getDecimalCharacter() +
|
||||
"6333]";
|
||||
"6333}}";
|
||||
RealMatrix expected = MatrixUtils.createRealMatrix(new double[][] {{-1.2323, 1.4343, 1.6333}});
|
||||
RealMatrix actual = realMatrixFormat.parse(source);
|
||||
Assert.assertEquals(expected, actual);
|
||||
|
@ -248,10 +250,10 @@ public abstract class RealMatrixFormatAbstractTest {
|
|||
@Test
|
||||
public void testParseNegativeAll() {
|
||||
String source =
|
||||
"[-1" + getDecimalCharacter() +
|
||||
"{{-1" + getDecimalCharacter() +
|
||||
"2323,-1" + getDecimalCharacter() +
|
||||
"4343,-1" + getDecimalCharacter() +
|
||||
"6333]";
|
||||
"6333}}";
|
||||
RealMatrix expected = MatrixUtils.createRealMatrix(new double[][] {{-1.2323, -1.4343, -1.6333}});
|
||||
RealMatrix actual = realMatrixFormat.parse(source);
|
||||
Assert.assertEquals(expected, actual);
|
||||
|
@ -260,10 +262,10 @@ public abstract class RealMatrixFormatAbstractTest {
|
|||
@Test
|
||||
public void testParseZeroComponent() {
|
||||
String source =
|
||||
"[0" + getDecimalCharacter() +
|
||||
"{{0" + getDecimalCharacter() +
|
||||
"0,-1" + getDecimalCharacter() +
|
||||
"4343,1" + getDecimalCharacter() +
|
||||
"6333]";
|
||||
"6333}}";
|
||||
RealMatrix expected = MatrixUtils.createRealMatrix(new double[][] {{0.0, -1.4343, 1.6333}});
|
||||
RealMatrix actual = realMatrixFormat.parse(source);
|
||||
Assert.assertEquals(expected, actual);
|
||||
|
@ -272,18 +274,18 @@ public abstract class RealMatrixFormatAbstractTest {
|
|||
@Test
|
||||
public void testParseNonDefaultSetting() {
|
||||
String source =
|
||||
"{1" + getDecimalCharacter() +
|
||||
"2323 : 1" + getDecimalCharacter() +
|
||||
"4343 : 1" + getDecimalCharacter() +
|
||||
"6333}";
|
||||
"[1" + getDecimalCharacter() +
|
||||
"2323, 1" + getDecimalCharacter() +
|
||||
"4343, 1" + getDecimalCharacter() +
|
||||
"6333]";
|
||||
RealMatrix expected = MatrixUtils.createRealMatrix(new double[][] {{1.2323, 1.4343, 1.6333}});
|
||||
RealMatrix actual = realMatrixFormatOther.parse(source);
|
||||
RealMatrix actual = realMatrixFormatOctave.parse(source);
|
||||
Assert.assertEquals(expected, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseNan() {
|
||||
String source = "[(NaN), (NaN), (NaN)]";
|
||||
String source = "{{(NaN), (NaN), (NaN)}}";
|
||||
RealMatrix actual = realMatrixFormat.parse(source);
|
||||
RealMatrix expected = MatrixUtils.createRealMatrix(new double[][] {{Double.NaN, Double.NaN, Double.NaN}});
|
||||
for (int i = 0; i < expected.getRowDimension(); i++) {
|
||||
|
@ -295,7 +297,7 @@ public abstract class RealMatrixFormatAbstractTest {
|
|||
|
||||
@Test
|
||||
public void testParsePositiveInfinity() {
|
||||
String source = "[(Infinity), (Infinity), (Infinity)]";
|
||||
String source = "{{(Infinity), (Infinity), (Infinity)}}";
|
||||
RealMatrix actual = realMatrixFormat.parse(source);
|
||||
RealMatrix expected = MatrixUtils.createRealMatrix(
|
||||
new double[][] {{Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY}});
|
||||
|
@ -304,7 +306,7 @@ public abstract class RealMatrixFormatAbstractTest {
|
|||
|
||||
@Test
|
||||
public void testParseNegativeInfinity() {
|
||||
String source = "[(-Infinity), (-Infinity), (-Infinity)]";
|
||||
String source = "{{(-Infinity), (-Infinity), (-Infinity)}}";
|
||||
RealMatrix actual = realMatrixFormat.parse(source);
|
||||
RealMatrix expected = MatrixUtils.createRealMatrix(
|
||||
new double[][] {{Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY}});
|
||||
|
@ -314,7 +316,7 @@ public abstract class RealMatrixFormatAbstractTest {
|
|||
@Test
|
||||
public void testParseNoComponents() {
|
||||
try {
|
||||
realMatrixFormat.parse("[ ]");
|
||||
realMatrixFormat.parse("{{ }}");
|
||||
Assert.fail("Expecting MathParseException");
|
||||
} catch (MathParseException pe) {
|
||||
// expected behavior
|
||||
|
@ -323,8 +325,8 @@ public abstract class RealMatrixFormatAbstractTest {
|
|||
|
||||
@Test
|
||||
public void testParseManyComponents() {
|
||||
RealMatrix parsed = realMatrixFormat.parse("[0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0]");
|
||||
Assert.assertEquals(24, parsed.getRowDimension());
|
||||
RealMatrix parsed = realMatrixFormat.parse("{{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}}");
|
||||
Assert.assertEquals(24, parsed.getColumnDimension());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -346,16 +348,16 @@ public abstract class RealMatrixFormatAbstractTest {
|
|||
@Test
|
||||
public void testForgottenSeparator() {
|
||||
ParsePosition pos = new ParsePosition(0);
|
||||
final String source = "[1; 1 1]";
|
||||
final String source = "{{1, 1 1}}";
|
||||
Assert.assertNull("Should not parse <"+source+">", new RealMatrixFormat().parse(source, pos));
|
||||
Assert.assertEquals(6, pos.getErrorIndex());
|
||||
Assert.assertEquals(7, pos.getErrorIndex());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testForgottenSuffix() {
|
||||
ParsePosition pos = new ParsePosition(0);
|
||||
final String source = "[1; 1; 1 ";
|
||||
final String source = "{{1, 1, 1 ";
|
||||
Assert.assertNull("Should not parse <"+source+">", new RealMatrixFormat().parse(source, pos));
|
||||
Assert.assertEquals(8, pos.getErrorIndex());
|
||||
Assert.assertEquals(9, pos.getErrorIndex());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue