Ejemplo n.º 1
0
        public void Main()
        {
            // Set matrix dimensions.
            int numberOfRows    = 3;
            int numberOfColumns = 2;

            // Create the data.
            var data = new List <Complex>(6)
            {
                new Complex(1, -1),
                new Complex(2, -2),
                new Complex(3, -3),
                new Complex(4, -4),
                new Complex(5, -5),
                new Complex(6, -6)
            } as IEnumerable <Complex>;

            // Create the matrix. Data are assumed as ColMajor ordered.
            var matrix = ComplexMatrix.Dense(
                numberOfRows, numberOfColumns, data);

            Console.WriteLine("Assuming ColMajor ordered data.");
            Console.WriteLine("The data matrix:");
            Console.WriteLine(matrix);
        }
        public void Main()
        {
            // Create a matrix.
            var data = new Complex[6] {
                new Complex(0, 0), new Complex(5, -5),
                new Complex(2, -2), new Complex(0, 0),
                new Complex(0, 0), new Complex(2, -2)
            };
            var matrix = ComplexMatrix.Dense(3, 2, data, StorageOrder.RowMajor);

            Console.WriteLine("The data matrix:");
            Console.WriteLine(matrix);

            // Find the linear indexes of nonzero entries in data.
            var indexes = matrix.FindNonzero();

            Console.WriteLine();
            Console.WriteLine("Linear indexes of nonzero entries in data:");
            Console.WriteLine(indexes);

            // FindNonzero is available for read-only matrices:
            // find nonzero entries using a read-only wrapper of the data matrix.
            ReadOnlyComplexMatrix readOnlyMatrix = matrix.AsReadOnly();

            indexes = readOnlyMatrix.FindNonzero();

            Console.WriteLine();
            Console.WriteLine("Using read-only data. Linear indexes of nonzero entries:");
            Console.WriteLine(indexes);
        }
Ejemplo n.º 3
0
        public void Main()
        {
            // Create a matrix.
            var data = new Complex[8] {
                new Complex(1, -1), new Complex(5, -5),
                new Complex(2, -2), new Complex(6, -6),
                new Complex(3, -3), new Complex(7, -7),
                new Complex(4, -4), new Complex(8, -8)
            };
            var matrix = ComplexMatrix.Dense(4, 2, data, StorageOrder.RowMajor);

            Console.WriteLine("Data matrix:");
            Console.WriteLine(matrix);
            Console.WriteLine();

            // Get the collection of matrix rows.
            var rows = matrix.AsRowCollection();

            // Set a column index.
            var columnIndex = 1;

            // Set the row entries corresponding to the specified column index:
            // this code updates the data matrix, too.
            foreach (var row in rows)
            {
                row[columnIndex] = new Complex(row.Index * 100.0, 0);
            }

            Console.WriteLine("Updated data matrix:");
            Console.WriteLine(matrix);
            Console.WriteLine();
        }
Ejemplo n.º 4
0
        public void Main()
        {
            // Set matrix dimensions.
            int numberOfRows    = 3;
            int numberOfColumns = 2;

            // Create the data.
            var data = new Complex[6] {
                new Complex(1, -1), new Complex(5, -5),
                new Complex(2, -2), new Complex(6, -6),
                new Complex(3, -3), new Complex(7, -7)
            };

            // Assume the data as RowMajor ordered.
            StorageOrder storageOrder = StorageOrder.RowMajor;

            // Create the matrix.
            var matrix = ComplexMatrix.Dense(
                numberOfRows, numberOfColumns, data, storageOrder);

            Console.WriteLine("Assuming RowMajor ordered data.");
            Console.WriteLine("The data matrix:");
            Console.WriteLine(matrix);

            // Assume the data as ColMajor ordered.
            storageOrder = StorageOrder.ColumnMajor;

            // Create the matrix.
            matrix = ComplexMatrix.Dense(
                numberOfRows, numberOfColumns, data, storageOrder);
            Console.WriteLine("Assuming ColMajor ordered data.");
            Console.WriteLine("The data matrix:");
            Console.WriteLine(matrix);
        }
