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    *      https://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  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
21   */
22  package org.hipparchus.util;
23  
24  import org.hipparchus.exception.MathIllegalArgumentException;
25  import org.hipparchus.special.Gamma;
26  import org.junit.jupiter.api.Test;
27  
28  import static org.junit.jupiter.api.Assertions.assertEquals;
29  import static org.junit.jupiter.api.Assertions.assertThrows;
30  
31  /**
32   * Test cases for the {@link CombinatoricsUtils.FactorialLog} class.
33   */
34  class FactorialLogTest {
35  
36      @Test
37      void testPrecondition1() {
38          assertThrows(MathIllegalArgumentException.class, () -> {
39              CombinatoricsUtils.FactorialLog.create().withCache(-1);
40          });
41      }
42  
43      @Test
44      void testNonPositiveArgument() {
45          assertThrows(MathIllegalArgumentException.class, () -> {
46              final CombinatoricsUtils.FactorialLog f = CombinatoricsUtils.FactorialLog.create();
47              f.value(-1);
48          });
49      }
50  
51      @Test
52      void testDelegation() {
53          final CombinatoricsUtils.FactorialLog f = CombinatoricsUtils.FactorialLog.create();
54  
55          // Starting at 21 because for smaller arguments, there is no delegation to the
56          // "Gamma" class.
57          for (int i = 21; i < 10000; i++) {
58              final double expected = Gamma.logGamma(i + 1);
59              assertEquals(expected, f.value(i), 0d, i + "! ");
60          }
61      }
62  
63      @Test
64      void testCompareDirectWithoutCache() {
65          // This test shows that delegating to the "Gamma" class will also lead to a
66          // less accurate result.
67  
68          final int max = 100;
69          final CombinatoricsUtils.FactorialLog f = CombinatoricsUtils.FactorialLog.create();
70  
71          for (int i = 0; i < max; i++) {
72              final double expected = factorialLog(i);
73              assertEquals(expected, f.value(i), 2 * Math.ulp(expected), i + "! ");
74          }
75      }
76  
77      @Test
78      void testCompareDirectWithCache() {
79          final int max = 1000;
80          final CombinatoricsUtils.FactorialLog f = CombinatoricsUtils.FactorialLog.create().withCache(max);
81  
82          for (int i = 0; i < max; i++) {
83              final double expected = factorialLog(i);
84              assertEquals(expected, f.value(i), 0d, i + "! ");
85          }
86      }
87  
88      @Test
89      void testCacheIncrease() {
90          final int max = 100;
91          final CombinatoricsUtils.FactorialLog f1 = CombinatoricsUtils.FactorialLog.create().withCache(max);
92          final CombinatoricsUtils.FactorialLog f2 = f1.withCache(2 * max);
93  
94          final int val = max + max / 2;
95          final double expected = factorialLog(val);
96          assertEquals(expected, f2.value(val), 0d);
97      }
98  
99      @Test
100     void testCacheDecrease() {
101         final int max = 100;
102         final CombinatoricsUtils.FactorialLog f1 = CombinatoricsUtils.FactorialLog.create().withCache(max);
103         final CombinatoricsUtils.FactorialLog f2 = f1.withCache(max / 2);
104 
105         final int val = max / 4;
106         final double expected = factorialLog(val);
107         assertEquals(expected, f2.value(val), 0d);
108     }
109 
110     // Direct implementation.
111     private double factorialLog(final int n) {
112         double logSum = 0;
113         for (int i = 2; i <= n; i++) {
114             logSum += FastMath.log(i);
115         }
116         return logSum;
117     }
118 }