Skip to content

Initialization ​

Constructors for new NDArrays. Default floating-point type is Float32.

Basic Initialization ​

Uninitialized arrays ​

User-facing uninitialized array constructors, such as NDArray{T}(undef, dims...), NDArray{T}(undef, dims::Tuple), and ordinary similar calls, zero-initialize storage.

julia
A = NDArray{Float32}(undef, 2, 3)
fill!(A, 1f0)
copyto!(A, rand(Float32, 2, 3))   # or fill from a host Array

From host data ​

NDArray(host) makes a new array from a Julia Array. copyto!(A, host) refills an existing one without allocating.

zeros ​

cuNumeric.zeros Function
julia
cuNumeric.zeros([T=Float32,] dims::Int...)
cuNumeric.zeros([T=Float32,] dims::Tuple)

Create an NDArray with element type T, of all zeros with size specified by dims. The default type is Float32 if not specified.

Examples

julia
cuNumeric.zeros(2, 2)
cuNumeric.zeros(Float64, 3)
cuNumeric.zeros(Int32, (2,3))
source

ones ​

cuNumeric.ones Function
julia
cuNumeric.ones([T=Float32,] dims::Int...)
cuNumeric.ones([T=Float32,] dims::Tuple)

Create an NDArray with element type T, of all zeros with size specified by dims. The default type is Float32 if not specified.

Examples

julia
cuNumeric.ones(2, 2)
cuNumeric.ones(Float32, 3)
cuNumeric.ones(Int32, (2, 3))
source

fill ​

cuNumeric.fill Function
julia
cuNumeric.fill(val::T, dims::Dims)
cuNumeric.fill(val::T, dims::Int...)

Create an NDArray filled with the scalar value val, with the shape specified by dims.

Examples

julia
cuNumeric.fill(7.5, (2, 3))
cuNumeric.fill(0, 4)
source

trues ​

cuNumeric.trues Function
julia
cuNumeric.trues(dims::Tuple, val)
cuNumeric.trues(dim::Int, val)
cuNumeric.trues(dims::Int...)

Create an NDArray filled with the true, with the shape specified by dims.

Examples

julia
cuNumeric.trues(2, 3)
source

falses ​

cuNumeric.falses Function
julia
cuNumeric.falses(dims::Tuple, val)
cuNumeric.falses(dim::Int, val)
cuNumeric.falses(dims::Int...)

Create an NDArray filled with the false, with the shape specified by dims.

Examples

julia
cuNumeric.falses(2, 3)
source

Special Matrices ​

Diagonal ​

Construct a Diagonal matrix whose elements (diagonal only) are stored in an NDArray. LinearAlgebra.I can be used to construct dense identity matrices as well.

julia
using LinearAlgebra
using cuNumeric

D = Diagonal(cuNumeric.ones(Float32, 5))  # preferred for diagonal work
I32 = NDArray{Float32}(LinearAlgebra.I, 5, 5)           # dense Float32 identity
Ib = NDArray(LinearAlgebra.I, 5, 5)                     # Bool identity

See Diagonal and identity for preferred patterns.

Random Numbers ​

rand ​

cuNumeric.rand Function
julia
cuNumeric.rand([T=Float32,] dims::Int...)
cuNumeric.rand([T=Float32,] dims::Tuple)
cuNumeric.rand(r::AbstractUnitRange, dims...)

Create a new NDArray filled with uniform random values.

Floating types (Float32 / Float64) are drawn natively in [0, 1). ComplexF32 / ComplexF64 draw independent real/imag uniforms (unit square). Bool is a fair coin flip. Integer types Int16, Int32, and Int64 use the full range of T. A unit range (1:10) draws inclusive integers, matching Julia rand(1:10, dims...).

Uses a process-global XORWOW generator. For a different engine or seed, see default_rng and Generator.

Examples

julia
cuNumeric.rand(2, 2)
cuNumeric.rand((4, 1))
cuNumeric.rand(0:9, 4, 4)
cuNumeric.rand(Bool, 8)
A = cuNumeric.zeros(Float32, 2, 2); cuNumeric.rand!(A)
source

rand! ​

Random.rand! Method
julia
Random.rand!(arr::NDArray{<:SUPPORTED_FLOAT_TYPES})
Random.rand!(arr::NDArray{<:SUPPORTED_COMPLEX_TYPES})
Random.rand!(arr::NDArray{<:Union{Int16,Int32,Int64}})
Random.rand!(arr::NDArray{Bool})

Fill arr in-place with uniform random values.

Floating arrays are filled from [0, 1). Complex arrays draw independent real/imag uniforms in [0, 1) (the unit square). Integer arrays use the full range of the element type, matching Julia rand(T). Bool arrays are fair coin flips.

source

randn ​

cuNumeric.randn Function
julia
cuNumeric.randn([T=Float32,] dims::Int...)
cuNumeric.randn([T=Float32,] dims::Tuple)

Create a new NDArray filled with standard normal samples. Complex types match Julia randn(Complex{T}) (E[|z|²] = 1).

Uses a process-global XORWOW generator. For a different engine, pass a Generator to randn!. Shift or scale in user code (μ .+ σ .* Z); loc/scale are not part of the public API.

Examples

julia
cuNumeric.randn(2, 2)
cuNumeric.randn(Float64, 1000)
source

randn! ​

Random.randn! Method
julia
Random.randn!(arr::NDArray{<:SUPPORTED_FLOAT_TYPES})
Random.randn!(arr::NDArray{<:SUPPORTED_COMPLEX_TYPES})

Fill arr in-place with standard normal samples. Real arrays have mean 0 and variance 1. Complex arrays match Julia randn(Complex{T}): independent real and imag parts with variance 1/2, so E[|z|²] = 1.

source

randexp ​

cuNumeric.randexp Function
julia
cuNumeric.randexp([T=Float32,] dims::Int...)
cuNumeric.randexp([T=Float32,] dims::Tuple)

Create a new NDArray filled with exponential samples of scale 1 (mean 1), matching Julia randexp.

Uses a process-global XORWOW generator. For a different engine or scale, use randexp!(generator, arr; scale).

Examples

julia
cuNumeric.randexp(2, 2)
cuNumeric.randexp(Float64, 1000)
source

randexp! ​

Random.randexp! Method
julia
Random.randexp!(arr::NDArray{<:SUPPORTED_FLOAT_TYPES})

Fill arr in-place with exponential samples of scale 1 (mean 1), matching Julia randexp.

source

See RNG for BitGenerators (XORWOW, MRG32k3a, PHILOX4_32_10), Generator, and default_rng.