Array{T,N}
8 methods元素类型为 T 的密集 N 维数组。以列主序连续内存存储;按引用传递,可用 bang 后缀函数原地修改。
zeros([T=Float64], dims...) -> Array{T,N}创建以 zero(T) 填充、具有给定形状的 N 维数组。
Parameters
| Name | Type | Description |
|---|---|---|
| T | Type | 元素类型(默认 Float64)。 |
| dims | Int... | 数组维度;维度数量决定 N。 |
Returns
Array{T,N} filled with zeros.
Example
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
| Name | Type | Description |
|---|---|---|
| T | Type | 元素类型(默认 Float64)。 |
| dims | Int... | 数组维度。 |
Returns
Array{T,N} filled with ones.
Example
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
| Name | Type | Description |
|---|---|---|
| A | AbstractArray | 源数组(数据共享,不复制)。 |
| dims | Int... or Tuple | 新维度;乘积必须等于 length(A)。 |
Returns
A reshaped view sharing memory with A.
Example
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 = 3transpose(A) -> AbstractArray返回二维矩阵的转置(行列互换)。复数矩阵用 adjoint 做共轭转置。
Parameters
| Name | Type | Description |
|---|---|---|
| A | AbstractMatrix | 输入矩阵。 |
Returns
A lazy transpose view; use copy(transpose(A)) for a materialized copy.
Example
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
| Name | Type | Description |
|---|---|---|
| A... | AbstractArray... | 要拼接的数组;非拼接维度的形状必须一致。 |
| dims | Int or Tuple | 拼接的维度。 |
Returns
A new array containing the concatenated inputs.
Example
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 matrixbroadcast(f, As...) -> Array (or f.(As...))对数组逐元素应用 f,扩展单例维度。点语法 f.(x, y) 是惯用形式。
Parameters
| Name | Type | Description |
|---|---|---|
| f | Function | 逐元素应用的函数。 |
| As... | Any... | 输入;数组在单例维度上广播。 |
Returns
Array of results; fusing broadcasts (@.) avoids temporaries.
Example
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 loopmapreduce(f, op, A; [init]) -> Any对 A 的每个元素应用 f,再用 op 归约。等价于 op(f(A[1]), op(f(A[2]), ...)) 但融合为一次遍历。
Parameters
| Name | Type | Description |
|---|---|---|
| f | Function | 对每个元素应用的映射函数。 |
| op | Function | 归约用的结合二元运算符。 |
| A | Iterable | 输入集合。 |
| init | Any (kw) | op 的可选初始值。 |
Returns
The reduced result. Type matches init / op's output.
Example
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
| Name | Type | Description |
|---|---|---|
| A | Array | 输入数组。 |
| dims | Int (kw) | 排序的维度(默认 1)。 |
| alg | Algorithm (kw) | 排序算法:InsertionSort、QuickSort、MergeSort、RadixSort。 |
| lt | Function (kw) | 自定义小于比较器。 |
| by | Function (kw) | 比较前应用的变换(如 by=abs)。 |
| rev | Bool (kw) | 反转排序顺序。 |
Returns
A new sorted array (or sorted view of the same data).
Example
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)