Create MLX array from R object
Usage
as_mlx(
x,
dtype = c("float32", "float64", "bool", "complex64", "int8", "int16", "int32", "int64",
"uint8", "uint16", "uint32", "uint64"),
device = mlx_default_device()
)Arguments
- x
Numeric, logical, or complex vector, matrix, or array to convert
- dtype
Data type for the MLX array. One of:
Floating point:
"float32","float64"Integer signed:
"int8","int16","int32","int64"Integer unsigned:
"uint8","uint16","uint32","uint64"Other:
"bool","complex64"
If not specified, defaults to
"float32"for numeric,"bool"for logical, and"complex64"for complex inputs.- device
Execution target: supply
"gpu","cpu", or anmlx_streamcreated viamlx_new_stream(). Defaults to the currentmlx_default_device()unless noted otherwise (helpers that act on an existing array typically reuse that array's device or stream).
Value
An object of class mlx
Integer types require explicit dtype
R integer vectors (like 1:10) convert to float32 by default.
To create integer MLX arrays, you must explicitly specify dtype:
Type precision notes
float64is supported but emits a warning and downcasts tofloat32Integer arithmetic may promote types (e.g., int32 + int32 might → int64)
Mixed integer/float operations promote to float
Missing values
MLX does not have an NA sentinel. When you pass numeric NA values from R,
they are stored as NaN inside MLX and returned to R as NaN.
Use is.nan() on MLX arrays if you need to detect them.
Examples
# Default float32 for numeric
x <- as_mlx(c(1.5, 2.5, 3.5))
mlx_dtype(x) # "float32"
#> [1] "float32"
# R integers also default to float32
x <- as_mlx(1:10)
mlx_dtype(x) # "float32"
#> [1] "float32"
# Explicit integer types
x_int <- as_mlx(1:10, dtype = "int32")
mlx_dtype(x_int) # "int32"
#> [1] "int32"
# Unsigned integers
x_uint <- as_mlx(c(0, 128, 255), dtype = "uint8")
# Logical → bool
mask <- as_mlx(c(TRUE, FALSE, TRUE))
mlx_dtype(mask) # "bool"
#> [1] "bool"