- Replaced LinearOperator with Operator in exception names in order to make them shorter (as discussed on the ML).

- Created IllConditionedOperatorException.
- Created SingularOperatorException (as discussed on the ML).

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1178306 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Sebastien Brisard 2011-10-03 06:33:21 +00:00
parent 2568420bf6
commit 1d0479bfb3
11 changed files with 136 additions and 47 deletions

View File

@ -110,6 +110,7 @@ public enum LocalizedFormats implements Localizable {
GCD_OVERFLOW_64_BITS("overflow: gcd({0}, {1}) is 2^63"),
HOLE_BETWEEN_MODELS_TIME_RANGES("{0} wide hole between models time ranges"),
IDENTICAL_ABSCISSAS_DIVISION_BY_ZERO("identical abscissas x[{0}] == x[{1}] == {2} cause division by zero"),
ILL_CONDITIONED_OPERATOR("condition number {1} is too high "),
INDEX_LARGER_THAN_MAX("the index specified: {0} is larger than the current maximal index {1}"),
INDEX_NOT_POSITIVE("index ({0}) is not positive"),
INDEX_OUT_OF_RANGE("index {0} out of allowed range [{1}, {2}]"),
@ -188,9 +189,9 @@ public enum LocalizedFormats implements Localizable {
NOT_POSITIVE_COLUMNDIMENSION("invalid column dimension: {0} (must be positive)"),
NOT_POSITIVE_DEFINITE_MATRIX("not positive definite matrix"), /* keep */
NON_POSITIVE_DEFINITE_MATRIX("not positive definite matrix: diagonal element at ({1},{1}) is smaller than {2} ({0})"),
NON_POSITIVE_DEFINITE_LINEAR_OPERATOR("non positive definite linear operator"), /* keep */
NON_SELF_ADJOINT_LINEAR_OPERATOR("non self-adjoint linear operator"), /* keep */
NON_SQUARE_LINEAR_OPERATOR("non square ({0}x{1}) linear operator"), /* keep */
NON_POSITIVE_DEFINITE_OPERATOR("non positive definite linear operator"), /* keep */
NON_SELF_ADJOINT_OPERATOR("non self-adjoint linear operator"), /* keep */
NON_SQUARE_OPERATOR("non square ({0}x{1}) linear operator"), /* keep */
DEGREES_OF_FREEDOM("degrees of freedom ({0})"), /* keep */
NOT_POSITIVE_DEGREES_OF_FREEDOM("degrees of freedom must be positive ({0})"),
NOT_POSITIVE_ELEMENT_AT_INDEX("element {0} is not positive: {1}"),
@ -294,6 +295,7 @@ public enum LocalizedFormats implements Localizable {
SIMPLEX_NEED_ONE_POINT("simplex must contain at least one point"),
SIMPLE_MESSAGE("{0}"),
SINGULAR_MATRIX("matrix is singular"), /* keep */
SINGULAR_OPERATOR("operator is singular"),
SUBARRAY_ENDS_AFTER_ARRAY_END("subarray ends after array end"),
TOO_LARGE_CUTOFF_SINGULAR_VALUE("cutoff singular value is {0}, should be at most {1}"),
TOO_MANY_ELEMENTS_TO_DISCARD_FROM_ARRAY("cannot discard {0} elements from a {1} elements array"),

View File

@ -47,7 +47,7 @@ import org.apache.commons.math.util.IterationManager;
* <h3><a id="context">Exception context</a></h3>
* <p>
* Besides standard {@link DimensionMismatchException}, this class might throw
* {@link NonPositiveDefiniteLinearOperatorException} if the linear operator or
* {@link NonPositiveDefiniteOperatorException} if the linear operator or
* the preconditioner are not positive definite. In this case, the
* {@link ExceptionContext} provides some more information
* <ul>
@ -166,7 +166,7 @@ public class ConjugateGradient
public RealVector solveInPlace(final RealLinearOperator a,
final InvertibleRealLinearOperator m,
final RealVector b, final RealVector x0)
throws NullArgumentException, NonSquareLinearOperatorException,
throws NullArgumentException, NonSquareOperatorException,
DimensionMismatchException, MaxCountExceededException {
checkParameters(a, m, b, x0);
final IterationManager manager = getIterationManager();
@ -219,8 +219,8 @@ public class ConjugateGradient
}
final double rhoNext = r.dotProduct(z);
if (check && (rhoNext <= 0.)) {
final NonPositiveDefiniteLinearOperatorException e;
e = new NonPositiveDefiniteLinearOperatorException();
final NonPositiveDefiniteOperatorException e;
e = new NonPositiveDefiniteOperatorException();
final ExceptionContext context = e.getContext();
context.setValue(OPERATOR, m);
context.setValue(VECTOR, r);
@ -235,8 +235,8 @@ public class ConjugateGradient
manager.incrementIterationCount();
final double pq = p.dotProduct(q);
if (check && (pq <= 0.)) {
final NonPositiveDefiniteLinearOperatorException e;
e = new NonPositiveDefiniteLinearOperatorException();
final NonPositiveDefiniteOperatorException e;
e = new NonPositiveDefiniteOperatorException();
final ExceptionContext context = e.getContext();
context.setValue(OPERATOR, a);
context.setValue(VECTOR, p);

View File

@ -0,0 +1,43 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.commons.math.exception.MathIllegalArgumentException;
import org.apache.commons.math.exception.util.LocalizedFormats;
/**
* An exception to be thrown when the condition number of a
* {@link RealLinearOperator} is too high.
*
* @version $Id$
* @since 3.0
*/
public class IllConditionedOperatorException
extends MathIllegalArgumentException {
/** Serializable version Id. */
private static final long serialVersionUID = -7883263944530490135L;
/**
* Creates a new instance of this class.
*
* @param cond An estimate of the condition number of the offending linear
* operator.
*/
public IllConditionedOperatorException(final double cond) {
super(LocalizedFormats.ILL_CONDITIONED_OPERATOR, cond);
}
}

View File

@ -67,20 +67,20 @@ public abstract class IterativeLinearSolver {
* @param b Right-hand side vector.
* @param x0 Initial guess of the solution.
* @throws NullArgumentException if one of the parameters is {@code null}.
* @throws NonSquareLinearOperatorException if {@code a} is not square.
* @throws NonSquareOperatorException if {@code a} is not square.
* @throws DimensionMismatchException if {@code b} or {@code x0} have
* dimensions inconsistent with {@code a}.
*/
protected static void checkParameters(final RealLinearOperator a,
final RealVector b,
final RealVector x0)
throws NullArgumentException, NonSquareLinearOperatorException,
throws NullArgumentException, NonSquareOperatorException,
DimensionMismatchException {
MathUtils.checkNotNull(a);
MathUtils.checkNotNull(b);
MathUtils.checkNotNull(x0);
if (a.getRowDimension() != a.getColumnDimension()) {
throw new NonSquareLinearOperatorException(a.getRowDimension(),
throw new NonSquareOperatorException(a.getRowDimension(),
a.getColumnDimension());
}
if (b.getDimension() != a.getRowDimension()) {
@ -110,7 +110,7 @@ public abstract class IterativeLinearSolver {
* @param b Right-hand side vector.
* @return A new vector containing the solution.
* @throws NullArgumentException if one of the parameters is {@code null}.
* @throws NonSquareLinearOperatorException if {@code a} is not square.
* @throws NonSquareOperatorException if {@code a} is not square.
* @throws DimensionMismatchException if {@code b} has dimensions
* inconsistent with {@code a}.
* @throws MaxCountExceededException at exhaustion of the iteration count,
@ -118,7 +118,7 @@ public abstract class IterativeLinearSolver {
* construction.
*/
public RealVector solve(RealLinearOperator a, RealVector b)
throws NullArgumentException, NonSquareLinearOperatorException,
throws NullArgumentException, NonSquareOperatorException,
DimensionMismatchException, MaxCountExceededException {
MathUtils.checkNotNull(a);
final RealVector x = new ArrayRealVector(a.getColumnDimension());
@ -135,7 +135,7 @@ public abstract class IterativeLinearSolver {
* @param x0 Initial guess of the solution.
* @return A new vector containing the solution.
* @throws NullArgumentException if one of the parameters is {@code null}.
* @throws NonSquareLinearOperatorException if {@code a} is not square.
* @throws NonSquareOperatorException if {@code a} is not square.
* @throws DimensionMismatchException if {@code b} or {@code x0} have
* dimensions inconsistent with {@code a}.
* @throws MaxCountExceededException at exhaustion of the iteration count,
@ -143,7 +143,7 @@ public abstract class IterativeLinearSolver {
* construction.
*/
public RealVector solve(RealLinearOperator a, RealVector b, RealVector x0)
throws NullArgumentException, NonSquareLinearOperatorException,
throws NullArgumentException, NonSquareOperatorException,
DimensionMismatchException, MaxCountExceededException {
MathUtils.checkNotNull(x0);
return solveInPlace(a, b, x0.copy());
@ -159,7 +159,7 @@ public abstract class IterativeLinearSolver {
* @return A reference to {@code x0} (shallow copy) updated with the
* solution.
* @throws NullArgumentException if one of the parameters is {@code null}.
* @throws NonSquareLinearOperatorException if {@code a} is not square.
* @throws NonSquareOperatorException if {@code a} is not square.
* @throws DimensionMismatchException if {@code b} or {@code x0} have
* dimensions inconsistent with {@code a}.
* @throws MaxCountExceededException at exhaustion of the iteration count,
@ -168,6 +168,6 @@ public abstract class IterativeLinearSolver {
*/
public abstract RealVector solveInPlace(RealLinearOperator a, RealVector b,
RealVector x0)
throws NullArgumentException, NonSquareLinearOperatorException,
throws NullArgumentException, NonSquareOperatorException,
DimensionMismatchException, MaxCountExceededException;
}

View File

@ -50,13 +50,13 @@ public class JacobiPreconditioner
* @param a Linear operator for which the preconditioner should be built.
* @return Preconditioner made of the diagonal coefficients of the specified
* linear operator.
* @throws NonSquareLinearOperatorException if {@code a} is not square.
* @throws NonSquareOperatorException if {@code a} is not square.
*/
public static JacobiPreconditioner create(final RealLinearOperator a)
throws NonSquareLinearOperatorException {
throws NonSquareOperatorException {
final int n = a.getColumnDimension();
if (a.getRowDimension() != n) {
throw new NonSquareLinearOperatorException(a.getRowDimension(), n);
throw new NonSquareOperatorException(a.getRowDimension(), n);
}
final double[] diag = new double[n];
if (a instanceof AbstractRealMatrix) {

View File

@ -32,10 +32,13 @@ import org.apache.commons.math.exception.util.LocalizedFormats;
* @version $Id$
* @since 3.0
*/
public class NonPositiveDefiniteLinearOperatorException
public class NonPositiveDefiniteOperatorException
extends MathIllegalArgumentException {
/** Serializable version Id. */
private static final long serialVersionUID = 917034489420549847L;
/** Creates a new instance of this class. */
public NonPositiveDefiniteLinearOperatorException() {
super(LocalizedFormats.NON_POSITIVE_DEFINITE_LINEAR_OPERATOR);
public NonPositiveDefiniteOperatorException() {
super(LocalizedFormats.NON_POSITIVE_DEFINITE_OPERATOR);
}
}

View File

@ -35,11 +35,13 @@ import org.apache.commons.math.exception.util.LocalizedFormats;
* @version $Id$
* @since 3.0
*/
public class NonSelfAdjointLinearOperatorException
public class NonSelfAdjointOperatorException
extends MathIllegalArgumentException {
/** Serializable version Id. */
private static final long serialVersionUID = 1784999305030258247L;
/** Creates a new instance of this class. */
public NonSelfAdjointLinearOperatorException() {
super(LocalizedFormats.NON_SELF_ADJOINT_LINEAR_OPERATOR);
public NonSelfAdjointOperatorException() {
super(LocalizedFormats.NON_SELF_ADJOINT_OPERATOR);
}
}

View File

@ -25,9 +25,9 @@ import org.apache.commons.math.exception.util.LocalizedFormats;
* @since 3.0
* @version $Id$
*/
public class NonSquareLinearOperatorException extends DimensionMismatchException {
public class NonSquareOperatorException extends DimensionMismatchException {
/** Serializable version Id. */
private static final long serialVersionUID = -660069396594485772L;
private static final long serialVersionUID = -4145007524150846242L;
/**
* Construct an exception from the mismatched dimensions.
@ -35,7 +35,7 @@ public class NonSquareLinearOperatorException extends DimensionMismatchException
* @param wrong Row dimension.
* @param expected Column dimension.
*/
public NonSquareLinearOperatorException(int wrong, int expected) {
super(LocalizedFormats.NON_SQUARE_LINEAR_OPERATOR, wrong, expected);
public NonSquareOperatorException(int wrong, int expected) {
super(LocalizedFormats.NON_SQUARE_OPERATOR, wrong, expected);
}
}

View File

@ -66,7 +66,7 @@ public abstract class PreconditionedIterativeLinearSolver
* @param x0 Initial guess of the solution.
* @return A new vector containing the solution.
* @throws NullArgumentException if one of the parameters is {@code null}.
* @throws NonSquareLinearOperatorException if {@code a} or {@code m} is not
* @throws NonSquareOperatorException if {@code a} or {@code m} is not
* square.
* @throws DimensionMismatchException if {@code m}, {@code b} or {@code x0}
* have dimensions inconsistent with {@code a}.
@ -77,7 +77,7 @@ public abstract class PreconditionedIterativeLinearSolver
public RealVector solve(final RealLinearOperator a,
final InvertibleRealLinearOperator m,
final RealVector b, final RealVector x0)
throws NullArgumentException, NonSquareLinearOperatorException,
throws NullArgumentException, NonSquareOperatorException,
DimensionMismatchException, MaxCountExceededException {
MathUtils.checkNotNull(x0);
return solveInPlace(a, m, b, x0.copy());
@ -86,7 +86,7 @@ public abstract class PreconditionedIterativeLinearSolver
/** {@inheritDoc} */
@Override
public RealVector solve(final RealLinearOperator a, final RealVector b)
throws NullArgumentException, NonSquareLinearOperatorException,
throws NullArgumentException, NonSquareOperatorException,
DimensionMismatchException, MaxCountExceededException {
MathUtils.checkNotNull(a);
final RealVector x = new ArrayRealVector(a.getColumnDimension());
@ -98,7 +98,7 @@ public abstract class PreconditionedIterativeLinearSolver
@Override
public RealVector solve(final RealLinearOperator a, final RealVector b,
final RealVector x0)
throws NullArgumentException, NonSquareLinearOperatorException,
throws NullArgumentException, NonSquareOperatorException,
DimensionMismatchException, MaxCountExceededException {
MathUtils.checkNotNull(x0);
return solveInPlace(a, null, b, x0.copy());
@ -116,7 +116,7 @@ public abstract class PreconditionedIterativeLinearSolver
* @param b Right-hand side vector.
* @param x0 Initial guess of the solution.
* @throws NullArgumentException if one of the parameters is {@code null}.
* @throws NonSquareLinearOperatorException if {@code a} or {@code m} is not
* @throws NonSquareOperatorException if {@code a} or {@code m} is not
* square.
* @throws DimensionMismatchException if {@code m}, {@code b} or {@code x0}
* have dimensions inconsistent with {@code a}.
@ -125,12 +125,12 @@ public abstract class PreconditionedIterativeLinearSolver
final InvertibleRealLinearOperator m,
final RealVector b,
final RealVector x0)
throws NullArgumentException, NonSquareLinearOperatorException,
throws NullArgumentException, NonSquareOperatorException,
DimensionMismatchException {
checkParameters(a, b, x0);
if (m != null) {
if (m.getColumnDimension() != m.getRowDimension()) {
throw new NonSquareLinearOperatorException(m.getColumnDimension(),
throw new NonSquareOperatorException(m.getColumnDimension(),
m.getRowDimension());
}
if (m.getRowDimension() != a.getRowDimension()) {
@ -149,7 +149,7 @@ public abstract class PreconditionedIterativeLinearSolver
* @param b Right-hand side vector.
* @return A new vector containing the solution.
* @throws NullArgumentException if one of the parameters is {@code null}.
* @throws NonSquareLinearOperatorException if {@code a} or {@code m} is not
* @throws NonSquareOperatorException if {@code a} or {@code m} is not
* square.
* @throws DimensionMismatchException if {@code m} or {@code b} have
* dimensions inconsistent with {@code a}.
@ -159,7 +159,7 @@ public abstract class PreconditionedIterativeLinearSolver
*/
public RealVector solve(RealLinearOperator a,
InvertibleRealLinearOperator m, RealVector b)
throws NullArgumentException, NonSquareLinearOperatorException,
throws NullArgumentException, NonSquareOperatorException,
DimensionMismatchException, MaxCountExceededException {
MathUtils.checkNotNull(a);
final RealVector x = new ArrayRealVector(a.getColumnDimension());
@ -177,7 +177,7 @@ public abstract class PreconditionedIterativeLinearSolver
* @return A reference to {@code x0} (shallow copy) updated with the
* solution.
* @throws NullArgumentException if one of the parameters is {@code null}.
* @throws NonSquareLinearOperatorException if {@code a} or {@code m} is not
* @throws NonSquareOperatorException if {@code a} or {@code m} is not
* square.
* @throws DimensionMismatchException if {@code m}, {@code b} or {@code x0}
* have dimensions inconsistent with {@code a}.
@ -188,14 +188,14 @@ public abstract class PreconditionedIterativeLinearSolver
public abstract RealVector solveInPlace(RealLinearOperator a,
InvertibleRealLinearOperator m,
RealVector b, RealVector x0)
throws NullArgumentException, NonSquareLinearOperatorException,
throws NullArgumentException, NonSquareOperatorException,
DimensionMismatchException, MaxCountExceededException;
/** {@inheritDoc} */
@Override
public RealVector solveInPlace(final RealLinearOperator a,
final RealVector b, final RealVector x0)
throws NullArgumentException, NonSquareLinearOperatorException,
throws NullArgumentException, NonSquareOperatorException,
DimensionMismatchException, MaxCountExceededException {
return solveInPlace(a, null, b, x0);
}

View File

@ -0,0 +1,39 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.commons.math.exception.MathIllegalArgumentException;
import org.apache.commons.math.exception.util.LocalizedFormats;
/**
* Exception to be thrown when trying to invert a singular operator.
*
* @version $Id$
* @since 3.0
*/
public class SingularOperatorException
extends MathIllegalArgumentException {
/** Serializable version Id. */
private static final long serialVersionUID = -476049978595245033L;
/**
* Creates a new instance of this class.
*/
public SingularOperatorException() {
super(LocalizedFormats.SINGULAR_OPERATOR);
}
}

View File

@ -25,7 +25,7 @@ import org.junit.Test;
public class ConjugateGradientTest {
@Test(expected = NonSquareLinearOperatorException.class)
@Test(expected = NonSquareOperatorException.class)
public void testNonSquareOperator() {
final Array2DRowRealMatrix a = new Array2DRowRealMatrix(2, 3);
final IterativeLinearSolver solver;
@ -55,7 +55,7 @@ public class ConjugateGradientTest {
solver.solve(a, b, x);
}
@Test(expected = NonPositiveDefiniteLinearOperatorException.class)
@Test(expected = NonPositiveDefiniteOperatorException.class)
public void testNonPositiveDefiniteLinearOperator() {
final Array2DRowRealMatrix a = new Array2DRowRealMatrix(2, 2);
a.setEntry(0, 0, -1.);
@ -199,7 +199,7 @@ public class ConjugateGradientTest {
}
}
@Test(expected = NonSquareLinearOperatorException.class)
@Test(expected = NonSquareOperatorException.class)
public void testNonSquarePreconditioner() {
final Array2DRowRealMatrix a = new Array2DRowRealMatrix(2, 2);
final InvertibleRealLinearOperator m;
@ -263,7 +263,7 @@ public class ConjugateGradientTest {
solver.solve(a, m, b);
}
@Test(expected = NonPositiveDefiniteLinearOperatorException.class)
@Test(expected = NonPositiveDefiniteOperatorException.class)
public void testNonPositiveDefinitePreconditioner() {
final Array2DRowRealMatrix a = new Array2DRowRealMatrix(2, 2);
a.setEntry(0, 0, 1d);