convolution.rs - source
Description: Source of the Rust file `/Users/erlendbasso/.cargo/registry/src/github.com-1ecc6299db9ec823/nalgebra-0.32.1/src/linalg/convolution.rs`.
Keywords: rust, rustlang, rust-lang

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144

    use std::cmp;

    use crate::base::allocator::Allocator;
    use crate::base::default_allocator::DefaultAllocator;
    use crate::base::dimension::{Const, Dim, DimAdd, DimDiff, DimSub, DimSum};
    use crate::storage::Storage;
    use crate::{zero, OVector, RealField, Vector, U1};

    impl<T: RealField, D1: Dim, S1: Storage<T, D1>> Vector<T, D1, S1> {
        /// Returns the convolution of the target vector and a kernel.
        ///
        /// # Arguments
        ///
        /// * `kernel` - A Vector with size > 0
        ///
        /// # Errors
        /// Inputs must satisfy `vector.len() >= kernel.len() > 0`.
        ///
        pub fn convolve_full<D2, S2>(
            &self,
            kernel: Vector<T, D2, S2>,
        ) -> OVector<T, DimDiff<DimSum<D1, D2>, U1>>
        where
            D1: DimAdd<D2>,
            D2: DimAdd<D1, Output = DimSum<D1, D2>>,
            DimSum<D1, D2>: DimSub<U1>,
            S2: Storage<T, D2>,
            DefaultAllocator: Allocator<T, DimDiff<DimSum<D1, D2>, U1>>,
        {
            let vec = self.len();
            let ker = kernel.len();

            if ker == 0 || ker > vec {
                panic!("convolve_full expects `self.len() >= kernel.len() > 0`, received {} and {} respectively.", vec, ker);
            }

            let result_len = self
                .data
                .shape()
                .0
                .add(kernel.shape_generic().0)
                .sub(Const::<1>);
            let mut conv = OVector::zeros_generic(result_len, Const::<1>);

            for i in 0..(vec + ker - 1) {
                let u_i = if i > vec { i - ker } else { 0 };
                let u_f = cmp::min(i, vec - 1);

                if u_i == u_f {
                    conv[i] += self[u_i].clone() * kernel[(i - u_i)].clone();
                } else {
                    for u in u_i..(u_f + 1) {
                        if i - u < ker {
                            conv[i] += self[u].clone() * kernel[(i - u)].clone();
                        }
                    }
                }
            }
            conv
        }
        /// Returns the convolution of the target vector and a kernel.
        ///
        /// The output convolution consists only of those elements that do not rely on the zero-padding.
        /// # Arguments
        ///
        /// * `kernel` - A Vector with size > 0
        ///
        ///
        /// # Errors
        /// Inputs must satisfy `self.len() >= kernel.len() > 0`.
        ///
        pub fn convolve_valid<D2, S2>(
            &self,
            kernel: Vector<T, D2, S2>,
        ) -> OVector<T, DimDiff<DimSum<D1, U1>, D2>>
        where
            D1: DimAdd<U1>,
            D2: Dim,
            DimSum<D1, U1>: DimSub<D2>,
            S2: Storage<T, D2>,
            DefaultAllocator: Allocator<T, DimDiff<DimSum<D1, U1>, D2>>,
        {
            let vec = self.len();
            let ker = kernel.len();

            if ker == 0 || ker > vec {
                panic!("convolve_valid expects `self.len() >= kernel.len() > 0`, received {} and {} respectively.",vec,ker);
            }

            let result_len = self
                .data
                .shape()
                .0
                .add(Const::<1>)
                .sub(kernel.shape_generic().0);
            let mut conv = OVector::zeros_generic(result_len, Const::<1>);

            for i in 0..(vec - ker + 1) {
                for j in 0..ker {
                    conv[i] += self[i + j].clone() * kernel[ker - j - 1].clone();
                }
            }
            conv
        }

        /// Returns the convolution of the target vector and a kernel.
        ///
        /// The output convolution is the same size as vector, centered with respect to the ‘full’ output.
        /// # Arguments
        ///
        /// * `kernel` - A Vector with size > 0
        ///
        /// # Errors
        /// Inputs must satisfy `self.len() >= kernel.len() > 0`.
        #[must_use]
        pub fn convolve_same<D2, S2>(&self, kernel: Vector<T, D2, S2>) -> OVector<T, D1>
        where
            D2: Dim,
            S2: Storage<T, D2>,
            DefaultAllocator: Allocator<T, D1>,
        {
            let vec = self.len();
            let ker = kernel.len();

            if ker == 0 || ker > vec {
                panic!("convolve_same expects `self.len() >= kernel.len() > 0`, received {} and {} respectively.",vec,ker);
            }

            let mut conv = OVector::zeros_generic(self.shape_generic().0, Const::<1>);

            for i in 0..vec {
                for j in 0..ker {
                    let val = if i + j < 1 || i + j >= vec + 1 {
                        zero::<T>()
                    } else {
                        self[i + j - 1].clone()
                    };
                    conv[i] += val * kernel[ker - j - 1].clone();
                }
            }

            conv
        }
    }