mathNet基础用法

// 
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com
// Copyright (c) 2009-2010 Math.NET
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
// 

using System;
using System.Numerics;
using System.Globalization;
using MathNet.Numerics.LinearAlgebra.Double;
//using System.Numerics, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;
using numl.Model;
using numl.Supervised.DecisionTree;
using System.Diagnostics;
using MathNet.Numerics.LinearAlgebra;


namespace Examples.LinearAlgebra.FactorizationExamples
{
    /// 
    /// EVD factorization example. If A is symmetric, then A = V*D*V' where the eigenvalue matrix D is
    /// diagonal and the eigenvector matrix V is orthogonal. I.e. A = V*D*V' and V*VT=I.
    /// If A is not symmetric, then the eigenvalue matrix D is block diagonal
    /// with the real eigenvalues in 1-by-1 blocks and any complex eigenvalues,
    /// lambda + i*mu, in 2-by-2 blocks, [lambda, mu; -mu, lambda].  The
    /// columns of V represent the eigenvectors in the sense thatA * V = V * D. 
    /// The matrix V may be badly conditioned, or even singular, so the validity of the equation
    /// A = V*D*Inverse(V) depends upon V.Condition()
    /// 
    /// 
    /// 
    namespace ConsoleApplication6
    {
        class Program
        {
            static void Main(string[] args)
            {
                //var m = MathNet.Numerics.LinearAlgebra.Matrix.Build.Dense(2, 2);
                //m[0, 0] = 6.8;
                //m[0, 1] = 3.2;
                //m[1, 0] = 3.2;
                //m[1, 1] = 6.8;

                //var evd = m.Evd(MathNet.Numerics.LinearAlgebra.Symmetricity.Symmetric);
                //Debug.WriteLine(evd.EigenValues);
                //Debug.WriteLine(evd.EigenVectors);

                Evd e = new Evd();
                e.Run();


                Console.ReadKey();
            }


            /// 
            /// Defines the base interface for examples.
            /// 
            public interface IExample
            {
                /// 
                /// Gets the name of this example
                /// 
                string Name
                {
                    get;
                }

                /// 
                /// Gets the description of this example
                /// 
                string Description
                {
                    get;
                }

                /// 
                /// Run example
                /// 
                void Run();
            }


            public class Evd : IExample
            {
                /// 
                /// Gets the name of this example
                /// 
                public string Name
                {
                    get
                    {
                        return "Evd factorization";
                    }
                }

                /// 
                /// Gets the description of this example
                /// 
                public string Description
                {
                    get
                    {
                        return "Perform the Evd factorization: eigenvalues and eigenvectors calculation";
                    }
                }

                /// 
                /// Run example
                /// 
                /// EVD decomposition
                public void Run()
                {
                    // Format matrix output to console
                    var formatProvider = (CultureInfo)CultureInfo.InvariantCulture.Clone();
                    formatProvider.TextInfo.ListSeparator = " ";

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

                    // Perform eigenvalue decomposition of symmetric matrix  执行对称矩阵的特征值分解
                    var evd = matrix.Evd();
                    Console.WriteLine(@"Perform eigenvalue decomposition of symmetric matrix");


                    // 1. Eigen vectors
                    Console.WriteLine(@"1. Eigen vectors");
                    Console.WriteLine(evd.EigenVectors.ToString("#0.00\t", formatProvider));
                    Console.WriteLine();

                    // 2. Eigen values as a complex vector
                    Console.WriteLine(@"2. Eigen values as a complex vector");
                    //if(evd.EigenValues);

                    Console.WriteLine(evd.EigenValues.ToString("N", formatProvider));
                    Console.WriteLine();

                    // 3. Eigen values as the block diagonal matrix块对角矩阵的特征值
                    Console.WriteLine(@"3. Eigen values as the block diagonal matrix");
                    Console.WriteLine(evd.D.ToString("#0.00\t", formatProvider));
                    Console.WriteLine();

                    // 4. Multiply V by its transpose VT
                    var identity = evd.EigenVectors.TransposeAndMultiply(evd.EigenVectors);
                    Console.WriteLine(@"4. Multiply V by its transpose VT: V*VT = I");
                    Console.WriteLine(identity.ToString("#0.00\t", formatProvider));
                    Console.WriteLine();

                    // 5. Reconstruct initial matrix: A = V*D*V'重建初始矩阵
                    var reconstruct = evd.EigenVectors * evd.D * evd.EigenVectors.Transpose();
                    Console.WriteLine(@"5. Reconstruct initial matrix: A = V*D*V'");
                    Console.WriteLine(reconstruct.ToString("#0.00\t", formatProvider));
                    Console.WriteLine();

                    // 6. Determinant of the matrix 矩阵的行列式
                    Console.WriteLine(@"6. Determinant of the matrix");
                    Console.WriteLine(evd.Determinant);
                    Console.WriteLine();

                    // 7. Rank of the matrix矩阵的秩
                    Console.WriteLine(@"7. Rank of the matrix");
                    Console.WriteLine(evd.Rank);
                    Console.WriteLine();

                    // Fill matrix by random values
                    var rnd = new Random(1);
                    for (var i = 0; i < matrix.RowCount; i++)
                    {
                        for (var j = 0; j < matrix.ColumnCount; j++)
                        {
                            matrix[i, j] = rnd.NextDouble();
                        }
                    }

                    Console.WriteLine(@"Fill matrix by random values");
                    Console.WriteLine(matrix.ToString("#0.00\t", formatProvider));
                    Console.WriteLine();

                    // Perform eigenvalue decomposition of non-symmetric matrix----执行非对称矩阵的特征值分解
                    evd = matrix.Evd();
                    Console.WriteLine(@"Perform eigenvalue decomposition of non-symmetric matrix");

                    // 8. Eigen vectors
                    Console.WriteLine(@"8. Eigen vectors");
                    Console.WriteLine(evd.EigenVectors.ToString("#0.00\t", formatProvider));
                    Console.WriteLine();

                    // 9. Eigen values as a complex vector
                    Console.WriteLine(@"9. Eigen values as a complex vector");
                    //Console.WriteLine(evd.EigenValues.ToString());
                    Console.WriteLine();

                    // 10. Eigen values as the block diagonal matrix
                    Console.WriteLine(@"10. Eigen values as the block diagonal matrix");
                    Console.WriteLine(evd.D.ToString("#0.00\t", formatProvider));
                    Console.WriteLine();

                    // 11. Multiply A * V
                    var av = matrix * evd.EigenVectors;
                    Console.WriteLine(@"11. Multiply A * V");
                    Console.WriteLine(av.ToString("#0.00\t", formatProvider));
                    Console.WriteLine();

                    // 12. Multiply V * D
                    var vd = evd.EigenVectors * evd.D;
                    Console.WriteLine(@"12. Multiply V * D");
                    Console.WriteLine(vd.ToString("#0.00\t", formatProvider));
                    Console.WriteLine();

                    // 13. Reconstruct non-symmetriv matrix A = V * D * Vinverse
                    reconstruct = evd.EigenVectors * evd.D * evd.EigenVectors.Inverse();
                    Console.WriteLine(@"13. Reconstruct non-symmetriv matrix A = V * D * Vinverse");
                    Console.WriteLine(reconstruct.ToString("#0.00\t", formatProvider));
                    Console.WriteLine();

                    // 14. Determinant of the matrix
                    Console.WriteLine(@"14. Determinant of the matrix");
                    Console.WriteLine(evd.Determinant);
                    Console.WriteLine();

                    // 15. Rank of the matrix
                    Console.WriteLine(@"15. Rank of the matrix");
                    Console.WriteLine(evd.Rank);
                    Console.WriteLine();
                }
            }
        }
    }
}



