# Create Forward Difference Kernel¶

## Synopsis¶

Create a forward difference kernel.

## Results¶

Output:

```Size: [3, 3]
Neighborhood:
Size:[3, 3]
DataBuffer:NeighborhoodAllocator { this = 0x7ffee23339e0, begin = 0x7fd6921a8f60, size=9 }
[-1, -1] 0
[0, -1] 0
[1, -1] 0
[-1, 0] 0
[0, 0] -1
[1, 0] 1
[-1, 1] 0
[0, 1] 0
[1, 1] 0
```

## Code¶

### Python¶

```#!/usr/bin/env python

import itk

forwardDifferenceOperator = itk.ForwardDifferenceOperator[itk.F, 2]()
forwardDifferenceOperator.SetDirection(
0
)  # Create the operator for the X axis derivative

print("Size: " + str(forwardDifferenceOperator.GetSize()))

print(forwardDifferenceOperator)

for i in range(9):
print(
str(forwardDifferenceOperator.GetOffset(i))
+ " "
+ str(forwardDifferenceOperator.GetElement(i))
)
```

### C++¶

```#include <itkForwardDifferenceOperator.h>

int
main(int, char *[])
{
using ForwardDifferenceOperatorType = itk::ForwardDifferenceOperator<float, 2>;
ForwardDifferenceOperatorType forwardDifferenceOperator;
forwardDifferenceOperator.SetDirection(0); // Create the operator for the X axis derivative

std::cout << "Size: " << forwardDifferenceOperator.GetSize() << std::endl;

std::cout << forwardDifferenceOperator << std::endl;

for (unsigned int i = 0; i < 9; i++)
{
std::cout << forwardDifferenceOperator.GetOffset(i) << " " << forwardDifferenceOperator.GetElement(i) << std::endl;
}
return EXIT_SUCCESS;
}
```

## Classes demonstrated¶

template<typename `TPixel`, unsigned int `VDimension` = 2, typename `TAllocator` = NeighborhoodAllocator<TPixel>>
class `ForwardDifferenceOperator` : public itk::NeighborhoodOperator<TPixel, VDimension, TAllocator>

Operator whose inner product with a neighborhood returns a “half” derivative at the center of the neighborhood.

ForwardDifferenceOperator uses forward differences i.e. F(x+1) - F(x) to calculate a “half” derivative useful, among other things, in solving differential equations. It is a directional NeighborhoodOperator that should be applied to a Neighborhood using the inner product.

Note

ForwardDifferenceOperator does not have any user-declared “special member function”, following the C++ Rule of Zero: the compiler will generate them if necessary.

ITK Sphinx Examples: