Beispiel #1
0
        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 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);
        }
 /// <summary>
 /// Subtracts a function from the polynomial.
 /// <para>
 /// If the function is not a <seealso cref="RealPolynomialFunction1D"/> then the subtract takes place
 /// as in <seealso cref="DoubleFunction1D"/>, otherwise the result will also be a polynomial.
 ///
 /// </para>
 /// </summary>
 /// <param name="f">  the function to subtract </param>
 /// <returns> $P-f$ </returns>
 public override DoubleFunction1D subtract(DoubleFunction1D f)
 {
     ArgChecker.notNull(f, "function");
     if (f is RealPolynomialFunction1D)
     {
         RealPolynomialFunction1D p1 = (RealPolynomialFunction1D)f;
         double[] c   = _coefficients;
         double[] c1  = p1.Coefficients;
         int      m   = c.Length;
         int      n   = c1.Length;
         int      min = Math.Min(m, n);
         int      max = Math.Max(m, n);
         double[] c3  = new double[max];
         for (int i = 0; i < min; i++)
         {
             c3[i] = c[i] - c1[i];
         }
         for (int i = min; i < max; i++)
         {
             if (m == max)
             {
                 c3[i] = c[i];
             }
             else
             {
                 c3[i] = -c1[i];
             }
         }
         return(new RealPolynomialFunction1D(c3));
     }
     return(DoubleFunction1D.this.subtract(f));
 }
        public override Pair <DoubleFunction1D, DoubleFunction1D>[] getPolynomialsAndFirstDerivative(int n)
        {
            ArgChecker.isTrue(n >= 0);
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @SuppressWarnings("unchecked") com.opengamma.strata.collect.tuple.Pair<com.opengamma.strata.math.impl.function.DoubleFunction1D, com.opengamma.strata.math.impl.function.DoubleFunction1D>[] polynomials = new com.opengamma.strata.collect.tuple.Pair[n + 1];
            Pair <DoubleFunction1D, DoubleFunction1D>[] polynomials = new Pair[n + 1];
            DoubleFunction1D p, dp, p1, p2;
            double           sqrt2 = Math.Sqrt(2);
            DoubleFunction1D x = X;

            for (int i = 0; i <= n; i++)
            {
                if (i == 0)
                {
                    polynomials[i] = Pair.of((DoubleFunction1D)F0, Zero);
                }
                else if (i == 1)
                {
                    polynomials[i] = Pair.of(polynomials[0].First.multiply(sqrt2).multiply(x), (DoubleFunction1D)DF1);
                }
                else
                {
                    p1             = polynomials[i - 1].First;
                    p2             = polynomials[i - 2].First;
                    p              = p1.multiply(x).multiply(Math.Sqrt(2.0 / i)).subtract(p2.multiply(Math.Sqrt((i - 1.0) / i)));
                    dp             = p1.multiply(Math.Sqrt(2 * i));
                    polynomials[i] = Pair.of(p, dp);
                }
            }
            return(polynomials);
        }
        /// <summary>
        /// Calculates polynomials and derivative. </summary>
        /// <param name="n">  the n value </param>
        /// <param name="alpha">  the alpha value </param>
        /// <param name="beta">  the beta value </param>
        /// <returns> the result </returns>
        public virtual Pair <DoubleFunction1D, DoubleFunction1D>[] getPolynomialsAndFirstDerivative(int n, double alpha, double beta)
        {
            ArgChecker.isTrue(n >= 0);
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @SuppressWarnings("unchecked") com.opengamma.strata.collect.tuple.Pair<com.opengamma.strata.math.impl.function.DoubleFunction1D, com.opengamma.strata.math.impl.function.DoubleFunction1D>[] polynomials = new com.opengamma.strata.collect.tuple.Pair[n + 1];
            Pair <DoubleFunction1D, DoubleFunction1D>[] polynomials = new Pair[n + 1];
            DoubleFunction1D p, dp, p1, p2;

            for (int i = 0; i <= n; i++)
            {
                if (i == 0)
                {
                    polynomials[i] = Pair.of(One, Zero);
                }
                else if (i == 1)
                {
                    double a1 = (alpha + beta + 2) / 2;
                    polynomials[i] = Pair.of((DoubleFunction1D) new RealPolynomialFunction1D(new double[] { (alpha - beta) / 2, a1 }), (DoubleFunction1D) new RealPolynomialFunction1D(new double[] { a1 }));
                }
                else
                {
                    int j = i - 1;
                    p1 = polynomials[j].First;
                    p2 = polynomials[j - 1].First;
                    DoubleFunction1D temp1 = p1.multiply(getB(alpha, beta, j));
                    DoubleFunction1D temp2 = p1.multiply(X).multiply(getC(alpha, beta, j));
                    DoubleFunction1D temp3 = p2.multiply(getD(alpha, beta, j));
                    p              = (temp1.add(temp2).add(temp3)).divide(getA(alpha, beta, j));
                    dp             = p.derivative();
                    polynomials[i] = Pair.of(p, dp);
                }
            }
            return(polynomials);
        }
        //-------------------------------------------------------------------------
        public virtual double?getRoot(System.Func <double, double> function, double?x)
        {
            ArgChecker.notNull(function, "function");
            ArgChecker.notNull(x, "x");
            DoubleFunction1D f = DoubleFunction1D.from(function);

            return(getRoot(f, f.derivative(), x));
        }
        /// <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 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>
 /// Adds a function to the polynomial.
 /// If the function is not a <seealso cref="RealPolynomialFunction1D"/> then the addition takes
 /// place as in <seealso cref="DoubleFunction1D"/>, otherwise the result will also be a polynomial.
 /// </summary>
 /// <param name="f">  the function to add </param>
 /// <returns> $P+f$ </returns>
 public override DoubleFunction1D add(DoubleFunction1D f)
 {
     ArgChecker.notNull(f, "function");
     if (f is RealPolynomialFunction1D)
     {
         RealPolynomialFunction1D p1 = (RealPolynomialFunction1D)f;
         double[] c1           = p1.Coefficients;
         double[] c            = _coefficients;
         int      n            = c1.Length;
         bool     longestIsNew = n > _n;
         double[] c3           = longestIsNew ? Arrays.copyOf(c1, n) : Arrays.copyOf(c, _n);
         for (int i = 0; i < (longestIsNew ? _n : n); i++)
         {
             c3[i] += longestIsNew ? c[i] : c1[i];
         }
         return(new RealPolynomialFunction1D(c3));
     }
     return(DoubleFunction1D.this.add(f));
 }
        /// <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>
 /// Gets the polynomials.
 /// </summary>
 /// <param name="n">  the n value </param>
 /// <param name="alpha">  the alpha value </param>
 /// <returns> the result </returns>
 public virtual DoubleFunction1D[] getPolynomials(int n, double alpha)
 {
     ArgChecker.isTrue(n >= 0);
     DoubleFunction1D[] polynomials = new DoubleFunction1D[n + 1];
     for (int i = 0; i <= n; i++)
     {
         if (i == 0)
         {
             polynomials[i] = One;
         }
         else if (i == 1)
         {
             polynomials[i] = new RealPolynomialFunction1D(new double[] { 1 + alpha, -1 });
         }
         else
         {
             polynomials[i] = (polynomials[i - 1].multiply(2.0 * i + alpha - 1).subtract(polynomials[i - 1].multiply(X)).subtract(polynomials[i - 2].multiply((i - 1.0 + alpha))).divide(i));
         }
     }
     return(polynomials);
 }
 public override DoubleFunction1D[] getPolynomials(int n)
 {
     ArgChecker.isTrue(n >= 0);
     DoubleFunction1D[] polynomials = new DoubleFunction1D[n + 1];
     for (int i = 0; i <= n; i++)
     {
         if (i == 0)
         {
             polynomials[i] = One;
         }
         else if (i == 1)
         {
             polynomials[i] = TWO_X;
         }
         else
         {
             polynomials[i] = polynomials[i - 1].multiply(2).multiply(X).subtract(polynomials[i - 2].multiply(2 * i - 2));
         }
     }
     return(polynomials);
 }
 public override DoubleFunction1D[] getPolynomials(int n)
 {
     ArgChecker.isTrue(n >= 0);
     DoubleFunction1D[] polynomials = new DoubleFunction1D[n + 1];
     for (int i = 0; i <= n; i++)
     {
         if (i == 0)
         {
             polynomials[i] = F0;
         }
         else if (i == 1)
         {
             polynomials[i] = polynomials[0].multiply(Math.Sqrt(2)).multiply(X);
         }
         else
         {
             polynomials[i] = polynomials[i - 1].multiply(X).multiply(Math.Sqrt(2.0 / i)).subtract(polynomials[i - 2].multiply(Math.Sqrt((i - 1.0) / i)));
         }
     }
     return(polynomials);
 }
 /// <summary>
 /// Multiplies the polynomial by a function.
 /// If the function is not a <seealso cref="RealPolynomialFunction1D"/> then the multiplication takes
 /// place as in <seealso cref="DoubleFunction1D"/>, otherwise the result will also be a polynomial.
 /// </summary>
 /// <param name="f">  the function by which to multiply </param>
 /// <returns> $P \dot f$ </returns>
 public override DoubleFunction1D multiply(DoubleFunction1D f)
 {
     ArgChecker.notNull(f, "function");
     if (f is RealPolynomialFunction1D)
     {
         RealPolynomialFunction1D p1 = (RealPolynomialFunction1D)f;
         double[] c    = _coefficients;
         double[] c1   = p1.Coefficients;
         int      m    = c1.Length;
         double[] newC = new double[_n + m - 1];
         for (int i = 0; i < newC.Length; i++)
         {
             newC[i] = 0;
             for (int j = Math.Max(0, i + 1 - m); j < Math.Min(_n, i + 1); j++)
             {
                 newC[i] += c[j] * c1[i - j];
             }
         }
         return(new RealPolynomialFunction1D(newC));
     }
     return(DoubleFunction1D.this.multiply(f));
 }
 /// <summary>
 /// Calculates polynomials. </summary>
 /// <param name="n">  the n value </param>
 /// <param name="alpha">  the alpha value </param>
 /// <param name="beta">  the beta value </param>
 /// <returns> the result </returns>
 public virtual DoubleFunction1D[] getPolynomials(int n, double alpha, double beta)
 {
     ArgChecker.isTrue(n >= 0);
     DoubleFunction1D[] polynomials = new DoubleFunction1D[n + 1];
     for (int i = 0; i <= n; i++)
     {
         if (i == 0)
         {
             polynomials[i] = One;
         }
         else if (i == 1)
         {
             polynomials[i] = new RealPolynomialFunction1D(new double[] { (alpha - beta) / 2, (alpha + beta + 2) / 2 });
         }
         else
         {
             int j = i - 1;
             polynomials[i] = (polynomials[j].multiply(getB(alpha, beta, j)).add(polynomials[j].multiply(X).multiply(getC(alpha, beta, j)).add(polynomials[j - 1].multiply(getD(alpha, beta, j))))).divide(getA(alpha, beta, j));
         }
     }
     return(polynomials);
 }
