如何在 C# 中生成 Accord.Math.Sparse<double> 元素?

How to generate a Accord.Math.Sparse<double> element in C#?

我正在尝试在 C# 中实现点积,为此,我正在使用 Accord.Math 及其方法 Dot,如下所示:

using Accord.Math;

namespace VectorOperations
{
    class DotProduct
    {
        private static double CalculateDotProduct(Sparse<double> Vector1, Sparse<double> Vector2)
        {
            double DotProduct = Vector.Dot(Vector1, Vector2);
            return DotProduct;
        }
    }
}

但是我无法创建一个示例来测试它是否正常工作,因为我不知道如何创建 Sparse<double> 类型的变量。我如何创建一个示例?理想情况下,我想要:

Sparse<double> Vector1 = new Sparse<double>();
Sparse<double> Vector2 = new Sparse<double>();

// Vector1 = [1, 2, 3];
// Vector2 = [1, 2, 3];

所以我可以调用 this.CalculateDotProduct(Vector1, Vector2) 并检查它是否正常工作。

如果您知道使用 List<double> 类型的向量计算点积的任何其他方法,也欢迎。

unit tests 展示了几种创建和填充实例的方法:

var s = new Sparse<double>();
s[0] = 1;
s[99] = 99;
s[10] = 42;

v = new double[] { 1, 2, 3, 0, 0, 6 };
d = Sparse.FromDense(v);

另一种方法是使用 Sparse(int[] indices, T[] values) 构造函数:

Sparse<double> Vector1 = new Sparse<double>(new[] { 0, 1, 2 }, new[]  { 1, 2, 3 });