关于使用F#创建可从C#使用的Matrix程序集

Ben*_*jol 5 c# f# language-interoperability

在C#中没有内置矩阵函数,但在F#powerpack中有.

我不知道在F#中使用第三方或开源C#库,而是将有用位暴露给C#.

想知道是否有人已经想过这个,或者尝试过,以及这是不是一个好主意.

我应该将它作为一个类或一堆静态函数公开吗?

或者我应该创建一个C#包装类,并将该调用转到F#?或者让F#使用C#类作为输入和输出?

有什么想法吗?

回答下面的Hath:您可以直接在C#中使用F#库(运算符也是如此!):

using System;
using System.Text;
using Microsoft.FSharp.Math;

namespace CSharp
{
  class Program
  {
    static void Main(string[] args)
    {

        double[,] x = { { 1.0, 2.0 }, { 4.0, 5.0 } };
        double[,] y = { { 1.0, 2.0 }, { 7.0, 8.0 } };
        Matrix<double> m1 = MatrixModule.of_array2(x);
        Matrix<double> m2 = MatrixModule.of_array2(y);
        var mp = m1 * m2;

        var output = mp.ToArray2();
        Console.WriteLine(output.StringIt());

      Console.ReadKey();
    }
  }

  public static class Extensions
  {
    public static string StringIt(this double[,] array)
    {
      var sb = new StringBuilder();
      for (int r = 0; r < array.Length / array.Rank; r++)
      {
          for (int c = 0; c < array.Rank; c++)
          {
              if (c > 0) sb.Append("\t");
              sb.Append(array[r, c].ToString());
          }
          sb.AppendLine();
      }
      return sb.ToString();
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

Hat*_*ath 6

你能不能只在c#中引用你需要的f#库并直接使用它?

我做了类似的事情来引用FSharp.Core.dll来获取

Microsoft.FSharp.Math.BigInt class.
Run Code Online (Sandbox Code Playgroud)

所以你可以直接引用FSharp.PowerPack.dll来获取

Microsoft.FSharp.Math.Matrix<A> class
Run Code Online (Sandbox Code Playgroud)