added setter methods for rows and columns in matrices
JIRA: MATH-234 git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@724158 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
227b7b2a00
commit
b5f540eda2
|
@ -194,6 +194,8 @@ public class MessagesResources_fr
|
|||
"nombre de colonnes invalide : {0} (doit \u00eatre positif)" },
|
||||
{ "vector length mismatch: got {0} but expected {1}",
|
||||
"taille de vecteur invalide : {0} au lieu de {1} attendue" },
|
||||
{ "dimensions mismatch: got {0}x{1} but expected {2}x{3}",
|
||||
"dimensions incoh\u00e9rentes : {0}x{1} \u00e0 la place de {2}x{3}" },
|
||||
|
||||
// org.apache.commons.math.linear.BigMatrixImpl
|
||||
// org.apache.commons.math.linear.RealMatrixImpl
|
||||
|
|
|
@ -345,6 +345,27 @@ public abstract class AbstractRealMatrix implements RealMatrix, Serializable {
|
|||
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public void setRowMatrix(final int row, final RealMatrix matrix)
|
||||
throws MatrixIndexException, InvalidMatrixException {
|
||||
|
||||
checkRowIndex(row);
|
||||
final int nCols = getColumnDimension();
|
||||
if ((matrix.getRowDimension() != 1) ||
|
||||
(matrix.getColumnDimension() != nCols)) {
|
||||
throw new InvalidMatrixException("dimensions mismatch: got {0}x{1} but expected {2}x{3}",
|
||||
new Object[] {
|
||||
matrix.getRowDimension(),
|
||||
matrix.getColumnDimension(),
|
||||
1, nCols
|
||||
});
|
||||
}
|
||||
for (int i = 0; i < nCols; ++i) {
|
||||
setEntry(row, i, matrix.getEntry(0, i));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public RealMatrix getColumnMatrix(final int column)
|
||||
throws MatrixIndexException {
|
||||
|
@ -361,17 +382,76 @@ public abstract class AbstractRealMatrix implements RealMatrix, Serializable {
|
|||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public RealVector getColumnVector(final int column)
|
||||
throws MatrixIndexException {
|
||||
return new RealVectorImpl(getColumn(column), false);
|
||||
}
|
||||
public void setColumnMatrix(final int column, final RealMatrix matrix)
|
||||
throws MatrixIndexException, InvalidMatrixException {
|
||||
|
||||
checkColumnIndex(column);
|
||||
final int nRows = getRowDimension();
|
||||
if ((matrix.getRowDimension() != nRows) ||
|
||||
(matrix.getColumnDimension() != 1)) {
|
||||
throw new InvalidMatrixException("dimensions mismatch: got {0}x{1} but expected {2}x{3}",
|
||||
new Object[] {
|
||||
matrix.getRowDimension(),
|
||||
matrix.getColumnDimension(),
|
||||
nRows, 1
|
||||
});
|
||||
}
|
||||
for (int i = 0; i < nRows; ++i) {
|
||||
setEntry(i, column, matrix.getEntry(i, 0));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public RealVector getRowVector(final int row)
|
||||
throws MatrixIndexException {
|
||||
return new RealVectorImpl(getRow(row), false);
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public void setRowVector(final int row, final RealVector vector)
|
||||
throws MatrixIndexException, InvalidMatrixException {
|
||||
|
||||
checkRowIndex(row);
|
||||
final int nCols = getColumnDimension();
|
||||
if (vector.getDimension() != nCols) {
|
||||
throw new InvalidMatrixException("dimensions mismatch: got {0}x{1} but expected {2}x{3}",
|
||||
new Object[] {
|
||||
1, vector.getDimension(),
|
||||
1, nCols
|
||||
});
|
||||
}
|
||||
for (int i = 0; i < nCols; ++i) {
|
||||
setEntry(row, i, vector.getEntry(i));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public RealVector getColumnVector(final int column)
|
||||
throws MatrixIndexException {
|
||||
return new RealVectorImpl(getColumn(column), false);
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public void setColumnVector(final int column, final RealVector vector)
|
||||
throws MatrixIndexException, InvalidMatrixException {
|
||||
|
||||
checkColumnIndex(column);
|
||||
final int nRows = getRowDimension();
|
||||
if (vector.getDimension() != nRows) {
|
||||
throw new InvalidMatrixException("dimensions mismatch: got {0}x{1} but expected {2}x{3}",
|
||||
new Object[] {
|
||||
vector.getDimension(), 1,
|
||||
nRows, 1
|
||||
});
|
||||
}
|
||||
for (int i = 0; i < nRows; ++i) {
|
||||
setEntry(i, column, vector.getEntry(i));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public double[] getRow(final int row)
|
||||
throws MatrixIndexException {
|
||||
|
@ -387,6 +467,25 @@ public abstract class AbstractRealMatrix implements RealMatrix, Serializable {
|
|||
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public void setRow(final int row, final double[] array)
|
||||
throws MatrixIndexException, InvalidMatrixException {
|
||||
|
||||
checkRowIndex(row);
|
||||
final int nCols = getColumnDimension();
|
||||
if (array.length != nCols) {
|
||||
throw new InvalidMatrixException("dimensions mismatch: got {0}x{1} but expected {2}x{3}",
|
||||
new Object[] {
|
||||
1, array.length,
|
||||
1, nCols
|
||||
});
|
||||
}
|
||||
for (int i = 0; i < nCols; ++i) {
|
||||
setEntry(row, i, array[i]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public double[] getColumn(final int column)
|
||||
throws MatrixIndexException {
|
||||
|
@ -402,6 +501,25 @@ public abstract class AbstractRealMatrix implements RealMatrix, Serializable {
|
|||
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public void setColumn(final int column, final double[] array)
|
||||
throws MatrixIndexException, InvalidMatrixException {
|
||||
|
||||
checkColumnIndex(column);
|
||||
final int nRows = getRowDimension();
|
||||
if (array.length != nRows) {
|
||||
throw new InvalidMatrixException("dimensions mismatch: got {0}x{1} but expected {2}x{3}",
|
||||
new Object[] {
|
||||
array.length, 1,
|
||||
nRows, 1
|
||||
});
|
||||
}
|
||||
for (int i = 0; i < nRows; ++i) {
|
||||
setEntry(i, column, array[i]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public abstract double getEntry(int row, int column)
|
||||
throws MatrixIndexException;
|
||||
|
|
|
@ -189,6 +189,20 @@ public interface RealMatrix {
|
|||
*/
|
||||
RealMatrix getRowMatrix(int row) throws MatrixIndexException;
|
||||
|
||||
/**
|
||||
* Sets the entries in row number <code>row</code>
|
||||
* as a row matrix. Row indices start at 0.
|
||||
*
|
||||
* @param row the row to be set
|
||||
* @param matrix row matrix (must have one row and the same number of columns
|
||||
* as the instance)
|
||||
* @throws MatrixIndexException if the specified row index is invalid
|
||||
* @throws InvalidMatrixException if the matrix dimensions do not match one
|
||||
* instance row
|
||||
*/
|
||||
void setRowMatrix(int row, RealMatrix matrix)
|
||||
throws MatrixIndexException, InvalidMatrixException;
|
||||
|
||||
/**
|
||||
* Returns the entries in column number <code>column</code>
|
||||
* as a column matrix. Column indices start at 0.
|
||||
|
@ -199,6 +213,20 @@ public interface RealMatrix {
|
|||
*/
|
||||
RealMatrix getColumnMatrix(int column) throws MatrixIndexException;
|
||||
|
||||
/**
|
||||
* Sets the entries in column number <code>column</code>
|
||||
* as a column matrix. Column indices start at 0.
|
||||
*
|
||||
* @param column the column to be set
|
||||
* @param matrix column matrix (must have one column and the same number of rows
|
||||
* as the instance)
|
||||
* @throws MatrixIndexException if the specified column index is invalid
|
||||
* @throws InvalidMatrixException if the matrix dimensions do not match one
|
||||
* instance column
|
||||
*/
|
||||
void setColumnMatrix(int column, RealMatrix matrix)
|
||||
throws MatrixIndexException, InvalidMatrixException;
|
||||
|
||||
/**
|
||||
* Returns the entries in row number <code>row</code>
|
||||
* as a vector. Row indices start at 0.
|
||||
|
@ -208,6 +236,20 @@ public interface RealMatrix {
|
|||
* @throws MatrixIndexException if the specified row index is invalid
|
||||
*/
|
||||
RealVector getRowVector(int row) throws MatrixIndexException;
|
||||
|
||||
/**
|
||||
* Sets the entries in row number <code>row</code>
|
||||
* as a row matrix. Row indices start at 0.
|
||||
*
|
||||
* @param row the row to be set
|
||||
* @param vector row vector (must have the same number of columns
|
||||
* as the instance)
|
||||
* @throws MatrixIndexException if the specified row index is invalid
|
||||
* @throws InvalidMatrixException if the vector dimension does not match one
|
||||
* instance row
|
||||
*/
|
||||
void setRowVector(int row, RealVector vector)
|
||||
throws MatrixIndexException, InvalidMatrixException;
|
||||
|
||||
/**
|
||||
* Returns the entries in column number <code>column</code>
|
||||
|
@ -218,7 +260,20 @@ public interface RealMatrix {
|
|||
* @throws MatrixIndexException if the specified column index is invalid
|
||||
*/
|
||||
RealVector getColumnVector(int column) throws MatrixIndexException;
|
||||
|
||||
|
||||
/**
|
||||
* Sets the entries in column number <code>column</code>
|
||||
* as a column matrix. Column indices start at 0.
|
||||
*
|
||||
* @param column the column to be set
|
||||
* @param vector column vector (must have the same number of rows as the instance)
|
||||
* @throws MatrixIndexException if the specified column index is invalid
|
||||
* @throws InvalidMatrixException if the vector dimension does not match one
|
||||
* instance column
|
||||
*/
|
||||
void setColumnVector(int column, RealVector vector)
|
||||
throws MatrixIndexException, InvalidMatrixException;
|
||||
|
||||
/**
|
||||
* Returns the entries in row number <code>row</code> as an array.
|
||||
* <p>
|
||||
|
@ -231,6 +286,19 @@ public interface RealMatrix {
|
|||
*/
|
||||
double[] getRow(int row) throws MatrixIndexException;
|
||||
|
||||
/**
|
||||
* Sets the entries in row number <code>row</code>
|
||||
* as a row matrix. Row indices start at 0.
|
||||
*
|
||||
* @param row the row to be set
|
||||
* @param array row matrix (must have the same number of columns as the instance)
|
||||
* @throws MatrixIndexException if the specified row index is invalid
|
||||
* @throws InvalidMatrixException if the array size does not match one
|
||||
* instance row
|
||||
*/
|
||||
void setRow(int row, double[] array)
|
||||
throws MatrixIndexException, InvalidMatrixException;
|
||||
|
||||
/**
|
||||
* Returns the entries in column number <code>col</code> as an array.
|
||||
* <p>
|
||||
|
@ -243,6 +311,19 @@ public interface RealMatrix {
|
|||
*/
|
||||
double[] getColumn(int column) throws MatrixIndexException;
|
||||
|
||||
/**
|
||||
* Sets the entries in column number <code>column</code>
|
||||
* as a column matrix. Column indices start at 0.
|
||||
*
|
||||
* @param column the column to be set
|
||||
* @param array column array (must have the same number of rows as the instance)
|
||||
* @throws MatrixIndexException if the specified column index is invalid
|
||||
* @throws InvalidMatrixException if the array size does not match one
|
||||
* instance column
|
||||
*/
|
||||
void setColumn(int column, double[] array)
|
||||
throws MatrixIndexException, InvalidMatrixException;
|
||||
|
||||
/**
|
||||
* Returns the entry in the specified row and column.
|
||||
* <p>
|
||||
|
|
|
@ -39,6 +39,9 @@ The <action> type attribute can be add,update,fix,remove.
|
|||
</properties>
|
||||
<body>
|
||||
<release version="2.0" date="TBD" description="TBD">
|
||||
<action dev="luc" type="add" issue="MATH-234" >
|
||||
Added setter methods for rows and columns in matrices.
|
||||
</action>
|
||||
<action dev="luc" type="add" issue="MATH-232" >
|
||||
Added Frobenius matrix norm.
|
||||
</action>
|
||||
|
|
|
@ -465,6 +465,26 @@ public final class RealMatrixImplTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
public void testSetRowMatrix() {
|
||||
RealMatrix m = new RealMatrixImpl(subTestData);
|
||||
RealMatrix mRow3 = new RealMatrixImpl(subRow3);
|
||||
assertNotSame(mRow3, m.getRowMatrix(0));
|
||||
m.setRowMatrix(0, mRow3);
|
||||
assertEquals(mRow3, m.getRowMatrix(0));
|
||||
try {
|
||||
m.setRowMatrix(-1, mRow3);
|
||||
fail("Expecting MatrixIndexException");
|
||||
} catch (MatrixIndexException ex) {
|
||||
// expected
|
||||
}
|
||||
try {
|
||||
m.setRowMatrix(0, m);
|
||||
fail("Expecting InvalidMatrixException");
|
||||
} catch (InvalidMatrixException ex) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
public void testGetColumnMatrix() {
|
||||
RealMatrix m = new RealMatrixImpl(subTestData);
|
||||
RealMatrix mColumn1 = new RealMatrixImpl(subColumn1);
|
||||
|
@ -487,6 +507,26 @@ public final class RealMatrixImplTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
public void testSetColumnMatrix() {
|
||||
RealMatrix m = new RealMatrixImpl(subTestData);
|
||||
RealMatrix mColumn3 = new RealMatrixImpl(subColumn3);
|
||||
assertNotSame(mColumn3, m.getColumnMatrix(1));
|
||||
m.setColumnMatrix(1, mColumn3);
|
||||
assertEquals(mColumn3, m.getColumnMatrix(1));
|
||||
try {
|
||||
m.setColumnMatrix(-1, mColumn3);
|
||||
fail("Expecting MatrixIndexException");
|
||||
} catch (MatrixIndexException ex) {
|
||||
// expected
|
||||
}
|
||||
try {
|
||||
m.setColumnMatrix(0, m);
|
||||
fail("Expecting InvalidMatrixException");
|
||||
} catch (InvalidMatrixException ex) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
public void testGetRowVector() {
|
||||
RealMatrix m = new RealMatrixImpl(subTestData);
|
||||
RealVector mRow0 = new RealVectorImpl(subRow0[0]);
|
||||
|
@ -505,7 +545,27 @@ public final class RealMatrixImplTest extends TestCase {
|
|||
} catch (MatrixIndexException ex) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void testSetRowVector() {
|
||||
RealMatrix m = new RealMatrixImpl(subTestData);
|
||||
RealVector mRow3 = new RealVectorImpl(subRow3[0]);
|
||||
assertNotSame(mRow3, m.getRowMatrix(0));
|
||||
m.setRowVector(0, mRow3);
|
||||
assertEquals(mRow3, m.getRowVector(0));
|
||||
try {
|
||||
m.setRowVector(-1, mRow3);
|
||||
fail("Expecting MatrixIndexException");
|
||||
} catch (MatrixIndexException ex) {
|
||||
// expected
|
||||
}
|
||||
try {
|
||||
m.setRowVector(0, new RealVectorImpl(5));
|
||||
fail("Expecting InvalidMatrixException");
|
||||
} catch (InvalidMatrixException ex) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
public void testGetColumnVector() {
|
||||
RealMatrix m = new RealMatrixImpl(subTestData);
|
||||
|
@ -527,6 +587,26 @@ public final class RealMatrixImplTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
public void testSetColumnVector() {
|
||||
RealMatrix m = new RealMatrixImpl(subTestData);
|
||||
RealVector mColumn3 = columnToVector(subColumn3);
|
||||
assertNotSame(mColumn3, m.getColumnVector(1));
|
||||
m.setColumnVector(1, mColumn3);
|
||||
assertEquals(mColumn3, m.getColumnVector(1));
|
||||
try {
|
||||
m.setColumnVector(-1, mColumn3);
|
||||
fail("Expecting MatrixIndexException");
|
||||
} catch (MatrixIndexException ex) {
|
||||
// expected
|
||||
}
|
||||
try {
|
||||
m.setColumnVector(0, new RealVectorImpl(5));
|
||||
fail("Expecting InvalidMatrixException");
|
||||
} catch (InvalidMatrixException ex) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
private RealVector columnToVector(double[][] column) {
|
||||
double[] data = new double[column.length];
|
||||
for (int i = 0; i < data.length; ++i) {
|
||||
|
@ -534,6 +614,98 @@ public final class RealMatrixImplTest extends TestCase {
|
|||
}
|
||||
return new RealVectorImpl(data, false);
|
||||
}
|
||||
|
||||
public void testGetRow() {
|
||||
RealMatrix m = new RealMatrixImpl(subTestData);
|
||||
checkArrays(subRow0[0], m.getRow(0));
|
||||
checkArrays(subRow3[0], m.getRow(3));
|
||||
try {
|
||||
m.getRow(-1);
|
||||
fail("Expecting MatrixIndexException");
|
||||
} catch (MatrixIndexException ex) {
|
||||
// expected
|
||||
}
|
||||
try {
|
||||
m.getRow(4);
|
||||
fail("Expecting MatrixIndexException");
|
||||
} catch (MatrixIndexException ex) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
public void testSetRow() {
|
||||
RealMatrix m = new RealMatrixImpl(subTestData);
|
||||
assertTrue(subRow3[0][0] != m.getRow(0)[0]);
|
||||
m.setRow(0, subRow3[0]);
|
||||
checkArrays(subRow3[0], m.getRow(0));
|
||||
try {
|
||||
m.setRow(-1, subRow3[0]);
|
||||
fail("Expecting MatrixIndexException");
|
||||
} catch (MatrixIndexException ex) {
|
||||
// expected
|
||||
}
|
||||
try {
|
||||
m.setRow(0, new double[5]);
|
||||
fail("Expecting InvalidMatrixException");
|
||||
} catch (InvalidMatrixException ex) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
public void testGetColumn() {
|
||||
RealMatrix m = new RealMatrixImpl(subTestData);
|
||||
double[] mColumn1 = columnToArray(subColumn1);
|
||||
double[] mColumn3 = columnToArray(subColumn3);
|
||||
checkArrays(mColumn1, m.getColumn(1));
|
||||
checkArrays(mColumn3, m.getColumn(3));
|
||||
try {
|
||||
m.getColumn(-1);
|
||||
fail("Expecting MatrixIndexException");
|
||||
} catch (MatrixIndexException ex) {
|
||||
// expected
|
||||
}
|
||||
try {
|
||||
m.getColumn(4);
|
||||
fail("Expecting MatrixIndexException");
|
||||
} catch (MatrixIndexException ex) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
public void testSetColumn() {
|
||||
RealMatrix m = new RealMatrixImpl(subTestData);
|
||||
double[] mColumn3 = columnToArray(subColumn3);
|
||||
assertTrue(mColumn3[0] != m.getColumn(1)[0]);
|
||||
m.setColumn(1, mColumn3);
|
||||
checkArrays(mColumn3, m.getColumn(1));
|
||||
try {
|
||||
m.setColumn(-1, mColumn3);
|
||||
fail("Expecting MatrixIndexException");
|
||||
} catch (MatrixIndexException ex) {
|
||||
// expected
|
||||
}
|
||||
try {
|
||||
m.setColumn(0, new double[5]);
|
||||
fail("Expecting InvalidMatrixException");
|
||||
} catch (InvalidMatrixException ex) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
private double[] columnToArray(double[][] column) {
|
||||
double[] data = new double[column.length];
|
||||
for (int i = 0; i < data.length; ++i) {
|
||||
data[i] = column[i][0];
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
private void checkArrays(double[] expected, double[] actual) {
|
||||
assertEquals(expected.length, actual.length);
|
||||
for (int i = 0; i < expected.length; ++i) {
|
||||
assertEquals(expected[i], actual[i]);
|
||||
}
|
||||
}
|
||||
|
||||
public void testEqualsAndHashCode() {
|
||||
RealMatrixImpl m = new RealMatrixImpl(testData);
|
||||
|
|
Loading…
Reference in New Issue