RealMatrix now throws two more runtime exceptions

InvalidMatrixException and MatrixIndexException


git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/math/trunk@141070 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Tim O'Brien 2004-01-28 20:15:03 +00:00
parent 8647ea1d57
commit 204063d317
5 changed files with 905 additions and 743 deletions

View File

@ -0,0 +1,69 @@
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2004 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowledgement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgement may appear in the software itself,
* if and wherever such third-party acknowledgements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their name without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.commons.math.linear;
/**
* Thrown when a system attempts an operation on a matrix, and
* that matrix does not satisfy the preconditions for the
* aforementioned operation.
* @version $Revision: 1.1 $ $Date: 2004/01/28 20:15:03 $
*/
public class InvalidMatrixException extends RuntimeException {
public InvalidMatrixException(String s) {
super( s );
}
}

View File

@ -0,0 +1,68 @@
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2004 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowledgement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgement may appear in the software itself,
* if and wherever such third-party acknowledgements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their name without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.commons.math.linear;
/**
* Thrown when an operation addresses a matrix coordinate (row,col)
* which is outside of the dimensions of a matrix.
* @version $Revision: 1.1 $ $Date: 2004/01/28 20:15:03 $
*/
public class MatrixIndexException extends RuntimeException {
public MatrixIndexException(String s) {
super( s );
}
}

View File

