Crate pcw_fn

source ·
Expand description

Generic piecewise functions that allow for different internal buffers.

Examples

use pcw_fn::{VecPcwFn, PcwFn};

let pcw_poly: VecPcwFn<_, &dyn Fn(i32) -> i32> = VecPcwFn::try_from_iters(
    vec![5, 10, 15],
    vec![
        &(|x| x-10) as &dyn Fn(i32) -> i32,
        &(|x| 10) as &dyn Fn(i32) -> i32,
        &(|x| x*x + 5) as &dyn Fn(i32) -> i32,
        &(|x| x) as &dyn Fn(i32) -> i32,
    ],
)
.unwrap();
assert_eq!(pcw_poly.eval(0), -10);
assert_eq!(pcw_poly.eval(12), 12*12 + 5);
assert_eq!(pcw_poly.eval(500), 500);

let pcw_const = VecPcwFn::try_from_iters(
    vec![5, 10, 15],
    vec!["hi", "how", "are", "you"],
)
.unwrap();
assert_eq!(pcw_const.func_at(&0), &"hi");

let f: VecPcwFn<_, String> = pcw_const.combine(VecPcwFn::global(2), |s, x| format!("{s} {x}"));
assert_eq!(f.func_at(&15), &"you 2");

TODO: Add SmallVec and StaticVec backed variants. TODO: Add normalization / removal of redundant jumps. TODO: remove is_sorted dependency once is_sorted is stabilized.

Modules

Variant for Partially ordered domains that panics on incomparibility.

Structs

A piecewise function internally backed by Vecs

Enums

The different errors that can occur when constructing a piecewise function:

Traits

A trait for types that can be mapped over to obtain an instance of “the same type” with a different type parameter.
A trait for types that can be mapped over by reference.
A trait for types of kind * -> *
A piecewise function given by ╭ f₁(x) if x < x₀ │ f₂(x) if x₀ ≤ x < x₁ f(x) = ┤ f₃(x) if x₁ ≤ x < x₂ │ ⋮ ⋮ ╰ fₙ(x) if xₙ ≤ x for all x ∈ X where f₁,…,fₙ : X -> Y, and x₀ < x₁ < … < xₙ from some strictly totally ordered set X (so X is Ord). Note that the fᵢ are not necessarily distinct.