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.math4.legacy.linear;
018
019import org.apache.commons.math4.legacy.exception.MathUnsupportedOperationException;
020
021/**
022 * This is the base class for all events occurring during the iterations of a
023 * {@link IterativeLinearSolver}.
024 *
025 * @since 3.0
026 */
027public abstract class IterativeLinearSolverEvent
028    extends IterationEvent {
029    /** Serialization identifier. */
030    private static final long serialVersionUID = 20120129L;
031
032    /**
033     * Creates a new instance of this class.
034     *
035     * @param source the iterative algorithm on which the event initially
036     * occurred
037     * @param iterations the number of iterations performed at the time
038     * {@code this} event is created
039     */
040    public IterativeLinearSolverEvent(final Object source, final int iterations) {
041        super(source, iterations);
042    }
043
044    /**
045     * Returns the current right-hand side of the linear system to be solved.
046     * This method should return an unmodifiable view, or a deep copy of the
047     * actual right-hand side vector, in order not to compromise subsequent
048     * iterations of the source {@link IterativeLinearSolver}.
049     *
050     * @return the right-hand side vector, b
051     */
052    public abstract RealVector getRightHandSideVector();
053
054    /**
055     * Returns the norm of the residual. The returned value is not required to
056     * be <em>exact</em>. Instead, the norm of the so-called <em>updated</em>
057     * residual (if available) should be returned. For example, the
058     * {@link ConjugateGradient conjugate gradient} method computes a sequence
059     * of residuals, the norm of which is cheap to compute. However, due to
060     * accumulation of round-off errors, this residual might differ from the
061     * true residual after some iterations. See e.g. A. Greenbaum and
062     * Z. Strakos, <em>Predicting the Behavior of Finite Precision Lanzos and
063     * Conjugate Gradient Computations</em>, Technical Report 538, Department of
064     * Computer Science, New York University, 1991 (available
065     * <a href="http://www.archive.org/details/predictingbehavi00gree">here</a>).
066     *
067     * @return the norm of the residual, ||r||
068     */
069    public abstract double getNormOfResidual();
070
071    /**
072     * <p>
073     * Returns the residual. This is an optional operation, as all iterative
074     * linear solvers do not provide cheap estimate of the updated residual
075     * vector, in which case
076     * </p>
077     * <ul>
078     * <li>this method should throw a
079     * {@link MathUnsupportedOperationException},</li>
080     * <li>{@link #providesResidual()} returns {@code false}.</li>
081     * </ul>
082     * <p>
083     * The default implementation throws a
084     * {@link MathUnsupportedOperationException}. If this method is overridden,
085     * then {@link #providesResidual()} should be overridden as well.
086     * </p>
087     *
088     * @return the updated residual, r
089     */
090    public RealVector getResidual() {
091        throw new MathUnsupportedOperationException();
092    }
093
094    /**
095     * Returns the current estimate of the solution to the linear system to be
096     * solved. This method should return an unmodifiable view, or a deep copy of
097     * the actual current solution, in order not to compromise subsequent
098     * iterations of the source {@link IterativeLinearSolver}.
099     *
100     * @return the solution, x
101     */
102    public abstract RealVector getSolution();
103
104    /**
105     * Returns {@code true} if {@link #getResidual()} is supported. The default
106     * implementation returns {@code false}.
107     *
108     * @return {@code false} if {@link #getResidual()} throws a
109     * {@link MathUnsupportedOperationException}
110     */
111    public boolean providesResidual() {
112        return false;
113    }
114}