DefaultIterativeLinearSolverEvent.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.  * A default concrete implementation of the abstract class
  21.  * {@link IterativeLinearSolverEvent}.
  22.  *
  23.  */
  24. public class DefaultIterativeLinearSolverEvent extends IterativeLinearSolverEvent {

  25.     /** */
  26.     private static final long serialVersionUID = 20120129L;

  27.     /** The right-hand side vector. */
  28.     private final RealVector b;

  29.     /** The current estimate of the residual. */
  30.     private final RealVector r;

  31.     /** The current estimate of the norm of the residual. */
  32.     private final double rnorm;

  33.     /** The current estimate of the solution. */
  34.     private final RealVector x;

  35.     /**
  36.      * Creates a new instance of this class. This implementation does
  37.      * <em>not</em> deep copy the specified vectors {@code x}, {@code b},
  38.      * {@code r}. Therefore the user must make sure that these vectors are
  39.      * either unmodifiable views or deep copies of the same vectors actually
  40.      * used by the {@code source}. Failure to do so may compromise subsequent
  41.      * iterations of the {@code source}. If the residual vector {@code r} is
  42.      * {@code null}, then {@link #getResidual()} throws a
  43.      * {@link MathUnsupportedOperationException}, and
  44.      * {@link #providesResidual()} returns {@code false}.
  45.      *
  46.      * @param source the iterative solver which fired this event
  47.      * @param iterations the number of iterations performed at the time
  48.      * {@code this} event is created
  49.      * @param x the current estimate of the solution
  50.      * @param b the right-hand side vector
  51.      * @param r the current estimate of the residual (can be {@code null})
  52.      * @param rnorm the norm of the current estimate of the residual
  53.      */
  54.     public DefaultIterativeLinearSolverEvent(final Object source, final int iterations,
  55.         final RealVector x, final RealVector b, final RealVector r,
  56.         final double rnorm) {
  57.         super(source, iterations);
  58.         this.x = x;
  59.         this.b = b;
  60.         this.r = r;
  61.         this.rnorm = rnorm;
  62.     }

  63.     /**
  64.      * Creates a new instance of this class. This implementation does
  65.      * <em>not</em> deep copy the specified vectors {@code x}, {@code b}.
  66.      * Therefore the user must make sure that these vectors are either
  67.      * unmodifiable views or deep copies of the same vectors actually used by
  68.      * the {@code source}. Failure to do so may compromise subsequent iterations
  69.      * of the {@code source}. Calling {@link #getResidual()} on instances
  70.      * returned by this constructor throws a
  71.      * {@link MathUnsupportedOperationException}, while
  72.      * {@link #providesResidual()} returns {@code false}.
  73.      *
  74.      * @param source the iterative solver which fired this event
  75.      * @param iterations the number of iterations performed at the time
  76.      * {@code this} event is created
  77.      * @param x the current estimate of the solution
  78.      * @param b the right-hand side vector
  79.      * @param rnorm the norm of the current estimate of the residual
  80.      */
  81.     public DefaultIterativeLinearSolverEvent(final Object source, final int iterations,
  82.         final RealVector x, final RealVector b, final double rnorm) {
  83.         super(source, iterations);
  84.         this.x = x;
  85.         this.b = b;
  86.         this.r = null;
  87.         this.rnorm = rnorm;
  88.     }

  89.     /** {@inheritDoc} */
  90.     @Override
  91.     public double getNormOfResidual() {
  92.         return rnorm;
  93.     }

  94.     /**
  95.      * {@inheritDoc}
  96.      *
  97.      * This implementation throws an {@link MathUnsupportedOperationException}
  98.      * if no residual vector {@code r} was provided at construction time.
  99.      */
  100.     @Override
  101.     public RealVector getResidual() {
  102.         if (r != null) {
  103.             return r;
  104.         }
  105.         throw new MathUnsupportedOperationException();
  106.     }

  107.     /** {@inheritDoc} */
  108.     @Override
  109.     public RealVector getRightHandSideVector() {
  110.         return b;
  111.     }

  112.     /** {@inheritDoc} */
  113.     @Override
  114.     public RealVector getSolution() {
  115.         return x;
  116.     }

  117.     /**
  118.      * {@inheritDoc}
  119.      *
  120.      * This implementation returns {@code true} if a non-{@code null} value was
  121.      * specified for the residual vector {@code r} at construction time.
  122.      *
  123.      * @return {@code true} if {@code r != null}
  124.      */
  125.     @Override
  126.     public boolean providesResidual() {
  127.         return r != null;
  128.     }
  129. }