Ejemplo n.º 5
0
        public void Main()
        {
            // Create a matrix.
            var data = new Complex[8] {
                new Complex(1, -1), new Complex(5, -5),
                new Complex(2, -2), new Complex(6, -6),
                new Complex(3, -3), new Complex(7, -7),
                new Complex(4, -4), new Complex(8, -8)
            };
            var matrix = ComplexMatrix.Dense(4, 2, data, StorageOrder.RowMajor);

            Console.WriteLine("Data matrix:");
            Console.WriteLine(matrix);
            Console.WriteLine();

            // Specify the rows to enumerate.
            var rowIndexes = IndexCollection.FromArray(new int[6] {
                0, 0, 1, 2, 3, 2
            });

            // Get the collection of the specified matrix rows.
            var rows = matrix.AsRowCollection(rowIndexes);

            // Enumerate the specified matrix rows.
            foreach (var row in rows)
            {
                Console.WriteLine("Row {0}: ", row.Index);
                Console.WriteLine(row);
            }
        }
Ejemplo n.º 6
0
        public void Main()
        {
            // Create a matrix.
            var data = new Complex[4] {
                new Complex(1, -1), new Complex(5, -5),
                new Complex(2, -2), new Complex(6, -6)
            };
            var matrix = ComplexMatrix.Dense(2, 2, data, StorageOrder.RowMajor);

            Console.WriteLine("Initial data matrix:");
            Console.WriteLine(matrix);

            // Get the linear indexes of elements on
            // the main diagonal.
            var mainDiagonalIndexes = IndexCollection.Sequence(0, 3, 3);

            // Get the vectorization of the matrix main diagonal.
            var mainDiagonal = matrix.Vec(mainDiagonalIndexes);

            Console.WriteLine();
            Console.WriteLine("Vectorized main diagonal:");
            Console.WriteLine(mainDiagonal);

            // Entries can also be vectorized using a read-only wrapper of the matrix.
            ReadOnlyComplexMatrix readOnlyMatrix = matrix.AsReadOnly();

            Console.WriteLine();
            Console.WriteLine("Vectorized main diagonal of a read-only data matrix:");
            Console.WriteLine(readOnlyMatrix.Vec(mainDiagonalIndexes));
        }
        public void Main()
        {
            // Create a matrix.
            var data = new Complex[6] {
                new Complex(1, -1), new Complex(5, -5),
                new Complex(2, -2), new Complex(6, -6),
                new Complex(3, -3), new Complex(7, -7)
            };
            var matrix = ComplexMatrix.Dense(3, 2, data, StorageOrder.RowMajor);

            Console.WriteLine("The data matrix:");
            Console.WriteLine(matrix);

            // Return a matrix obtained by adding 1 to each entry
            // of the initial matrix.
            var plusOneMatrix = matrix.Apply((x) => x + 1);

            Console.WriteLine();
            Console.WriteLine("Matrix obtained by adding 1 to each entry:");
            Console.WriteLine(plusOneMatrix);

            // Add one using a read-only wrapper of the data matrix.
            ReadOnlyComplexMatrix readOnlyMatrix = matrix.AsReadOnly();
            var plusOneReadOnlyMatrix            = readOnlyMatrix.Apply((x) => x + 1);

            Console.WriteLine();
            Console.WriteLine("Adding 1 to each entry of a read only matrix:");
            Console.WriteLine(plusOneReadOnlyMatrix);
        }
