public void ChebyshevSpecialCases()
 {
     foreach (double x in bArguments)
     {
         Assert.IsTrue(OrthogonalPolynomials.ChebyshevT(0, x) == 1.0);
         Assert.IsTrue(OrthogonalPolynomials.ChebyshevT(1, x) == x);
     }
     foreach (int n in orders)
     {
         if (n % 2 == 0)
         {
             if (n % 4 == 0)
             {
                 Assert.IsTrue(OrthogonalPolynomials.ChebyshevT(n, 0.0) == 1.0);
             }
             else
             {
                 Assert.IsTrue(OrthogonalPolynomials.ChebyshevT(n, 0.0) == -1.0);
             }
         }
         else
         {
             Assert.IsTrue(OrthogonalPolynomials.ChebyshevT(n, 0.0) == 0.0);
         }
         Assert.IsTrue(OrthogonalPolynomials.ChebyshevT(n, 1.0) == 1.0);
     }
 }
        public void AssociatedLegendreOrthonormalityL()
        {
            // don't let l and m get too big, or numerical integration will fail due to heavily oscilatory behavior

            int[] ells = TestUtilities.GenerateIntegerValues(1, 10, 4);
            for (int ki = 0; ki < ells.Length; ki++)
            {
                int k = ells[ki];
                for (int li = 0; li <= ki; li++)
                {
                    int l = ells[li];
                    foreach (int m in TestUtilities.GenerateUniformIntegerValues(0, Math.Min(k, l), 4))
                    {
                        Func <double, double> f = delegate(double x) {
                            return(OrthogonalPolynomials.LegendreP(k, m, x) * OrthogonalPolynomials.LegendreP(l, m, x));
                        };
                        double I = FunctionMath.Integrate(f, Interval.FromEndpoints(-1.0, 1.0));

                        Console.WriteLine("k={0} l={1} m={2} I={3}", k, l, m, I);

                        if (k == l)
                        {
                            Assert.IsTrue(TestUtilities.IsNearlyEqual(
                                              I, 2.0 / (2 * k + 1) * Math.Exp(AdvancedIntegerMath.LogFactorial(l + m) - AdvancedIntegerMath.LogFactorial(l - m))
                                              ));
                        }
                        else
                        {
                            Assert.IsTrue(Math.Abs(I) < TestUtilities.TargetPrecision);
                        }
                    }
                }
            }
        }
 public void HermiteAddition()
 {
     foreach (int n in orders)
     {
         if (n > 100)
         {
             continue;
         }
         foreach (double x in aArguments)
         {
             foreach (double y in aArguments)
             {
                 double   value = OrthogonalPolynomials.HermiteH(n, x + y);
                 double[] terms = new double[n + 1]; double sum = 0.0;
                 for (int k = 0; k <= n; k++)
                 {
                     terms[k] = AdvancedIntegerMath.BinomialCoefficient(n, k) * OrthogonalPolynomials.HermiteH(k, x) * Math.Pow(2 * y, n - k);
                     sum     += terms[k];
                 }
                 Console.WriteLine("n={0}, x={1}, y={2}, H(x+y)={3}, sum={4}", n, x, y, value, sum);
                 Assert.IsTrue(TestUtilities.IsSumNearlyEqual(terms, value));
             }
         }
     }
 }
 public void HermiteSpecialCases()
 {
     foreach (double x in aArguments)
     {
         Assert.IsTrue(OrthogonalPolynomials.HermiteH(0, x) == 1.0);
         Assert.IsTrue(OrthogonalPolynomials.HermiteH(1, x) == 2.0 * x);
     }
     foreach (int n in orders)
     {
         if (n > 100)
         {
             continue;
         }
         if (n % 2 == 0)
         {
             int m = n / 2;
             int s = 1;
             if (m % 2 != 0)
             {
                 s = -s;
             }
             Assert.IsTrue(TestUtilities.IsNearlyEqual(OrthogonalPolynomials.HermiteH(n, 0.0), s * AdvancedIntegerMath.Factorial(n) / AdvancedIntegerMath.Factorial(m)));
         }
         else
         {
             Assert.IsTrue(OrthogonalPolynomials.HermiteH(n, 0.0) == 0.0);
         }
     }
 }
 public void ChebyshevMultiplication()
 {
     foreach (int n in orders)
     {
         foreach (int m in orders)
         {
             if (n * m > 500)
             {
                 continue;              // we don't test very high order
             }
             foreach (double x in bArguments)
             {
                 Console.WriteLine("n={0}, m={1}, x={2}", n, m, x);
                 double Tm = OrthogonalPolynomials.ChebyshevT(m, x);
                 Console.WriteLine("T_m(x)= {0}", Tm);
                 double Tn = OrthogonalPolynomials.ChebyshevT(n, Tm);
                 Console.WriteLine("T_n(T_m) = {0}", Tn);
                 double Tnm = OrthogonalPolynomials.ChebyshevT(n * m, x);
                 Console.WriteLine("T_nm(x) = {0}", Tnm);
                 double d = Math.Abs(Tn - Tnm) / Math.Abs(Tnm);
                 Console.WriteLine("relative d = {0}", d);
                 Assert.IsTrue(TestUtilities.IsNearlyEqual(OrthogonalPolynomials.ChebyshevT(n, OrthogonalPolynomials.ChebyshevT(m, x)), OrthogonalPolynomials.ChebyshevT(n * m, x)));
             }
         }
     }
 }
 public void ChebyshevInequality()
 {
     foreach (int n in orders)
     {
         foreach (double x in bArguments)
         {
             Assert.IsTrue(Math.Abs(OrthogonalPolynomials.ChebyshevT(n, x)) <= 1.0);
         }
     }
 }
 public void LegendreInequality()
 {
     foreach (int n in orders)
     {
         foreach (double x in bArguments)
         {
             Assert.IsTrue(Math.Abs(OrthogonalPolynomials.LegendreP(n, x)) <= 1.0);
         }
     }
 }
 public void LegendreRecurrence()
 {
     foreach (int n in orders)
     {
         foreach (double x in bArguments)
         {
             Assert.IsTrue(TestUtilities.IsNearlyEqual((n + 1) * OrthogonalPolynomials.LegendreP(n + 1, x), (2 * n + 1) * x * OrthogonalPolynomials.LegendreP(n, x) - n * OrthogonalPolynomials.LegendreP(n - 1, x)));
         }
     }
 }
 public void LaguerreInequality()
 {
     foreach (int n in TestUtilities.GenerateRealValues(1.0, 1.0E2, 5))
     {
         foreach (double x in TestUtilities.GenerateRealValues(1.0E-2, 1.0E2, 5))
         {
             Assert.IsTrue(OrthogonalPolynomials.LaguerreL(n, x) <= Math.Exp(x / 2.0));
         }
     }
 }
Exemplo n.º 10
0
 public void LegendreNegativeOrder()
 {
     foreach (int n in orders)
     {
         foreach (double x in bArguments)
         {
             Assert.IsTrue(OrthogonalPolynomials.LegendreP(-n, x) == OrthogonalPolynomials.LegendreP(n - 1, x));
         }
     }
 }
Exemplo n.º 11
0
 public void ChebyshevRecurrence()
 {
     foreach (int n in orders)
     {
         foreach (double x in bArguments)
         {
             Assert.IsTrue(TestUtilities.IsNearlyEqual(OrthogonalPolynomials.ChebyshevT(n + 1, x), 2.0 * x * OrthogonalPolynomials.ChebyshevT(n, x) - OrthogonalPolynomials.ChebyshevT(n - 1, x)));
         }
     }
 }
Exemplo n.º 12
0
 public void ChebyshevCosine()
 {
     // test T_n(cos t) = cos(n t)
     foreach (int n in TestUtilities.GenerateIntegerValues(1, 100, 5))
     {
         foreach (double t in GenerateRandomAngles(-Math.PI, Math.PI, 5))
         {
             Assert.IsTrue(TestUtilities.IsNearlyEqual(OrthogonalPolynomials.ChebyshevT(n, Math.Cos(t)), Math.Cos(n * t)));
         }
     }
 }
