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 /*
19 * This is not the original file distributed by the Apache Software Foundation
20 * It has been modified by the Hipparchus project
21 */
22 package org.hipparchus.linear;
23
24 import org.hipparchus.exception.LocalizedCoreFormats;
25 import org.hipparchus.exception.MathRuntimeException;
26
27 /**
28 * A default concrete implementation of the abstract class
29 * {@link IterativeLinearSolverEvent}.
30 *
31 */
32 public class DefaultIterativeLinearSolverEvent extends IterativeLinearSolverEvent {
33
34 /** */
35 private static final long serialVersionUID = 20120129L;
36
37 /** The right-hand side vector. */
38 private final RealVector b;
39
40 /** The current estimate of the residual. */
41 private final RealVector r;
42
43 /** The current estimate of the norm of the residual. */
44 private final double rnorm;
45
46 /** The current estimate of the solution. */
47 private final RealVector x;
48
49 /**
50 * Creates a new instance of this class. This implementation does
51 * <em>not</em> deep copy the specified vectors {@code x}, {@code b},
52 * {@code r}. Therefore the user must make sure that these vectors are
53 * either unmodifiable views or deep copies of the same vectors actually
54 * used by the {@code source}. Failure to do so may compromise subsequent
55 * iterations of the {@code source}. If the residual vector {@code r} is
56 * {@code null}, then {@link #getResidual()} throws a
57 * {@link MathRuntimeException}, and
58 * {@link #providesResidual()} returns {@code false}.
59 *
60 * @param source the iterative solver which fired this event
61 * @param iterations the number of iterations performed at the time
62 * {@code this} event is created
63 * @param x the current estimate of the solution
64 * @param b the right-hand side vector
65 * @param r the current estimate of the residual (can be {@code null})
66 * @param rnorm the norm of the current estimate of the residual
67 */
68 public DefaultIterativeLinearSolverEvent(final Object source, final int iterations,
69 final RealVector x, final RealVector b, final RealVector r,
70 final double rnorm) {
71 super(source, iterations);
72 this.x = x;
73 this.b = b;
74 this.r = r;
75 this.rnorm = rnorm;
76 }
77
78 /**
79 * Creates a new instance of this class. This implementation does
80 * <em>not</em> deep copy the specified vectors {@code x}, {@code b}.
81 * Therefore the user must make sure that these vectors are either
82 * unmodifiable views or deep copies of the same vectors actually used by
83 * the {@code source}. Failure to do so may compromise subsequent iterations
84 * of the {@code source}. Callling {@link #getResidual()} on instances
85 * returned by this constructor throws a
86 * {@link MathRuntimeException}, while
87 * {@link #providesResidual()} returns {@code false}.
88 *
89 * @param source the iterative solver which fired this event
90 * @param iterations the number of iterations performed at the time
91 * {@code this} event is created
92 * @param x the current estimate of the solution
93 * @param b the right-hand side vector
94 * @param rnorm the norm of the current estimate of the residual
95 */
96 public DefaultIterativeLinearSolverEvent(final Object source, final int iterations,
97 final RealVector x, final RealVector b, final double rnorm) {
98 super(source, iterations);
99 this.x = x;
100 this.b = b;
101 this.r = null;
102 this.rnorm = rnorm;
103 }
104
105 /** {@inheritDoc} */
106 @Override
107 public double getNormOfResidual() {
108 return rnorm;
109 }
110
111 /**
112 * {@inheritDoc}
113 *
114 * This implementation throws a {@link MathRuntimeException}
115 * if no residual vector {@code r} was provided at construction time.
116 */
117 @Override
118 public RealVector getResidual() {
119 if (r != null) {
120 return r;
121 }
122 throw new MathRuntimeException(LocalizedCoreFormats.UNSUPPORTED_OPERATION);
123 }
124
125 /** {@inheritDoc} */
126 @Override
127 public RealVector getRightHandSideVector() {
128 return b;
129 }
130
131 /** {@inheritDoc} */
132 @Override
133 public RealVector getSolution() {
134 return x;
135 }
136
137 /**
138 * {@inheritDoc}
139 *
140 * This implementation returns {@code true} if a non-{@code null} value was
141 * specified for the residual vector {@code r} at construction time.
142 *
143 * @return {@code true} if {@code r != null}
144 */
145 @Override
146 public boolean providesResidual() {
147 return r != null;
148 }
149 }