Beispiel #16
0
        /// <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));
        }
Beispiel #17
0
        /// <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>
 /// 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(System.Func <double, double> function, System.Func <double, double> derivative, double?x)
 {
     return(getRoot(DoubleFunction1D.from(function), DoubleFunction1D.from(derivative), x));
 }
 /// <summary>
 /// {@inheritDoc} </summary>
 /// <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 override double?getRoot(System.Func <double, double> function, double?x1, double?x2)
 {
     ArgChecker.notNull(function, "function");
     return(getRoot(DoubleFunction1D.from(function), x1, x2));
 }
 /// <summary>
 /// Uses the <seealso cref="DoubleFunction1D#derivative()"/> method. <i>x<sub>1</sub></i> and
 /// <i>x<sub>2</sub></i> do not have to be increasing.
 /// </summary>
 /// <param name="function"> The function, 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, double?x1, double?x2)
 {
     ArgChecker.notNull(function, "function");
     return(getRoot(function, function.derivative(), x1, x2));
 }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test(expectedExceptions = IllegalArgumentException.class) public void testConvertNull()
        public virtual void testConvertNull()
        {
            DoubleFunction1D.from(null);
        }
 /// <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(System.Func <double, double> function, System.Func <double, double> derivative, double?x1, double?x2)
 {
     checkInputs(function, x1, x2);
     ArgChecker.notNull(derivative, "derivative");
     return(getRoot(DoubleFunction1D.from(function), DoubleFunction1D.from(derivative), x1, x2));
 }