Exemplo n.º 13
0
 public void LaguerreSpecialCases()
 {
     foreach (int n in TestUtilities.GenerateIntegerValues(1, 100, 5))
     {
         Assert.IsTrue(OrthogonalPolynomials.LaguerreL(n, 0.0) == 1.0);
     }
     foreach (double x in TestUtilities.GenerateRealValues(1.0E-4, 1.0E4, 5))
     {
         Assert.IsTrue(OrthogonalPolynomials.LaguerreL(0, x) == 1.0);
     }
 }
Exemplo n.º 14
0
 public void LegendreSpecialCases()
 {
     foreach (double x in bArguments)
     {
         Assert.IsTrue(OrthogonalPolynomials.LegendreP(0, x) == 1.0);
         Assert.IsTrue(OrthogonalPolynomials.LegendreP(1, x) == x);
     }
     foreach (int n in orders)
     {
         Assert.IsTrue(OrthogonalPolynomials.LegendreP(n, 1.0) == 1.0);
     }
 }
Exemplo n.º 15
0
 public void AssociatedLegendreAgreement()
 {
     foreach (int l in TestUtilities.GenerateIntegerValues(1, 100, 5))
     {
         foreach (double x in TestUtilities.GenerateRealValues(0.01, 1.0, 5))
         {
             Assert.IsTrue(TestUtilities.IsNearlyEqual(
                               OrthogonalPolynomials.LegendreP(l, 0, x), OrthogonalPolynomials.LegendreP(l, x)
                               ));
         }
     }
 }
Exemplo n.º 16
0
 public void AssociatedLegendreLowOrders()
 {
     foreach (double x in TestUtilities.GenerateRealValues(0.01, 1.0, 10))
     {
         Assert.IsTrue(TestUtilities.IsNearlyEqual(OrthogonalPolynomials.LegendreP(0, 0, x), 1.0));
         Assert.IsTrue(TestUtilities.IsNearlyEqual(OrthogonalPolynomials.LegendreP(1, 0, x), x));
         Assert.IsTrue(TestUtilities.IsNearlyEqual(OrthogonalPolynomials.LegendreP(1, 1, x), -Math.Sqrt(1.0 - x * x)));
         Assert.IsTrue(TestUtilities.IsNearlyEqual(OrthogonalPolynomials.LegendreP(2, 0, x), (3.0 * x * x - 1.0) / 2.0));
         Assert.IsTrue(TestUtilities.IsNearlyEqual(OrthogonalPolynomials.LegendreP(2, 1, x), -3.0 * x * Math.Sqrt(1.0 - x * x)));
         Assert.IsTrue(TestUtilities.IsNearlyEqual(OrthogonalPolynomials.LegendreP(2, 2, x), 3.0 * (1.0 - x * x)));
     }
 }
Exemplo n.º 17
0
 public void LegendreTuronInequality()
 {
     foreach (int n in orders)
     {
         foreach (double x in bArguments)
         {
             Assert.IsTrue(
                 MoreMath.Pow(OrthogonalPolynomials.LegendreP(n, x), 2) >=
                 OrthogonalPolynomials.LegendreP(n - 1, x) * OrthogonalPolynomials.LegendreP(n + 1, x)
                 );
         }
     }
 }
Exemplo n.º 18
0
 public void LaguerreRecurrence()
 {
     foreach (int n in TestUtilities.GenerateRealValues(1.0, 1.0E2, 5))
     {
         foreach (double x in TestUtilities.GenerateRealValues(1.0E-4, 1.0E4, 10))
         {
             double LP = OrthogonalPolynomials.LaguerreL(n + 1, x);
             double L  = OrthogonalPolynomials.LaguerreL(n, x);
             double LM = OrthogonalPolynomials.LaguerreL(n - 1, x);
             Assert.IsTrue(TestUtilities.IsSumNearlyEqual((n + 1) * LP, n * LM, (2 * n + 1 - x) * L));
         }
     }
 }
