简体   繁体   English

使用Math.Net Numerics和C#在数字上添加两个向量的值

[英]add numerically the values of two vectors using Math.Net Numerics with C#

I have two vectors like the following: 我有两个向量,如下所示:

vdA = { 8.0, 7.0, 6.0 }
vdB = { 0.0, 1.0, 2.0, 3.0 }

I basicly want a vector vdX that result is to sum all element of vdA by all values of vdB. 我基本上想要一个向量vdX,其结果是将vdA的所有元素与vdB的所有值相加。

vdX = {
        8.0, 9.0, 10.0 11.0,
        7.0, 8.0, 9.0, 10.0,
        6.0, 7.0, 8.0, 9.0
      }

With MathNet.Numerics I couldn't find an function to do this. 使用MathNet.Numerics,我找不到执行此操作的函数。

In C# I make this code to do this 在C#中,我编写此代码来执行此操作

Vector<double> vdA = new DenseVector(new[] { 8.0, 7.0, 6.0 });
Vector<double> vdB = new DenseVector(new[] { 0.0, 1.0, 2.0, 3.0 });

List<double> resultSumVector = new List<double>();
foreach (double vectorValueA in vdA.Enumerate())
   foreach (double vectorValueB in vdB.Enumerate())
      resultSumVector.Add(vectorValueA + vectorValueB);
Vector<double> vdX = new DenseVector(resultSumVector.ToArray());

Are there any other options to accomplish this faster with Math.Net Numerics in c#? 还有其他选择可以使用c#中的Math.Net Numerics更快地完成此任务吗?

You basically need a cross join in Linq . 您基本上需要在Linq中进行交叉联接 You can write an extension method, this way it looks like it is a Math.Net method: 您可以编写一个扩展方法,这样看起来就像是Math.Net方法:

namespace MathNet.Numerics
{
    public static class DenseVectorExtensions
    {
        public static DenseVector AddAlls(this DenseVector vdA, DenseVector vdB)
        {
           return DenseVector.OfEnumerable(
                     vdA.SelectMany(x => vdB, (y, z) => { return y + z; })
                  );
        }
    }
}

Usage : 用法:

var vdA = new DenseVector(new[] { 8.0, 7.0, 6.0 });
var vdB = new DenseVector(new[] { 0.0, 1.0, 2.0, 3.0 });
var vdX = vdA.AddAlls(vdB);

This is not particularly faster. 这并不是特别快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM