Skip to main content

piecewise

Function piecewise 

Source
pub fn piecewise<T, D>(
    x: &Array<T, D>,
    condlist: &[Array<bool, D>],
    funclist: &[&dyn Fn(T) -> T],
    default: T,
) -> FerrayResult<Array<T, D>>
where T: Element + Copy, D: Dimension,
Expand description

Evaluate a piecewise-defined function.

For each element position, the first condition in condlist that is true determines which function from funclist is applied. Elements where no condition is true receive the default value.

This is equivalent to numpy.piecewise(x, condlist, funclist).

§Arguments

  • x - The input array.
  • condlist - A slice of boolean arrays, each the same shape as x.
  • funclist - A slice of function references, one per condition. The slice element type is &dyn Fn(T) -> T (not Box<dyn Fn>) so callers can pass stack-allocated closures without heap allocation (#297).
  • default - The default value for elements where no condition is true.

§Errors

  • Returns FerrayError::InvalidValue if condlist and funclist have different lengths.
  • Returns FerrayError::ShapeMismatch if any condition array has a different shape than x.