3

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

如何设置和运行诸如Matrix Transpose之类的示例。我应该创建一个类并将此代码复制到其中吗?我注意到缺少接口(错误:找不到命名空间 IExample),但我也注意到这可能在此处提供Interface。我把这个放在哪里?

这就是我所拥有的 Program.cs(省略了基本细节):

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

1 回答 1

2

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

代码片段(省略第 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();
        }
    }
}
于 2014-06-02T17:17:21.660 回答