C# 中没有内置矩阵函数,但 F# powerpack 中有。
与其使用第三方或开源 C# 库,我想知道在 F# 中滚动我自己的库,并将有用的部分暴露给 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();
}
}
}