pub trait ReverseKernel<'a, S: RawData, const N: usize> {
// Required methods
fn reverse(self) -> KernelWithDilation<'a, S, N>;
fn no_reverse(self) -> KernelWithDilation<'a, S, N>;
}
Expand description
Trait for controlling kernel reversal behavior in convolution operations.
In standard convolution, the kernel is reversed (flipped) along all axes. This trait allows you to control whether the kernel should be reversed or not.
§Convolution vs Cross-Correlation
- Convolution (default,
reverse()
): The kernel is reversed, which is the mathematical definition of convolution. - Cross-correlation (
no_reverse()
): The kernel is NOT reversed. This is commonly used in machine learning frameworks.
§Example
use ndarray::array;
use ndarray_conv::{WithDilation, ReverseKernel, ConvExt, ConvMode, PaddingMode};
let input = array![1, 2, 3, 4, 5];
let kernel = array![1, 2, 3];
// Standard convolution (kernel is reversed)
let result1 = input.conv(&kernel, ConvMode::Same, PaddingMode::Zeros).unwrap();
// Equivalent to:
let result1_explicit = input.conv(kernel.reverse(), ConvMode::Same, PaddingMode::Zeros).unwrap();
// Cross-correlation (kernel is NOT reversed)
let result2 = input.conv(kernel.no_reverse(), ConvMode::Same, PaddingMode::Zeros).unwrap();
Required Methods§
Sourcefn reverse(self) -> KernelWithDilation<'a, S, N>
fn reverse(self) -> KernelWithDilation<'a, S, N>
Explicitly enables kernel reversal (standard convolution).
This is the default behavior, so calling this method is usually not necessary.
Sourcefn no_reverse(self) -> KernelWithDilation<'a, S, N>
fn no_reverse(self) -> KernelWithDilation<'a, S, N>
Disables kernel reversal (cross-correlation).
Use this when you want the kernel to be applied without flipping, which is common in machine learning applications.