001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.math3.linear;
018
019import org.apache.commons.math3.util.IterationEvent;
020import org.apache.commons.math3.exception.MathUnsupportedOperationException;
021
022/**
023 * This is the base class for all events occurring during the iterations of a
024 * {@link IterativeLinearSolver}.
025 *
026 * @since 3.0
027 */
028public abstract class IterativeLinearSolverEvent
029    extends IterationEvent {
030    /** Serialization identifier. */
031    private static final long serialVersionUID = 20120129L;
032
033    /**
034     * Creates a new instance of this class.
035     *
036     * @param source the iterative algorithm on which the event initially
037     * occurred
038     * @param iterations the number of iterations performed at the time
039     * {@code this} event is created
040     */
041    public IterativeLinearSolverEvent(final Object source, final int iterations) {
042        super(source, iterations);
043    }
044
045    /**
046     * Returns the current right-hand side of the linear system to be solved.
047     * This method should return an unmodifiable view, or a deep copy of the
048     * actual right-hand side vector, in order not to compromise subsequent
049     * iterations of the source {@link IterativeLinearSolver}.
050     *
051     * @return the right-hand side vector, b
052     */
053    public abstract RealVector getRightHandSideVector();
054
055    /**
056     * Returns the norm of the residual. The returned value is not required to
057     * be <em>exact</em>. Instead, the norm of the so-called <em>updated</em>
058     * residual (if available) should be returned. For example, the
059     * {@link ConjugateGradient conjugate gradient} method computes a sequence
060     * of residuals, the norm of which is cheap to compute. However, due to
061     * accumulation of round-off errors, this residual might differ from the
062     * true residual after some iterations. See e.g. A. Greenbaum and
063     * Z. Strakos, <em>Predicting the Behavior of Finite Precision Lanzos and
064     * Conjugate Gradient Computations</em>, Technical Report 538, Department of
065     * Computer Science, New York University, 1991 (available
066     * <a href="http://www.archive.org/details/predictingbehavi00gree">here</a>).
067     *
068     * @return the norm of the residual, ||r||
069     */
070    public abstract double getNormOfResidual();
071
072    /**
073     * <p>
074     * Returns the residual. This is an optional operation, as all iterative
075     * linear solvers do not provide cheap estimate of the updated residual
076     * vector, in which case
077     * </p>
078     * <ul>
079     * <li>this method should throw a
080     * {@link MathUnsupportedOperationException},</li>
081     * <li>{@link #providesResidual()} returns {@code false}.</li>
082     * </ul>
083     * <p>
084     * The default implementation throws a
085     * {@link MathUnsupportedOperationException}. If this method is overriden,
086     * then {@link #providesResidual()} should be overriden as well.
087     * </p>
088     *
089     * @return the updated residual, r
090     */
091    public RealVector getResidual() {
092        throw new MathUnsupportedOperationException();
093    }
094
095    /**
096     * Returns the current estimate of the solution to the linear system to be
097     * solved. This method should return an unmodifiable view, or a deep copy of
098     * the actual current solution, in order not to compromise subsequent
099     * iterations of the source {@link IterativeLinearSolver}.
100     *
101     * @return the solution, x
102     */
103    public abstract RealVector getSolution();
104
105    /**
106     * Returns {@code true} if {@link #getResidual()} is supported. The default
107     * implementation returns {@code false}.
108     *
109     * @return {@code false} if {@link #getResidual()} throws a
110     * {@link MathUnsupportedOperationException}
111     */
112    public boolean providesResidual() {
113        return false;
114    }
115}