psteitz 2004/10/24 22:36:15
Modified: math/src/java/org/apache/commons/math/linear
MatrixUtils.java
math/src/test/org/apache/commons/math/linear
MatrixUtilsTest.java
Log:
Added methods to create BigMatrix instances.
Revision Changes Path
1.2 +147 -1
jakarta-commons/math/src/java/org/apache/commons/math/linear/MatrixUtils.java
Index: MatrixUtils.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/math/src/java/org/apache/commons/math/linear/MatrixUtils.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- MatrixUtils.java 12 Oct 2004 06:27:44 -0000 1.1
+++ MatrixUtils.java 25 Oct 2004 05:36:15 -0000 1.2
@@ -16,6 +16,8 @@
package org.apache.commons.math.linear;
+import java.math.BigDecimal;
+
/**
* A collection of static methods that operate on or return matrices.
*
@@ -45,6 +47,48 @@
}
/**
+ * Returns a [EMAIL PROTECTED] BigMatrix} whose entries are the the values in
the
+ * the input array. The input array is copied, not referenced.
+ *
+ * @param data input array
+ * @return RealMatrix containing the values of the array
+ * @throws IllegalArgumentException if <code>data</code> is not rectangular
+ * (not all rows have the same length) or empty
+ * @throws NullPointerException if data is null
+ */
+ public static BigMatrix createBigMatrix(double[][] data) {
+ return new BigMatrixImpl(data);
+ }
+
+ /**
+ * Returns a [EMAIL PROTECTED] BigMatrix} whose entries are the the values in
the
+ * the input array. The input array is copied, not referenced.
+ *
+ * @param data input array
+ * @return RealMatrix containing the values of the array
+ * @throws IllegalArgumentException if <code>data</code> is not rectangular
+ * (not all rows have the same length) or empty
+ * @throws NullPointerException if data is null
+ */
+ public static BigMatrix createBigMatrix(BigDecimal[][] data) {
+ return new BigMatrixImpl(data);
+ }
+
+ /**
+ * Returns a [EMAIL PROTECTED] BigMatrix} whose entries are the the values in
the
+ * the input array. The input array is copied, not referenced.
+ *
+ * @param data input array
+ * @return RealMatrix containing the values of the array
+ * @throws IllegalArgumentException if <code>data</code> is not rectangular
+ * (not all rows have the same length) or empty
+ * @throws NullPointerException if data is null
+ */
+ public static BigMatrix createBigMatrix(String[][] data) {
+ return new BigMatrixImpl(data);
+ }
+
+ /**
* Creates a row [EMAIL PROTECTED] RealMatrix} using the data from the input
* array.
*
@@ -61,6 +105,54 @@
}
/**
+ * Creates a row [EMAIL PROTECTED] BigMatrix} using the data from the input
+ * array.
+ *
+ * @param rowData the input row data
+ * @return a 1 x rowData.length BigMatrix
+ * @throws IllegalArgumentException if <code>rowData</code> is empty
+ * @throws NullPointerException if <code>rowData</code>is null
+ */
+ public static BigMatrix createRowBigMatrix(double[] rowData) {
+ int nCols = rowData.length;
+ double[][] data = new double[1][nCols];
+ System.arraycopy(rowData, 0, data[0], 0, nCols);
+ return new BigMatrixImpl(data);
+ }
+
+ /**
+ * Creates a row [EMAIL PROTECTED] BigMatrix} using the data from the input
+ * array.
+ *
+ * @param rowData the input row data
+ * @return a 1 x rowData.length BigMatrix
+ * @throws IllegalArgumentException if <code>rowData</code> is empty
+ * @throws NullPointerException if <code>rowData</code>is null
+ */
+ public static BigMatrix createRowBigMatrix(BigDecimal[] rowData) {
+ int nCols = rowData.length;
+ BigDecimal[][] data = new BigDecimal[1][nCols];
+ System.arraycopy(rowData, 0, data[0], 0, nCols);
+ return new BigMatrixImpl(data);
+ }
+
+ /**
+ * Creates a row [EMAIL PROTECTED] BigMatrix} using the data from the input
+ * array.
+ *
+ * @param rowData the input row data
+ * @return a 1 x rowData.length BigMatrix
+ * @throws IllegalArgumentException if <code>rowData</code> is empty
+ * @throws NullPointerException if <code>rowData</code>is null
+ */
+ public static BigMatrix createRowBigMatrix(String[] rowData) {
+ int nCols = rowData.length;
+ String[][] data = new String[1][nCols];
+ System.arraycopy(rowData, 0, data[0], 0, nCols);
+ return new BigMatrixImpl(data);
+ }
+
+ /**
* Creates a column [EMAIL PROTECTED] RealMatrix} using the data from the input
* array.
*
@@ -76,6 +168,60 @@
data[row][0] = columnData[row];
}
return new RealMatrixImpl(data);
+ }
+
+ /**
+ * Creates a column [EMAIL PROTECTED] BigMatrix} using the data from the input
+ * array.
+ *
+ * @param columnData the input column data
+ * @return a columnData x 1 BigMatrix
+ * @throws IllegalArgumentException if <code>columnData</code> is empty
+ * @throws NullPointerException if <code>columnData</code>is null
+ */
+ public static BigMatrix createColumnBigMatrix(double[] columnData) {
+ int nRows = columnData.length;
+ double[][] data = new double[nRows][1];
+ for (int row = 0; row < nRows; row++) {
+ data[row][0] = columnData[row];
+ }
+ return new BigMatrixImpl(data);
+ }
+
+ /**
+ * Creates a column [EMAIL PROTECTED] BigMatrix} using the data from the input
+ * array.
+ *
+ * @param columnData the input column data
+ * @return a columnData x 1 BigMatrix
+ * @throws IllegalArgumentException if <code>columnData</code> is empty
+ * @throws NullPointerException if <code>columnData</code>is null
+ */
+ public static BigMatrix createColumnBigMatrix(BigDecimal[] columnData) {
+ int nRows = columnData.length;
+ BigDecimal[][] data = new BigDecimal[nRows][1];
+ for (int row = 0; row < nRows; row++) {
+ data[row][0] = columnData[row];
+ }
+ return new BigMatrixImpl(data);
+ }
+
+ /**
+ * Creates a column [EMAIL PROTECTED] BigMatrix} using the data from the input
+ * array.
+ *
+ * @param columnData the input column data
+ * @return a columnData x 1 BigMatrix
+ * @throws IllegalArgumentException if <code>columnData</code> is empty
+ * @throws NullPointerException if <code>columnData</code>is null
+ */
+ public static BigMatrix createColumnBigMatrix(String[] columnData) {
+ int nRows = columnData.length;
+ String[][] data = new String[nRows][1];
+ for (int row = 0; row < nRows; row++) {
+ data[row][0] = columnData[row];
+ }
+ return new BigMatrixImpl(data);
}
}
1.2 +89 -1
jakarta-commons/math/src/test/org/apache/commons/math/linear/MatrixUtilsTest.java
Index: MatrixUtilsTest.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/math/src/test/org/apache/commons/math/linear/MatrixUtilsTest.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- MatrixUtilsTest.java 12 Oct 2004 06:27:44 -0000 1.1
+++ MatrixUtilsTest.java 25 Oct 2004 05:36:15 -0000 1.2
@@ -15,6 +15,7 @@
*/
package org.apache.commons.math.linear;
+import java.math.BigDecimal;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
@@ -28,13 +29,30 @@
public final class MatrixUtilsTest extends TestCase {
protected double[][] testData = { {1d,2d,3d}, {2d,5d,3d}, {1d,0d,8d} };
+ protected double[][] nullMatrix = null;
protected double[] row = {1,2,3};
+ protected BigDecimal[] bigRow =
+ {new BigDecimal(1),new BigDecimal(2),new BigDecimal(3)};
+ protected String[] stringRow = {"1", "2", "3"};
protected double[][] rowMatrix = {{1,2,3}};
+ protected BigDecimal[][] bigRowMatrix =
+ {{new BigDecimal(1), new BigDecimal(2), new BigDecimal(3)}};
+ protected String[][] stringRowMatrix = {{"1", "2", "3"}};
protected double[] col = {0,4,6};
+ protected BigDecimal[] bigCol =
+ {new BigDecimal(0),new BigDecimal(4),new BigDecimal(6)};
+ protected String[] stringCol = {"0","4","6"};
+ protected double[] nullDoubleArray = null;
protected double[][] colMatrix = {{0},{4},{6}};
+ protected BigDecimal[][] bigColMatrix =
+ {{new BigDecimal(0)},{new BigDecimal(4)},{new BigDecimal(6)}};
+ protected String[][] stringColMatrix = {{"0"}, {"4"}, {"6"}};
public MatrixUtilsTest(String name) {
super(name);
+ for (int i = 0; i < row.length; i++) {
+
+ }
}
public void setUp() {
@@ -68,6 +86,33 @@
// expected
}
}
+
+ public void testCreateBigMatrix() {
+ assertEquals(new BigMatrixImpl(testData),
+ MatrixUtils.createBigMatrix(testData));
+ assertEquals(new BigMatrixImpl(bigColMatrix),
+ MatrixUtils.createBigMatrix(bigColMatrix));
+ assertEquals(new BigMatrixImpl(stringColMatrix),
+ MatrixUtils.createBigMatrix(stringColMatrix));
+ try {
+ MatrixUtils.createBigMatrix(new double[][] {{1}, {1,2}}); // ragged
+ fail("Expecting IllegalArgumentException");
+ } catch (IllegalArgumentException ex) {
+ // expected
+ }
+ try {
+ MatrixUtils.createBigMatrix(new double[][] {{}, {}}); // no columns
+ fail("Expecting IllegalArgumentException");
+ } catch (IllegalArgumentException ex) {
+ // expected
+ }
+ try {
+ MatrixUtils.createBigMatrix(nullMatrix); // null
+ fail("Expecting NullPointerException");
+ } catch (NullPointerException ex) {
+ // expected
+ }
+ }
public void testCreateRowRealMatrix() {
assertEquals((RealMatrixImpl) MatrixUtils.createRowRealMatrix(row),
@@ -86,6 +131,27 @@
}
}
+ public void testCreateRowBigMatrix() {
+ assertEquals((BigMatrixImpl) MatrixUtils.createRowBigMatrix(row),
+ new BigMatrixImpl(rowMatrix));
+ assertEquals((BigMatrixImpl) MatrixUtils.createRowBigMatrix(bigRow),
+ new BigMatrixImpl(bigRowMatrix));
+ assertEquals((BigMatrixImpl) MatrixUtils.createRowBigMatrix(stringRow),
+ new BigMatrixImpl(stringRowMatrix));
+ try {
+ MatrixUtils.createRowBigMatrix(new double[] {}); // empty
+ fail("Expecting IllegalArgumentException");
+ } catch (IllegalArgumentException ex) {
+ // expected
+ }
+ try {
+ MatrixUtils.createRowBigMatrix(nullDoubleArray); // null
+ fail("Expecting NullPointerException");
+ } catch (NullPointerException ex) {
+ // expected
+ }
+ }
+
public void testCreateColumnRealMatrix() {
assertEquals((RealMatrixImpl) MatrixUtils.createColumnRealMatrix(col),
new RealMatrixImpl(colMatrix));
@@ -97,6 +163,28 @@
}
try {
MatrixUtils.createColumnRealMatrix(null); // null
+ fail("Expecting NullPointerException");
+ } catch (NullPointerException ex) {
+ // expected
+ }
+ }
+
+ public void testCreateColumnBigMatrix() {
+ assertEquals((BigMatrixImpl) MatrixUtils.createColumnBigMatrix(col),
+ new BigMatrixImpl(colMatrix));
+ assertEquals((BigMatrixImpl) MatrixUtils.createColumnBigMatrix(bigCol),
+ new BigMatrixImpl(bigColMatrix));
+ assertEquals((BigMatrixImpl) MatrixUtils.createColumnBigMatrix(stringCol),
+ new BigMatrixImpl(stringColMatrix));
+
+ try {
+ MatrixUtils.createColumnBigMatrix(new double[] {}); // empty
+ fail("Expecting IllegalArgumentException");
+ } catch (IllegalArgumentException ex) {
+ // expected
+ }
+ try {
+ MatrixUtils.createColumnBigMatrix(nullDoubleArray); // null
fail("Expecting NullPointerException");
} catch (NullPointerException ex) {
// expected
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]