public void Main()
        {
            // Create a matrix containing the
            // lower triangular part of the Hermitian matrix
            // whose eigenvalues must be computed.
            var matrix = ComplexMatrix.Dense(2, 2, new Complex[4] {
                new Complex(1, 0), new Complex(-3, -2),
                new Complex(5, 4), new Complex(-6, 0)
            }, StorageOrder.RowMajor);

            // Set the relevant triangular part.
            bool lowerTriangularPart = false;

            // Compute the eigenvalues.
            var eigenvalues = SpectralDecomposition.GetEigenvalues(
                matrix,
                lowerTriangularPart);

            Console.WriteLine("Matrix whose eigenvalues must be computed:");
            Console.WriteLine(ComplexMatrix.Dense(2, 2, new Complex[4] {
                new Complex(1, 0), new Complex(-3, -2),
                new Complex(-3, 2), new Complex(-6, 0)
            }, StorageOrder.RowMajor));


            Console.WriteLine("Matrix eigenvalues:");
            Console.WriteLine(eigenvalues);
        }
Пример #2
0
            /// <summary>
            /// Tests the operation
            /// when its operand is set through a value represented by a <b>null</b> instance.
            /// </summary>
            public static void MatrixIsNull()
            {
                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    SpectralDecomposition.GetEigenvalues(
                        matrix: (DoubleMatrix)null,
                        lowerTriangularPart: true);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage: ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: "matrix");

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    SpectralDecomposition.GetEigenvalues(
                        matrix: (ReadOnlyDoubleMatrix)null,
                        lowerTriangularPart: true);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage: ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: "matrix");

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    SpectralDecomposition.GetEigenvalues(
                        matrix: (ComplexMatrix)null,
                        lowerTriangularPart: true);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage: ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: "matrix");

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    SpectralDecomposition.GetEigenvalues(
                        matrix: (ReadOnlyComplexMatrix)null,
                        lowerTriangularPart: true);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage: ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: "matrix");
            }
Пример #3
0
            /// Tests that method
            /// <see cref="SpectralDecomposition
            /// .GetEigenvalues(ComplexMatrix)"/>,
            /// method has
            /// been properly implemented.
            public static void Succeed(
                TestableSpectralDecomposition <TestableComplexMatrix, ComplexMatrix> testableSD)
            {
                var testableMatrix = testableSD.TestableMatrix;

                #region Writable

                // Dense, Lower
                {
                    var actualValues = SpectralDecomposition.GetEigenvalues(
                        testableMatrix.AsDense,
                        lowerTriangularPart: true);

                    DoubleMatrixAssert.AreEqual(
                        expected: GetMainDiagonal(testableSD.Values),
                        actual: actualValues,
                        delta: SpectralDecompositionTest.Accuracy);
                }

                // Sparse, Lower
                {
                    var actualValues = SpectralDecomposition.GetEigenvalues(
                        testableMatrix.AsSparse,
                        lowerTriangularPart: true);

                    DoubleMatrixAssert.AreEqual(
                        expected: GetMainDiagonal(testableSD.Values),
                        actual: actualValues,
                        delta: SpectralDecompositionTest.Accuracy);
                }

                // Dense, Upper
                {
                    var actualValues = SpectralDecomposition.GetEigenvalues(
                        testableMatrix.AsDense,
                        lowerTriangularPart: false);

                    DoubleMatrixAssert.AreEqual(
                        expected: GetMainDiagonal(testableSD.Values),
                        actual: actualValues,
                        delta: SpectralDecompositionTest.Accuracy);
                }

                // Sparse, Upper
                {
                    var actualValues = SpectralDecomposition.GetEigenvalues(
                        testableMatrix.AsSparse,
                        lowerTriangularPart: false);

                    DoubleMatrixAssert.AreEqual(
                        expected: GetMainDiagonal(testableSD.Values),
                        actual: actualValues,
                        delta: SpectralDecompositionTest.Accuracy);
                }

                #endregion

                #region ReadOnly

                // Dense, Lower
                {
                    var actualValues = SpectralDecomposition.GetEigenvalues(
                        testableMatrix.AsDense.AsReadOnly(),
                        lowerTriangularPart: true);

                    DoubleMatrixAssert.AreEqual(
                        expected: GetMainDiagonal(testableSD.Values),
                        actual: actualValues,
                        delta: SpectralDecompositionTest.Accuracy);
                }

                // Sparse, Lower
                {
                    var actualValues = SpectralDecomposition.GetEigenvalues(
                        testableMatrix.AsSparse.AsReadOnly(),
                        lowerTriangularPart: true);

                    DoubleMatrixAssert.AreEqual(
                        expected: GetMainDiagonal(testableSD.Values),
                        actual: actualValues,
                        delta: SpectralDecompositionTest.Accuracy);
                }

                // Dense, Upper
                {
                    var actualValues = SpectralDecomposition.GetEigenvalues(
                        testableMatrix.AsDense.AsReadOnly(),
                        lowerTriangularPart: false);

                    DoubleMatrixAssert.AreEqual(
                        expected: GetMainDiagonal(testableSD.Values),
                        actual: actualValues,
                        delta: SpectralDecompositionTest.Accuracy);
                }

                // Sparse, Upper
                {
                    var actualValues = SpectralDecomposition.GetEigenvalues(
                        testableMatrix.AsSparse.AsReadOnly(),
                        lowerTriangularPart: false);

                    DoubleMatrixAssert.AreEqual(
                        expected: GetMainDiagonal(testableSD.Values),
                        actual: actualValues,
                        delta: SpectralDecompositionTest.Accuracy);
                }

                #endregion
            }