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