You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by lu...@apache.org on 2013/03/13 12:33:07 UTC
svn commit: r1455890 - in /commons/proper/math/trunk/src: changes/changes.xml
main/java/org/apache/commons/math3/linear/DiagonalMatrix.java
test/java/org/apache/commons/math3/linear/DiagonalMatrixTest.java
Author: luc
Date: Wed Mar 13 11:33:06 2013
New Revision: 1455890
URL: http://svn.apache.org/r1455890
Log:
Finalized implementation of diagonal matrix.
JIRA: MATH-925
Modified:
commons/proper/math/trunk/src/changes/changes.xml
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/DiagonalMatrix.java
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/DiagonalMatrixTest.java
Modified: commons/proper/math/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/changes/changes.xml?rev=1455890&r1=1455889&r2=1455890&view=diff
==============================================================================
--- commons/proper/math/trunk/src/changes/changes.xml (original)
+++ commons/proper/math/trunk/src/changes/changes.xml Wed Mar 13 11:33:06 2013
@@ -55,6 +55,9 @@ This is a minor release: It combines bug
Changes to existing features were made in a backwards-compatible
way such as to allow drop-in replacement of the v3.1[.1] JAR file.
">
+ <action dev="luc" type="fix" issue="MATH-925" >
+ Finalized implementation of diagonal matrix.
+ </action>
<action dev="luc" type="fix" issue="MATH-630" due-to="Christopher Nix" >
Added rank revealing QR decomposition.
</action>
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/DiagonalMatrix.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/DiagonalMatrix.java?rev=1455890&r1=1455889&r2=1455890&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/DiagonalMatrix.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/DiagonalMatrix.java Wed Mar 13 11:33:06 2013
@@ -17,19 +17,21 @@
package org.apache.commons.math3.linear;
import java.io.Serializable;
+
import org.apache.commons.math3.exception.DimensionMismatchException;
import org.apache.commons.math3.exception.NotStrictlyPositiveException;
+import org.apache.commons.math3.exception.NullArgumentException;
+import org.apache.commons.math3.exception.NumberIsTooLargeException;
import org.apache.commons.math3.exception.OutOfRangeException;
-import org.apache.commons.math3.exception.MathUnsupportedOperationException;
+import org.apache.commons.math3.util.FastMath;
+import org.apache.commons.math3.util.MathUtils;
+import org.apache.commons.math3.util.Precision;
/**
* Implementation of a diagonal matrix.
- * <br/>
- * Caveat: This implementation is minimal; it is currently solely aimed
- * at solving issue MATH-924. In particular many methods just throw
- * {@code MathUnsupportedOperationException}.
*
* @version $Id$
+ * @since 3.1.1
*/
public class DiagonalMatrix extends AbstractRealMatrix
implements Serializable {
@@ -74,8 +76,11 @@ public class DiagonalMatrix extends Abst
* @param d Data for new matrix.
* @param copyArray if {@code true}, the input array will be copied,
* otherwise it will be referenced.
+ * @exception NullArgumentException if d is null
*/
- public DiagonalMatrix(final double[] d, final boolean copyArray) {
+ public DiagonalMatrix(final double[] d, final boolean copyArray)
+ throws NullArgumentException {
+ MathUtils.checkNotNull(d);
data = copyArray ? d.clone() : d;
}
@@ -98,7 +103,7 @@ public class DiagonalMatrix extends Abst
/** {@inheritDoc} */
@Override
- public RealMatrix copy() {
+ public DiagonalMatrix copy() {
return new DiagonalMatrix(data);
}
@@ -214,17 +219,6 @@ public class DiagonalMatrix extends Abst
return data;
}
- /** {@inheritDoc}
- * @throws MathUnsupportedOperationException
- */
- @Override
- public void setSubMatrix(final double[][] subMatrix,
- final int row,
- final int column)
- throws MathUnsupportedOperationException {
- throw new MathUnsupportedOperationException();
- }
-
/** {@inheritDoc} */
@Override
public double getEntry(final int row, final int column)
@@ -234,61 +228,58 @@ public class DiagonalMatrix extends Abst
}
/** {@inheritDoc}
- * @throws MathUnsupportedOperationException if {@code row != column}.
+ * @throws NumberIsTooLargeException if {@code row != column} and value is non-zero.
*/
@Override
public void setEntry(final int row, final int column, final double value)
- throws OutOfRangeException,
- MathUnsupportedOperationException {
- if (row != column) {
- throw new MathUnsupportedOperationException();
+ throws OutOfRangeException, NumberIsTooLargeException {
+ if (row == column) {
+ MatrixUtils.checkRowIndex(this, row);
+ data[row] = value;
+ } else {
+ ensureZero(value);
}
- MatrixUtils.checkMatrixIndex(this, row, column);
- data[row] = value;
}
/** {@inheritDoc}
- * @throws MathUnsupportedOperationException if {@code row != column}.
+ * @throws NumberIsTooLargeException if {@code row != column} and increment is non-zero.
*/
@Override
public void addToEntry(final int row,
final int column,
final double increment)
- throws OutOfRangeException,
- MathUnsupportedOperationException {
- if (row != column) {
- throw new MathUnsupportedOperationException();
+ throws OutOfRangeException, NumberIsTooLargeException {
+ if (row == column) {
+ MatrixUtils.checkRowIndex(this, row);
+ data[row] += increment;
+ } else {
+ ensureZero(increment);
}
- MatrixUtils.checkMatrixIndex(this, row, column);
- data[row] += increment;
}
- /** {@inheritDoc}
- * @throws MathUnsupportedOperationException if {@code row != column}.
- */
+ /** {@inheritDoc} */
@Override
public void multiplyEntry(final int row,
final int column,
final double factor)
- throws OutOfRangeException,
- MathUnsupportedOperationException {
- if (row != column) {
- throw new MathUnsupportedOperationException();
+ throws OutOfRangeException {
+ // we don't care about non-diagonal elements for multiplication
+ if (row == column) {
+ MatrixUtils.checkRowIndex(this, row);
+ data[row] *= factor;
}
- MatrixUtils.checkMatrixIndex(this, row, column);
- data[row] *= factor;
}
/** {@inheritDoc} */
@Override
public int getRowDimension() {
- return data == null ? 0 : data.length;
+ return data.length;
}
/** {@inheritDoc} */
@Override
public int getColumnDimension() {
- return getRowDimension();
+ return data.length;
}
/** {@inheritDoc} */
@@ -305,67 +296,14 @@ public class DiagonalMatrix extends Abst
return operate(v);
}
- /** {@inheritDoc} */
- @Override
- public double walkInRowOrder(final RealMatrixChangingVisitor visitor)
- throws MathUnsupportedOperationException {
- throw new MathUnsupportedOperationException();
- }
-
- /** {@inheritDoc} */
- @Override
- public double walkInRowOrder(final RealMatrixPreservingVisitor visitor)
- throws MathUnsupportedOperationException {
- throw new MathUnsupportedOperationException();
- }
-
- /** {@inheritDoc} */
- @Override
- public double walkInRowOrder(final RealMatrixChangingVisitor visitor,
- final int startRow, final int endRow,
- final int startColumn, final int endColumn)
- throws MathUnsupportedOperationException {
- throw new MathUnsupportedOperationException();
- }
-
- /** {@inheritDoc} */
- @Override
- public double walkInRowOrder(final RealMatrixPreservingVisitor visitor,
- final int startRow, final int endRow,
- final int startColumn, final int endColumn)
- throws MathUnsupportedOperationException {
- throw new MathUnsupportedOperationException();
- }
-
- /** {@inheritDoc} */
- @Override
- public double walkInColumnOrder(final RealMatrixChangingVisitor visitor)
- throws MathUnsupportedOperationException {
- throw new MathUnsupportedOperationException();
- }
-
- /** {@inheritDoc} */
- @Override
- public double walkInColumnOrder(final RealMatrixPreservingVisitor visitor)
- throws MathUnsupportedOperationException {
- throw new MathUnsupportedOperationException();
- }
-
- /** {@inheritDoc} */
- @Override
- public double walkInColumnOrder(final RealMatrixChangingVisitor visitor,
- final int startRow, final int endRow,
- final int startColumn, final int endColumn)
- throws MathUnsupportedOperationException {
- throw new MathUnsupportedOperationException();
+ /** Ensure a value is zero.
+ * @param value value to check
+ * @exception NumberIsTooLargeException if value is not zero
+ */
+ private void ensureZero(final double value) throws NumberIsTooLargeException {
+ if (!Precision.equals(0.0, value, 1)) {
+ throw new NumberIsTooLargeException(FastMath.abs(value), 0, true);
+ }
}
- /** {@inheritDoc} */
- @Override
- public double walkInColumnOrder(final RealMatrixPreservingVisitor visitor,
- final int startRow, final int endRow,
- final int startColumn, final int endColumn)
- throws MathUnsupportedOperationException {
- throw new MathUnsupportedOperationException();
- }
}
Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/DiagonalMatrixTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/DiagonalMatrixTest.java?rev=1455890&r1=1455889&r2=1455890&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/DiagonalMatrixTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/DiagonalMatrixTest.java Wed Mar 13 11:33:06 2013
@@ -16,12 +16,14 @@
*/
package org.apache.commons.math3.linear;
-import org.junit.Test;
-import org.junit.Assert;
-
-import org.apache.commons.math3.exception.MathUnsupportedOperationException;
-import org.apache.commons.math3.exception.OutOfRangeException;
import org.apache.commons.math3.TestUtils;
+import org.apache.commons.math3.exception.DimensionMismatchException;
+import org.apache.commons.math3.exception.NullArgumentException;
+import org.apache.commons.math3.exception.NumberIsTooLargeException;
+import org.apache.commons.math3.exception.OutOfRangeException;
+import org.apache.commons.math3.util.Precision;
+import org.junit.Assert;
+import org.junit.Test;
/**
* Test cases for the {@link DiagonalMatrix} class.
@@ -74,6 +76,33 @@ public class DiagonalMatrixTest {
}
+ @Test(expected=DimensionMismatchException.class)
+ public void testCreateError() {
+ final double[] d = { -1.2, 3.4, 5 };
+ final DiagonalMatrix m = new DiagonalMatrix(d, false);
+ m.createMatrix(5, 3);
+ }
+
+ @Test
+ public void testCreate() {
+ final double[] d = { -1.2, 3.4, 5 };
+ final DiagonalMatrix m = new DiagonalMatrix(d, false);
+ final RealMatrix p = m.createMatrix(5, 5);
+ Assert.assertTrue(p instanceof DiagonalMatrix);
+ Assert.assertEquals(5, p.getRowDimension());
+ Assert.assertEquals(5, p.getColumnDimension());
+ }
+
+ @Test
+ public void testCopy() {
+ final double[] d = { -1.2, 3.4, 5 };
+ final DiagonalMatrix m = new DiagonalMatrix(d, false);
+ final DiagonalMatrix p = m.copy();
+ for (int i = 0; i < m.getRowDimension(); ++i) {
+ Assert.assertEquals(m.getEntry(i, i), p.getEntry(i, i), 1.0e-20);
+ }
+ }
+
@Test
public void testGetData() {
final double[] data = { -1.2, 3.4, 5 };
@@ -119,6 +148,27 @@ public class DiagonalMatrixTest {
}
@Test
+ public void testSubtract() {
+ final double[] data1 = { -1.2, 3.4, 5 };
+ final DiagonalMatrix m1 = new DiagonalMatrix(data1);
+
+ final double[] data2 = { 10.1, 2.3, 45 };
+ final DiagonalMatrix m2 = new DiagonalMatrix(data2);
+
+ final DiagonalMatrix result = m1.subtract(m2);
+ Assert.assertEquals(m1.getRowDimension(), result.getRowDimension());
+ for (int i = 0; i < result.getRowDimension(); i++) {
+ for (int j = 0; j < result.getRowDimension(); j++) {
+ if (i == j) {
+ Assert.assertEquals(data1[i] - data2[i], result.getEntry(i, j), 0d);
+ } else {
+ Assert.assertEquals(0d, result.getEntry(i, j), 0d);
+ }
+ }
+ }
+ }
+
+ @Test
public void testAddToEntry() {
final double[] data = { -1.2, 3.4, 5 };
final DiagonalMatrix m = new DiagonalMatrix(data);
@@ -147,7 +197,7 @@ public class DiagonalMatrixTest {
final double[] data2 = { 10.1, 2.3, 45 };
final DiagonalMatrix m2 = new DiagonalMatrix(data2);
- final DiagonalMatrix result = m1.multiply(m2);
+ final DiagonalMatrix result = (DiagonalMatrix) m1.multiply((RealMatrix) m2);
Assert.assertEquals(m1.getRowDimension(), result.getRowDimension());
for (int i = 0; i < result.getRowDimension(); i++) {
for (int j = 0; j < result.getRowDimension(); j++) {
@@ -208,15 +258,72 @@ public class DiagonalMatrixTest {
TestUtils.assertEquals(diagResult, denseResult, 0d);
}
- @Test(expected=MathUnsupportedOperationException.class)
+ @Test(expected=NumberIsTooLargeException.class)
public void testSetNonDiagonalEntry() {
final DiagonalMatrix diag = new DiagonalMatrix(3);
diag.setEntry(1, 2, 3.4);
}
+ @Test
+ public void testSetNonDiagonalZero() {
+ final DiagonalMatrix diag = new DiagonalMatrix(3);
+ diag.setEntry(1, 2, 0.0);
+ Assert.assertEquals(0.0, diag.getEntry(1, 2), Precision.SAFE_MIN);
+ }
+
+ @Test(expected=NumberIsTooLargeException.class)
+ public void testAddNonDiagonalEntry() {
+ final DiagonalMatrix diag = new DiagonalMatrix(3);
+ diag.addToEntry(1, 2, 3.4);
+ }
+
+ @Test
+ public void testAddNonDiagonalZero() {
+ final DiagonalMatrix diag = new DiagonalMatrix(3);
+ diag.addToEntry(1, 2, 0.0);
+ Assert.assertEquals(0.0, diag.getEntry(1, 2), Precision.SAFE_MIN);
+ }
+
+ @Test
+ public void testMultiplyNonDiagonalEntry() {
+ final DiagonalMatrix diag = new DiagonalMatrix(3);
+ diag.multiplyEntry(1, 2, 3.4);
+ Assert.assertEquals(0.0, diag.getEntry(1, 2), Precision.SAFE_MIN);
+ }
+
+ @Test
+ public void testMultiplyNonDiagonalZero() {
+ final DiagonalMatrix diag = new DiagonalMatrix(3);
+ diag.multiplyEntry(1, 2, 0.0);
+ Assert.assertEquals(0.0, diag.getEntry(1, 2), Precision.SAFE_MIN);
+ }
+
@Test(expected=OutOfRangeException.class)
public void testSetEntryOutOfRange() {
final DiagonalMatrix diag = new DiagonalMatrix(3);
diag.setEntry(3, 3, 3.4);
}
+
+ @Test(expected=NullArgumentException.class)
+ public void testNull() {
+ new DiagonalMatrix(null, false);
+ }
+
+ @Test(expected=NumberIsTooLargeException.class)
+ public void testSetSubMatrixError() {
+ final double[] data = { -1.2, 3.4, 5 };
+ final DiagonalMatrix diag = new DiagonalMatrix(data);
+ diag.setSubMatrix(new double[][] { {1.0, 1.0}, {1.0, 1.0}}, 1, 1);
+ }
+
+ @Test
+ public void testSetSubMatrix() {
+ final double[] data = { -1.2, 3.4, 5 };
+ final DiagonalMatrix diag = new DiagonalMatrix(data);
+ diag.setSubMatrix(new double[][] { {0.0, 5.0, 0.0}, {0.0, 0.0, 6.0}}, 1, 0);
+ Assert.assertEquals(-1.2, diag.getEntry(0, 0), 1.0e-20);
+ Assert.assertEquals( 5.0, diag.getEntry(1, 1), 1.0e-20);
+ Assert.assertEquals( 6.0, diag.getEntry(2, 2), 1.0e-20);
+ }
+
}