View Javadoc
1   /*
2    * Licensed to the Hipparchus project 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 Hipparchus project 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  package org.hipparchus.util;
18  
19  import org.hipparchus.CalculusFieldElement;
20  import org.hipparchus.exception.MathIllegalStateException;
21  import org.junit.Assert;
22  import org.junit.Test;
23  
24  /**
25   * Tests for ContinuedFraction.
26   */
27  public class FieldContinuedFractionTest {
28  
29      @Test
30      public void testGoldenRatio() {
31          FieldContinuedFraction cf = new FieldContinuedFraction() {
32  
33              @Override
34              public <T extends CalculusFieldElement<T>> T getA(final int n, final T x) {
35                  return x.getField().getOne();
36              }
37  
38              @Override
39              public <T extends CalculusFieldElement<T>> T getB(final int n, final T x) {
40                  return x.getField().getOne();
41              }
42  
43          };
44  
45          Binary64 gr = cf.evaluate(new Binary64(0.0), 10e-9);
46          Assert.assertEquals(1.61803399, gr.getReal(), 10e-9);
47      }
48  
49      @Test(expected = MathIllegalStateException.class)
50      public void testNonConvergentContinuedFraction() {
51          FieldContinuedFraction cf = new FieldContinuedFraction() {
52  
53              @Override
54              public <T extends CalculusFieldElement<T>> T getA(final int n, final T x) {
55                  return x.getField().getOne();
56              }
57  
58              @Override
59              public <T extends CalculusFieldElement<T>> T getB(final int n, final T x) {
60                  return x.getField().getOne();
61              }
62  
63          };
64  
65          cf.evaluate(new Binary64(0.0), 10e-9, 10);
66      }
67  
68      @Test(expected = MathIllegalStateException.class)
69      public void testInfinityDivergence() {
70          FieldContinuedFraction cf = new FieldContinuedFraction() {
71  
72              @Override
73              public <T extends CalculusFieldElement<T>> T getA(final int n, final T x) {
74                  return x.getField().getOne().divide(n);
75              }
76  
77              @Override
78              public <T extends CalculusFieldElement<T>> T getB(final int n, final T x) {
79                  return x.getField().getOne();
80              }
81  
82          };
83  
84          cf.evaluate(new Binary64(1));
85      }
86  }