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.
A = NDArray{Float32}(undef, 2, 3)
fill!(A, 1f0)
copyto!(A, rand(Float32, 2, 3)) # or fill from a host ArrayFrom 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
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
cuNumeric.zeros(2, 2)
cuNumeric.zeros(Float64, 3)
cuNumeric.zeros(Int32, (2,3))ones
cuNumeric.ones Function
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
cuNumeric.ones(2, 2)
cuNumeric.ones(Float32, 3)
cuNumeric.ones(Int32, (2, 3))fill
cuNumeric.fill Function
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
cuNumeric.fill(7.5, (2, 3))
cuNumeric.fill(0, 4)trues
cuNumeric.trues Function
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
cuNumeric.trues(2, 3)falses
cuNumeric.falses Function
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
cuNumeric.falses(2, 3)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.
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 identitySee Diagonal and identity for preferred patterns.
Random Numbers
rand
cuNumeric.rand Function
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
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)rand!
Random.rand! Method
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.
randn
cuNumeric.randn Function
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
cuNumeric.randn(2, 2)
cuNumeric.randn(Float64, 1000)randn!
Random.randn! Method
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.
randexp
cuNumeric.randexp Function
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
cuNumeric.randexp(2, 2)
cuNumeric.randexp(Float64, 1000)randexp!
Random.randexp! Method
Random.randexp!(arr::NDArray{<:SUPPORTED_FLOAT_TYPES})Fill arr in-place with exponential samples of scale 1 (mean 1), matching Julia randexp.
See RNG for BitGenerators (XORWOW, MRG32k3a, PHILOX4_32_10), Generator, and default_rng.