Skip to content

Convolve1d rewrites #1319

Open
Open
@ricardoV94

Description

@ricardoV94

Description

Follow up to #1318

Some of these may apply to higher-dimensional convolutions.

Sum of convolution as product of sum of inputs

If we have convolve1d(x, y, mode="full").sum(), we can rewrite it as x.sum() * y.sum()

import numpy as np

rng = np.random.default_rng(37)
x = rng.normal(size=(5))
y = rng.normal(size=(9))
np.testing.assert_allclose(np.convolve(x, y).sum(), x.sum() * y.sum())

convolve constant kernels

If we have two sequential applications of convolve, with constant inputs we can convolve the constant inputs first, reducing the number of runtime convolutions

import numpy as np

rng = np.random.default_rng(37)
x = rng.normal(size=(5))
y1 = rng.normal(size=(9))
y2 = rng.normal(size=(3,))

r1 = np.convolve(np.convolve(x, y1), y2)
r2 = np.convolve(x, np.convolve(y1, y2))
np.testing.assert_allclose(r1, r2)

Merge convolutions with flipped inputs

Convolutions give the same output regardless of order of inputs, so if we see two with the same inputs but in different order we can merge them. In general, we may want to have an Op property that tells us when the output is invariant to the order of inputs to apply such merge automatically. This Applies to Add, Mul, ...

import numpy as np

rng = np.random.default_rng(37)
x = rng.normal(size=(5))
y = rng.normal(size=(9))

r1 = np.convolve(x, y)
r2 = np.convolve(y, x)
np.testing.assert_allclose(r1, r2)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions