View Javadoc
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.numbers.gamma;
19  
20  /**
21   * Encapsulate the policy for function evaluation.
22   * This is a reduced implementation of the Boost {@code boost::math::policies}
23   * functionality. No settings are preserved for the error handling policy or
24   * promotion of data types for computations.
25   * This controls the convergence criteria and maximum iterations for series evaluations.
26   *
27   * @see <a href="https://www.boost.org/doc/libs/1_77_0/libs/math/doc/html/policy.html">
28   * Policies: Controlling Precision, Error Handling etc</a>
29   */
30  final class Policy {
31      /** Default policy. The Boost default uses 2^-52 for the epsilon. This uses
32       * 2^-53 to use an extra guard digit in the Kahan series summations.
33       * The minimum value for the Commons continued fraction epsilon is also 2^-53. */
34      private static final Policy DEFAULT = new Policy(0x1.0p-53, 1000000);
35  
36      /** Epsilon value for relative error. */
37      private final double eps;
38      /** The maximum number of iterations permitted in a series evaluation. */
39      private final int maxIterations;
40  
41      /**
42       * Instantiates a new policy.
43       *
44       * @param eps the eps
45       * @param maxIterations the maximum number of iterations permitted in a series
46       * evaluation
47       */
48      Policy(double eps, int maxIterations) {
49          this.eps = eps;
50          this.maxIterations = maxIterations;
51      }
52  
53      /**
54       * Gets the default.
55       *
56       * @return the default policy
57       */
58      static Policy getDefault() {
59          return DEFAULT;
60      }
61  
62      /**
63       * Gets the epsilon value for relative error.
64       *
65       * @return the epsilon
66       */
67      double getEps() {
68          return eps;
69      }
70  
71      /**
72       * Gets the maximum number of iterations permitted in a series evaluation.
73       *
74       * @return max iterations
75       */
76      int getMaxIterations() {
77          return maxIterations;
78      }
79  }