Exemplo n.º 19
0
 public void HermiteRecurrence()
 {
     foreach (int n in TestUtilities.GenerateIntegerValues(1, 100, 10))
     {
         foreach (double x in TestUtilities.GenerateRealValues(1.0, 1000.0, 10))
         {
             Console.WriteLine("n={0} x={1}", n, x);
             Assert.IsTrue(TestUtilities.IsSumNearlyEqual(
                               OrthogonalPolynomials.HermiteH(n + 1, x), 2.0 * n * OrthogonalPolynomials.HermiteH(n - 1, x),
                               2.0 * x * OrthogonalPolynomials.HermiteH(n, x)
                               ));
         }
     }
 }
Exemplo n.º 20
0
        public void HermiteNormalExpectation()
        {
            // Wikipedia article on Hermite polynomials
            // X ~ N(\mu, 1) => E(He_n(X)) = \mu^n

            NormalDistribution d = new NormalDistribution(2.0, 1.0);

            foreach (int n in TestUtilities.GenerateIntegerValues(1, 32, 7))
            {
                Assert.IsTrue(TestUtilities.IsNearlyEqual(
                                  d.ExpectationValue(x => OrthogonalPolynomials.HermiteHe(n, x)),
                                  MoreMath.Pow(d.Mean, n)
                                  ));
            }
        }
Exemplo n.º 21
0
 public void AssociatedLaguerreSpecialCases()
 {
     foreach (double a in TestUtilities.GenerateRealValues(0.01, 100.0, 5))
     {
         foreach (double x in TestUtilities.GenerateRealValues(0.01, 100.0, 5))
         {
             Assert.IsTrue(TestUtilities.IsNearlyEqual(
                               OrthogonalPolynomials.LaguerreL(0, a, x), 1.0
                               ));
             Assert.IsTrue(TestUtilities.IsNearlyEqual(
                               OrthogonalPolynomials.LaguerreL(1, a, x), 1.0 + a - x
                               ));
         }
     }
 }
Exemplo n.º 22
0
        public void AssociatedLaguerreOrthonormality()
        {
            // don't let orders get too big, or (1) the Gamma function will overflow and (2) our integral will become highly oscilatory
            foreach (int n in TestUtilities.GenerateIntegerValues(1, 10, 3))
            {
                foreach (int m in TestUtilities.GenerateIntegerValues(1, 10, 3))
                {
                    foreach (double a in TestUtilities.GenerateRealValues(0.1, 10.0, 5))
                    {
                        //int n = 2;
                        //int m = 4;
                        //double a = 3.5;

                        Console.WriteLine("n={0} m={1} a={2}", n, m, a);

                        // evaluate the orthonormal integral
                        Func <double, double> f = delegate(double x) {
                            return(Math.Pow(x, a) * Math.Exp(-x) *
                                   OrthogonalPolynomials.LaguerreL(m, a, x) *
                                   OrthogonalPolynomials.LaguerreL(n, a, x)
                                   );
                        };
                        Interval r = Interval.FromEndpoints(0.0, Double.PositiveInfinity);

                        // need to loosen default evaluation settings in order to get convergence in some of these cases
                        // seems to have most convergence problems for large a
                        IntegrationSettings e = new IntegrationSettings();
                        e.AbsolutePrecision = TestUtilities.TargetPrecision;
                        e.RelativePrecision = TestUtilities.TargetPrecision;

                        double I = FunctionMath.Integrate(f, r, e).Value;
                        Console.WriteLine(I);

                        // test for orthonormality
                        if (n == m)
                        {
                            Assert.IsTrue(TestUtilities.IsNearlyEqual(
                                              I, AdvancedMath.Gamma(n + a + 1) / AdvancedIntegerMath.Factorial(n)
                                              ));
                        }
                        else
                        {
                            Assert.IsTrue(Math.Abs(I) < TestUtilities.TargetPrecision);
                        }
                    }
                }
            }
        }
