Math.Net Numerics - how to run examples

孤街醉人 提交于 2019-12-11 02:28:46

问题


First trial with Math.Net and moving from C++\Cli to C# to use Math.Net, so everything is new today.

How do I set-up and run the examples such as this one Matrix Transpose. Should I create a class and copy this code into it? I notice the interface is missing (Error: namespace IExample could not be found), but I also notice this may be provided here Interface. Where do I put this?

This is what I have Program.cs (left out basic details):

namespace Examples.LinearAlgebraExamples
{
  /// Defines the base interface for examples.
   public interface IExample
    {
        string Name
        {
            get;
        }
        string Description
        {
            get;
        }
        void Run();
    }
   /// Matrix transpose and inverse
   public class MatrixTransposeAndInverse : IExample
    {
    // rest of the example code
    }
    class Program
    {
        static void Main(string[] args)
        {
           // how to call the above routines? 
        }
    }
} 

回答1:


This is what works: create a C# console application (VS2012), and then paste the main body of the Math.Net example in the main body of the console app. Add includes and namespace. The above referenced example then runs.

Code snippet (left out items 2-5):

using System;
using MathNet.Numerics;
using MathNet.Numerics.LinearAlgebra.Double;
using System.Globalization;

namespace Examples.LinearAlgebraExamples
{
    class Program
    {
        static void Main(string[] args)
        {
            // Format matrix output to console
            var formatProvider = (CultureInfo)CultureInfo.InvariantCulture.Clone();
            formatProvider.TextInfo.ListSeparator = " ";

            // Create random square matrix
            var matrix = new DenseMatrix(5);
            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(@"Initial matrix");
            Console.WriteLine(matrix.ToString("#0.00\t", formatProvider));
            Console.WriteLine();

            // 1. Get matrix inverse
            var inverse = matrix.Inverse();
            Console.WriteLine(@"1. Matrix inverse");
            Console.WriteLine(inverse.ToString("#0.00\t", formatProvider));
            Console.WriteLine();

// removed examples here

            Console.WriteLine();
            Console.ReadLine();
        }
    }
}


来源:https://stackoverflow.com/questions/23996140/math-net-numerics-how-to-run-examples

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!