Exemplo n.º 1
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));
     }
 }
Exemplo n.º 2
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);
            }
        }
Exemplo n.º 3
0
 public void ComplexLogSum()
 {
     Complex[] z = TestUtilities.GenerateComplexValues(1.0E-4, 1.0E4, 10);
     for (int i = 0; i < z.Length; i++)
     {
         for (int j = 0; j < i; j++)
         {
             if (Math.Abs(ComplexMath.Arg(z[i]) + ComplexMath.Arg(z[j])) >= Math.PI)
             {
                 continue;
             }
             Console.WriteLine("{0} {1}", z[i], z[j]);
             Console.WriteLine("ln(z1) + ln(z2) = {0}", ComplexMath.Log(z[i]) + ComplexMath.Log(z[j]));
             Console.WriteLine("ln(z1*z2) = {0}", ComplexMath.Log(z[i] * z[j]));
             Assert.IsTrue(TestUtilities.IsSumNearlyEqual(ComplexMath.Log(z[i]), ComplexMath.Log(z[j]), ComplexMath.Log(z[i] * z[j])));
         }
     }
 }
Exemplo n.º 4
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));
            }
        }
Exemplo n.º 5
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));
         }
     }
 }