//using MathNet.Numerics;
//using MathNet.Numerics.LinearAlgebra;
//using MathNet.Numerics.LinearAlgebra.Complex32;
//using MathNet.Numerics.LinearAlgebra.Factorization;
//using System;
//using System;
//using System.Collections.Generic;
//using System.Globalization;
//using System.Linq;
//using System.Text;
//using System.Threading.Tasks;

//namespace ConsoleApplication6
//{
//    class Program
//    {
//        static void Main(string[] args)
//        {
//            //初始化一个矩阵和向量的构建对象
//            Matrix mb = Matrix.Build.Random(3,3);
//            var vb = Vector.Build;
//            var formatProvider = (CultureInfo)CultureInfo.InvariantCulture.Clone();
//            formatProvider.TextInfo.ListSeparator = " ";
//            //获取随机矩阵,也可以设置随机数所属的分布
//            //var randomMatrix = mb.Random(3, 3);
//            Evd eigen = mb.Evd();
//             //eigen.EigenValues;
//            //Console.WriteLine("121212: " + eigenVecter.ToString());
//            //向量相当于是一个一维数组,只有长度
//            var vector0 = vb.Random(3);//也可以选择分布
//            Console.WriteLine("resultM: " + eigen.EigenVectors.ToString());
//            //Console.WriteLine("resultM: " + eigen.EigenValues.ToString());
//            Console.WriteLine(eigen.EigenValues.ToString("N", formatProvider));
//            矩阵还可以这样初始化
//            //var matrix1 = mb.Dense(2, 2, 1);
//            使用函数初始化
//            //var matrix2 = mb.Dense(2, 3, (i, j) => 3 * i + j);

//            对角矩阵
//            //var diagMaxtrix = mb.DenseDiagonal(3, 3, 5);

//            //var multipl = vector0 * randomMatrix;
//            //var resultM = 3.0 * randomMatrix;
//            //Console.WriteLine("randomMatrix: " + randomMatrix.ToString());
//            //Console.WriteLine("resultM: " + vector0.ToString());
//            //Console.WriteLine("resultM: " + multipl.ToString());
//            //Console.WriteLine("matrix1: " + matrix1.ToString());
//            //Console.WriteLine("matrix2: " + matrix2.ToString());
//            //Console.WriteLine("diagMaxtrix: " + diagMaxtrix.ToString());

//            当然也可以直接从数组中创建
//            //double[,] x = { { 1.0, 2.0 }, { 3.0, 4.0 } };
//            //var fromArray = mb.DenseOfArray(x);

//            //Console.WriteLine("fromArray: " + fromArray.ToString());
//            Console.ReadKey();
//        }
//    }
//}

你可能感兴趣的:(c#)