You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ps...@apache.org on 2004/07/11 06:49:24 UTC
cvs commit: jakarta-commons/math/src/test/org/apache/commons/math/linear BigMatrixImplTest.java
psteitz 2004/07/10 21:49:24
Modified: math/src/java/org/apache/commons/math/linear BigMatrix.java
BigMatrixImpl.java
math/src/test/org/apache/commons/math/linear
BigMatrixImplTest.java
Log:
Added methods allowing exact values to be set using string representations.
Revision Changes Path
1.4 +32 -4 jakarta-commons/math/src/java/org/apache/commons/math/linear/BigMatrix.java
Index: BigMatrix.java
===================================================================
RCS file: /home/cvs/jakarta-commons/math/src/java/org/apache/commons/math/linear/BigMatrix.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- BigMatrix.java 23 Jun 2004 16:26:17 -0000 1.3
+++ BigMatrix.java 11 Jul 2004 04:49:24 -0000 1.4
@@ -115,6 +115,17 @@
* @param data 2-dimensional array of entries
*/
void setData(double[][] data);
+
+ /**
+ * Overwrites the underlying data for the matrix with
+ * <code>BigDecimal</code> entries with values represented by the strings
+ * in <code>data</code>.
+ *
+ * @param data 2-dimensional array of entries
+ * @throws NumberFormatException if any of the entries in <code>data</code>
+ * are not valid representations of <code>BigDecimal</code> values
+ */
+ void setData(String[][] data);
/***
* Sets the rounding mode to use when dividing values
@@ -224,6 +235,21 @@
*/
void setEntry(int row, int column, double value)
throws MatrixIndexException;
+
+ /**
+ * Sets the entry in the specified row and column to the
+ * <code>BigDecimal</code> value represented by the input string.
+ *
+ * @param row row location of entry to be set
+ * @param column column location of entry to be set
+ * @param value value to set
+ * @throws org.apache.commons.math.linear.MatrixIndexException if the
+ * specified coordinate is outside the dimensions of this matrix
+ * @throws NumberFormatException if <code>value</code> is not a valid
+ * representation of a <code>BigDecimal</code> value
+ */
+ void setEntry(int row, int column, String value)
+ throws MatrixIndexException;
/**
* Returns the transpose of this matrix.
@@ -236,7 +262,8 @@
* Returns the inverse of this matrix.
*
* @return inverse matrix
- * @throws org.apache.commons.math.linear.InvalidMatrixException if this is not invertible
+ * @throws org.apache.commons.math.linear.InvalidMatrixException if
+ * this is not invertible
*/
BigMatrix inverse() throws InvalidMatrixException;
@@ -244,9 +271,10 @@
* Returns the determinant of this matrix.
*
* @return determinant
- *@throws InvalidMatrixException if matrix is not square
+ *@throws org.apache.commons.math.linear.InvalidMatrixException if
+ * matrix is not square
*/
- BigDecimal getDeterminant();
+ BigDecimal getDeterminant() throws InvalidMatrixException;
/**
* Is this a square matrix?
1.3 +71 -12 jakarta-commons/math/src/java/org/apache/commons/math/linear/BigMatrixImpl.java
Index: BigMatrixImpl.java
===================================================================
RCS file: /home/cvs/jakarta-commons/math/src/java/org/apache/commons/math/linear/BigMatrixImpl.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- BigMatrixImpl.java 23 Jun 2004 16:26:17 -0000 1.2
+++ BigMatrixImpl.java 11 Jul 2004 04:49:24 -0000 1.3
@@ -119,6 +119,17 @@
}
/**
+ * Create a new BigMatrix using the values represented by the strings in
+ * <code>data</code> as the underlying data array.
+ *
+ * @param d data for new matrix
+ */
+ public BigMatrixImpl(String[][] d) {
+ this.copyIn(d);
+ lu = null;
+ }
+
+ /**
* Create a new (column) BigMatrix using <code>v</code> as the
* data for the unique column of the <code>v.length x 1</code> matrix
* created.
@@ -321,6 +332,20 @@
}
/**
+ * Overwrites the underlying data for the matrix with
+ * <code>BigDecimal</code> entries with values represented by the strings
+ * in <code>data</code>.
+ *
+ * @param data 2-dimensional array of entries
+ * @throws NumberFormatException if any of the entries in <code>data</code>
+ * are not valid representations of <code>BigDecimal</code> values
+ */
+ public void setData(String[][] data) {
+ copyIn(data);
+ lu = null;
+ }
+
+ /**
* Returns a reference to the underlying data array.
* <p>
* Does not make a fresh copy of the underlying data.
@@ -346,7 +371,7 @@
/***
* Gets the rounding mode for division operations
- * The default is {@link BigDecimal#ROUND_HALF_UP}
+ * The default is {@link java.math.BigDecimal#ROUND_HALF_UP}
* @see BigDecimal
* @return the rounding mode.
*/
@@ -424,8 +449,8 @@
*
* @param row the row to be fetched
* @return array of entries in the row
- * @throws org.apache.commons.math.linear.MatrixIndexException if the specified row is greater
- * than the number of rows in this matrix
+ * @throws org.apache.commons.math.linear.MatrixIndexException if the
+ * specified row is greater than the number of rows in this matrix
*/
public double[] getRowAsDoubleArray(int row) throws MatrixIndexException {
if ( !isValidCoordinate( row, 1 ) ) {
@@ -444,8 +469,8 @@
*
* @param col column to fetch
* @return array of entries in the column
- * @throws MatrixIndexException if the specified column is greater
- * than the number of columns in this matrix
+ * @throws org.apache.commons.math.linear.MatrixIndexException if the
+ * specified column is greater than the number of columns in this matrix
*/
public BigDecimal[] getColumn(int col) throws MatrixIndexException {
if ( !isValidCoordinate(1, col) ) {
@@ -465,8 +490,8 @@
*
* @param col column to fetch
* @return array of entries in the column
- * @throws org.apache.commons.math.linear.MatrixIndexException if the specified column is greater
- * than the number of columns in this matrix
+ * @throws org.apache.commons.math.linear.MatrixIndexException if the
+ * specified column is greater than the number of columns in this matrix
*/
public double[] getColumnAsDoubleArray(int col) throws MatrixIndexException {
if ( !isValidCoordinate( 1, col ) ) {
@@ -503,8 +528,8 @@
* @param row row location of entry to be fetched
* @param column column location of entry to be fetched
* @return matrix entry in row,column
- * @throws org.apache.commons.math.linear.MatrixIndexException if the specified coordinate is outside
- * the dimensions of this matrix
+ * @throws org.apache.commons.math.linear.MatrixIndexException if the
+ * specified coordinate is outside the dimensions of this matrix
*/
public double getEntryAsDouble(int row, int column) throws MatrixIndexException {
return getEntry(row,column).doubleValue();
@@ -534,14 +559,30 @@
* @param row row location of entry to be set
* @param column column location of entry to be set
* @param value value to set
- * @throws org.apache.commons.math.linear.MatrixIndexException if the specified coordinate is outside
- * he dimensions of this matrix
+ * @throws org.apache.commons.math.linear.MatrixIndexException if the
+ * specified coordinate is outside the dimensions of this matrix
*/
public void setEntry(int row, int column, double value) throws MatrixIndexException {
setEntry(row, column, new BigDecimal(value));
}
/**
+ * Sets the entry in the specified row and column to the
+ * <code>BigDecimal</code> value represented by the input string.
+ *
+ * @param row row location of entry to be set
+ * @param column column location of entry to be set
+ * @param value value to set
+ * @throws org.apache.commons.math.linear.MatrixIndexException if the
+ * specified coordinate is outside the dimensions of this matrix
+ * @throws NumberFormatException if <code>value</code> is not a valid
+ * representation of a <code>BigDecimal</code> value
+ */
+ public void setEntry(int row, int column, String value) throws MatrixIndexException {
+ setEntry(row, column, new BigDecimal(value));
+ }
+
+ /**
* Returns the transpose matrix.
*
* @return transpose matrix
@@ -1046,6 +1087,24 @@
* @param in data to copy in
*/
private void copyIn(double[][] in) {
+ int nRows = in.length;
+ int nCols = in[0].length;
+ data = new BigDecimal[nRows][nCols];
+ for (int i = 0; i < nRows; i++) {
+ for (int j=0; j < nCols; j++) {
+ data[i][j] = new BigDecimal(in[i][j]);
+ }
+ }
+ lu = null;
+ }
+
+ /**
+ * Replaces data with BigDecimals represented by the strings in the input
+ * array.
+ *
+ * @param in data to copy in
+ */
+ private void copyIn(String[][] in) {
int nRows = in.length;
int nCols = in[0].length;
data = new BigDecimal[nRows][nCols];
1.2 +29 -2 jakarta-commons/math/src/test/org/apache/commons/math/linear/BigMatrixImplTest.java
Index: BigMatrixImplTest.java
===================================================================
RCS file: /home/cvs/jakarta-commons/math/src/test/org/apache/commons/math/linear/BigMatrixImplTest.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- BigMatrixImplTest.java 6 Jun 2004 04:20:45 -0000 1.1
+++ BigMatrixImplTest.java 11 Jul 2004 04:49:24 -0000 1.2
@@ -30,6 +30,7 @@
public final class BigMatrixImplTest extends TestCase {
private double[][] testData = { {1d,2d,3d}, {2d,5d,3d}, {1d,0d,8d} };
+ private String[][] testDataString = { {"1","2","3"}, {"2","5","3"}, {"1","0","8"} };
private double[][] testDataLU = {{2d, 5d, 3d}, {.5d, -2.5d, 6.5d}, {0.5d, 0.2d, .2d}};
private double[][] testDataPlus2 = { {3d,4d,5d}, {4d,7d,5d}, {3d,2d,10d} };
private double[][] testDataMinus = { {-1d,-2d,-3d}, {-2d,-5d,-3d},
@@ -134,7 +135,23 @@
m3.setDataRef(asBigDecimal(stompMe));
assertClose("no copy side effect",m,new BigMatrixImpl(testData),
entryTolerance);
- }
+ }
+
+ /** test constructors */
+ public void testConstructors() {
+ BigMatrix m1 = new BigMatrixImpl(testData);
+ BigMatrix m2 = new BigMatrixImpl(testDataString);
+ BigMatrix m3 = new BigMatrixImpl(asBigDecimal(testData));
+ assertClose("double, string", m1, m2, Double.MIN_VALUE);
+ assertClose("double, BigDecimal", m1, m3, Double.MIN_VALUE);
+ assertClose("string, BigDecimal", m2, m3, Double.MIN_VALUE);
+ try {
+ BigMatrix m4 = new BigMatrixImpl(new String[][] {{"0", "hello", "1"}});
+ fail("Expecting NumberFormatException");
+ } catch (NumberFormatException ex) {
+ // expected
+ }
+ }
/** test add */
public void testAdd() {
@@ -441,6 +458,16 @@
} catch (MatrixIndexException ex) {
;
}
+ m.setEntry(1, 2, "0.1");
+ m.setEntry(1, 1, 0.1d);
+ assertFalse(m.getEntry(1, 2).equals(m.getEntry(1, 1)));
+ assertTrue(m.getEntry(1, 2).equals(new BigDecimal("0.1")));
+ try {
+ m.setEntry(1, 2, "not a number");
+ fail("Expecting NumberFormatException");
+ } catch (NumberFormatException ex) {
+ ;
+ }
}
public void testLUDecomposition() throws Exception {
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org