Exemplo n.º 23
0
 public void ZernikeSpecialCases()
 {
     foreach (double x in TestUtilities.GenerateUniformRealValues(0.0, 1.0, 4))
     {
         Console.WriteLine(x);
         Assert.IsTrue(TestUtilities.IsNearlyEqual(OrthogonalPolynomials.ZernikeR(0, 0, x), 1.0));
         Assert.IsTrue(TestUtilities.IsNearlyEqual(OrthogonalPolynomials.ZernikeR(1, 1, x), x));
         Assert.IsTrue(TestUtilities.IsNearlyEqual(OrthogonalPolynomials.ZernikeR(2, 0, x), 2.0 * x * x - 1.0));
         Assert.IsTrue(TestUtilities.IsNearlyEqual(OrthogonalPolynomials.ZernikeR(2, 2, x), x * x));
         Assert.IsTrue(TestUtilities.IsNearlyEqual(OrthogonalPolynomials.ZernikeR(3, 1, x), 3.0 * x * x * x - 2.0 * x));
         Assert.IsTrue(TestUtilities.IsNearlyEqual(OrthogonalPolynomials.ZernikeR(3, 3, x), x * x * x));
         Assert.IsTrue(TestUtilities.IsNearlyEqual(OrthogonalPolynomials.ZernikeR(4, 0, x), 6.0 * x * x * x * x - 6.0 * x * x + 1.0));
         Assert.IsTrue(TestUtilities.IsNearlyEqual(OrthogonalPolynomials.ZernikeR(4, 2, x), 4.0 * x * x * x * x - 3.0 * x * x));
         Assert.IsTrue(TestUtilities.IsNearlyEqual(OrthogonalPolynomials.ZernikeR(4, 4, x), x * x * x * x));
     }
 }
Exemplo n.º 24
0
 public void AssociatedLaguerreAlphaRecurrence()
 {
     foreach (int n in TestUtilities.GenerateIntegerValues(1, 100, 5))
     {
         foreach (double a in TestUtilities.GenerateRealValues(0.01, 100.0, 5))
         {
             foreach (double x in TestUtilities.GenerateRealValues(0.01, 100.0, 5))
             {
                 Assert.IsTrue(TestUtilities.IsSumNearlyEqual(
                                   OrthogonalPolynomials.LaguerreL(n, a - 1.0, x), OrthogonalPolynomials.LaguerreL(n - 1, a, x),
                                   OrthogonalPolynomials.LaguerreL(n, a, x)
                                   ));
             }
         }
     }
 }
Exemplo n.º 25
0
 public void AssociatedLegendreRecurrenceL()
 {
     foreach (int l in TestUtilities.GenerateIntegerValues(1, 100, 4))
     {
         foreach (int m in TestUtilities.GenerateUniformIntegerValues(0, l - 1, 4))
         {
             foreach (double x in TestUtilities.GenerateRealValues(0.01, 1.0, 4))
             {
                 Assert.IsTrue(TestUtilities.IsSumNearlyEqual(
                                   (l - m + 1) * OrthogonalPolynomials.LegendreP(l + 1, m, x),
                                   (l + m) * OrthogonalPolynomials.LegendreP(l - 1, m, x),
                                   (2 * l + 1) * x * OrthogonalPolynomials.LegendreP(l, m, x)
                                   ));
             }
         }
     }
 }
Exemplo n.º 26
0
 public void SphericalHarmonicLegendre()
 {
     foreach (int ell in TestUtilities.GenerateIntegerValues(1, 100, 6))
     {
         foreach (double theta in GenerateRandomAngles(-Math.PI, Math.PI, 5))
         {
             // this loop shouldn't matter since the answer is phi-independent
             foreach (double phi in GenerateRandomAngles(0.0, 2.0 * Math.PI, 3))
             {
                 Complex Y  = AdvancedMath.SphericalHarmonic(ell, 0, theta, phi);
                 double  LP = Math.Sqrt((2 * ell + 1) / (4.0 * Math.PI)) * OrthogonalPolynomials.LegendreP(ell, Math.Cos(theta));
                 Console.WriteLine("l={0}, m=0, t={1}, p={2}, Y={3}, LP={4}", ell, theta, phi, Y, LP);
                 Assert.IsTrue(TestUtilities.IsNearlyEqual(Y, LP));
             }
         }
     }
 }
Exemplo n.º 27
0
 public void LegendreReflection()
 {
     foreach (int n in orders)
     {
         foreach (double x in bArguments)
         {
             if (n % 2 == 0)
             {
                 Assert.IsTrue(OrthogonalPolynomials.LegendreP(n, -x) == OrthogonalPolynomials.LegendreP(n, x));
             }
             else
             {
                 Assert.IsTrue(OrthogonalPolynomials.LegendreP(n, -x) == -OrthogonalPolynomials.LegendreP(n, x));
             }
         }
     }
 }
