Home | History | Annotate | Download | only in math
      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  * Provide a default implementation for several functions useful to generic
     22  * converging algorithms.
     23  *
     24  * @version $Revision: 1062691 $ $Date: 2011-01-24 10:12:47 +0100 (lun. 24 janv. 2011) $
     25  * @since 2.0
     26  * @deprecated in 2.2 (to be removed in 3.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     /** The last iteration count. */
     49     protected int iterationCount;
     50 
     51     /**
     52      * Construct an algorithm with given iteration count and accuracy.
     53      *
     54      * @param defaultAbsoluteAccuracy maximum absolute error
     55      * @param defaultMaximalIterationCount maximum number of iterations
     56      * @throws IllegalArgumentException if f is null or the
     57      * defaultAbsoluteAccuracy is not valid
     58      * @deprecated in 2.2. Derived classes should use the "setter" methods
     59      * in order to assign meaningful values to all the instances variables.
     60      */
     61     @Deprecated
     62     protected ConvergingAlgorithmImpl(final int defaultMaximalIterationCount,
     63                                       final double defaultAbsoluteAccuracy) {
     64         this.defaultAbsoluteAccuracy = defaultAbsoluteAccuracy;
     65         this.defaultRelativeAccuracy = 1.0e-14;
     66         this.absoluteAccuracy = defaultAbsoluteAccuracy;
     67         this.relativeAccuracy = defaultRelativeAccuracy;
     68         this.defaultMaximalIterationCount = defaultMaximalIterationCount;
     69         this.maximalIterationCount = defaultMaximalIterationCount;
     70         this.iterationCount = 0;
     71     }
     72 
     73     /**
     74      * Default constructor.
     75      *
     76      * @since 2.2
     77      * @deprecated in 2.2 (to be removed as soon as the single non-default one
     78      * has been removed).
     79      */
     80     @Deprecated
     81     protected ConvergingAlgorithmImpl() {}
     82 
     83     /** {@inheritDoc} */
     84     public int getIterationCount() {
     85         return iterationCount;
     86     }
     87 
     88     /** {@inheritDoc} */
     89     public void setAbsoluteAccuracy(double accuracy) {
     90         absoluteAccuracy = accuracy;
     91     }
     92 
     93     /** {@inheritDoc} */
     94     public double getAbsoluteAccuracy() {
     95         return absoluteAccuracy;
     96     }
     97 
     98     /** {@inheritDoc} */
     99     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 }
    156