Ejemplo n.º 8
0
        public void Main()
        {
            // Create the main diagonal data.
            var data = new Complex[2] {
                new Complex(1, -1),
                new Complex(2, -2)
            };

            // Create a matrix storing the main diagonal data.
            // Note that such matrix can have any size: if it is not
            // a vector, its entries will be inserted in the main
            // diagonal of the diagonal matrix assuming ColMajor ordering.
            var mainDiagonal = ComplexMatrix.Dense(
                2, 1, data);

            Console.WriteLine("The matrix storing main diagonal data:");
            Console.WriteLine(mainDiagonal);

            Console.WriteLine();

            // Create the diagonal matrix.
            var diagonalMatrix = ComplexMatrix.Diagonal(
                mainDiagonal);

            Console.WriteLine("The diagonal matrix:");
            Console.WriteLine(diagonalMatrix);
        }
        public void Main()
        {
            // Create a matrix.
            var data = new Complex[8] {
                new Complex(1, -1), new Complex(5, -5),
                new Complex(2, -2), new Complex(6, -6),
                new Complex(3, -3), new Complex(7, -7),
                new Complex(4, -4), new Complex(8, -8)
            };
            var matrix = ComplexMatrix.Dense(4, 2, data, StorageOrder.RowMajor);

            Console.WriteLine("Initial data matrix:");
            Console.WriteLine(matrix);

            // Specify a linear index.
            int linearIndex = 3;

            Console.WriteLine();
            Console.WriteLine("Linear index: {0}", linearIndex);

            // Set the corresponding entry.
            matrix[linearIndex] = new Complex(40, -40);

            Console.WriteLine();
            Console.WriteLine("Updated data matrix:");
            Console.WriteLine(matrix);

            // Entries can also be accessed using a read-only wrapper of the matrix.
            ReadOnlyComplexMatrix readOnlyMatrix = matrix.AsReadOnly();

            Console.WriteLine();
            Console.WriteLine("Updated matrix entry:");
            Console.WriteLine(readOnlyMatrix[linearIndex]);
        }
Ejemplo n.º 10
0
        public void ToComplexMatrixTest()
        {
            // value is null
            {
                ComplexMatrix actual = (ComplexMatrixRow)(null);

                Assert.IsNull(actual);

                actual = ComplexMatrixRow.ToComplexMatrix(null);

                Assert.IsNull(actual);
            }

            // value is not null
            {
                ComplexMatrix matrix = ComplexMatrix.Dense(2, 3,
                                                           new Complex[6] {
                    1, 2, 3, 4, 5, 6
                });
                var rows = matrix.AsRowCollection();

                for (int i = 0; i < matrix.NumberOfRows; i++)
                {
                    var           expected = matrix[i, ":"];
                    ComplexMatrix actual   = rows[i];
                    ComplexMatrixAssert.AreEqual(expected, actual, ComplexMatrixTest.Accuracy);

                    actual = ComplexMatrixRow.ToComplexMatrix(rows[i]);
                    ComplexMatrixAssert.AreEqual(expected, actual, ComplexMatrixTest.Accuracy);
                }
            }
        }
Ejemplo n.º 11
0
        public void Main()
        {
            // Create a matrix.
            var data = new Complex[8] {
                new Complex(1, -1), new Complex(5, -5),
                new Complex(2, -2), new Complex(6, -6),
                new Complex(3, -3), new Complex(7, -7),
                new Complex(4, -4), new Complex(8, -8)
            };
            var matrix = ComplexMatrix.Dense(4, 2, data, StorageOrder.RowMajor);

            Console.WriteLine("Data matrix:");
            Console.WriteLine(matrix);
            Console.WriteLine();

            // Get the collection of matrix rows.
            var rows = matrix.AsRowCollection();

            // Enumerate matrix rows.
            foreach (var row in rows)
            {
                Console.WriteLine("Row {0}: ", row.Index);
                Console.WriteLine(row);
            }
        }
