简体   繁体   English

内置函数diff()可用于MathNet中的矢量?

[英]built-in function diff() available for vector in MathNet?

I'm new to MathNet and implementing a code in C#.Net . 我是MathNet ,正在C#.Net实现代码。

There is a vector: 有一个向量:

var X = new DenseVector(new double[] { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150 });

I need to find Y = **diff(X)** calculating differences between adjacent elements of X like: 我需要找到Y = **diff(X)**来计算X相邻元素之间的差异,例如:

Y = [X(2)-X(1) X(3)-X(2) ... X(m)-X(m-1)]

Is there built-in function diff() available in MathNet ? MathNet是否有内置函数diff() I searched on MathNet.Numerics/Differentiate , but its not available. 我在MathNet.Numerics / Differentiate上搜索,但无法使用。

You are correct. 你是对的。 It does not appear to be available. 它似乎不可用。 But here is a simple function to achieve that. 但是这里有一个简单的功能可以实现。

public MathNet.Numerics.LinearAlgebra.Double.DenseVector 
Diff(MathNet.Numerics.LinearAlgebra.Double.DenseVector X)
{
    var R = new MathNet.Numerics.LinearAlgebra.Double.DenseVector(X.Count - 2);
    for (var i = 0; i <= X.Count - 2; i++)
        R(i) = X(i + 1) - X(i);
    return R;
}

You can use advantages of MathNet to make it more expressive. 您可以利用MathNet优势使其更具表现力。

static class VectorExtension
{
    public static Vector<double> Differentiate(this Vector<double> vector)
    {
        var high = Vector<double>.Build.DenseOfEnumerable(vector.Skip(1));
        var low = Vector<double>.Build.DenseOfEnumerable(vector.Take(vector.Count - 1));
        return  high - low;
    }
}

Then 然后

var X = new DenseVector(new double[] { 10, 20, 30, 40, 50, 60, 70 });
Console.WriteLine(X.Differentiate());

Gives

DenseVector 6-Double
10
10
10
10
10
10

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

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