public virtual GaussianQuadratureData generate(int n) { ArgChecker.isTrue(n > 0); double[] x = new double[n]; double[] w = new double[n]; bool odd = n % 2 != 0; int m = (n + 1) / 2 - (odd ? 1 : 0); Pair <DoubleFunction1D, DoubleFunction1D>[] polynomials = HERMITE.getPolynomialsAndFirstDerivative(n); Pair <DoubleFunction1D, DoubleFunction1D> pair = polynomials[n]; DoubleFunction1D function = pair.First; DoubleFunction1D derivative = pair.Second; double root = 0; for (int i = 0; i < m; i++) { root = getInitialRootGuess(root, i, n, x); root = ROOT_FINDER.getRoot(function, derivative, root).Value; double dp = derivative.applyAsDouble(root); x[i] = -root; x[n - 1 - i] = root; w[i] = 2.0 / (dp * dp); w[n - 1 - i] = w[i]; } if (odd) { double dp = derivative.applyAsDouble(0.0); w[m] = 2.0 / dp / dp; } return(new GaussianQuadratureData(x, w)); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void testDerivative() public virtual void testDerivative() { assertEquals(F1.derivative().applyAsDouble(X), DF1.applyAsDouble(X), 1e-3); assertEquals(F2.derivative().applyAsDouble(X), DF2.applyAsDouble(X), 1e-3); assertEquals(F1.derivative(FiniteDifferenceType.CENTRAL, 1e-5).applyAsDouble(X), DF1.applyAsDouble(X), 1e-3); assertEquals(F2.derivative(FiniteDifferenceType.CENTRAL, 1e-5).applyAsDouble(X), DF2.applyAsDouble(X), 1e-3); assertEquals(F1.derivative(FiniteDifferenceType.FORWARD, 1e-5).applyAsDouble(X), DF1.applyAsDouble(X), 1e-3); assertEquals(F2.derivative(FiniteDifferenceType.FORWARD, 1e-5).applyAsDouble(X), DF2.applyAsDouble(X), 1e-3); assertEquals(F1.derivative(FiniteDifferenceType.BACKWARD, 1e-5).applyAsDouble(X), DF1.applyAsDouble(X), 1e-3); assertEquals(F2.derivative(FiniteDifferenceType.BACKWARD, 1e-5).applyAsDouble(X), DF2.applyAsDouble(X), 1e-3); assertEquals(F3.derivative().applyAsDouble(X), DF1.applyAsDouble(X), 1e-15); assertEquals(F4.derivative().applyAsDouble(X), DF2.applyAsDouble(X), 1e-15); }
/// <summary> /// Uses the function and its derivative. </summary> /// <param name="function"> The function, not null </param> /// <param name="derivative"> The derivative, not null </param> /// <param name="x1"> The first bound of the root, not null </param> /// <param name="x2"> The second bound of the root, not null </param> /// <returns> The root </returns> /// <exception cref="MathException"> If the root is not found in 1000 attempts; if the Newton /// step takes the estimate for the root outside the original bounds. </exception> public virtual double?getRoot(DoubleFunction1D function, DoubleFunction1D derivative, double?x1, double?x2) { checkInputs(function, x1, x2); ArgChecker.notNull(derivative, "derivative function"); double y1 = function.applyAsDouble(x1); if (Math.Abs(y1) < _accuracy) { return(x1); } double y2 = function.applyAsDouble(x2); if (Math.Abs(y2) < _accuracy) { return(x2); } double x = (x1 + x2) / 2; double x3 = y2 < 0 ? x2.Value : x1.Value; double x4 = y2 < 0 ? x1.Value : x2.Value; double xLower = x1 > x2 ? x2 : x1; double xUpper = x1 > x2 ? x1 : x2; for (int i = 0; i < MAX_ITER; i++) { double y = function.applyAsDouble(x); double dy = derivative.applyAsDouble(x); double dx = -y / dy; if (Math.Abs(dx) <= _accuracy) { return(x + dx); } x += dx; if (x < xLower || x > xUpper) { dx = (x4 - x3) / 2; x = x3 + dx; } if (y < 0) { x3 = x; } else { x4 = x; } } throw new MathException("Could not find root in " + MAX_ITER + " attempts"); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void testAlpha1() public virtual void testAlpha1() { DoubleFunction1D[] l1, l2; const double x = 2.34; for (int i = 0; i <= 6; i++) { l1 = LAGUERRE.getPolynomials(i, 0); l2 = LAGUERRE.getPolynomials(i); for (int j = 0; j <= i; j++) { assertEquals(l1[j].applyAsDouble(x), l2[j].applyAsDouble(x), EPS); } } const double alpha = 3.45; l1 = LAGUERRE.getPolynomials(6, alpha); //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final com.opengamma.strata.math.impl.function.DoubleFunction1D f0 = d -> 1d; DoubleFunction1D f0 = d => 1d; //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final com.opengamma.strata.math.impl.function.DoubleFunction1D f1 = d -> 1 + alpha - d; DoubleFunction1D f1 = d => 1 + alpha - d; //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final com.opengamma.strata.math.impl.function.DoubleFunction1D f2 = d -> d * d / 2 - (alpha + 2) * d + (alpha + 2) * (alpha + 1) / 2.0; DoubleFunction1D f2 = d => d * d / 2 - (alpha + 2) * d + (alpha + 2) * (alpha + 1) / 2.0; //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final com.opengamma.strata.math.impl.function.DoubleFunction1D f3 = d -> -d * d * d / 6 + (alpha + 3) * d * d / 2 - (alpha + 2) * (alpha + 3) * d / 2 + (alpha + 1) * (alpha + 2) * (alpha + 3) / 6; DoubleFunction1D f3 = d => - d * d * d / 6 + (alpha + 3) * d * d / 2 - (alpha + 2) * (alpha + 3) * d / 2 + (alpha + 1) * (alpha + 2) * (alpha + 3) / 6; assertEquals(l1[0].applyAsDouble(x), f0.applyAsDouble(x), EPS); assertEquals(l1[1].applyAsDouble(x), f1.applyAsDouble(x), EPS); assertEquals(l1[2].applyAsDouble(x), f2.applyAsDouble(x), EPS); assertEquals(l1[3].applyAsDouble(x), f3.applyAsDouble(x), EPS); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void testConversion() public virtual void testConversion() { //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final java.util.function.Function<double, double> f1 = x -> x * x * x + 2 * x * x - 7 * x + 12; System.Func <double, double> f1 = x => x * x * x + 2 * x * x - 7 * x + 12; //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final DoubleFunction1D f2 = DoubleFunction1D.from(f1); DoubleFunction1D f2 = DoubleFunction1D.from(f1); for (int i = 0; i < 100; i++) { //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final double x = Math.random(); double x = GlobalRandom.NextDouble; assertEquals(f2.applyAsDouble(x), F1.applyAsDouble(x), 0); assertEquals(f2.derivative().applyAsDouble(x), F1.derivative().applyAsDouble(x), 0); } }
/// <summary> /// Uses the function and its derivative. This method uses an initial guess for the root, rather than bounds. </summary> /// <param name="function"> The function, not null </param> /// <param name="derivative"> The derivative, not null </param> /// <param name="x"> The initial guess for the root, not null </param> /// <returns> The root </returns> /// <exception cref="MathException"> If the root is not found in 1000 attempts. </exception> public virtual double?getRoot(DoubleFunction1D function, DoubleFunction1D derivative, double?x) { ArgChecker.notNull(function, "function"); ArgChecker.notNull(derivative, "derivative function"); ArgChecker.notNull(x, "x"); double root = x.Value; for (int i = 0; i < MAX_ITER; i++) { double y = function.applyAsDouble(root); double dy = derivative.applyAsDouble(root); double dx = y / dy; if (Math.Abs(dx) <= _accuracy) { return(root - dx); } root -= dx; } throw new MathException("Could not find root in " + MAX_ITER + " attempts"); }
/// <summary> /// {@inheritDoc} /// </summary> public virtual GaussianQuadratureData generate(int n) { ArgChecker.isTrue(n > 0); int mid = (n + 1) / 2; double[] x = new double[n]; double[] w = new double[n]; Pair <DoubleFunction1D, DoubleFunction1D>[] polynomials = LEGENDRE.getPolynomialsAndFirstDerivative(n); Pair <DoubleFunction1D, DoubleFunction1D> pair = polynomials[n]; DoubleFunction1D function = pair.First; DoubleFunction1D derivative = pair.Second; for (int i = 0; i < mid; i++) { double root = ROOT_FINDER.getRoot(function, derivative, getInitialRootGuess(i, n)).Value; x[i] = -root; x[n - i - 1] = root; double dp = derivative.applyAsDouble(root); w[i] = 2 / ((1 - root * root) * dp * dp); w[n - i - 1] = w[i]; } return(new GaussianQuadratureData(x, w)); }
/// <summary> /// {@inheritDoc} /// </summary> public virtual GaussianQuadratureData generate(int n) { ArgChecker.isTrue(n > 0, "n > 0"); Pair <DoubleFunction1D, DoubleFunction1D>[] polynomials = JACOBI.getPolynomialsAndFirstDerivative(n, _alpha, _beta); Pair <DoubleFunction1D, DoubleFunction1D> pair = polynomials[n]; DoubleFunction1D previous = polynomials[n - 1].First; DoubleFunction1D function = pair.First; DoubleFunction1D derivative = pair.Second; double[] x = new double[n]; double[] w = new double[n]; double root = 0; for (int i = 0; i < n; i++) { double d = 2 * n + _c; root = getInitialRootGuess(root, i, n, x); root = ROOT_FINDER.getRoot(function, derivative, root).Value; x[i] = root; w[i] = GAMMA_FUNCTION.applyAsDouble(_alpha + n) * GAMMA_FUNCTION.applyAsDouble(_beta + n) / CombinatoricsUtils.factorialDouble(n) / GAMMA_FUNCTION.applyAsDouble(n + _c + 1) * d * Math.Pow(2, _c) / (derivative.applyAsDouble(root) * previous.applyAsDouble(root)); } return(new GaussianQuadratureData(x, w)); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void testAdd() public virtual void testAdd() { assertEquals(F1.add(F2).applyAsDouble(X), F1.applyAsDouble(X) + F2.applyAsDouble(X), EPS); assertEquals(F1.add(A).applyAsDouble(X), F1.applyAsDouble(X) + A, EPS); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void testEvaluate() public virtual void testEvaluate() { //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final double x = RANDOM.nextDouble(); double x = RANDOM.NextDouble(); assertEquals(C[3] * Math.Pow(x, 3) + C[2] * Math.Pow(x, 2) + C[1] * x + C[0], F.applyAsDouble(x), EPS); }