Added methods allowing exact values to be set using string representations.
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/math/trunk@141374 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
9ba5a237d7
commit
af2937a3ee
|
@ -22,7 +22,7 @@ import java.math.BigDecimal;
|
|||
* Interface defining a real-valued matrix with basic algebraic operations, using
|
||||
* BigDecimal representations for the entries.
|
||||
*
|
||||
* @version $Revision: 1.3 $ $Date: 2004/06/23 16:26:17 $
|
||||
* @version $Revision: 1.4 $ $Date: 2004/07/11 04:49:24 $
|
||||
*/
|
||||
public interface BigMatrix {
|
||||
|
||||
|
@ -115,6 +115,17 @@ public interface BigMatrix {
|
|||
* @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 @@ public interface BigMatrix {
|
|||
*/
|
||||
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 @@ public interface BigMatrix {
|
|||
* 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 @@ public interface BigMatrix {
|
|||
* 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?
|
||||
|
|
|
@ -39,7 +39,7 @@ import java.math.BigDecimal;
|
|||
* explicitly invoke <code>LUDecompose()</code> to recompute the decomposition
|
||||
* before using any of the methods above.
|
||||
*
|
||||
* @version $Revision: 1.2 $ $Date: 2004/06/23 16:26:17 $
|
||||
* @version $Revision: 1.3 $ $Date: 2004/07/11 04:49:24 $
|
||||
*/
|
||||
public class BigMatrixImpl implements BigMatrix, Serializable {
|
||||
|
||||
|
@ -118,6 +118,17 @@ public class BigMatrixImpl implements BigMatrix, Serializable {
|
|||
lu = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
|
@ -320,6 +331,20 @@ public class BigMatrixImpl implements BigMatrix, Serializable {
|
|||
lu = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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>
|
||||
|
@ -346,7 +371,7 @@ public class BigMatrixImpl implements BigMatrix, Serializable {
|
|||
|
||||
/***
|
||||
* 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 @@ public class BigMatrixImpl implements BigMatrix, Serializable {
|
|||
*
|
||||
* @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 @@ public class BigMatrixImpl implements BigMatrix, Serializable {
|
|||
*
|
||||
* @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 @@ public class BigMatrixImpl implements BigMatrix, Serializable {
|
|||
*
|
||||
* @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 @@ public class BigMatrixImpl implements BigMatrix, Serializable {
|
|||
* @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,13 +559,29 @@ public class BigMatrixImpl implements BigMatrix, Serializable {
|
|||
* @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.
|
||||
*
|
||||
|
@ -1057,6 +1098,24 @@ public class BigMatrixImpl implements BigMatrix, Serializable {
|
|||
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];
|
||||
for (int i = 0; i < nRows; i++) {
|
||||
for (int j=0; j < nCols; j++) {
|
||||
data[i][j] = new BigDecimal(in[i][j]);
|
||||
}
|
||||
}
|
||||
lu = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests a given coordinate as being valid or invalid
|
||||
*
|
||||
|
|
|
@ -24,12 +24,13 @@ import java.math.BigDecimal;
|
|||
/**
|
||||
* Test cases for the {@link BigMatrixImpl} class.
|
||||
*
|
||||
* @version $Revision: 1.1 $ $Date: 2004/06/06 04:20:45 $
|
||||
* @version $Revision: 1.2 $ $Date: 2004/07/11 04:49:24 $
|
||||
*/
|
||||
|
||||
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 @@ public final class BigMatrixImplTest extends TestCase {
|
|||
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 @@ public final class BigMatrixImplTest extends TestCase {
|
|||
} 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 {
|
||||
|
|
Loading…
Reference in New Issue