2

I normally use python but am learning C#, is there a preexisting class in C# for getting an element-by-element difference of an array to basically get a derivative, like numpy.diff in python?

wordsforthewise
  • 13,746
  • 5
  • 87
  • 117

1 Answers1

8

According to this page, numpy.diff does this:

>>> x = np.array([1, 2, 4, 7, 0])
>>> np.diff(x)
array([ 1,  2,  3, -7])

If this is the effect that you want to see, use LINQ:

var np = new [] {1, 2, 4, 7, 0};
var res = np.Zip(np.Skip(1), (a,b)=>b-a).ToArray();

Demo.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523