FWHT
A fast and efficient implementation of the Fast Walsh-Hadamard Transform (FWHT) in Rust.
Features
- 🔧 Flexible: Works with
Vec<T>, static arrays, andndarray::Array1<T> - 🦀 Safe: Leverages Rust's type system
- 📦 No required dependencies: ndarray support is optional
- 🧮 Generic: Works with any numeric type implementing
Add + Sub + Copy
Installation
Add this to your Cargo.toml:
[]
= "0.1.0"
# With ndarray support (optional)
= { = "0.1.0", = ["ndarray"] }
Quick Start
With Vec
use FWHT;
With ndarray (requires "ndarray" feature)
use FWHT;
use Array1;
With Static Arrays
use FWHT;
API
FWHT Trait
The main trait providing uniform methods for all supported types:
Implementations
Vec<T>: Both methods available[T; N]: Both methods availablendarray::Array1<T>: Both methods available (with "ndarray" feature)
Function-based API
For cases where the trait API isn't suitable:
fwht_mut<T, V>(data: &mut T): Works with any type implementingAsMut<[V]>fwht<T, V>(data: &T) -> T: Returns a new copy with the transform appliedfwht_slice<T>(data: &mut [T]): Direct function for slices
Type Requirements
Elements must implement:
Add<Output = T>: For additionSub<Output = T>: For subtractionCopy: For efficient copying
Compatible types include: f32, f64, i32, i64, complex numbers, etc.
Advanced Examples
Verifying the Involution Property
FWHT is its own inverse (up to scaling):
use FWHT;
let original = vec!;
let mut data = original.clone;
// Apply FWHT twice
data.fwht_mut.unwrap;
data.fwht_mut.unwrap;
// Scale by 1/n to recover original
let n = data.len as f64;
for x in &mut data
assert_eq!;
With Different Numeric Types
use FWHT;
// With integers
let mut data_int = vec!;
data_int.fwht_mut.unwrap;
// With 32-bit floats
let data_f32 = vec!;
let result_f32 = data_f32.fwht.unwrap;
Features
default = ["ndarray"]: Includes ndarray support by defaultndarray: Enables functions specific tondarray::Array1<T>
To use without ndarray:
[]
= { = "0.1.0", = false }
Requirements
- Length: Data must have a length that is a power of 2
- Contiguity: For ndarray, the array must be contiguous in memory
Tests
# All tests
# Basic tests only (without ndarray)
# Tests with ndarray
License
This project is licensed under the MIT License - see the LICENSE file for details.