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