IterativeLinearSolverEvent.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.math4.legacy.linear;

  18. import org.apache.commons.math4.legacy.exception.MathUnsupportedOperationException;

  19. /**
  20.  * This is the base class for all events occurring during the iterations of a
  21.  * {@link IterativeLinearSolver}.
  22.  *
  23.  * @since 3.0
  24.  */
  25. public abstract class IterativeLinearSolverEvent
  26.     extends IterationEvent {
  27.     /** Serialization identifier. */
  28.     private static final long serialVersionUID = 20120129L;

  29.     /**
  30.      * Creates a new instance of this class.
  31.      *
  32.      * @param source the iterative algorithm on which the event initially
  33.      * occurred
  34.      * @param iterations the number of iterations performed at the time
  35.      * {@code this} event is created
  36.      */
  37.     public IterativeLinearSolverEvent(final Object source, final int iterations) {
  38.         super(source, iterations);
  39.     }

  40.     /**
  41.      * Returns the current right-hand side of the linear system to be solved.
  42.      * This method should return an unmodifiable view, or a deep copy of the
  43.      * actual right-hand side vector, in order not to compromise subsequent
  44.      * iterations of the source {@link IterativeLinearSolver}.
  45.      *
  46.      * @return the right-hand side vector, b
  47.      */
  48.     public abstract RealVector getRightHandSideVector();

  49.     /**
  50.      * Returns the norm of the residual. The returned value is not required to
  51.      * be <em>exact</em>. Instead, the norm of the so-called <em>updated</em>
  52.      * residual (if available) should be returned. For example, the
  53.      * {@link ConjugateGradient conjugate gradient} method computes a sequence
  54.      * of residuals, the norm of which is cheap to compute. However, due to
  55.      * accumulation of round-off errors, this residual might differ from the
  56.      * true residual after some iterations. See e.g. A. Greenbaum and
  57.      * Z. Strakos, <em>Predicting the Behavior of Finite Precision Lanzos and
  58.      * Conjugate Gradient Computations</em>, Technical Report 538, Department of
  59.      * Computer Science, New York University, 1991 (available
  60.      * <a href="http://www.archive.org/details/predictingbehavi00gree">here</a>).
  61.      *
  62.      * @return the norm of the residual, ||r||
  63.      */
  64.     public abstract double getNormOfResidual();

  65.     /**
  66.      * <p>
  67.      * Returns the residual. This is an optional operation, as all iterative
  68.      * linear solvers do not provide cheap estimate of the updated residual
  69.      * vector, in which case
  70.      * </p>
  71.      * <ul>
  72.      * <li>this method should throw a
  73.      * {@link MathUnsupportedOperationException},</li>
  74.      * <li>{@link #providesResidual()} returns {@code false}.</li>
  75.      * </ul>
  76.      * <p>
  77.      * The default implementation throws a
  78.      * {@link MathUnsupportedOperationException}. If this method is overridden,
  79.      * then {@link #providesResidual()} should be overridden as well.
  80.      * </p>
  81.      *
  82.      * @return the updated residual, r
  83.      */
  84.     public RealVector getResidual() {
  85.         throw new MathUnsupportedOperationException();
  86.     }

  87.     /**
  88.      * Returns the current estimate of the solution to the linear system to be
  89.      * solved. This method should return an unmodifiable view, or a deep copy of
  90.      * the actual current solution, in order not to compromise subsequent
  91.      * iterations of the source {@link IterativeLinearSolver}.
  92.      *
  93.      * @return the solution, x
  94.      */
  95.     public abstract RealVector getSolution();

  96.     /**
  97.      * Returns {@code true} if {@link #getResidual()} is supported. The default
  98.      * implementation returns {@code false}.
  99.      *
  100.      * @return {@code false} if {@link #getResidual()} throws a
  101.      * {@link MathUnsupportedOperationException}
  102.      */
  103.     public boolean providesResidual() {
  104.         return false;
  105.     }
  106. }