Added row and column matrix accessors. Pr #30897.
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/math/trunk@141462 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
49a0189360
commit
ef2faa199c
|
@ -22,7 +22,7 @@ package org.apache.commons.math.linear;
|
|||
* Matrix element indexing is 0-based -- e.g., <code>getEntry(0, 0)</code>
|
||||
* returns the element in the first row, first column of the matrix.
|
||||
*
|
||||
* @version $Revision: 1.23 $ $Date: 2004/10/09 21:15:56 $
|
||||
* @version $Revision: 1.24 $ $Date: 2004/10/09 22:39:22 $
|
||||
*/
|
||||
public interface RealMatrix {
|
||||
|
||||
|
@ -128,15 +128,35 @@ public interface RealMatrix {
|
|||
* Gets a submatrix. Rows and columns are indicated
|
||||
* counting from 0 to n-1.
|
||||
*
|
||||
* @param rows Array of row indices.
|
||||
* @param columns Array of column indices.
|
||||
* @param selectedRows Array of row indices.
|
||||
* @param selectedColumns Array of column indices.
|
||||
* @return The subMatrix containing the data in the
|
||||
* specified rows and columns
|
||||
* @exception MatrixIndexException if row or column selections are not valid
|
||||
*/
|
||||
RealMatrix getSubMatrix(int[] selectedRows, int[] selectedColumns)
|
||||
throws MatrixIndexException;
|
||||
|
||||
|
||||
/**
|
||||
* Returns the entries in row number <code>row</code>
|
||||
* as a row matrix. Row indices start at 0.
|
||||
*
|
||||
* @param row the row to be fetched
|
||||
* @return row matrix
|
||||
* @throws MatrixIndexException if the specified row index is invalid
|
||||
*/
|
||||
RealMatrix getRowMatrix(int row) throws MatrixIndexException;
|
||||
|
||||
/**
|
||||
* Returns the entries in column number <code>column</code>
|
||||
* as a column matrix. Column indices start at 0.
|
||||
*
|
||||
* @param column the column to be fetched
|
||||
* @return column matrix
|
||||
* @throws MatrixIndexException if the specified column index is invalid
|
||||
*/
|
||||
RealMatrix getColumnMatrix(int column) throws MatrixIndexException;
|
||||
|
||||
/**
|
||||
* Returns the entries in row number <code>row</code> as an array.
|
||||
* <p>
|
||||
|
|
|
@ -43,7 +43,7 @@ import java.io.Serializable;
|
|||
* is 0-based -- e.g., <code>getEntry(0, 0)</code>
|
||||
* returns the element in the first row, first column of the matrix.</li></ul>
|
||||
*
|
||||
* @version $Revision: 1.29 $ $Date: 2004/10/09 21:15:56 $
|
||||
* @version $Revision: 1.30 $ $Date: 2004/10/09 22:39:22 $
|
||||
*/
|
||||
public class RealMatrixImpl implements RealMatrix, Serializable {
|
||||
|
||||
|
@ -343,8 +343,8 @@ public class RealMatrixImpl implements RealMatrix, Serializable {
|
|||
* Gets a submatrix. Rows and columns are indicated
|
||||
* counting from 0 to n-1.
|
||||
*
|
||||
* @param rows Array of row indices must be non-empty
|
||||
* @param columns Array of column indices must be non-empty
|
||||
* @param selectedRows Array of row indices must be non-empty
|
||||
* @param selectedColumns Array of column indices must be non-empty
|
||||
* @return The subMatrix containing the data in the
|
||||
* specified rows and columns
|
||||
* @exception MatrixIndexException if supplied row or column index arrays
|
||||
|
@ -371,6 +371,44 @@ public class RealMatrixImpl implements RealMatrix, Serializable {
|
|||
}
|
||||
return subMatrix;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the entries in row number <code>row</code>
|
||||
* as a row matrix. Row indices start at 0.
|
||||
*
|
||||
* @param row the row to be fetched
|
||||
* @return row matrix
|
||||
* @throws MatrixIndexException if the specified row index is invalid
|
||||
*/
|
||||
public RealMatrix getRowMatrix(int row) throws MatrixIndexException {
|
||||
if ( !isValidCoordinate( row, 0)) {
|
||||
throw new MatrixIndexException("illegal row argument");
|
||||
}
|
||||
int ncols = this.getColumnDimension();
|
||||
double[][] out = new double[1][ncols];
|
||||
System.arraycopy(data[row], 0, out[0], 0, ncols);
|
||||
return new RealMatrixImpl(out);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the entries in column number <code>column</code>
|
||||
* as a column matrix. Column indices start at 0.
|
||||
*
|
||||
* @param column the column to be fetched
|
||||
* @return column matrix
|
||||
* @throws MatrixIndexException if the specified column index is invalid
|
||||
*/
|
||||
public RealMatrix getColumnMatrix(int column) throws MatrixIndexException {
|
||||
if ( !isValidCoordinate( 0, column)) {
|
||||
throw new MatrixIndexException("illegal column argument");
|
||||
}
|
||||
int nRows = this.getRowDimension();
|
||||
double[][] out = new double[nRows][1];
|
||||
for (int row = 0; row < nRows; row++) {
|
||||
out[row][0] = data[row][column];
|
||||
}
|
||||
return new RealMatrixImpl(out);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the entries in row number <code>row</code> as an array.
|
||||
|
|
|
@ -22,7 +22,7 @@ import junit.framework.TestSuite;
|
|||
/**
|
||||
* Test cases for the {@link RealMatrixImpl} class.
|
||||
*
|
||||
* @version $Revision: 1.16 $ $Date: 2004/10/09 21:15:56 $
|
||||
* @version $Revision: 1.17 $ $Date: 2004/10/09 22:39:22 $
|
||||
*/
|
||||
|
||||
public final class RealMatrixImplTest extends TestCase {
|
||||
|
@ -76,6 +76,12 @@ public final class RealMatrixImplTest extends TestCase {
|
|||
protected double[][] subRows01Cols23 = {{3,4} , {3.5, 4.5}};
|
||||
protected double[][] subRows23Cols00 = {{2} , {4}};
|
||||
protected double[][] subRows00Cols33 = {{4}};
|
||||
// row matrices
|
||||
protected double[][] subRow0 = {{1,2,3,4}};
|
||||
protected double[][] subRow3 = {{4,5,6,7}};
|
||||
// column matrices
|
||||
protected double[][] subColumn1 = {{2}, {2.5}, {4}, {5}};
|
||||
protected double[][] subColumn3 = {{4}, {4.5}, {8}, {7}};
|
||||
|
||||
// tolerances
|
||||
protected double entryTolerance = 10E-16;
|
||||
|
@ -556,6 +562,50 @@ public final class RealMatrixImplTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
public void testGetRowMatrix() {
|
||||
RealMatrix m = new RealMatrixImpl(subTestData);
|
||||
RealMatrix mRow0 = new RealMatrixImpl(subRow0);
|
||||
RealMatrix mRow3 = new RealMatrixImpl(subRow3);
|
||||
assertClose("Row0", mRow0,
|
||||
m.getRowMatrix(0), normTolerance );
|
||||
assertClose("Row3", mRow3,
|
||||
m.getRowMatrix(3), normTolerance );
|
||||
try {
|
||||
m.getRowMatrix(-1);
|
||||
fail("Expecting MatrixIndexException");
|
||||
} catch (MatrixIndexException ex) {
|
||||
// expected
|
||||
}
|
||||
try {
|
||||
m.getRowMatrix(4);
|
||||
fail("Expecting MatrixIndexException");
|
||||
} catch (MatrixIndexException ex) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
public void testGetColumnMatrix() {
|
||||
RealMatrix m = new RealMatrixImpl(subTestData);
|
||||
RealMatrix mColumn1 = new RealMatrixImpl(subColumn1);
|
||||
RealMatrix mColumn3 = new RealMatrixImpl(subColumn3);
|
||||
assertClose("Column1", mColumn1,
|
||||
m.getColumnMatrix(1), normTolerance );
|
||||
assertClose("Column3", mColumn3,
|
||||
m.getColumnMatrix(3), normTolerance );
|
||||
try {
|
||||
m.getColumnMatrix(-1);
|
||||
fail("Expecting MatrixIndexException");
|
||||
} catch (MatrixIndexException ex) {
|
||||
// expected
|
||||
}
|
||||
try {
|
||||
m.getColumnMatrix(4);
|
||||
fail("Expecting MatrixIndexException");
|
||||
} catch (MatrixIndexException ex) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
//--------------- -----------------Protected methods
|
||||
|
||||
/** verifies that two matrices are close (1-norm) */
|
||||
|
|
Loading…
Reference in New Issue