gt:             w = FastMath.sqrt(w) - 3.25;
context: /*
   *  Java Information Dynamics Toolkit (JIDT)
   *  Copyright (C) 2017, Joseph T. Lizier
   *  
   *  This program is free software: you can redistribute it and/or modify
   *  it under the terms of the GNU General Public License as published by
   *  the Free Software Foundation, either version 3 of the License, or
   *  (at your option) any later version.
   *  
   *  This program is distributed in the hope that it will be useful,
   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   *  GNU General Public License for more details.
   *  
   *  You should have received a copy of the GNU General Public License
   *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
  
  /*
   * This class was originally distributed as part of the Apache Commons
   *  Math3 library (3.6.1), under the Apache License Version 2.0, which is 
   *  copied below. This Apache 2 software is now included as a derivative
   *  work in the GPLv3 licensed JIDT project, as per:
   *  http://www.apache.org/licenses/GPL-compatibility.html
   *  
   * The original Apache source code has been modified as follows:
   * -- We have modified package names to sit inside the JIDT structure.
   */
  
  /*
   * Licensed to the Apache Software Foundation (ASF) under one or more
   * contributor license agreements.  See the NOTICE file distributed with
   * this work for additional information regarding copyright ownership.
   * The ASF licenses this file to You under the Apache License, Version 2.0
   * (the "License"); you may not use this file except in compliance with
   * the License.  You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package infodynamics.utils.commonsmath3.special;
  
  import infodynamics.utils.commonsmath3.util.FastMath;
  
  /**
   * This is a utility class that provides computation methods related to the
   * error functions.
   *
   */
  public class Erf {
  
      /**
       * The number {@code X_CRIT} is used by {@link #erf(double, double)} internally.
       * This number solves {@code erf(x)=0.5} within 1ulp.
       * More precisely, the current implementations of
       * {@link #erf(double)} and {@link #erfc(double)} satisfy:<br/>
       * {@code erf(X_CRIT) < 0.5},<br/>
       * {@code erf(Math.nextUp(X_CRIT) > 0.5},<br/>
       * {@code erfc(X_CRIT) = 0.5}, and<br/>
       * {@code erfc(Math.nextUp(X_CRIT) < 0.5}
       */
      private static final double X_CRIT = 0.4769362762044697;
  
      /**
       * Default constructor.  Prohibit instantiation.
       */
      private Erf() {}
  
      /**
       * Returns the error function.
       *
       * <p>erf(x) = 2/&radic;&pi; <sub>0</sub>&int;<sup>x</sup> e<sup>-t<sup>2</sup></sup>dt </p>
       *
       * <p>This implementation computes erf(x) using the
       * {@link Gamma#regularizedGammaP(double, double, double, int) regularized gamma function},
       * following <a href="http://mathworld.wolfram.com/Erf.html"> Erf</a>, equation (3)</p>
       *
       * <p>The value returned is always between -1 and 1 (inclusive).
       * If {@code abs(x) > 40}, then {@code erf(x)} is indistinguishable from
       * either 1 or -1 as a double, so the appropriate extreme value is returned.
       * </p>
       *
       * @param x the value.
       * @return the error function erf(x)
       * @throws infodynamics.utils.commonsmath3.exception.MaxCountExceededException
       * if the algorithm fails to converge.
       * @see Gamma#regularizedGammaP(double, double, double, int)
       */
      public static double erf(double x) {
          if (FastMath.abs(x) > 40) {
              return x > 0 ? 1 : -1;
          }
          final double ret = Gamma.regularizedGammaP(0.5, x * x, 1.0e-15, 10000);
          return x < 0 ? -ret : ret;
      }
  
      /**
       * Returns the complementary error function.
       *
       * <p>erfc(x) = 2/&radic;&pi; <sub>x</sub>&int;<sup>&infin;</sup> e<sup>-t<sup>2</sup></sup>dt
       * <br/>
       *    = 1 - {@link #erf(double) erf(x)} </p>
       *
       * <p>This implementation computes erfc(x) using the
       * {@link Gamma#regularizedGammaQ(double, double, double, int) regularized gamma function},
       * following <a href="http://mathworld.wolfram.com/Erf.html"> Erf</a>, equation (3).</p>
       *
       * <p>The value returned is always between 0 and 2 (inclusive).
       * If {@code abs(x) > 40}, then {@code erf(x)} is indistinguishable from
       * either 0 or 2 as a double, so the appropriate extreme value is returned.
       * </p>
       *
       * @param x the value
       * @return the complementary error function erfc(x)
       * @throws infodynamics.utils.commonsmath3.exception.MaxCountExceededException
       * if the algorithm fails to converge.
       * @see Gamma#regularizedGammaQ(double, double, double, int)
       * @since 2.2
       */
      public static double erfc(double x) {
          if (FastMath.abs(x) > 40) {
              return x > 0 ? 0 : 2;
          }
          final double ret = Gamma.regularizedGammaQ(0.5, x * x, 1.0e-15, 10000);
          return x < 0 ? 2 - ret : ret;
      }
  
      /**
       * Returns the difference between erf(x1) and erf(x2).
       *
       * The implementation uses either erf(double) or erfc(double)
       * depending on which provides the most precise result.
       *
       * @param x1 the first value
       * @param x2 the second value
       * @return erf(x2) - erf(x1)
       */
      public static double erf(double x1, double x2) {
          if(x1 > x2) {
              return -erf(x2, x1);
          }
  
          return
          x1 < -X_CRIT ?
              x2 < 0.0 ?
                  erfc(-x2) - erfc(-x1) :
                  erf(x2) - erf(x1) :
              x2 > X_CRIT && x1 > 0.0 ?
                  erfc(x1) - erfc(x2) :
                  erf(x2) - erf(x1);
      }
  
      /**
       * Returns the inverse erf.
       * <p>
       * This implementation is described in the paper:
       * <a href="http://people.maths.ox.ac.uk/gilesm/files/gems_erfinv.pdf">Approximating
       * the erfinv function</a> by Mike Giles, Oxford-Man Institute of Quantitative Finance,
       * which was published in GPU Computing Gems, volume 2, 2010.
       * The source code is available <a href="http://gpucomputing.net/?q=node/1828">here</a>.
       * </p>
       * @param x the value
       * @return t such that x = erf(t)
       * @since 3.2
       */
      public static double erfInv(final double x) {
  
          // beware that the logarithm argument must be
          // commputed as (1.0 - x) * (1.0 + x),
          // it must NOT be simplified as 1.0 - x * x as this
          // would induce rounding errors near the boundaries +/-1
          double w = - FastMath.log((1.0 - x) * (1.0 + x));
          double p;
  
          if (w < 6.25) {
              w -= 3.125;
              p =  -3.6444120640178196996e-21;
              p =   -1.685059138182016589e-19 + p * w;
              p =   1.2858480715256400167e-18 + p * w;
              p =    1.115787767802518096e-17 + p * w;
              p =   -1.333171662854620906e-16 + p * w;
              p =   2.0972767875968561637e-17 + p * w;
              p =   6.6376381343583238325e-15 + p * w;
              p =  -4.0545662729752068639e-14 + p * w;
              p =  -8.1519341976054721522e-14 + p * w;
              p =   2.6335093153082322977e-12 + p * w;
              p =  -1.2975133253453532498e-11 + p * w;
              p =  -5.4154120542946279317e-11 + p * w;
              p =    1.051212273321532285e-09 + p * w;
              p =  -4.1126339803469836976e-09 + p * w;
              p =  -2.9070369957882005086e-08 + p * w;
              p =   4.2347877827932403518e-07 + p * w;
              p =  -1.3654692000834678645e-06 + p * w;
              p =  -1.3882523362786468719e-05 + p * w;
              p =    0.0001867342080340571352 + p * w;
              p =  -0.00074070253416626697512 + p * w;
              p =   -0.0060336708714301490533 + p * w;
              p =      0.24015818242558961693 + p * w;
              p =       1.6536545626831027356 + p * w;
          } else if (w < 16.0) {
  