Skip to contents

Clamps every element of x to the range [min_val, max_val].

Usage

prim_clamp(min_val, x, max_val)

Arguments

min_val, max_val

(arrayish)
Lower and upper bound. Each must be scalar or the same shape as x, and shares its data type.

x

(arrayish)
The array to clamp. Can be any data type. min_val, x and max_val must have the same data type. An R value among them assumes the data type of the others when it is in its data type category, and its default data type when none of them has one.

Value

(arrayish)
Has x's shape and the data type the operands agreed on.

Implemented Rules

  • stablehlo

  • reverse

StableHLO

Lowers to hlo_clamp(), specified under clamp.

See also

Examples

x <- nv_array(c(-1, 0.5, 2))
# the R bounds take x's data type
prim_clamp(0, x, 1)
#> AnvlArray
#>  0.0000
#>  0.5000
#>  1.0000
#> [ CPUf32{3} ] 

# an integer array takes integer bounds
prim_clamp(0L, nv_array(1:5), 3L)
#> AnvlArray
#>  1
#>  2
#>  3
#>  3
#>  3
#> [ CPUi32{5} ] 

# the f64 bound settles it: x and max_val are built at f64 too
prim_clamp(nv_scalar(0, "f64"), 1, 2)
#> AnvlArray
#>  1
#> [ CPUf64{} ]