Exemplo n.º 28
0
        public void LaguerreOrthonormality()
        {
            // to avoid highly oscilatory integrals, don't use very high orders
            int[] orders = TestUtilities.GenerateIntegerValues(1, 30, 3);
            for (int i = 0; i < orders.Length; i++)
            {
                int n = orders[i];
                for (int j = 0; j <= i; j++)
                {
                    int m = orders[j];

                    Func <double, double> f = delegate(double x) {
                        return(Math.Exp(-x) * OrthogonalPolynomials.LaguerreL(n, x) * OrthogonalPolynomials.LaguerreL(m, x));
                    };
                    Interval r = Interval.FromEndpoints(0.0, Double.PositiveInfinity);
                    double   I = FunctionMath.Integrate(f, r);
                    Console.WriteLine("{0} {1} {2}", n, m, I);
                    if (n == m)
                    {
                        Assert.IsTrue(TestUtilities.IsNearlyEqual(I, 1.0));
                    }
                    else
                    {
                        Assert.IsTrue(Math.Abs(I) < TestUtilities.TargetPrecision);
                    }
                }
            }

            /*
             * foreach (int n in TestUtilities.GenerateIntegerValues(1, 100, 3)) {
             *  foreach (int m in TestUtilities.GenerateIntegerValues(1, 100, 3)) {
             *      Function<double, double> f = delegate(double x) {
             *          return (Math.Exp(-x) * OrthogonalPolynomials.LaguerreL(n, x) * OrthogonalPolynomials.LaguerreL(m, x));
             *      };
             *      Interval r = Interval.FromEndpoints(0.0, Double.PositiveInfinity);
             *      double I = FunctionMath.Integrate(f, r);
             *      Console.WriteLine("{0} {1} {2}", n, m, I);
             *      if (n == m) {
             *          Assert.IsTrue(TestUtilities.IsNearlyEqual(I, 1.0));
             *      } else {
             *          Assert.IsTrue(Math.Abs(I) < TestUtilities.TargetPrecision);
             *      }
             *  }
             * }
             */
        }
Exemplo n.º 29
0
        public void HypergeometricPolynomials()
        {
            foreach (int n in TestUtilities.GenerateUniformIntegerValues(0, 10, 4))
            {
                foreach (double x in TestUtilities.GenerateRealValues(1.0E-2, 1.0, 4))
                {
                    Assert.IsTrue(TestUtilities.IsNearlyEqual(
                                      AdvancedMath.Hypergeometric2F1(-n, n, 0.5, x),
                                      OrthogonalPolynomials.ChebyshevT(n, 1.0 - 2.0 * x)
                                      ));

                    Assert.IsTrue(TestUtilities.IsNearlyEqual(
                                      AdvancedMath.Hypergeometric2F1(-n, n + 1, 1.0, x),
                                      OrthogonalPolynomials.LegendreP(n, 1.0 - 2.0 * x)
                                      ));
                }
            }
        }
Exemplo n.º 30
0
 public void AssociatedLegendreRecurrenceM()
 {
     foreach (int l in TestUtilities.GenerateIntegerValues(1, 100, 4))
     {
         foreach (int m in TestUtilities.GenerateUniformIntegerValues(0, l - 1, 4))
         {
             foreach (double x in TestUtilities.GenerateRealValues(0.1, 1.0, 4))
             {
                 Console.WriteLine("l={0} m={1} x={2}", l, m, x);
                 Assert.IsTrue(TestUtilities.IsSumNearlyEqual(
                                   OrthogonalPolynomials.LegendreP(l, m + 1, x),
                                   (l + m) * (l - m + 1) * OrthogonalPolynomials.LegendreP(l, m - 1, x),
                                   -2 * m * x / Math.Sqrt(1.0 - x * x) * OrthogonalPolynomials.LegendreP(l, m, x)
                                   ));
             }
         }
     }
 }