Beispiel #1
0
        public static bool IsNearlyEqual(Complex x, Complex y, EvaluationSettings s)
        {
            double m = ComplexMath.Abs(x) + ComplexMath.Abs(y);
            double e = s.AbsolutePrecision + m * s.RelativePrecision;

            return(ComplexMath.Abs(x - y) <= e);
        }
Beispiel #2
0
 public void ComplexAbsSquared()
 {
     foreach (Complex z in TestUtilities.GenerateComplexValues(1.0E-4, 1.0E4, 10))
     {
         double abs = ComplexMath.Abs(z);
         Assert.IsTrue(TestUtilities.IsNearlyEqual(abs * abs, z * z.Conjugate));
     }
 }
 public void ComplexGammaInequality()
 {
     foreach (Complex z in TestUtilities.GenerateComplexValues(1.0E-2, 1.0E2, 10))
     {
         Console.WriteLine(z);
         Assert.IsTrue(ComplexMath.Abs(AdvancedComplexMath.Gamma(z)) <= Math.Abs(AdvancedMath.Gamma(z.Re)));
     }
 }
Beispiel #4
0
 public void ComplexAbsSpecialValues()
 {
     Assert.IsTrue(ComplexMath.Abs(Complex.Zero) == 0.0);
     Assert.IsTrue(ComplexMath.Abs(Complex.One) == 1.0);
     Assert.IsTrue(ComplexMath.Abs(-Complex.One) == 1.0);
     Assert.IsTrue(ComplexMath.Abs(Complex.I) == 1.0);
     Assert.IsTrue(ComplexMath.Abs(-Complex.I) == 1.0);
 }
Beispiel #5
0
 public void ComplexAbsExtremeValues()
 {
     Assert.IsTrue(ComplexMath.Abs(Double.MaxValue) == Double.MaxValue);
     Assert.IsTrue(ComplexMath.Abs(-Double.MaxValue) == Double.MaxValue);
     Assert.IsTrue(ComplexMath.Abs(Complex.I * Double.MaxValue) == Double.MaxValue);
     Assert.IsTrue(ComplexMath.Abs(Double.PositiveInfinity) == Double.PositiveInfinity);
     Assert.IsTrue(ComplexMath.Abs(Double.NegativeInfinity) == Double.PositiveInfinity);
     Assert.IsTrue(Complex.IsNaN(ComplexMath.Abs(Double.NaN)));
 }
Beispiel #6
0
 public void ComplexSqrt()
 {
     foreach (Complex z in TestUtilities.GenerateComplexValues(1.0E-4, 1.0E4, 32))
     {
         Complex sz = ComplexMath.Sqrt(z);
         Assert.IsTrue(TestUtilities.IsNearlyEqual(ComplexMath.Sqr(sz), z, TestUtilities.RelativeTarget));
         Assert.IsTrue(TestUtilities.IsNearlyEqual(ComplexMath.Arg(z) / 2.0, ComplexMath.Arg(sz), TestUtilities.RelativeTarget));
         Assert.IsTrue(TestUtilities.IsNearlyEqual(Math.Sqrt(ComplexMath.Abs(z)), ComplexMath.Abs(sz), TestUtilities.RelativeTarget));
     }
 }
Beispiel #7
0
 public static bool IsNearlyEqual(Complex u, Complex v, double e)
 {
     if (ComplexMath.Abs(u - v) <= e * (ComplexMath.Abs(u) + ComplexMath.Abs(v)))
     {
         return(true);
     }
     else
     {
         return(false);
     }
 }
