class BlackScholesFormulaRepository {

  @ExternalFunction
  public static double vomma(final double spot, final double strike, final double timeToExpiry, final double lognormalVol, final double interestRate, final double costOfCarry) {
    ArgumentChecker.isTrue(spot >= 0.0, "negative/NaN spot; have {}", spot);
    ArgumentChecker.isTrue(strike >= 0.0, "negative/NaN strike; have {}", strike);
    ArgumentChecker.isTrue(timeToExpiry >= 0.0, "negative/NaN timeToExpiry; have {}", timeToExpiry);
    ArgumentChecker.isTrue(lognormalVol >= 0.0, "negative/NaN lognormalVol; have {}", lognormalVol);
    ArgumentChecker.isFalse(Double.isNaN(interestRate), "interestRate is NaN");
    ArgumentChecker.isFalse(Double.isNaN(costOfCarry), "costOfCarry is NaN");

    final double rootT = Math.sqrt(timeToExpiry);
    double sigmaRootT = lognormalVol * rootT;
    if (Double.isNaN(sigmaRootT)) {
      sigmaRootT = 1.; //ref value is returned
    }

    if (spot > LARGE * strike || strike > LARGE * spot || rootT < SMALL) {
      return 0.;
    }

    double d1 = 0.;
    double d1d2Mod = 0.;
    if (Math.abs(spot - strike) < SMALL || (spot > LARGE && strike > LARGE) || rootT > LARGE) {
      final double costOvVol = (Math.abs(costOfCarry) < SMALL && lognormalVol < SMALL) ? Math.signum(costOfCarry) : costOfCarry / lognormalVol;
      final double coefD1 = costOvVol + 0.5 * lognormalVol;
      final double coefD1D2Mod = costOvVol * costOvVol / lognormalVol - 0.25 * lognormalVol;
      final double tmpD1 = coefD1 * rootT;
      final double tmpD1d2Mod = coefD1D2Mod * rootT * timeToExpiry;
      d1 = Double.isNaN(tmpD1) ? 0. : tmpD1;
      d1d2Mod = Double.isNaN(tmpD1d2Mod) ? 1. : tmpD1d2Mod;
    } else {
      if (lognormalVol > LARGE) {
        d1 = 0.5 * sigmaRootT;
        d1d2Mod = -0.25 * sigmaRootT * timeToExpiry;
      } else {
        if (lognormalVol < SMALL) {
          final double d1Tmp = (Math.log(spot / strike) / rootT + costOfCarry * rootT) / lognormalVol;
          d1 = Double.isNaN(d1Tmp) ? 1. : d1Tmp;
          d1d2Mod = d1 * d1 * rootT / lognormalVol;
        } else {
          final double tmp = Math.log(spot / strike) / sigmaRootT + costOfCarry * rootT / lognormalVol;
          d1 = tmp + 0.5 * sigmaRootT;
          d1d2Mod = (tmp * tmp - 0.25 * sigmaRootT * sigmaRootT) * rootT / lognormalVol;
        }
      }
    }
    //    if (Double.isNaN(d1) || Double.isNaN(d1d2Mod)) {
    //      throw new IllegalArgumentException("NaN found");
    //    }

    double coef = 0.;
    if ((interestRate > LARGE && costOfCarry > LARGE) || (-interestRate > LARGE && -costOfCarry > LARGE) || Math.abs(costOfCarry - interestRate) < SMALL) {
      coef = 1.; //ref value is returned
    } else {
      final double rate = costOfCarry - interestRate;
      if (rate > LARGE) {
        return costOfCarry > LARGE ? 0. : (d1d2Mod >= 0. ? Double.POSITIVE_INFINITY : Double.NEGATIVE_INFINITY);
      }
      if (-rate > LARGE) {
        return 0.;
      }
      coef = Math.exp(rate * timeToExpiry);
    }

    final double norm = NORMAL.getPDF(d1);
    double tmp = d1d2Mod * spot * coef;
    if (Double.isNaN(tmp)) {
      tmp = coef;
    }

    return norm < SMALL ? 0. : norm * tmp;
  }

}