Skip to content

Julia Base.Array API

Julia's dense multidimensional array type — the central data structure for numerical and scientific computing, with element-wise broadcasting and rich indexing.

1 class · 8 methods

Array{T,N}

8 methods

A dense N-dimensional array of element type T. Backed by contiguous memory in column-major order; passed by reference and mutated in place with bang-suffixed functions.

zeros([T=Float64], dims...) -> Array{T,N}

Create an N-dimensional array filled with zero(T), with the given shape.

Parameters

NameTypeDescription
TTypeElement type (defaults to Float64).
dimsInt...Dimensions of the array; number of dims sets N.

Returns

Array{T,N} filled with zeros.

Example

julia
zeros(3)            # 3-element Vector{Float64} of 0.0
zeros(Int, 2, 4)    # 2x4 Matrix{Int} of 0
zeros(Float32, 2, 2, 2)  # 2x2x2 Array{Float32,3}
ones([T=Float64], dims...) -> Array{T,N}

Create an N-dimensional array filled with one(T).

Parameters

NameTypeDescription
TTypeElement type (defaults to Float64).
dimsInt...Dimensions of the array.

Returns

Array{T,N} filled with ones.

Example

julia
ones(3)            # [1.0, 1.0, 1.0]
ones(Int, 2, 2)    # 2x2 matrix of 1
3.0 .* ones(5)     # vector of 3.0 (broadcast)
reshape(A, dims...) -> AbstractArray

Return a view of A with the same data but a different shape. The total length must match.

Parameters

NameTypeDescription
AAbstractArraySource array (data is shared, not copied).
dimsInt... or TupleNew dimensions; product must equal length(A).

Returns

A reshaped view sharing memory with A.

Example

julia
v = 1:12
M = reshape(v, 3, 4)      # 3x4 view into 1:12
reshape(M, 2, 6)          # 2x6 view of the same data
reshape(1:6, 2, :)        # colon infers last dim = 3
transpose(A) -> AbstractArray

Return the transpose of a 2-D matrix (rows <-> columns). For complex matrices use adjoint for conjugate transpose.

Parameters

NameTypeDescription
AAbstractMatrixInput matrix.

Returns

A lazy transpose view; use copy(transpose(A)) for a materialized copy.

Example

julia
A = [1 2 3; 4 5 6]     # 2x3
transpose(A)            # 3x2 view:
                       #  [1 4; 2 5; 3 6]
adjoint(A)              # conjugate transpose (same for reals)
A'                     # shorthand for adjoint(A)
cat(A...; dims) -> Array

Concatenate arrays along dimension dims. vcat, hcat, and hvcat are common shortcuts.

Parameters

NameTypeDescription
A...AbstractArray...Arrays to concatenate; shapes must agree on non-cat dims.
dimsInt or TupleDimension(s) along which to concatenate.

Returns

A new array containing the concatenated inputs.

Example

julia
vcat([1,2], [3,4])               # [1,2,3,4]
hcat([1 2], [3 4])               # [1 2 3 4]
cat([1 2; 3 4], [5 6; 7 8], dims=2)  # horizontal stack
hvcat((2,2), 1,2,3,4)            # 2x2 matrix
broadcast(f, As...) -> Array (or f.(As...))

Apply f element-wise over arrays, expanding singleton dimensions. The dot syntax f.(x, y) is the idiomatic form.

Parameters

NameTypeDescription
fFunctionFunction to apply element-wise.
As...Any...Inputs; arrays broadcast over singleton dims.

Returns

Array of results; fusing broadcasts (@.) avoids temporaries.

Example

julia
sin.([0, π/2, π])        # [0.0, 1.0, ~0]
[1,2,3] .+ [10,20,30]    # [11,22,33]
[1,2,3] .+ 10            # scalar broadcast -> [11,12,13]
@. sin(x)^2 + cos(x)^2   # fused, single loop
mapreduce(f, op, A; [init]) -> Any

Apply f to each element of A, then reduce with op. Equivalent to op(f(A[1]), op(f(A[2]), ...)) but fused into one pass.

Parameters

NameTypeDescription
fFunctionMap function applied per element.
opFunctionAssociative binary operator for reduction.
AIterableInput collection.
initAny (kw)Optional initial value for op.

Returns

The reduced result. Type matches init / op's output.

Example

julia
mapreduce(abs, +, [-1, 2, -3])     # 6  (sum of abs)
mapreduce(uppercase, *, "abc")      # "ABC"
sum(x -> x^2, 1:10)                 # 385 (sum of squares)
mapreduce(sin, +, 1:1000; init=0.0)
sort(A; [dims=1], [alg], [lt=isless], [by], [rev=false]) -> Array

Return a sorted copy of A (or sort along dims). Use sort! for in-place sorting of a Vector.

Parameters

NameTypeDescription
AArrayInput array.
dimsInt (kw)Dimension to sort along (default 1).
algAlgorithm (kw)Sort algorithm: InsertionSort, QuickSort, MergeSort, RadixSort.
ltFunction (kw)Custom less-than comparator.
byFunction (kw)Transform applied before comparison (e.g. by=abs).
revBool (kw)Reverse the sort order.

Returns

A new sorted array (or sorted view of the same data).

Example

julia
sort([3, 1, 2])                # [1, 2, 3]
sort([3, -1, 2]; by=abs)        # [-1, 2, 3]
sort([1,2,3]; rev=true)         # [3, 2, 1]
sort!([3,1,2])                  # in-place sort
sortperm([30, 10, 20])          # [2, 3, 1] (indices)