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
18 package org.apache.commons.math;
19
20
21 /**
22 * Provide a default implementation for several functions useful to generic
23 * converging algorithms.
24 *
25 * @version $Revision: 786927 $ $Date: 2009-06-20 19:37:47 -0400 (Sat, 20 Jun 2009) $
26 * @since 2.0
27 */
28 public abstract class ConvergingAlgorithmImpl implements ConvergingAlgorithm{
29
30 /** Maximum absolute error. */
31 protected double absoluteAccuracy;
32
33 /** Maximum relative error. */
34 protected double relativeAccuracy;
35
36 /** Maximum number of iterations. */
37 protected int maximalIterationCount;
38
39 /** Default maximum absolute error. */
40 protected double defaultAbsoluteAccuracy;
41
42 /** Default maximum relative error. */
43 protected double defaultRelativeAccuracy;
44
45 /** Default maximum number of iterations. */
46 protected int defaultMaximalIterationCount;
47
48 // Mainly for test framework.
49 /** The last iteration count. */
50 protected int iterationCount;
51
52 /**
53 * Construct an algorithm with given iteration count and accuracy.
54 *
55 * @param defaultAbsoluteAccuracy maximum absolute error
56 * @param defaultMaximalIterationCount maximum number of iterations
57 * @throws IllegalArgumentException if f is null or the
58 * defaultAbsoluteAccuracy is not valid
59 */
60 protected ConvergingAlgorithmImpl(final int defaultMaximalIterationCount,
61 final double defaultAbsoluteAccuracy) {
62 this.defaultAbsoluteAccuracy = defaultAbsoluteAccuracy;
63 this.defaultRelativeAccuracy = 1.0e-14;
64 this.absoluteAccuracy = defaultAbsoluteAccuracy;
65 this.relativeAccuracy = defaultRelativeAccuracy;
66 this.defaultMaximalIterationCount = defaultMaximalIterationCount;
67 this.maximalIterationCount = defaultMaximalIterationCount;
68 this.iterationCount = 0;
69 }
70
71 /** {@inheritDoc} */
72 public int getIterationCount() {
73 return iterationCount;
74 }
75
76 /** {@inheritDoc} */
77 public void setAbsoluteAccuracy(double accuracy) {
78 absoluteAccuracy = accuracy;
79 }
80
81 /** {@inheritDoc} */
82 public double getAbsoluteAccuracy() {
83 return absoluteAccuracy;
84 }
85
86 /** {@inheritDoc} */
87 public void resetAbsoluteAccuracy() {
88 absoluteAccuracy = defaultAbsoluteAccuracy;
89 }
90
91 /** {@inheritDoc} */
92 public void setMaximalIterationCount(int count) {
93 maximalIterationCount = count;
94 }
95
96 /** {@inheritDoc} */
97 public int getMaximalIterationCount() {
98 return maximalIterationCount;
99 }
100
101 /** {@inheritDoc} */
102 public void resetMaximalIterationCount() {
103 maximalIterationCount = defaultMaximalIterationCount;
104 }
105
106 /** {@inheritDoc} */
107 public void setRelativeAccuracy(double accuracy) {
108 relativeAccuracy = accuracy;
109 }
110
111 /** {@inheritDoc} */
112 public double getRelativeAccuracy() {
113 return relativeAccuracy;
114 }
115
116 /** {@inheritDoc} */
117 public void resetRelativeAccuracy() {
118 relativeAccuracy = defaultRelativeAccuracy;
119 }
120
121 }