Skip to contents

These helpers mirror NumPy-style reductions, optionally collapsing one or more axes. Use drop = FALSE to retain reduced axes with length one (akin to setting drop = FALSE in base R).

Usage

mlx_sum(x, axis = NULL, drop = TRUE)

mlx_prod(x, axis = NULL, drop = TRUE)

mlx_all(x, axis = NULL, drop = TRUE)

mlx_any(x, axis = NULL, drop = TRUE)

mlx_mean(x, axis = NULL, drop = TRUE)

mlx_var(x, axis = NULL, drop = TRUE, ddof = 0L)

mlx_std(x, axis = NULL, drop = TRUE, ddof = 0L)

Arguments

x

An mlx array, or an R array/matrix/vector that will be converted via as_mlx().

axis

Optional integer vector of axes (1-indexed) to reduce. When NULL, the reduction is performed over all elements.

drop

Logical flag controlling dimension dropping: TRUE (default) removes reduced axes, while FALSE retains them with length one.

ddof

Non-negative integer delta degrees of freedom for variance or standard deviation reductions.

Value

An mlx array containing the reduction result.

Examples

x <- as_mlx(matrix(1:4, 2, 2))
mlx_sum(x)
#> mlx array []
#>   dtype: float32
#>   device: gpu
#>   values:
#> [1] 10
mlx_sum(x, axis = 1)
#> mlx array [2]
#>   dtype: float32
#>   device: gpu
#>   values:
#> [1] 3 7
mlx_prod(x, axis = 2, drop = FALSE)
#> mlx array [2 x 1]
#>   dtype: float32
#>   device: gpu
#>   values:
#>      [,1]
#> [1,]    3
#> [2,]    8
mlx_all(x > 0)
#> mlx array []
#>   dtype: bool
#>   device: gpu
#>   values:
#> [1] TRUE
mlx_any(x > 3)
#> mlx array []
#>   dtype: bool
#>   device: gpu
#>   values:
#> [1] TRUE
mlx_mean(x, axis = 1)
#> mlx array [2]
#>   dtype: float32
#>   device: gpu
#>   values:
#> [1] 1.5 3.5
mlx_var(x, axis = 2)
#> mlx array [2]
#>   dtype: float32
#>   device: gpu
#>   values:
#> [1] 1 1
mlx_std(x, ddof = 1)
#> mlx array []
#>   dtype: float32
#>   device: gpu
#>   values:
#> [1] 1.290994