Skip to content

Commit 2036023

Browse files
committed
Use fast inverse Walsh transform
1 parent 55ed0fc commit 2036023

File tree

2 files changed

+12
-44
lines changed

2 files changed

+12
-44
lines changed

src/big_boolean_function.rs

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -197,18 +197,10 @@ impl BigBooleanFunction {
197197
}
198198
let num_variables = walsh_values_count.trailing_zeros() as usize;
199199
let mut truth_table = BigUint::zero();
200+
let mut inverse_walsh_transform = Vec::from(walsh_values);
201+
crate::utils::fast_walsh_transform(&mut inverse_walsh_transform);
200202
for i in 0..(1 << num_variables) {
201-
let value = walsh_values
202-
.iter()
203-
.enumerate()
204-
.map(|(w, walsh_value)| {
205-
walsh_value * (if (w & i).count_ones() & 1 == 0 { 1 } else { -1 })
206-
})
207-
.sum::<i32>()
208-
< 0;
209-
if value {
210-
truth_table.set_bit(i as u64, true);
211-
}
203+
truth_table.set_bit(i as u64, inverse_walsh_transform[i] < 0);
212204
}
213205
Ok(Self {
214206
variables_count: num_variables,
@@ -232,18 +224,10 @@ impl BigBooleanFunction {
232224
}
233225
let num_variables = walsh_values_count.trailing_zeros() as usize;
234226
let mut truth_table = BigUint::zero();
227+
let mut inverse_walsh_transform = Vec::from(walsh_values);
228+
crate::utils::fast_walsh_transform(&mut inverse_walsh_transform);
235229
for i in 0..(1 << num_variables) {
236-
let value = walsh_values
237-
.iter()
238-
.enumerate()
239-
.map(|(w, walsh_value)| {
240-
walsh_value * (if (w & i).count_ones() & 1 == 0 { 1 } else { -1 })
241-
})
242-
.sum::<i32>()
243-
!= 0;
244-
if value {
245-
truth_table.set_bit(i as u64, true);
246-
}
230+
truth_table.set_bit(i as u64, inverse_walsh_transform[i] != 0);
247231
}
248232
Ok(Self {
249233
variables_count: num_variables,

src/small_boolean_function.rs

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -210,18 +210,10 @@ impl SmallBooleanFunction {
210210
return Err(TooBigVariableCount(6));
211211
}
212212
let mut truth_table = 0u64;
213+
let mut inverse_walsh_transform = Vec::from(walsh_values);
214+
crate::utils::fast_walsh_transform(&mut inverse_walsh_transform);
213215
for i in 0..(1 << num_variables) {
214-
let value = walsh_values
215-
.iter()
216-
.enumerate()
217-
.map(|(w, walsh_value)| {
218-
walsh_value * (if (w & i).count_ones() & 1 == 0 { 1 } else { -1 })
219-
})
220-
.sum::<i32>()
221-
< 0;
222-
if value {
223-
truth_table |= 1 << i;
224-
}
216+
truth_table |= ((inverse_walsh_transform[i] < 0) as u64) << i;
225217
}
226218
Ok(Self {
227219
variables_count: num_variables,
@@ -251,18 +243,10 @@ impl SmallBooleanFunction {
251243
return Err(TooBigVariableCount(6));
252244
}
253245
let mut truth_table = 0u64;
246+
let mut inverse_walsh_transform = Vec::from(walsh_values);
247+
crate::utils::fast_walsh_transform(&mut inverse_walsh_transform);
254248
for i in 0..(1 << num_variables) {
255-
let value = walsh_values
256-
.iter()
257-
.enumerate()
258-
.map(|(w, walsh_value)| {
259-
walsh_value * (if (w & i).count_ones() & 1 == 0 { 1 } else { -1 })
260-
})
261-
.sum::<i32>()
262-
!= 0;
263-
if value {
264-
truth_table |= 1 << i;
265-
}
249+
truth_table |= ((inverse_walsh_transform[i] >> num_variables) as u64) << i;
266250
}
267251
Ok(Self {
268252
variables_count: num_variables,

0 commit comments

Comments
 (0)