Ejemplo n.º 12
0
        public void Main()
        {
            // Create a matrix.
            var data = new Complex[6] {
                new Complex(1, -1), new Complex(5, -5),
                new Complex(2, -2), new Complex(6, -6),
                new Complex(3, -3), new Complex(2, -2)
            };
            var matrix = ComplexMatrix.Dense(3, 2, data, StorageOrder.RowMajor);

            Console.WriteLine("The data matrix:");
            Console.WriteLine(matrix);

            // Set the value to search for.
            Complex value = new(2, -2);

            // Find entries equal to value (2, -2).
            var indexes = matrix.Find(value);

            Console.WriteLine();
            Console.WriteLine("Linear indexes of entries equal to (2, -2) in data:");
            Console.WriteLine(indexes);

            // Find is available for read-only matrices:
            // find entries equal to (2, -2) using a read-only wrapper of the data matrix.
            ReadOnlyComplexMatrix readOnlyMatrix = matrix.AsReadOnly();

            indexes = readOnlyMatrix.Find(value);

            Console.WriteLine();
            Console.WriteLine("Using read-only data. Linear indexes of entries equal to (2, -2):");
            Console.WriteLine(indexes);
        }
        public void ToComplexMatrixTest()
        {
            // value is null
            {
                ComplexMatrix actual = (ComplexMatrixRowCollection)(null);

                Assert.IsNull(actual);

                actual = ComplexMatrixRowCollection.ToComplexMatrix(null);

                Assert.IsNull(actual);
            }

            // value is not null
            {
                ComplexMatrix matrix = ComplexMatrix.Dense(2, 3,
                                                           new Complex[6] {
                    1, 2, 3, 4, 5, 6
                });
                var rows = matrix.AsRowCollection();

                var expected = matrix;

                var actual = (ComplexMatrix)rows;
                ComplexMatrixAssert.AreEqual(expected, actual, ComplexMatrixTest.Accuracy);

                actual = ComplexMatrixRowCollection.ToComplexMatrix(rows);
                ComplexMatrixAssert.AreEqual(expected, actual, ComplexMatrixTest.Accuracy);
            }
        }
Ejemplo n.º 14
0
        ///<inheritdoc cref="SingularValueDecomposition.Decompose(
        ///DoubleMatrix, out DoubleMatrix, out DoubleMatrix)"/>
        public static DoubleMatrix Decompose(
            ComplexMatrix matrix,
            out ComplexMatrix leftSingularVectors,
            out ComplexMatrix conjugateTransposedRightSingularVectors)
        {
            #region Input validation

            if (matrix is null)
            {
                throw new ArgumentNullException(nameof(matrix));
            }

            #endregion

            int m       = matrix.NumberOfRows;
            int n       = matrix.NumberOfColumns;
            int min_m_n = m < n ? m : n;

            Complex[] a  = matrix.AsColumnMajorDenseArray();
            double[]  s  = new double[min_m_n];
            Complex[] u  = new Complex[m * m];
            Complex[] vt = new Complex[n * n];
            int       lapackInfo;
            double[]  superb = new double[min_m_n - 1];
            lapackInfo = SafeNativeMethods.LAPACK.ZGESVD(
                matrix_layout: SafeNativeMethods.LAPACK.ORDER.ColMajor,
                jobu: 'A',
                jobvt: 'A',
                m,
                n,
                a,
                lda: m,
                s,
                u,
                ldu: m,
                vt,
                ldvt: n,
                superb);

            if (lapackInfo > 0)
            {
                throw new InvalidOperationException(
                          ImplementationServices.GetResourceString(
                              "STR_EXCEPT_ALG_DOES_NOT_CONVERGE"));
            }

            DoubleMatrix values = DoubleMatrix.Dense(m, n);
            for (int i = 0; i < min_m_n; i++)
            {
                values[i, i] = s[i];
            }

            leftSingularVectors =
                ComplexMatrix.Dense(m, m, u, copyData: false);

            conjugateTransposedRightSingularVectors =
                ComplexMatrix.Dense(n, n, vt, copyData: false);

            return(values);
        }
Ejemplo n.º 15
0
        public void Main()
        {
            // Create a matrix.
            var data = new Complex[8] {
                new Complex(1, -1), new Complex(5, -5),
                new Complex(2, -2), new Complex(6, -6),
                new Complex(3, -3), new Complex(7, -7),
                new Complex(4, -4), new Complex(8, -8)
            };
            var matrix = ComplexMatrix.Dense(4, 2, data, StorageOrder.RowMajor);

            Console.WriteLine("Initial data matrix:");
            Console.WriteLine(matrix);

            // Get the vectorization of the data matrix.
            var vectorized = matrix.Vec();

            Console.WriteLine();
            Console.WriteLine("Vectorized data matrix:");
            Console.WriteLine(vectorized);

            // Entries can also be vectorized using a read-only wrapper of the matrix.
            ReadOnlyComplexMatrix readOnlyMatrix = matrix.AsReadOnly();

            Console.WriteLine();
            Console.WriteLine("Vectorized read-only data matrix :");
            Console.WriteLine(readOnlyMatrix.Vec());
        }
Ejemplo n.º 16
0
        public void Main()
        {
            // Create a matrix.
            var data = new Complex[6] {
                new Complex(1, -1), new Complex(5, -5),
                new Complex(2, -2), new Complex(6, -6),
                new Complex(3, -3), new Complex(7, -7)
            };
            var matrix = ComplexMatrix.Dense(3, 2, data, StorageOrder.RowMajor);

            Console.WriteLine("The data matrix:");
            Console.WriteLine(matrix);

            // Return its conjugate transpose.
            var transposedMatrix = matrix.ConjugateTranspose();

            Console.WriteLine();
            Console.WriteLine("Matrix conjugate transpose:");
            Console.WriteLine(transposedMatrix);

            // Compute the conjugate transpose using a read-only wrapper
            // of the data matrix.
            ReadOnlyComplexMatrix readOnlyMatrix = matrix.AsReadOnly();
            var transposedReadOnlyMatrix         = readOnlyMatrix.ConjugateTranspose();

            Console.WriteLine();
            Console.WriteLine("Read only matrix transpose:");
            Console.WriteLine(transposedReadOnlyMatrix);
        }
        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);
        }
Ejemplo n.º 18
0
        /// <summary>
        /// Computes eigenvalues and eigenvectors of the
        /// specified Hermitian complex matrix.
        /// </summary>
        /// <param name="matrix">
        /// The matrix containing the lower or upper triangular part of the matrix
        /// whose spectral decomposition must be computed.
        /// </param>
        /// <param name="lowerTriangularPart">
        /// <c>true</c> if <paramref name="matrix"/> contains the lower
        /// triangular part of the matrix to be decomposed;
        /// <c>false</c> if <paramref name="matrix"/> contains
        /// its upper triangular part.
        /// </param>
        /// <param name="eigenvectors">
        /// A matrix whose columns represent the eigenvectors
        /// of the decomposed matrix.
        /// </param>
        /// <returns>
        /// A diagonal matrix containing the eigenvalues
        /// of the decomposed matrix, in ascending order.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="matrix"/> is <b>null</b>.
        /// </exception>
        /// <exception cref="ArgumentException">
        /// <paramref name="matrix"/> is not square.
        /// </exception>
        public static DoubleMatrix Decompose(
            ComplexMatrix matrix,
            bool lowerTriangularPart,
            out ComplexMatrix eigenvectors)
        {
            #region Input validation

            if (matrix is null)
            {
                throw new ArgumentNullException(nameof(matrix));
            }

            if (!matrix.IsSquare)
            {
                throw new ArgumentException(
                          message: ImplementationServices.GetResourceString(
                              "STR_EXCEPT_PAR_MUST_BE_SQUARE"),
                          paramName: nameof(matrix));
            }

            #endregion

            int       m            = matrix.NumberOfRows;
            double[]  valuesArray  = new double[m];
            Complex[] vectorsArray = matrix.AsColumnMajorDenseArray();

            int info = SafeNativeMethods.LAPACK.ZHEEV(
                matrix_layout: SafeNativeMethods.LAPACK.ORDER.ColMajor,
                jobz: 'V',
                uplo: lowerTriangularPart ? 'L' : 'U',
                n: m,
                a: vectorsArray,
                lda: m,
                w: valuesArray);

            if (info > 0)
            {
                throw new InvalidOperationException(
                          message: ImplementationServices.GetResourceString(
                              "STR_EXCEPT_ALG_DOES_NOT_CONVERGE"));
            }

            DoubleMatrix eigenvalues = DoubleMatrix.Sparse(m, m, m);
            for (int i = 0; i < m; i++)
            {
                eigenvalues[i, i] = valuesArray[i];
            }

            eigenvectors =
                ComplexMatrix.Dense(m, m, vectorsArray, copyData: false);

            Debug.Assert(eigenvalues != null);
            Debug.Assert(eigenvectors != null);

            return(eigenvalues);
        }