Beispiel #8
0
 private IntegrationResult RambleIntegral(int d, int s, IntegrationSettings settings)
 {
     return(MultiFunctionMath.Integrate((IReadOnlyList <double> x) => {
         Complex z = 0.0;
         for (int k = 0; k < d; k++)
         {
             z += ComplexMath.Exp(2.0 * Math.PI * Complex.I * x[k]);
         }
         return (MoreMath.Pow(ComplexMath.Abs(z), s));
     }, UnitCube(d), settings));
 }
 public void ComplexGammaInequalities()
 {
     foreach (Complex z in TestUtilities.GenerateComplexValues(1.0E-2, 1.0E2, 16))
     {
         Assert.IsTrue(ComplexMath.Abs(AdvancedComplexMath.Gamma(z)) <= Math.Abs(AdvancedMath.Gamma(z.Re)));
         if (z.Re >= 0.5)
         {
             Assert.IsTrue(ComplexMath.Abs(AdvancedComplexMath.Gamma(z)) >= Math.Abs(AdvancedMath.Gamma(z.Re)) / Math.Sqrt(Math.Cosh(Math.PI * z.Im)));
         }
     }
 }
Beispiel #10
0
        public void ComplexRealAgreement()
        {
            foreach (double x in TestUtilities.GenerateRealValues(0.01, 1000.0, 8))
            {
                Assert.IsTrue(ComplexMath.Exp(x) == Math.Exp(x));
                Assert.IsTrue(ComplexMath.Log(x) == Math.Log(x));
                Assert.IsTrue(ComplexMath.Sqrt(x) == Math.Sqrt(x));

                Assert.IsTrue(ComplexMath.Abs(x) == Math.Abs(x));
                Assert.IsTrue(ComplexMath.Arg(x) == 0.0);
            }
        }
Beispiel #11
0
        public static bool IsSumNearlyEqual(IEnumerable <Complex> zs, Complex zz)
        {
            Complex sum   = 0.0;
            double  error = 0.0;

            foreach (Complex z in zs)
            {
                sum   += z;
                error += TargetPrecision * ComplexMath.Abs(z);
            }
            error += TargetPrecision * ComplexMath.Abs(zz);

            return(ComplexMath.Abs(sum - zz) <= error);
        }
Beispiel #12
0
 public void Bug5504()
 {
     // this eigenvalue non-convergence is solved by an ad hoc shift
     // these "cyclic matrices" are good examples of situations that are difficult for the QR algorithm
     for (int d = 2; d <= 8; d++)
     {
         Console.WriteLine(d);
         SquareMatrix C       = CyclicMatrix(d);
         Complex[]    lambdas = C.Eigenvalues();
         foreach (Complex lambda in lambdas)
         {
             Console.WriteLine("{0} ({1} {2})", lambda, ComplexMath.Abs(lambda), ComplexMath.Arg(lambda));
             Assert.IsTrue(TestUtilities.IsNearlyEqual(ComplexMath.Abs(lambda), 1.0));
         }
     }
 }
