Skip to content

Julia Base.Array API

Julia 的密集多维数组类型 —— 数值和科学计算的核心数据结构,支持逐元素广播和丰富的索引。

1 class · 8 methods

Array{T,N}

8 methods

元素类型为 T 的密集 N 维数组。以列主序连续内存存储;按引用传递,可用 bang 后缀函数原地修改。

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

创建以 zero(T) 填充、具有给定形状的 N 维数组。

Parameters

NameTypeDescription
TType元素类型(默认 Float64)。
dimsInt...数组维度;维度数量决定 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}

创建以 one(T) 填充的 N 维数组。

Parameters

NameTypeDescription
TType元素类型(默认 Float64)。
dimsInt...数组维度。

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

返回 A 的视图,数据相同但形状不同。总长度必须匹配。

Parameters

NameTypeDescription
AAbstractArray源数组(数据共享,不复制)。
dimsInt... or Tuple新维度;乘积必须等于 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

返回二维矩阵的转置(行列互换)。复数矩阵用 adjoint 做共轭转置。

Parameters

NameTypeDescription
AAbstractMatrix输入矩阵。

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

沿维度 dims 拼接数组。vcat、hcat 和 hvcat 是常用快捷方式。

Parameters

NameTypeDescription
A...AbstractArray...要拼接的数组;非拼接维度的形状必须一致。
dimsInt or Tuple拼接的维度。

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...))

对数组逐元素应用 f,扩展单例维度。点语法 f.(x, y) 是惯用形式。

Parameters

NameTypeDescription
fFunction逐元素应用的函数。
As...Any...输入;数组在单例维度上广播。

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

对 A 的每个元素应用 f,再用 op 归约。等价于 op(f(A[1]), op(f(A[2]), ...)) 但融合为一次遍历。

Parameters

NameTypeDescription
fFunction对每个元素应用的映射函数。
opFunction归约用的结合二元运算符。
AIterable输入集合。
initAny (kw)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

返回 A 的排序副本(或沿维度排序)。Vector 的原地排序用 sort!。

Parameters

NameTypeDescription
AArray输入数组。
dimsInt (kw)排序的维度(默认 1)。
algAlgorithm (kw)排序算法:InsertionSort、QuickSort、MergeSort、RadixSort。
ltFunction (kw)自定义小于比较器。
byFunction (kw)比较前应用的变换(如 by=abs)。
revBool (kw)反转排序顺序。

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)