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.exception.MathUnsupportedOperationException;
020
021/**
022 * A default concrete implementation of the abstract class
023 * {@link IterativeLinearSolverEvent}.
024 *
025 */
026public class DefaultIterativeLinearSolverEvent extends IterativeLinearSolverEvent {
027
028    /** */
029    private static final long serialVersionUID = 20120129L;
030
031    /** The right-hand side vector. */
032    private final RealVector b;
033
034    /** The current estimate of the residual. */
035    private final RealVector r;
036
037    /** The current estimate of the norm of the residual. */
038    private final double rnorm;
039
040    /** The current estimate of the solution. */
041    private final RealVector x;
042
043    /**
044     * Creates a new instance of this class. This implementation does
045     * <em>not</em> deep copy the specified vectors {@code x}, {@code b},
046     * {@code r}. Therefore the user must make sure that these vectors are
047     * either unmodifiable views or deep copies of the same vectors actually
048     * used by the {@code source}. Failure to do so may compromise subsequent
049     * iterations of the {@code source}. If the residual vector {@code r} is
050     * {@code null}, then {@link #getResidual()} throws a
051     * {@link MathUnsupportedOperationException}, and
052     * {@link #providesResidual()} returns {@code false}.
053     *
054     * @param source the iterative solver which fired this event
055     * @param iterations the number of iterations performed at the time
056     * {@code this} event is created
057     * @param x the current estimate of the solution
058     * @param b the right-hand side vector
059     * @param r the current estimate of the residual (can be {@code null})
060     * @param rnorm the norm of the current estimate of the residual
061     */
062    public DefaultIterativeLinearSolverEvent(final Object source, final int iterations,
063        final RealVector x, final RealVector b, final RealVector r,
064        final double rnorm) {
065        super(source, iterations);
066        this.x = x;
067        this.b = b;
068        this.r = r;
069        this.rnorm = rnorm;
070    }
071
072    /**
073     * Creates a new instance of this class. This implementation does
074     * <em>not</em> deep copy the specified vectors {@code x}, {@code b}.
075     * Therefore the user must make sure that these vectors are either
076     * unmodifiable views or deep copies of the same vectors actually used by
077     * the {@code source}. Failure to do so may compromise subsequent iterations
078     * of the {@code source}. Callling {@link #getResidual()} on instances
079     * returned by this constructor throws a
080     * {@link MathUnsupportedOperationException}, while
081     * {@link #providesResidual()} returns {@code false}.
082     *
083     * @param source the iterative solver which fired this event
084     * @param iterations the number of iterations performed at the time
085     * {@code this} event is created
086     * @param x the current estimate of the solution
087     * @param b the right-hand side vector
088     * @param rnorm the norm of the current estimate of the residual
089     */
090    public DefaultIterativeLinearSolverEvent(final Object source, final int iterations,
091        final RealVector x, final RealVector b, final double rnorm) {
092        super(source, iterations);
093        this.x = x;
094        this.b = b;
095        this.r = null;
096        this.rnorm = rnorm;
097    }
098
099    /** {@inheritDoc} */
100    @Override
101    public double getNormOfResidual() {
102        return rnorm;
103    }
104
105    /**
106     * {@inheritDoc}
107     *
108     * This implementation throws an {@link MathUnsupportedOperationException}
109     * if no residual vector {@code r} was provided at construction time.
110     */
111    @Override
112    public RealVector getResidual() {
113        if (r != null) {
114            return r;
115        }
116        throw new MathUnsupportedOperationException();
117    }
118
119    /** {@inheritDoc} */
120    @Override
121    public RealVector getRightHandSideVector() {
122        return b;
123    }
124
125    /** {@inheritDoc} */
126    @Override
127    public RealVector getSolution() {
128        return x;
129    }
130
131    /**
132     * {@inheritDoc}
133     *
134     * This implementation returns {@code true} if a non-{@code null} value was
135     * specified for the residual vector {@code r} at construction time.
136     *
137     * @return {@code true} if {@code r != null}
138     */
139    @Override
140    public boolean providesResidual() {
141        return r != null;
142    }
143}