BoostMath.java

  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.  */

  17. //  (C) Copyright John Maddock 2006.
  18. //  Use, modification and distribution are subject to the
  19. //  Boost Software License, Version 1.0. (See accompanying file
  20. //  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

  21. package org.apache.commons.numbers.gamma;

  22. /**
  23.  * Math functions used by the Boost functions.
  24.  *
  25.  * <p>This code has been adapted from the <a href="https://www.boost.org/">Boost</a>
  26.  * {@code c++} implementations in {@code <boost/math/special_functions/>}.
  27.  * All work is copyright John Maddock 2006 and subject to the Boost Software License.
  28.  */
  29. final class BoostMath {
  30.     /** Private constructor. */
  31.     private BoostMath() {
  32.         // intentionally empty.
  33.     }

  34.     /**
  35.      * Returns {@code pow(x, y) - 1}. This function is accurate when {@code x -> 1} or {@code y} is
  36.      * small.
  37.      *
  38.      * <p>Adapted from {@code boost/math/special_functions/powm1.hpp}. Explicit handling of
  39.      * edges cases (overflow, domain error) using the policy has been removed.
  40.      *
  41.      * @param x the x
  42.      * @param y the y
  43.      * @return {@code pow(x, y) - 1}
  44.      */
  45.     static double powm1(double x, double y) {
  46.         if (x > 0) {
  47.             // Check for small y or x close to 1.
  48.             // Require term < 0.5
  49.             // => log(x) * y < 0.5
  50.             // Assume log(x) ~ (x - 1) [true when x is close to 1]
  51.             // => |(x-1) * y| < 0.5

  52.             if (Math.abs(y * (x - 1)) < 0.5 || Math.abs(y) < 0.2) {
  53.                 // We don't have any good/quick approximation for log(x) * y
  54.                 // so just try it and see:
  55.                 final double l = y * Math.log(x);
  56.                 if (l < 0.5) {
  57.                     return Math.expm1(l);
  58.                 }
  59.                 // fall through....
  60.             }
  61.         } else if (x < 0 &&
  62.                    // y had better be an integer:
  63.                    // x is negative.
  64.                    // pow(x, y) only allowed if y is an integer.
  65.                    // if y is even then we can invert non-zero finite x.
  66.                    Math.rint(y * 0.5) == y * 0.5) {
  67.             return powm1(-x, y);
  68.         }
  69.         return Math.pow(x, y) - 1;
  70.     }
  71. }