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