MCPcopy Create free account
hub / github.com/docknetwork/crypto / weighted_inner_product

Function weighted_inner_product

utils/src/ff.rs:50–60  ·  view source on GitHub ↗

Weighted inner product of 2 vectors `a` and `b` with weight `w`. Calculated as `\sum_{i=0}(a_i * b_i * w^{i+1})`

(a: &[F], b: &[F], w: &F)

Source from the content-addressed store, hash-verified

48
49/// Weighted inner product of 2 vectors `a` and `b` with weight `w`. Calculated as `\sum_{i=0}(a_i * b_i * w^{i+1})`
50pub fn weighted_inner_product<F: Field>(a: &[F], b: &[F], w: &F) -> F {
51 let size = a.len().min(b.len());
52
53 let mut weights = powers(w, size as u32 + 1);
54 weights.remove(0);
55
56 let product = cfg_into_iter!(0..size).map(|i| a[i] * b[i] * weights[i]);
57
58 let zero = F::zero;
59 cfg_iter_sum!(product, zero)
60}
61
62/// Weighted inner product of the vector `n` with itself. Calculated as `\sum_{i=0}(n_i * n_i * w^{i+1})`
63pub fn weighted_norm<F: Field>(n: &[F], w: &F) -> F {

Callers 3

weighted_normFunction · 0.85
newMethod · 0.85

Calls 4

powersFunction · 0.85
mapMethod · 0.80
lenMethod · 0.45
removeMethod · 0.45

Tested by 1