source: src/main/java/agents/anac/y2019/harddealer/math3/util/ContinuedFraction.java

Last change on this file was 204, checked in by Katsuhide Fujita, 5 years ago

Fixed errors of ANAC2019 agents

  • Property svn:executable set to *
File size: 6.5 KB
Line 
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 */
17package agents.anac.y2019.harddealer.math3.util;
18
19import agents.anac.y2019.harddealer.math3.exception.ConvergenceException;
20import agents.anac.y2019.harddealer.math3.exception.MaxCountExceededException;
21import agents.anac.y2019.harddealer.math3.exception.util.LocalizedFormats;
22
23/**
24 * Provides a generic means to evaluate continued fractions. Subclasses simply
25 * provided the a and b coefficients to evaluate the continued fraction.
26 *
27 * <p>
28 * References:
29 * <ul>
30 * <li><a href="http://mathworld.wolfram.com/ContinuedFraction.html">
31 * Continued Fraction</a></li>
32 * </ul>
33 * </p>
34 *
35 */
36public abstract class ContinuedFraction {
37 /** Maximum allowed numerical error. */
38 private static final double DEFAULT_EPSILON = 10e-9;
39
40 /**
41 * Default constructor.
42 */
43 protected ContinuedFraction() {
44 super();
45 }
46
47 /**
48 * Access the n-th a coefficient of the continued fraction. Since a can be
49 * a function of the evaluation point, x, that is passed in as well.
50 * @param n the coefficient index to retrieve.
51 * @param x the evaluation point.
52 * @return the n-th a coefficient.
53 */
54 protected abstract double getA(int n, double x);
55
56 /**
57 * Access the n-th b coefficient of the continued fraction. Since b can be
58 * a function of the evaluation point, x, that is passed in as well.
59 * @param n the coefficient index to retrieve.
60 * @param x the evaluation point.
61 * @return the n-th b coefficient.
62 */
63 protected abstract double getB(int n, double x);
64
65 /**
66 * Evaluates the continued fraction at the value x.
67 * @param x the evaluation point.
68 * @return the value of the continued fraction evaluated at x.
69 * @throws ConvergenceException if the algorithm fails to converge.
70 */
71 public double evaluate(double x) throws ConvergenceException {
72 return evaluate(x, DEFAULT_EPSILON, Integer.MAX_VALUE);
73 }
74
75 /**
76 * Evaluates the continued fraction at the value x.
77 * @param x the evaluation point.
78 * @param epsilon maximum error allowed.
79 * @return the value of the continued fraction evaluated at x.
80 * @throws ConvergenceException if the algorithm fails to converge.
81 */
82 public double evaluate(double x, double epsilon) throws ConvergenceException {
83 return evaluate(x, epsilon, Integer.MAX_VALUE);
84 }
85
86 /**
87 * Evaluates the continued fraction at the value x.
88 * @param x the evaluation point.
89 * @param maxIterations maximum number of convergents
90 * @return the value of the continued fraction evaluated at x.
91 * @throws ConvergenceException if the algorithm fails to converge.
92 * @throws MaxCountExceededException if maximal number of iterations is reached
93 */
94 public double evaluate(double x, int maxIterations)
95 throws ConvergenceException, MaxCountExceededException {
96 return evaluate(x, DEFAULT_EPSILON, maxIterations);
97 }
98
99 /**
100 * Evaluates the continued fraction at the value x.
101 * <p>
102 * The implementation of this method is based on the modified Lentz algorithm as described
103 * on page 18 ff. in:
104 * <ul>
105 * <li>
106 * I. J. Thompson, A. R. Barnett. "Coulomb and Bessel Functions of Complex Arguments and Order."
107 * <a target="_blank" href="http://www.fresco.org.uk/papers/Thompson-JCP64p490.pdf">
108 * http://www.fresco.org.uk/papers/Thompson-JCP64p490.pdf</a>
109 * </li>
110 * </ul>
111 * <b>Note:</b> the implementation uses the terms a<sub>i</sub> and b<sub>i</sub> as defined in
112 * <a href="http://mathworld.wolfram.com/ContinuedFraction.html">Continued Fraction @ MathWorld</a>.
113 * </p>
114 *
115 * @param x the evaluation point.
116 * @param epsilon maximum error allowed.
117 * @param maxIterations maximum number of convergents
118 * @return the value of the continued fraction evaluated at x.
119 * @throws ConvergenceException if the algorithm fails to converge.
120 * @throws MaxCountExceededException if maximal number of iterations is reached
121 */
122 public double evaluate(double x, double epsilon, int maxIterations)
123 throws ConvergenceException, MaxCountExceededException {
124 final double small = 1e-50;
125 double hPrev = getA(0, x);
126
127 // use the value of small as epsilon criteria for zero checks
128 if (Precision.equals(hPrev, 0.0, small)) {
129 hPrev = small;
130 }
131
132 int n = 1;
133 double dPrev = 0.0;
134 double cPrev = hPrev;
135 double hN = hPrev;
136
137 while (n < maxIterations) {
138 final double a = getA(n, x);
139 final double b = getB(n, x);
140
141 double dN = a + b * dPrev;
142 if (Precision.equals(dN, 0.0, small)) {
143 dN = small;
144 }
145 double cN = a + b / cPrev;
146 if (Precision.equals(cN, 0.0, small)) {
147 cN = small;
148 }
149
150 dN = 1 / dN;
151 final double deltaN = cN * dN;
152 hN = hPrev * deltaN;
153
154 if (Double.isInfinite(hN)) {
155 throw new ConvergenceException(LocalizedFormats.CONTINUED_FRACTION_INFINITY_DIVERGENCE,
156 x);
157 }
158 if (Double.isNaN(hN)) {
159 throw new ConvergenceException(LocalizedFormats.CONTINUED_FRACTION_NAN_DIVERGENCE,
160 x);
161 }
162
163 if (FastMath.abs(deltaN - 1.0) < epsilon) {
164 break;
165 }
166
167 dPrev = dN;
168 cPrev = cN;
169 hPrev = hN;
170 n++;
171 }
172
173 if (n >= maxIterations) {
174 throw new MaxCountExceededException(LocalizedFormats.NON_CONVERGENT_CONTINUED_FRACTION,
175 maxIterations, x);
176 }
177
178 return hN;
179 }
180
181}
Note: See TracBrowser for help on using the repository browser.