Skip to main content

fwht

Function fwht 

Source
pub fn fwht(a: &[Q]) -> Vec<Q> ⓘ
Expand description

Walsh–Hadamard transform (SymPy fwht), Hadamard ordering: A[k] = Σⱼ (−1)^{popcount(j & k)} a[j]. The input is zero-padded to a power of two.

§Examples

use symplex::discrete::fwht;
use num_bigint::BigInt;
use num_rational::Ratio;

let q = |v: &[i64]| -> Vec<Ratio<BigInt>> {
    v.iter().map(|&t| Ratio::from_integer(BigInt::from(t))).collect()
};
// SymPy: fwht([1, 2, 3, 4]) == [10, -2, -4, 0]
assert_eq!(fwht(&q(&[1, 2, 3, 4])), q(&[10, -2, -4, 0]));
// SymPy: fwht([1, 2, 3]) == [6, 2, 0, -4]   (padded to length 4)
assert_eq!(fwht(&q(&[1, 2, 3])), q(&[6, 2, 0, -4]));