psteitz 2004/10/11 23:27:44 Added: math/src/java/org/apache/commons/math/linear MatrixUtils.java math/src/test/org/apache/commons/math/linear MatrixUtilsTest.java Log: Initial commit of MatrixUtils, including RealMatrix factory methods. Revision Changes Path 1.1 jakarta-commons/math/src/java/org/apache/commons/math/linear/MatrixUtils.java Index: MatrixUtils.java =================================================================== /* * Copyright 2004 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.commons.math.linear; /** * A collection of static methods that operate on or return matrices. * * @version $Revision: 1.1 $ $Date: 2004/10/12 06:27:44 $ */ public class MatrixUtils { /** * Default constructor. Package scope to prevent unwanted instantiation. */ public MatrixUtils() { super(); } /** * Returns a [EMAIL PROTECTED] RealMatrix} 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 RealMatrix createRealMatrix(double[][] data) { return new RealMatrixImpl(data); } /** * Creates a row [EMAIL PROTECTED] RealMatrix} using the data from the input * array. * * @param rowData the input row data * @return a 1 x rowData.length RealMatrix * @throws IllegalArgumentException if <code>rowData</code> is empty * @throws NullPointerException if <code>rowData</code>is null */ public static RealMatrix createRowRealMatrix(double[] rowData) { int nCols = rowData.length; double[][] data = new double[1][nCols]; System.arraycopy(rowData, 0, data[0], 0, nCols); return new RealMatrixImpl(data); } /** * Creates a column [EMAIL PROTECTED] RealMatrix} using the data from the input * array. * * @param columnData the input column data * @return a columnData x 1 RealMatrix * @throws IllegalArgumentException if <code>columnData</code> is empty * @throws NullPointerException if <code>columnData</code>is null */ public static RealMatrix createColumnRealMatrix(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 RealMatrixImpl(data); } } 1.1 jakarta-commons/math/src/test/org/apache/commons/math/linear/MatrixUtilsTest.java Index: MatrixUtilsTest.java =================================================================== /* * Copyright 2004 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.commons.math.linear; import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; /** * Test cases for the [EMAIL PROTECTED] MatrixUtils} class. * * @version $Revision: 1.1 $ $Date: 2004/10/12 06:27:44 $ */ public final class MatrixUtilsTest extends TestCase { protected double[][] testData = { {1d,2d,3d}, {2d,5d,3d}, {1d,0d,8d} }; protected double[] row = {1,2,3}; protected double[][] rowMatrix = {{1,2,3}}; protected double[] col = {0,4,6}; protected double[][] colMatrix = {{0},{4},{6}}; public MatrixUtilsTest(String name) { super(name); } public void setUp() { } public static Test suite() { TestSuite suite = new TestSuite(MatrixUtilsTest.class); suite.setName("MatrixUtils Tests"); return suite; } public void testCreateRealMatrix() { assertEquals(new RealMatrixImpl(testData), MatrixUtils.createRealMatrix(testData)); try { MatrixUtils.createRealMatrix(new double[][] {{1}, {1,2}}); // ragged fail("Expecting IllegalArgumentException"); } catch (IllegalArgumentException ex) { // expected } try { MatrixUtils.createRealMatrix(new double[][] {{}, {}}); // no columns fail("Expecting IllegalArgumentException"); } catch (IllegalArgumentException ex) { // expected } try { MatrixUtils.createRealMatrix(null); // null fail("Expecting NullPointerException"); } catch (NullPointerException ex) { // expected } } public void testCreateRowRealMatrix() { assertEquals((RealMatrixImpl) MatrixUtils.createRowRealMatrix(row), new RealMatrixImpl(rowMatrix)); try { MatrixUtils.createRowRealMatrix(new double[] {}); // empty fail("Expecting IllegalArgumentException"); } catch (IllegalArgumentException ex) { // expected } try { MatrixUtils.createRowRealMatrix(null); // null fail("Expecting NullPointerException"); } catch (NullPointerException ex) { // expected } } public void testCreateColumnRealMatrix() { assertEquals((RealMatrixImpl) MatrixUtils.createColumnRealMatrix(col), new RealMatrixImpl(colMatrix)); try { MatrixUtils.createColumnRealMatrix(new double[] {}); // empty fail("Expecting IllegalArgumentException"); } catch (IllegalArgumentException ex) { // expected } try { MatrixUtils.createColumnRealMatrix(null); // null fail("Expecting NullPointerException"); } catch (NullPointerException ex) { // expected } } }
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]