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 19 import org.apache.commons.math4.legacy.exception.MathUnsupportedOperationException; 20 21 /** 22 * A default concrete implementation of the abstract class 23 * {@link IterativeLinearSolverEvent}. 24 * 25 */ 26 public class DefaultIterativeLinearSolverEvent extends IterativeLinearSolverEvent { 27 28 /** */ 29 private static final long serialVersionUID = 20120129L; 30 31 /** The right-hand side vector. */ 32 private final RealVector b; 33 34 /** The current estimate of the residual. */ 35 private final RealVector r; 36 37 /** The current estimate of the norm of the residual. */ 38 private final double rnorm; 39 40 /** The current estimate of the solution. */ 41 private final RealVector x; 42 43 /** 44 * Creates a new instance of this class. This implementation does 45 * <em>not</em> deep copy the specified vectors {@code x}, {@code b}, 46 * {@code r}. Therefore the user must make sure that these vectors are 47 * either unmodifiable views or deep copies of the same vectors actually 48 * used by the {@code source}. Failure to do so may compromise subsequent 49 * iterations of the {@code source}. If the residual vector {@code r} is 50 * {@code null}, then {@link #getResidual()} throws a 51 * {@link MathUnsupportedOperationException}, and 52 * {@link #providesResidual()} returns {@code false}. 53 * 54 * @param source the iterative solver which fired this event 55 * @param iterations the number of iterations performed at the time 56 * {@code this} event is created 57 * @param x the current estimate of the solution 58 * @param b the right-hand side vector 59 * @param r the current estimate of the residual (can be {@code null}) 60 * @param rnorm the norm of the current estimate of the residual 61 */ 62 public DefaultIterativeLinearSolverEvent(final Object source, final int iterations, 63 final RealVector x, final RealVector b, final RealVector r, 64 final double rnorm) { 65 super(source, iterations); 66 this.x = x; 67 this.b = b; 68 this.r = r; 69 this.rnorm = rnorm; 70 } 71 72 /** 73 * Creates a new instance of this class. This implementation does 74 * <em>not</em> deep copy the specified vectors {@code x}, {@code b}. 75 * Therefore the user must make sure that these vectors are either 76 * unmodifiable views or deep copies of the same vectors actually used by 77 * the {@code source}. Failure to do so may compromise subsequent iterations 78 * of the {@code source}. Calling {@link #getResidual()} on instances 79 * returned by this constructor throws a 80 * {@link MathUnsupportedOperationException}, while 81 * {@link #providesResidual()} returns {@code false}. 82 * 83 * @param source the iterative solver which fired this event 84 * @param iterations the number of iterations performed at the time 85 * {@code this} event is created 86 * @param x the current estimate of the solution 87 * @param b the right-hand side vector 88 * @param rnorm the norm of the current estimate of the residual 89 */ 90 public DefaultIterativeLinearSolverEvent(final Object source, final int iterations, 91 final RealVector x, final RealVector b, final double rnorm) { 92 super(source, iterations); 93 this.x = x; 94 this.b = b; 95 this.r = null; 96 this.rnorm = rnorm; 97 } 98 99 /** {@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 }