Function create_approximation

Source
pub fn create_approximation<F>(
    x: Node,
    f: F,
    left: f32,
    right: f32,
    precision: u64,
    config: PWLConfig,
) -> Result<Node>
where F: Fn(f32) -> f32,
Expand description

This helper approximates any given function with a piecewise-linear approximation. It is assumed that we’re operating in fixed-precision arithmetic with precision bits after point. We’re approximating the function on the segment [left, right], using 2 ** config.log_buckets equally-distanced control points. Note that the function has to be defined everywhere, even outside the segment. Behavior outside of the segment is determined by config.flatten_left and config.flatten_right (they control whether it will be approximated with a constant or linearly).

Some notes on implementation. It is optimized for performance, vectorizing operations whenever possible. It uses Rounds(A2B) + max((config.log_buckets + 1) * Rounds(MixedMultiply), 6) + 2 network rounds. It is possible to remove the config.log_buckets at expense of more compute, but it is probably not worth it. It is also possible to slightly improve compute at expense of 7 more rounds.