@ -56,7 +56,7 @@ package org.apache.commons.math.linear;
/**
* Interface defining a real-valued matrix with basic algebraic operations
* @version $Revision: 1.7 $ $Date: 2003/11/14 22:22:19 $
* @version $Revision: 1.8 $ $Date: 2004/01/28 20:15:03 $
*/
public interface RealMatrix {
@ -147,28 +147,31 @@ public interface RealMatrix {
*
* @param row the row to be fetched
* @return array of entries in the row
* @throws IllegalArgumentException if row > rowDimension
* @throws MatrixIndexException if the specified row is greater
* than the number of rows in this matrix
*/
double[] getRow(int row) throws IllegalArgumentException;
double[] getRow(int row) throws MatrixIndexException;
/**
* Returns the entries in column number <code>col</code> as an array.
*
* @param col column to fetch
* @return array of entries in the column
* @throws IllegalArgumentException if column > columnDimension
* @throws MatrixIndexException if the specified column is greater
* than the number of columns in this matrix
*/
double[] getColumn(int col) throws IllegalArgumentException;
double[] getColumn(int col) throws MatrixIndexException;
/**
* Returns the entry in the specified row and column.
*
* @param row row location of entry to be fetched
* @param column column location of entry to be fetched
* @return matrix entry in row,column
* @throws IllegalArgumentException if entry does not exist
* @return matrix entry in row,column
* @throws MatrixIndexException if the specified coordinate is outside
* the dimensions of this matrix
*/
double getEntry(int row, int column) throws IllegalArgumentException;
double getEntry(int row, int column) throws MatrixIndexException;
/**
* Sets the entry in the specified row and column to the specified value.
@ -176,10 +179,11 @@ public interface RealMatrix {
* @param row row location of entry to be set
* @param column column location of entry to be set
* @param value value to set
* @throws IllegalArgumentException if entry does not exist
* @throws MatrixIndexException if the specified coordinate is outside
* he dimensions of this matrix
*/
void setEntry(int row, int column, double value)
throws IllegalArgumentException;
throws MatrixIndexException;
/**
* Returns the transpose of this matrix.
@ -260,11 +264,11 @@ public interface RealMatrix {
* matrix = this and constant vector = <code>b</code>.
*
* @param b constant vector
* @return vector of solution values to AX = b, where A is *this
* @throws IllegalArgumentException if rowDimension != b.length or matrix
* is singular
* @return vector of solution values to AX = b, where A is *this
* @throws IllegalArgumentException if this.rowDimension != b.length
* @throws InvalidMatrixException if this matrix is square or singular
*/
double[] solve(double[] b) throws IllegalArgumentException;
double[] solve(double[] b) throws IllegalArgumentException, InvalidMatrixException;
/**
* Returns a matrix of (column) solution vectors for linear systems with
@ -274,9 +278,9 @@ public interface RealMatrix {
* @param b matrix of constant vectors forming RHS of linear systems to
* to solve
* @return matrix of solution vectors
* @throws IllegalArgumentException if rowDimension != row dimension of b
* or this is not square or singular
* @throws IllegalArgumentException if this.rowDimension != row dimension
* @throws InvalidMatrixException if this matrix is square or singular
*/
RealMatrix solve(RealMatrix b) throws IllegalArgumentException;
RealMatrix solve(RealMatrix b) throws IllegalArgumentException, InvalidMatrixException;
}

File diff suppressed because it is too large Load Diff

View File

@ -60,7 +60,7 @@ import junit.framework.TestSuite;
/**
* Test cases for the {@link RealMatrixImpl} class.
*
* @version $Revision: 1.8 $ $Date: 2003/11/23 20:34:41 $
* @version $Revision: 1.9 $ $Date: 2004/01/28 20:15:03 $
*/
public final class RealMatrixImplTest extends TestCase {
@ -256,13 +256,13 @@ public final class RealMatrixImplTest extends TestCase {
RealMatrix bs = new RealMatrixImpl(bigSingular);
try {
RealMatrix a = bs.solve(bs);
fail("Expecting illegalArgumentException");
} catch (IllegalArgumentException ex) {
fail("Expecting InvalidMatrixException");
} catch (InvalidMatrixException ex) {
;
}
try {
RealMatrix a = m.solve(bs);
fail("Expecting illegalArgumentException");
fail("Expecting IllegalArgumentException");
} catch (IllegalArgumentException ex) {
;
}
@ -274,8 +274,8 @@ public final class RealMatrixImplTest extends TestCase {
}
try {
(new RealMatrixImpl(testData2)).LUDecompose();
fail("Expecting illegalArgumentException");
} catch (IllegalArgumentException ex) {
fail("Expecting InvalidMatrixException");
} catch (InvalidMatrixException ex) {
;
}
}
@ -288,8 +288,8 @@ public final class RealMatrixImplTest extends TestCase {
assertEquals("nonsingular test",-3d,m.getDeterminant(),normTolerance);
try {
double a = new RealMatrixImpl(testData2).getDeterminant();
fail("Expecting illegalArgumentException");
} catch (IllegalArgumentException ex) {
fail("Expecting InvalidMatrixException");
} catch (InvalidMatrixException ex) {
;
}
}
@ -358,14 +358,14 @@ public final class RealMatrixImplTest extends TestCase {
assertClose("get col",m.getColumn(3),testDataCol3,entryTolerance);
try {
double[] x = m.getRow(10);
fail("expecting IllegalArgumentException");
} catch (IllegalArgumentException ex) {
fail("expecting MatrixIndexException");
} catch (MatrixIndexException ex) {
;
}
try {
double[] x = m.getColumn(-1);
fail("expecting IllegalArgumentException");
} catch (IllegalArgumentException ex) {
fail("expecting MatrixIndexException");
} catch (MatrixIndexException ex) {
;
}
}
@ -377,14 +377,14 @@ public final class RealMatrixImplTest extends TestCase {
assertEquals("get entry",m.getEntry(1,2),100d,entryTolerance);
try {
double x = m.getEntry(0,2);
fail("expecting IllegalArgumentException");
} catch (IllegalArgumentException ex) {
fail("expecting MatrixIndexException");
} catch (MatrixIndexException ex) {
;
}
try {
m.setEntry(1,4,200d);
fail("expecting IllegalArgumentException");
} catch (IllegalArgumentException ex) {
fail("expecting MatrixIndexException");
} catch (MatrixIndexException ex) {
;
}
}