Skip to content

Commit 08ca3e5

Browse files
committed
Unify API
1 parent 4825f07 commit 08ca3e5

File tree

2 files changed

+24
-108
lines changed

2 files changed

+24
-108
lines changed

src/lib.rs

Lines changed: 4 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -136,38 +136,16 @@ pub trait BooleanFunctionImpl: Debug {
136136
.sum()
137137
}
138138

139-
/// Computes the Walsh-Hadamard values for all points.
140-
///
141-
/// # Returns
142-
/// A vector containing the Walsh-Hadamard values for all points.
143-
#[deprecated(note = "Use `fast_walsh_hadamard_values` instead")]
144-
fn walsh_hadamard_values(&self) -> Vec<i32> {
145-
(0..=self.get_max_input_value())
146-
.map(|w| self.walsh_hadamard_transform(w))
147-
.collect()
148-
}
149-
150139
/// Computes the Walsh-Hadamard values for all points using fast Fourier transform.
151140
///
152141
/// # Returns
153142
/// A vector containing the Walsh-Hadamard values for all points.
154-
fn fast_walsh_hadamard_values(&self) -> Vec<i32> {
143+
fn walsh_hadamard_values(&self) -> Vec<i32> {
155144
let mut values = vec![0; (self.get_max_input_value() + 1) as usize];
156145
for i in 0..=self.get_max_input_value() {
157146
values[i as usize] = 1 | -((self.compute_cellular_automata_rule(i)) as i32);
158147
}
159-
let mut h = 1usize;
160-
while h <= self.get_max_input_value() as usize {
161-
for i in (0..=self.get_max_input_value() as usize).step_by(h * 2) {
162-
for j in 0..h {
163-
let a = values[i + j];
164-
let b = values[i + j + h];
165-
values[i + j] = a + b;
166-
values[i + j + h] = a - b;
167-
}
168-
}
169-
h *= 2;
170-
}
148+
utils::fast_walsh_transform(&mut values);
171149
values
172150
}
173151

@@ -233,38 +211,16 @@ pub trait BooleanFunctionImpl: Debug {
233211
.sum()
234212
}
235213

236-
/// Computes the Walsh-Fourier values for all points.
237-
///
238-
/// # Returns
239-
/// A vector containing the Walsh-Fourier values for all points.
240-
#[deprecated(note = "Use `fast_walsh_fourier_values` instead")]
241-
fn walsh_fourier_values(&self) -> Vec<i32> {
242-
(0..=self.get_max_input_value())
243-
.map(|w| self.walsh_fourier_transform(w))
244-
.collect()
245-
}
246-
247214
/// Computes the Walsh-Fourier values for all points using fast Fourier transform.
248215
///
249216
/// # Returns
250217
/// A vector containing the Walsh-Fourier values for all points.
251-
fn fast_walsh_fourier_values(&self) -> Vec<i32> {
218+
fn walsh_fourier_values(&self) -> Vec<i32> {
252219
let mut values = vec![0; (self.get_max_input_value() + 1) as usize];
253220
for i in 0..=self.get_max_input_value() {
254221
values[i as usize] = self.compute_cellular_automata_rule(i) as i32;
255222
}
256-
let mut h = 1usize;
257-
while h <= self.get_max_input_value() as usize {
258-
for i in (0..=self.get_max_input_value() as usize).step_by(h * 2) {
259-
for j in 0..h {
260-
let a = values[i + j];
261-
let b = values[i + j + h];
262-
values[i + j] = a + b;
263-
values[i + j + h] = a - b;
264-
}
265-
}
266-
h *= 2;
267-
}
223+
utils::fast_walsh_transform(&mut values);
268224
values
269225
}
270226

@@ -1882,27 +1838,6 @@ mod tests {
18821838
);
18831839
}
18841840

1885-
#[test]
1886-
fn test_fast_walsh_hadamard_values() {
1887-
let boolean_function = BooleanFunction::from_hex_string_truth_table("dd0e").unwrap();
1888-
assert_eq!(
1889-
boolean_function.fast_walsh_hadamard_values(),
1890-
[-2, -2, 6, -2, -6, 2, 2, 2, 6, 6, -2, 6, -6, 2, 2, 2]
1891-
);
1892-
1893-
let boolean_function = BooleanFunction::from_hex_string_truth_table("0000").unwrap();
1894-
assert_eq!(
1895-
boolean_function.fast_walsh_hadamard_values(),
1896-
[16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
1897-
);
1898-
1899-
let boolean_function = BooleanFunction::from_hex_string_truth_table("ffff").unwrap();
1900-
assert_eq!(
1901-
boolean_function.fast_walsh_hadamard_values(),
1902-
[-16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
1903-
);
1904-
}
1905-
19061841
#[test]
19071842
fn test_boolean_function_from_reverse_walsh_transform() {
19081843
let boolean_function = BooleanFunction::from_reverse_walsh_hadamard_transform(&[
@@ -2479,45 +2414,6 @@ mod tests {
24792414
);
24802415
}
24812416

2482-
#[test]
2483-
fn test_fast_walsh_fourier_values() {
2484-
let boolean_function = BooleanFunction::from_hex_string_truth_table("ff").unwrap();
2485-
assert_eq!(
2486-
boolean_function.fast_walsh_fourier_values(),
2487-
[8, 0, 0, 0, 0, 0, 0, 0]
2488-
);
2489-
2490-
let boolean_function = BooleanFunction::from_hex_string_truth_table("00").unwrap();
2491-
assert_eq!(
2492-
boolean_function.fast_walsh_fourier_values(),
2493-
[0, 0, 0, 0, 0, 0, 0, 0]
2494-
);
2495-
2496-
let boolean_function = BooleanFunction::from_hex_string_truth_table("0f").unwrap();
2497-
assert_eq!(
2498-
boolean_function.fast_walsh_fourier_values(),
2499-
[4, 0, 0, 0, 4, 0, 0, 0]
2500-
);
2501-
2502-
let boolean_function = BooleanFunction::from_hex_string_truth_table("55").unwrap();
2503-
assert_eq!(
2504-
boolean_function.fast_walsh_fourier_values(),
2505-
[4, 4, 0, 0, 0, 0, 0, 0]
2506-
);
2507-
2508-
let boolean_function = BooleanFunction::from_hex_string_truth_table("aa").unwrap();
2509-
assert_eq!(
2510-
boolean_function.fast_walsh_fourier_values(),
2511-
[4, -4, 0, 0, 0, 0, 0, 0]
2512-
);
2513-
2514-
let boolean_function = BooleanFunction::from_hex_string_truth_table("8001").unwrap();
2515-
assert_eq!(
2516-
boolean_function.fast_walsh_fourier_values(),
2517-
[2, 0, 0, 2, 0, 2, 2, 0, 0, 2, 2, 0, 2, 0, 0, 2]
2518-
);
2519-
}
2520-
25212417
#[test]
25222418
fn test_boolean_function_from_reverse_walsh_fourier_transform() {
25232419
let boolean_function =

src/utils.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,26 @@ pub(crate) fn fast_anf_transform_biguint(truth_table: &BigUint, variables_count:
134134
anf_form
135135
}
136136

137+
pub(crate) fn fast_walsh_transform(values: &mut [i32]) {
138+
let values_count = values.len();
139+
#[cfg(not(feature = "unsafe_disable_safety_checks"))]
140+
if values_count < 1 || !values_count.is_power_of_two() {
141+
panic!("fast_walsh_transform: values length must be a power of 2");
142+
}
143+
let mut h = 1usize;
144+
while h < values_count {
145+
for i in (0..values_count).step_by(h * 2) {
146+
for j in 0..h {
147+
let a = values[i + j];
148+
let b = values[i + j + h];
149+
values[i + j] = a + b;
150+
values[i + j + h] = a - b;
151+
}
152+
}
153+
h <<= 1;
154+
}
155+
}
156+
137157
#[allow(dead_code)] // maybe useless, but I keep it for the beauty of the code
138158
pub(crate) fn walsh_matrix(dim: usize) -> Vec<Vec<i8>> {
139159
(0usize..(1 << dim))

0 commit comments

Comments
 (0)