Add Fast Walsh-Hadamard Transform (FWHT) for bitwise convolutions - #15084
Add Fast Walsh-Hadamard Transform (FWHT) for bitwise convolutions#15084Clear20-22 wants to merge 3 commits into
Conversation
There was a problem hiding this comment.
Pull request overview
This PR introduces a Fast Walsh–Hadamard Transform (FWHT) implementation to support fast bitwise (XOR/OR/AND) convolutions in the maths module, enabling (\mathcal{O}(N \log N)) convolution for power-of-two sized inputs.
Changes:
- Added FWHT XOR/OR/AND forward and inverse transforms.
- Added XOR/OR/AND convolution helpers built on the transforms.
- Included doctest examples for core behavior and some error cases.
Suppressed comments (1)
maths/fast_walsh_hadamard_transform.py:152
fwht_andraises ValueError for empty input (length 0), but unlikefwht_xorthis edge case isn't exercised by a doctest in the docstring. Adding an explicitfwht_and([])doctest would improve coverage for error handling.
>>> fwht_and([1, 2, 3])
Traceback (most recent call last):
...
ValueError: Length of sequence must be a positive power of 2.
"""
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| >>> fwht_or([1, 2]) | ||
| [1, 3] | ||
| >>> fwht_or([1, 3], inverse=True) | ||
| [1, 2] | ||
| >>> fwht_or([1, 2, 3]) | ||
| Traceback (most recent call last): | ||
| ... | ||
| ValueError: Length of sequence must be a positive power of 2. | ||
| """ |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 1 out of 1 changed files in this pull request and generated no new comments.
Suppressed comments (2)
Previously missed (1) — in code that hasn't changed since the last review.
maths/fast_walsh_hadamard_transform.py:8
- The module docstring says this works on "numeric" sequences, but the public API is annotated as
list[int]andfwht_xor(..., inverse=True)relies on integer-only operations (%divisibility check and//scaling). This mismatch is likely to confuse users and can break when passing floats/decimals.
This issue also appears on line 14 of the same file.
Computes bitwise XOR, AND, and OR convolutions of two numeric sequences in O(N log N)
time, where N is a positive power of 2.
maths/fast_walsh_hadamard_transform.py:16
fwht_xor(..., inverse=True)currently performs integer division and requires exact divisibility by N. The docstring doesn’t mention this constraint, which makes the inverse behavior easy to misinterpret as a general numeric inverse transform.
Perform Fast Walsh-Hadamard Transform (or inverse) for XOR operation.
Time Complexity: O(N log N)
Summary of Changes
maths/fast_walsh_hadamard_transform.pyimplementing the Fast Walsh-Hadamard Transform (FWHT) and inverse transforms.ValueErrorfor non-power-of-two lengths and mismatched inputs).pytest,ruff check, andmypy.References
Checklist