Beispiel #1
0
        /// <summary>
        /// Run example
        /// </summary>
        /// <seealso cref="http://en.wikipedia.org/wiki/Matrix_norm">Matrix norm</seealso>
        public void Run()
        {
            // Format matrix output to console
            var formatProvider = (CultureInfo)CultureInfo.InvariantCulture.Clone();
            formatProvider.TextInfo.ListSeparator = " ";

            // Create square matrix
            var matrix = new DenseMatrix(new[,] { { 1.0, 2.0, 3.0 }, { 6.0, 5.0, 4.0 }, { 8.0, 9.0, 7.0 } });
            Console.WriteLine(@"Initial square matrix");
            Console.WriteLine(matrix.ToString("#0.00\t", formatProvider));
            Console.WriteLine();

            // 1. 1-norm of the matrix
            Console.WriteLine(@"1. 1-norm of the matrix");
            Console.WriteLine(matrix.L1Norm());
            Console.WriteLine();

            // 2. 2-norm of the matrix
            Console.WriteLine(@"2. 2-norm of the matrix");
            Console.WriteLine(matrix.L2Norm());
            Console.WriteLine();

            // 3. Frobenius norm of the matrix
            Console.WriteLine(@"3. Frobenius norm of the matrix");
            Console.WriteLine(matrix.FrobeniusNorm());
            Console.WriteLine();

            // 4. Infinity norm of the matrix
            Console.WriteLine(@"4. Infinity norm of the matrix");
            Console.WriteLine(matrix.InfinityNorm());
            Console.WriteLine();

            // 5. Normalize matrix columns
            Console.WriteLine(@"5. Normalize matrix columns: before normalize");
            foreach (var keyValuePair in matrix.ColumnEnumerator())
            {
                Console.WriteLine(@"Column {0} 2-nd norm is: {1}", keyValuePair.Item1, keyValuePair.Item2.Norm(2));
            }

            Console.WriteLine();
            var normalized = matrix.NormalizeColumns(2);
            Console.WriteLine(@"5. Normalize matrix columns: after normalize");
            foreach (var keyValuePair in normalized.ColumnEnumerator())
            {
                Console.WriteLine(@"Column {0} 2-nd norm is: {1}", keyValuePair.Item1, keyValuePair.Item2.Norm(2));
            }

            Console.WriteLine();

            // 6. Normalize matrix columns
            Console.WriteLine(@"6. Normalize matrix rows: before normalize");
            foreach (var keyValuePair in matrix.RowEnumerator())
            {
                Console.WriteLine(@"Row {0} 2-nd norm is: {1}", keyValuePair.Item1, keyValuePair.Item2.Norm(2));
            }

            Console.WriteLine();
            normalized = matrix.NormalizeRows(2);
            Console.WriteLine(@"6. Normalize matrix rows: after normalize");
            foreach (var keyValuePair in normalized.RowEnumerator())
            {
                Console.WriteLine(@"Row {0} 2-nd norm is: {1}", keyValuePair.Item1, keyValuePair.Item2.Norm(2));
            }
        }