Skip to content

Commit 17ea544

Browse files
committed
Add fast Walsh Fourier transform
1 parent 5ba30ad commit 17ea544

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

src/lib.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,30 @@ pub trait BooleanFunctionImpl: Debug {
218218
.collect()
219219
}
220220

221+
/// Computes the Walsh-Fourier values for all points using fast Fourier transform.
222+
///
223+
/// # Returns
224+
/// A vector containing the Walsh-Fourier values for all points.
225+
fn fast_walsh_fourier_values(&self) -> Vec<i32> {
226+
let mut values = vec![0; (self.get_max_input_value() + 1) as usize];
227+
for i in 0..=self.get_max_input_value() {
228+
values[i as usize] = self.compute_cellular_automata_rule(i) as i32;
229+
}
230+
let mut h = 1usize;
231+
while h <= self.get_max_input_value() as usize {
232+
for i in (0..=self.get_max_input_value() as usize).step_by(h * 2) {
233+
for j in 0..h {
234+
let a = values[i + j];
235+
let b = values[i + j + h];
236+
values[i + j] = a + b;
237+
values[i + j + h] = a - b;
238+
}
239+
}
240+
h *= 2;
241+
}
242+
values
243+
}
244+
221245
/// Computes the autocorrelation transform of the Boolean function for a given point.
222246
/// The autocorrelation transform of a Boolean function $f$, for a given point $\omega$, is defined as:
223247
///
@@ -2408,6 +2432,45 @@ mod tests {
24082432
);
24092433
}
24102434

2435+
#[test]
2436+
fn test_fast_walsh_hadamard_values() {
2437+
let boolean_function = BooleanFunction::from_hex_string_truth_table("ff").unwrap();
2438+
assert_eq!(
2439+
boolean_function.fast_walsh_fourier_values(),
2440+
[8, 0, 0, 0, 0, 0, 0, 0]
2441+
);
2442+
2443+
let boolean_function = BooleanFunction::from_hex_string_truth_table("00").unwrap();
2444+
assert_eq!(
2445+
boolean_function.fast_walsh_fourier_values(),
2446+
[0, 0, 0, 0, 0, 0, 0, 0]
2447+
);
2448+
2449+
let boolean_function = BooleanFunction::from_hex_string_truth_table("0f").unwrap();
2450+
assert_eq!(
2451+
boolean_function.fast_walsh_fourier_values(),
2452+
[4, 0, 0, 0, 4, 0, 0, 0]
2453+
);
2454+
2455+
let boolean_function = BooleanFunction::from_hex_string_truth_table("55").unwrap();
2456+
assert_eq!(
2457+
boolean_function.fast_walsh_fourier_values(),
2458+
[4, 4, 0, 0, 0, 0, 0, 0]
2459+
);
2460+
2461+
let boolean_function = BooleanFunction::from_hex_string_truth_table("aa").unwrap();
2462+
assert_eq!(
2463+
boolean_function.fast_walsh_fourier_values(),
2464+
[4, -4, 0, 0, 0, 0, 0, 0]
2465+
);
2466+
2467+
let boolean_function = BooleanFunction::from_hex_string_truth_table("8001").unwrap();
2468+
assert_eq!(
2469+
boolean_function.fast_walsh_fourier_values(),
2470+
[2, 0, 0, 2, 0, 2, 2, 0, 0, 2, 2, 0, 2, 0, 0, 2]
2471+
);
2472+
}
2473+
24112474
#[test]
24122475
fn test_boolean_function_from_reverse_walsh_fourier_transform() {
24132476
let boolean_function =

0 commit comments

Comments
 (0)