Ejemplo n.º 19
0
        public void Main()
        {
            // Create a matrix.
            var data = new Complex[8] {
                new Complex(1, -1), new Complex(5, -5),
                new Complex(2, -2), new Complex(6, -6),
                new Complex(3, -3), new Complex(7, -7),
                new Complex(4, -4), new Complex(8, -8)
            };
            var matrix = ComplexMatrix.Dense(4, 2, data, StorageOrder.RowMajor);

            Console.WriteLine("Initial data matrix:");
            Console.WriteLine(matrix);

            // Specify all row indexes.
            var rowIndexes = ":";

            Console.WriteLine();
            Console.WriteLine("Row indexes: from 0 to {0}", matrix.NumberOfRows - 1);

            // Specify all column indexes.
            var columnIndexes = ":";

            Console.WriteLine();
            Console.WriteLine("Column indexes: from 0 to {0}", matrix.NumberOfColumns - 1);

            // Specify the value matrix.
            var valueData = new Complex[8] {
                new Complex(10, -10), new Complex(50, -50),
                new Complex(20, -20), new Complex(60, -60),
                new Complex(30, -30), new Complex(70, -70),
                new Complex(40, -40), new Complex(80, -80)
            };
            var value = ComplexMatrix.Dense(4, 2, valueData, StorageOrder.RowMajor);

            Console.WriteLine();
            Console.WriteLine("Value matrix:");
            Console.WriteLine(value);

            // Set the entries having the specified indexes to the value matrix.
            matrix[rowIndexes, columnIndexes] = value;

            Console.WriteLine();
            Console.WriteLine("Updated data matrix:");
            Console.WriteLine(matrix);

            // Entries can also be accessed using a read-only wrapper
            // of the matrix.
            ReadOnlyComplexMatrix readOnlyMatrix = matrix.AsReadOnly();

            Console.WriteLine();
            Console.WriteLine("Updated matrix entries:");
            Console.WriteLine(readOnlyMatrix[rowIndexes, columnIndexes]);
        }
        public void Main()
        {
            // Create a matrix.
            var data = new Complex[8] {
                new Complex(1, -1), new Complex(5, -5),
                new Complex(2, -2), new Complex(6, -6),
                new Complex(3, -3), new Complex(7, -7),
                new Complex(4, -4), new Complex(8, -8)
            };
            var matrix = ComplexMatrix.Dense(4, 2, data, StorageOrder.RowMajor);

            Console.WriteLine("Initial data matrix:");
            Console.WriteLine(matrix);

            // Specify some row indexes.
            var rowIndexes = IndexCollection.Range(1, 3);

            Console.WriteLine();
            Console.WriteLine("Row indexes: {0}", rowIndexes);

            // Specify a column index.
            int columnIndex = 0;

            Console.WriteLine();
            Console.WriteLine("Column index: {0}", columnIndex);

            // Specify the value matrix.
            var valueData = new Complex[3] {
                new Complex(20, -20),
                new Complex(30, -30),
                new Complex(40, -40)
            };
            var value = ComplexMatrix.Dense(3, 1, valueData, StorageOrder.RowMajor);

            Console.WriteLine();
            Console.WriteLine("Value matrix:");
            Console.WriteLine(value);

            // Set the entries having the specified indexes to the value matrix.
            matrix[rowIndexes, columnIndex] = value;

            Console.WriteLine();
            Console.WriteLine("Updated data matrix:");
            Console.WriteLine(matrix);

            // Entries can also be accessed using a read-only wrapper
            // of the matrix.
            ReadOnlyComplexMatrix readOnlyMatrix = matrix.AsReadOnly();

            Console.WriteLine();
            Console.WriteLine("Updated matrix entries:");
            Console.WriteLine(readOnlyMatrix[rowIndexes, columnIndex]);
        }
Ejemplo n.º 21
0
        public void Main()
        {
            // Create the left operand.
            var data = new Complex[8] {
                new Complex(1, -1), new Complex(5, -5),
                new Complex(2, -2), new Complex(6, -6),
                new Complex(3, -3), new Complex(7, -7),
                new Complex(4, -4), new Complex(8, -8)
            };
            var left = ComplexMatrix.Dense(4, 2, data, StorageOrder.RowMajor);

            Console.WriteLine("left =");
            Console.WriteLine(left);

            // Create the right operand.
            data = new Complex[8] {
                new Complex(10, -1), new Complex(50, -5),
                new Complex(20, -2), new Complex(60, -6),
                new Complex(30, -3), new Complex(70, -7),
                new Complex(40, -4), new Complex(80, -8)
            };
            var right = ComplexMatrix.Dense(4, 2, data, StorageOrder.RowMajor);

            Console.WriteLine("right =");
            Console.WriteLine(right);

            // Subtract right from left.
            var result = left - right;

            Console.WriteLine();
            Console.WriteLine("left - right =");
            Console.WriteLine(result);

            // In .NET languages that do not support overloaded operators,
            // you can use the alternative methods named Subtract.
            result = ComplexMatrix.Subtract(left, right);

            Console.WriteLine();
            Console.WriteLine("ComplexMatrix.Subtract(left, right) returns");
            Console.WriteLine();
            Console.WriteLine(result);

            // Both operators and alternative methods are overloaded to
            // support read-only matrix arguments.
            // Compute the subtraction using a read-only wrapper of left.
            ReadOnlyComplexMatrix readOnlyLeft = left.AsReadOnly();

            result = readOnlyLeft - right;

            Console.WriteLine();
            Console.WriteLine("readOnlyLeft - right =");
            Console.WriteLine(result);
        }
Ejemplo n.º 22
0
        public void Main()
        {
            // Create a matrix.
            var data = new Complex[6] {
                new Complex(0, 0), new Complex(5, -5),
                new Complex(2, -2), new Complex(0, 0),
                new Complex(0, 0), new Complex(2, -2)
            };
            var matrix = ComplexMatrix.Dense(3, 2, data, StorageOrder.RowMajor);

            Console.WriteLine("The data matrix:");
            Console.WriteLine(matrix);
Ejemplo n.º 23
0
        /// <summary>
        /// Gets a dense implemented <see cref="ComplexMatrix"/> having
        /// the state specified by this instance.
        /// </summary>
        /// <returns>A dense implemented <see cref="ComplexMatrix"/> having
        /// the state specified by this instance.</returns>
        public ComplexMatrix AsDense()
        {
            var matrix = ComplexMatrix.Dense(
                this.NumberOfRows,
                this.NumberOfColumns,
                this.AsColumnMajorDenseArray);

            matrix.Name = this.Name;
            SetRowNames(matrix, this.RowNames);
            SetColumnNames(matrix, this.ColumnNames);

            return(matrix);
        }
Ejemplo n.º 24
0
        public void Main()
        {
            // Set matrix dimensions.
            int numberOfRows    = 3;
            int numberOfColumns = 2;

            // Create the matrix. All entries will be equal to zero.
            var matrix = ComplexMatrix.Dense(
                numberOfRows, numberOfColumns);

            Console.WriteLine("Each entry is equal to zero.");
            Console.WriteLine("The data matrix:");
            Console.WriteLine(matrix);
        }
Ejemplo n.º 25
0
        public void MatrixTest()
        {
            ComplexMatrix matrix = ComplexMatrix.Dense(2, 3,
                                                       new Complex[6] {
                1, 2, 3, 4, 5, 6
            });
            var rows = matrix.AsRowCollection();

            var actual = rows.Matrix;

            var expected = matrix;

            Assert.IsTrue(object.ReferenceEquals(expected, actual));
        }
Ejemplo n.º 26
0
        public void Main()
        {
            // Create the left operand.
            var data = new Complex[6] {
                new Complex(1, -1), new Complex(5, -5),
                new Complex(2, -2), new Complex(6, -6),
                new Complex(3, -3), new Complex(7, -7)
            };
            var left = ComplexMatrix.Dense(3, 2, data, StorageOrder.RowMajor);

            Console.WriteLine("left =");
            Console.WriteLine(left);

            // Create the right operand.
            data = new Complex[4] {
                new Complex(10, -10), new Complex(50, -50),
                new Complex(20, -20), new Complex(60, -60)
            };
            var right = ComplexMatrix.Dense(2, 2, data, StorageOrder.RowMajor);

            Console.WriteLine("right =");
            Console.WriteLine(right);

            // Multiply left by right.
            var result = left * right;

            Console.WriteLine();
            Console.WriteLine("left * right =");
            Console.WriteLine(result);

            // In .NET languages that do not support overloaded operators,
            // you can use the alternative methods named Multiply.
            result = ComplexMatrix.Multiply(left, right);

            Console.WriteLine();
            Console.WriteLine("ComplexMatrix.Multiply(left, right) returns");
            Console.WriteLine();
            Console.WriteLine(result);

            // Both operators and alternative methods are overloaded to
            // support read-only matrix arguments.
            // Compute the product using a read-only wrapper of left.
            ReadOnlyComplexMatrix readOnlyLeft = left.AsReadOnly();

            result = readOnlyLeft * right;

            Console.WriteLine();
            Console.WriteLine("readOnlyLeft * right =");
            Console.WriteLine(result);
        }
Ejemplo n.º 27
0
        public void LengthTest()
        {
            ComplexMatrix matrix = ComplexMatrix.Dense(2, 3,
                                                       new Complex[6] {
                1, 2, 3, 4, 5, 6
            });
            var rows = matrix.AsRowCollection();

            ComplexMatrixRow target = rows[0];
            var actual = target.Length;

            var expected = matrix.NumberOfColumns;

            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 28
0
        public void Main()
        {
            // Create the left operand.
            Complex left = new(7, -7);

            Console.WriteLine("left =");
            Console.WriteLine(left);
            Console.WriteLine();

            // Create the right operand.
            var data = new Complex[6] {
                new Complex(1, -1), new Complex(5, -5),
                new Complex(2, -2), new Complex(6, -6),
                new Complex(3, -3), new Complex(7, -7)
            };
            var right = ComplexMatrix.Dense(3, 2, data, StorageOrder.RowMajor);

            Console.WriteLine("right =");
            Console.WriteLine(right);

            // Compute the sum of left and right.
            var result = left + right;

            Console.WriteLine();
            Console.WriteLine("left + right =");
            Console.WriteLine(result);

            // In .NET languages that do not support overloaded operators,
            // you can use the alternative methods named Add.
            result = ComplexMatrix.Add(left, right);

            Console.WriteLine();
            Console.WriteLine("ComplexMatrix.Add(left, right) returns");
            Console.WriteLine();
            Console.WriteLine(result);

            // Both operators and alternative methods are overloaded to
            // support read-only matrix arguments.
            // Compute the sum using a read-only wrapper of right.
            ReadOnlyComplexMatrix readOnlyRight = right.AsReadOnly();

            result = left + readOnlyRight;

            Console.WriteLine();
            Console.WriteLine("left + readOnlyRight =");
            Console.WriteLine(result);
        }
Ejemplo n.º 29
0
        public void NotifyPropertyChangedTest()
        {
            ComplexMatrix matrix = ComplexMatrix.Dense(2, 3,
                                                       new Complex[6] {
                1, 2, 3, 4, 5, 6
            });

            var rows = matrix.AsRowCollection();

            ComplexMatrixRow target = rows[0];

            target.PropertyChanged += new PropertyChangedEventHandler(
                this.PropertyChangedEventHandler);

            string propertyName = "UNKNOWN";

            target.NotifyPropertyChanged(propertyName);
        }
Ejemplo n.º 30
0
        public void Main()
        {
            // Set matrix dimensions.
            int numberOfRows    = 3;
            int numberOfColumns = 2;

            // Set the value for each entry.
            Complex data = new(1, -1);

            // Create the matrix. All entries will be equal to the
            // same value.
            var matrix = ComplexMatrix.Dense(
                numberOfRows, numberOfColumns, data);

            Console.WriteLine("Each entry is equal to the same value.");
            Console.WriteLine("The data matrix:");
            Console.WriteLine(matrix);
        }