Compute the difference along a dimension. More...

Modules

 diff
 The C++ interface for diff.
 
 af_diff
 The C interface for diff.
 

Detailed Description

Compute the difference along a dimension.

The difference functions compute either the first or second order difference between adjacent elements in an array along a given dimension.

diff1 computes the first order difference (basic adjacent difference) along a given dimension. If the input array has N elements in the dimension of interest, the resulting array has N-1 elements along the given dimension.

array a; // a = [0 1 2 3 4 5]
print(diff1(a)); // [1 1 1 1 1]
array b; // b = [0 2 4]
// [1 3 5]
//Difference along first dimension, down columns
print(diff1(b)); // diff1(b) = [1 1 1]
//Difference alone second dimension, across rows
print(diff1(b,1)); // diff1(b,1) = [2 2]
a // [2 2]

diff2 computes the second order difference (i.e. the difference between the adjacent first order differences) along a given dimension.

float f[] = { 5, 10, 20, 25, 50, 100};
array a(6,1,f); //a = f
// Take diff1 twice
array b = diff1(a); // b = [5 10 5 25 50]
array c = diff1(b); // c = [5 -5 20 25]
//or use diff2 once
array d = diff2(a); // d = c = [5 -5 20 25]