예제 #1
0
        public void GaussianIntegrals()
        {
            Random rng = new Random(1);

            for (int d = 2; d < 4; d++)
            {
                if (d == 4 || d == 5 || d == 6)
                {
                    continue;
                }
                Console.WriteLine(d);

                // Create a symmetric matrix
                SymmetricMatrix A = new SymmetricMatrix(d);
                for (int r = 0; r < d; r++)
                {
                    for (int c = 0; c < r; c++)
                    {
                        A[r, c] = rng.NextDouble();
                    }
                    // Ensure it is positive definite by diagonal dominance
                    A[r, r] = r + 1.0;
                }

                // Compute its determinant, which appears in the analytic value of the integral
                CholeskyDecomposition CD = A.CholeskyDecomposition();
                double detA = CD.Determinant();

                // Compute the integral
                Func <IList <double>, double> f = (IList <double> x) => {
                    ColumnVector v = new ColumnVector(x);
                    double       s = v.Transpose() * (A * v);
                    return(Math.Exp(-s));
                };

                Interval[] volume = new Interval[d];
                for (int i = 0; i < d; i++)
                {
                    volume[i] = Interval.FromEndpoints(Double.NegativeInfinity, Double.PositiveInfinity);
                }

                IntegrationResult I = MultiFunctionMath.Integrate(f, volume);

                // Compare to the analytic result
                Console.WriteLine("{0} ({1}) {2}", I.Value, I.Precision, Math.Sqrt(MoreMath.Pow(Math.PI, d) / detA));
                Assert.IsTrue(TestUtilities.IsNearlyEqual(I.Value, Math.Sqrt(MoreMath.Pow(Math.PI, d) / detA), new EvaluationSettings()
                {
                    AbsolutePrecision = 2.0 * I.Precision
                }));
            }
        }
예제 #2
0
        public void GaussianIntegrals()
        {
            Random rng = new Random(1);

            for (int d = 2; d < 8; d++)
            {
                Console.WriteLine(d);

                // Create a symmetric matrix
                SymmetricMatrix A = new SymmetricMatrix(d);
                for (int r = 0; r < d; r++)
                {
                    for (int c = 0; c < r; c++)
                    {
                        A[r, c] = rng.NextDouble();
                    }
                    // Ensure it is positive definite by diagonal dominance
                    A[r, r] = r + 1.0;
                }

                // Compute its determinant, which appears in the analytic value of the integral
                CholeskyDecomposition CD = A.CholeskyDecomposition();
                double detA = CD.Determinant();

                // Compute the integral
                Func <IReadOnlyList <double>, double> f = (IReadOnlyList <double> x) => {
                    ColumnVector v = new ColumnVector(x);
                    double       s = v.Transpose * (A * v);
                    return(Math.Exp(-s));
                };

                Interval[] volume = new Interval[d];
                for (int i = 0; i < d; i++)
                {
                    volume[i] = Interval.FromEndpoints(Double.NegativeInfinity, Double.PositiveInfinity);
                }

                // These are difficult integrals; demand reduced precision.
                IntegrationSettings settings = new IntegrationSettings()
                {
                    RelativePrecision = Math.Pow(10.0, -(4.0 - d / 2.0))
                };

                IntegrationResult I = MultiFunctionMath.Integrate(f, volume, settings);

                // Compare to the analytic result
                Assert.IsTrue(I.Estimate.ConfidenceInterval(0.95).ClosedContains(Math.Sqrt(MoreMath.Pow(Math.PI, d) / detA)));
            }
        }
예제 #3
0
        public void CatalanHankelMatrixDeterminant()
        {
            for (int d = 1; d <= 8; d++)
            {
                SymmetricMatrix S = new SymmetricMatrix(d);
                for (int r = 0; r < d; r++)
                {
                    for (int c = 0; c <= r; c++)
                    {
                        int n = r + c;
                        S[r, c] = AdvancedIntegerMath.BinomialCoefficient(2 * n, n) / (n + 1);
                    }
                }

                CholeskyDecomposition CD = S.CholeskyDecomposition();
                Assert.IsTrue(TestUtilities.IsNearlyEqual(CD.Determinant(), 1.0));
            }
        }