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 /** 022 * Provide a default implementation for several functions useful to generic 023 * converging algorithms. 024 * 025 * @version $Revision: 786927 $ $Date: 2009-06-20 19:37:47 -0400 (Sat, 20 Jun 2009) $ 026 * @since 2.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 // Mainly for test framework. 049 /** The last iteration count. */ 050 protected int iterationCount; 051 052 /** 053 * Construct an algorithm with given iteration count and accuracy. 054 * 055 * @param defaultAbsoluteAccuracy maximum absolute error 056 * @param defaultMaximalIterationCount maximum number of iterations 057 * @throws IllegalArgumentException if f is null or the 058 * defaultAbsoluteAccuracy is not valid 059 */ 060 protected ConvergingAlgorithmImpl(final int defaultMaximalIterationCount, 061 final double defaultAbsoluteAccuracy) { 062 this.defaultAbsoluteAccuracy = defaultAbsoluteAccuracy; 063 this.defaultRelativeAccuracy = 1.0e-14; 064 this.absoluteAccuracy = defaultAbsoluteAccuracy; 065 this.relativeAccuracy = defaultRelativeAccuracy; 066 this.defaultMaximalIterationCount = defaultMaximalIterationCount; 067 this.maximalIterationCount = defaultMaximalIterationCount; 068 this.iterationCount = 0; 069 } 070 071 /** {@inheritDoc} */ 072 public int getIterationCount() { 073 return iterationCount; 074 } 075 076 /** {@inheritDoc} */ 077 public void setAbsoluteAccuracy(double accuracy) { 078 absoluteAccuracy = accuracy; 079 } 080 081 /** {@inheritDoc} */ 082 public double getAbsoluteAccuracy() { 083 return absoluteAccuracy; 084 } 085 086 /** {@inheritDoc} */ 087 public void resetAbsoluteAccuracy() { 088 absoluteAccuracy = defaultAbsoluteAccuracy; 089 } 090 091 /** {@inheritDoc} */ 092 public void setMaximalIterationCount(int count) { 093 maximalIterationCount = count; 094 } 095 096 /** {@inheritDoc} */ 097 public int getMaximalIterationCount() { 098 return maximalIterationCount; 099 } 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 }