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
018 package org.apache.commons.math;
019
020 /**
021 * Provide a default implementation for several functions useful to generic
022 * converging algorithms.
023 *
024 * @version $Revision: 1062691 $ $Date: 2011-01-24 10:12:47 +0100 (lun. 24 janv. 2011) $
025 * @since 2.0
026 * @deprecated in 2.2 (to be removed in 3.0).
027 */
028 public abstract class ConvergingAlgorithmImpl implements ConvergingAlgorithm {
029
030 /** Maximum absolute error. */
031 protected double absoluteAccuracy;
032
033 /** Maximum relative error. */
034 protected double relativeAccuracy;
035
036 /** Maximum number of iterations. */
037 protected int maximalIterationCount;
038
039 /** Default maximum absolute error. */
040 protected double defaultAbsoluteAccuracy;
041
042 /** Default maximum relative error. */
043 protected double defaultRelativeAccuracy;
044
045 /** Default maximum number of iterations. */
046 protected int defaultMaximalIterationCount;
047
048 /** The last iteration count. */
049 protected int iterationCount;
050
051 /**
052 * Construct an algorithm with given iteration count and accuracy.
053 *
054 * @param defaultAbsoluteAccuracy maximum absolute error
055 * @param defaultMaximalIterationCount maximum number of iterations
056 * @throws IllegalArgumentException if f is null or the
057 * defaultAbsoluteAccuracy is not valid
058 * @deprecated in 2.2. Derived classes should use the "setter" methods
059 * in order to assign meaningful values to all the instances variables.
060 */
061 @Deprecated
062 protected ConvergingAlgorithmImpl(final int defaultMaximalIterationCount,
063 final double defaultAbsoluteAccuracy) {
064 this.defaultAbsoluteAccuracy = defaultAbsoluteAccuracy;
065 this.defaultRelativeAccuracy = 1.0e-14;
066 this.absoluteAccuracy = defaultAbsoluteAccuracy;
067 this.relativeAccuracy = defaultRelativeAccuracy;
068 this.defaultMaximalIterationCount = defaultMaximalIterationCount;
069 this.maximalIterationCount = defaultMaximalIterationCount;
070 this.iterationCount = 0;
071 }
072
073 /**
074 * Default constructor.
075 *
076 * @since 2.2
077 * @deprecated in 2.2 (to be removed as soon as the single non-default one
078 * has been removed).
079 */
080 @Deprecated
081 protected ConvergingAlgorithmImpl() {}
082
083 /** {@inheritDoc} */
084 public int getIterationCount() {
085 return iterationCount;
086 }
087
088 /** {@inheritDoc} */
089 public void setAbsoluteAccuracy(double accuracy) {
090 absoluteAccuracy = accuracy;
091 }
092
093 /** {@inheritDoc} */
094 public double getAbsoluteAccuracy() {
095 return absoluteAccuracy;
096 }
097
098 /** {@inheritDoc} */
099 public void resetAbsoluteAccuracy() {
100 absoluteAccuracy = defaultAbsoluteAccuracy;
101 }
102
103 /** {@inheritDoc} */
104 public void setMaximalIterationCount(int count) {
105 maximalIterationCount = count;
106 }
107
108 /** {@inheritDoc} */
109 public int getMaximalIterationCount() {
110 return maximalIterationCount;
111 }
112
113 /** {@inheritDoc} */
114 public void resetMaximalIterationCount() {
115 maximalIterationCount = defaultMaximalIterationCount;
116 }
117
118 /** {@inheritDoc} */
119 public void setRelativeAccuracy(double accuracy) {
120 relativeAccuracy = accuracy;
121 }
122
123 /** {@inheritDoc} */
124 public double getRelativeAccuracy() {
125 return relativeAccuracy;
126 }
127
128 /** {@inheritDoc} */
129 public void resetRelativeAccuracy() {
130 relativeAccuracy = defaultRelativeAccuracy;
131 }
132
133 /**
134 * Reset the iterations counter to 0.
135 *
136 * @since 2.2
137 */
138 protected void resetIterationsCounter() {
139 iterationCount = 0;
140 }
141
142 /**
143 * Increment the iterations counter by 1.
144 *
145 * @throws MaxIterationsExceededException if the maximal number
146 * of iterations is exceeded.
147 * @since 2.2
148 */
149 protected void incrementIterationsCounter()
150 throws MaxIterationsExceededException {
151 if (++iterationCount > maximalIterationCount) {
152 throw new MaxIterationsExceededException(maximalIterationCount);
153 }
154 }
155 }