The C++ interface for diff. More...

array diff1 (const array &in, int dim=-1)
 
array diff2 (const array &in, int dim=-1)
 

Detailed Description

The C++ interface for diff.

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]

Function Documentation

array af::diff1 ( const array &  in,
int  dim = -1 
)
Parameters
[in]in
[in]dimdimension along which to operate (-1 indicates first non-singleton dimension)
Returns
difference between neighboring elements
Examples:
examples/pde/fdtd.cpp.
array af::diff2 ( const array &  in,
int  dim = -1 
)
Parameters
[in]in
[in]dimdimension along which to operate (-1 indicates first non-singleton dimension)
Returns
second order difference between elements