Skip to content

Commit fb78b8f

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

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

src/lib.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,12 +212,37 @@ pub trait BooleanFunctionImpl: Debug {
212212
///
213213
/// # Returns
214214
/// A vector containing the Walsh-Fourier values for all points.
215+
#[deprecated(note = "Use `fast_walsh_fourier_values` instead")]
215216
fn walsh_fourier_values(&self) -> Vec<i32> {
216217
(0..=self.get_max_input_value())
217218
.map(|w| self.walsh_fourier_transform(w))
218219
.collect()
219220
}
220221

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

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

0 commit comments

Comments
 (0)