Math.Net数值-如何运行示例 [英] Math.Net Numerics - how to run examples

查看:115
本文介绍了Math.Net数值-如何运行示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首次使用Math.Net进行试用,并从C ++ \ Cli迁移到C#以使用Math.Net,因此今天一切都是新的.

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

我如何设置和运行例如矩阵转置.我应该创建一个类并将此代码复制到其中吗?我注意到该接口丢失了(错误:找不到命名空间IExample),但我也注意到可以在此处提供

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?

这是我拥有的Program.cs(省略了基本详细信息):

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? 
        }
    }
} 

推荐答案

这是有效的方法:创建一个C#控制台应用程序(VS2012),然后将Math.Net示例的主体粘贴到控制台主体中应用程序.添加包含和名称空间.上面引用的示例然后运行.

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.

代码段(未列出项目2-5):

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();
        }
    }
}

这篇关于Math.Net数值-如何运行示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