Beispiel #13
0
        public static bool IsSumNearlyEqual(Complex z1, Complex z2, Complex zz)
        {
            Complex z = z1 + z2;

            double u1 = ComplexMath.Abs(z1) * TargetPrecision;
            double u2 = ComplexMath.Abs(z2) * TargetPrecision;
            double u  = u1 + u2;
            double uu = ComplexMath.Abs(zz) * TargetPrecision;

            if (2.0 * ComplexMath.Abs(z - zz) <= (u + uu))
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Beispiel #14
0
        public void DifficultEigenvalue()
        {
            // This is from a paper describing difficult eigenvalue problems.
            // https://www.mathworks.com/company/newsletters/news_notes/pdf/sum95cleve.pdf

            SquareMatrix A = new SquareMatrix(4);

            A[0, 0] = 0.0; A[0, 1] = 2.0; A[0, 2] = 0.0; A[0, 3] = -1.0;
            A[1, 0] = 1.0; A[1, 1] = 0.0; A[1, 2] = 0.0; A[1, 3] = 0.0;
            A[2, 0] = 0.0; A[2, 1] = 1.0; A[2, 2] = 0.0; A[2, 3] = 0.0;
            A[3, 0] = 0.0; A[3, 1] = 0.0; A[3, 2] = 1.0; A[3, 3] = 0.0;

            Complex[] zs = A.Eigenvalues();
            foreach (Complex z in zs)
            {
                Console.WriteLine("{0} ({1} {2})", z, ComplexMath.Abs(z), ComplexMath.Arg(z));
            }
        }
Beispiel #15
0
        public static bool IsNearlyEigenpair(AnySquareMatrix A, Complex[] v, Complex a)
        {
            int d = A.Dimension;

            // compute products
            Complex[] Av = new Complex[d];
            for (int i = 0; i < d; i++)
            {
                Av[i] = 0.0;
                for (int j = 0; j < d; j++)
                {
                    Av[i] += A[i, j] * v[j];
                }
            }
            Complex[] av = new Complex[d];
            for (int i = 0; i < d; i++)
            {
                av[i] = a * v[i];
            }

            // compute tolorance
            double N = MatrixNorm(A);
            double n = 0.0;

            for (int i = 0; i < d; i++)
            {
                n += ComplexMath.Abs(v[i]);
            }
            double ep = TargetPrecision * (N * n / d + ComplexMath.Abs(a) * n);

            // compare elements within tollerance
            for (int i = 0; i < d; i++)
            {
                if (ComplexMath.Abs(Av[i] - av[i]) > ep)
                {
                    return(false);
                }
            }
            return(true);
        }
Beispiel #16
0
        public static bool IsNearlyEigenpair(AnySquareMatrix A, ComplexColumnVector v, Complex a)
        {
            int d = A.Dimension;

            // compute products

            /*
             * Complex[] Av = new Complex[d];
             * for (int i=0; i<d; i++) {
             *  Av[i] = 0.0;
             *  for (int j=0; j<d; j++) {
             *      Av[i] += A[i,j]*v[j];
             *  }
             * }
             */
            ComplexColumnVector Av = A * v;
            ComplexColumnVector av = a * v;

            // compute tolerance
            double N = A.MaxNorm();
            double n = 0.0;

            for (int i = 0; i < d; i++)
            {
                n += ComplexMath.Abs(v[i]);
            }
            double ep = TargetPrecision * (N * n / d + ComplexMath.Abs(a) * n);

            // compare elements within tolerance
            for (int i = 0; i < d; i++)
            {
                if (ComplexMath.Abs(Av[i] - av[i]) > ep)
                {
                    return(false);
                }
            }
            return(true);
        }
Beispiel #17
0
        public void FourierSpecialCases()
        {
            for (int n = 2; n <= 10; n++)
            {
                FourierTransformer ft = new FourierTransformer(n);
                Assert.IsTrue(ft.Length == n);

                // transform of uniform is zero frequency only
                Complex[] x1 = new Complex[n];
                for (int i = 0; i < n; i++)
                {
                    x1[i] = 1.0;
                }
                Complex[] y1 = ft.Transform(x1);
                Assert.IsTrue(TestUtilities.IsNearlyEqual(y1[0], n));
                for (int i = 1; i < n; i++)
                {
                    Assert.IsTrue(ComplexMath.Abs(y1[i]) < TestUtilities.TargetPrecision);
                }
                Complex[] z1 = ft.InverseTransform(y1);
                for (int i = 0; i < n; i++)
                {
                    Assert.IsTrue(TestUtilities.IsNearlyEqual(z1[i], 1.0));
                }

                // transform of pulse at 1 are nth roots of unity, read clockwise
                Complex[] x2 = new Complex[n];
                x2[1] = 1.0;
                Complex[] y2 = ft.Transform(x2);
                for (int i = 0; i < n; i++)
                {
                    double t = -2.0 * Math.PI / n * i;
                    Assert.IsTrue(TestUtilities.IsNearlyEqual(y2[i], new Complex(Math.Cos(t), Math.Sin(t))));
                }
            }
        }