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.  *      https://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. /*
  18.  * This is not the original file distributed by the Apache Software Foundation
  19.  * It has been modified by the Hipparchus project
  20.  */
  21. package org.hipparchus.linear;

  22. import org.hipparchus.exception.LocalizedCoreFormats;
  23. import org.hipparchus.exception.MathRuntimeException;

  24. /**
  25.  * A default concrete implementation of the abstract class
  26.  * {@link IterativeLinearSolverEvent}.
  27.  *
  28.  */
  29. public class DefaultIterativeLinearSolverEvent extends IterativeLinearSolverEvent {

  30.     /** */
  31.     private static final long serialVersionUID = 20120129L;

  32.     /** The right-hand side vector. */
  33.     private final RealVector b;

  34.     /** The current estimate of the residual. */
  35.     private final RealVector r;

  36.     /** The current estimate of the norm of the residual. */
  37.     private final double rnorm;

  38.     /** The current estimate of the solution. */
  39.     private final RealVector x;

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

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

  94.     /** {@inheritDoc} */
  95.     @Override
  96.     public double getNormOfResidual() {
  97.         return rnorm;
  98.     }

  99.     /**
  100.      * {@inheritDoc}
  101.      *
  102.      * This implementation throws a {@link MathRuntimeException}
  103.      * if no residual vector {@code r} was provided at construction time.
  104.      */
  105.     @Override
  106.     public RealVector getResidual() {
  107.         if (r != null) {
  108.             return r;
  109.         }
  110.         throw new MathRuntimeException(LocalizedCoreFormats.UNSUPPORTED_OPERATION);
  111.     }

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

  117.     /** {@inheritDoc} */
  118.     @Override
  119.     public RealVector getSolution() {
  120.         return x;
  121.     }

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