陣列
建構函式和類型
Core.AbstractArray
— 類型AbstractArray{T,N}
N
維陣列(或類陣列類型)的超類型,其元素類型為 T
。 Array
和其他類型是此類型的子類型。請參閱手冊中關於 AbstractArray
介面 的章節。
Base.AbstractVector
— 類型AbstractVector{T}
一維陣列(或類陣列類型)的超類型,其元素類型為 T
。 AbstractArray{T,1}
的別名。
Base.AbstractMatrix
— 類型AbstractMatrix{T}
二維陣列(或類陣列類型)的超類型,其元素類型為 T
。 AbstractArray{T,2}
的別名。
Base.AbstractVecOrMat
— 類型AbstractVecOrMat{T}
AbstractVector{T}
和 AbstractMatrix{T}
的聯集類型。
Core.Array
— 類型Array{T,N} <: AbstractArray{T,N}
N
維稠密陣列,其元素類型為 T
。
Core.Array
— 方法Array{T}(undef, dims)
Array{T,N}(undef, dims)
建構一個未初始化的 N
維 Array
,其元素類型為 T
。N
可以明確提供,例如 Array{T,N}(undef, dims)
,或由 dims
的長度或數量決定。dims
可以是元組或一系列對應於每個維度長度的整數引數。如果明確提供秩 N
,則它必須與 dims
的長度或數量相符。這裡的 undef
是 UndefInitializer
。
範例
julia> A = Array{Float64, 2}(undef, 2, 3) # N given explicitly
2×3 Matrix{Float64}:
6.90198e-310 6.90198e-310 6.90198e-310
6.90198e-310 6.90198e-310 0.0
julia> B = Array{Float64}(undef, 4) # N determined by the input
4-element Vector{Float64}:
2.360075077e-314
NaN
2.2671131793e-314
2.299821756e-314
julia> similar(B, 2, 4, 1) # use typeof(B), and the given size
2×4×1 Array{Float64, 3}:
[:, :, 1] =
2.26703e-314 2.26708e-314 0.0 2.80997e-314
0.0 2.26703e-314 2.26708e-314 0.0
Core.Array
— 方法Array{T}(nothing, dims)
Array{T,N}(nothing, dims)
建構一個 N 維的 Array
,包含型別為 T
的元素,並以 nothing
條目初始化。元素型別 T
必須能夠容納這些值,亦即 Nothing <: T
。
範例
julia> Array{Union{Nothing, String}}(nothing, 2)
2-element Vector{Union{Nothing, String}}:
nothing
nothing
julia> Array{Union{Nothing, Int}}(nothing, 2, 3)
2×3 Matrix{Union{Nothing, Int64}}:
nothing nothing nothing
nothing nothing nothing
Core.Array
— 方法Array{T}(missing, dims)
Array{T,N}(missing, dims)
建構一個 N 維的 Array
,包含型別為 T
的元素,並以 missing
條目初始化。元素型別 T
必須能夠容納這些值,亦即 Missing <: T
。
範例
julia> Array{Union{Missing, String}}(missing, 2)
2-element Vector{Union{Missing, String}}:
missing
missing
julia> Array{Union{Missing, Int}}(missing, 2, 3)
2×3 Matrix{Union{Missing, Int64}}:
missing missing missing
missing missing missing
Core.UndefInitializer
— 型別UndefInitializer
在陣列初始化中使用的單例型別,表示陣列建構函式呼叫者想要一個未初始化的陣列。另請參閱 undef
,它是 UndefInitializer()
的別名。
範例
julia> Array{Float64, 1}(UndefInitializer(), 3)
3-element Array{Float64, 1}:
2.2752528595e-314
2.202942107e-314
2.275252907e-314
Core.undef
— 常數undef
UndefInitializer()
的別名,它建構一個單例型別 UndefInitializer
的實例,用於陣列初始化中,以表示陣列建構函式呼叫者想要一個未初始化的陣列。
範例
julia> Array{Float64, 1}(undef, 3)
3-element Vector{Float64}:
2.2752528595e-314
2.202942107e-314
2.275252907e-314
Base.Vector
— 型別Vector{T} <: AbstractVector{T}
元素類型為 T
的一維密集陣列,通常用於表示數學向量。Array{T,1}
的別名。
Base.Vector
— 方法Vector{T}(undef, n)
建構一個長度為 n
的未初始化 Vector{T}
。
範例
julia> Vector{Float64}(undef, 3)
3-element Array{Float64, 1}:
6.90966e-310
6.90966e-310
6.90966e-310
Base.Vector
— 方法Vector{T}(nothing, m)
建構一個長度為 m
的 Vector{T}
,並使用 nothing
條目初始化。元素類型 T
必須能夠儲存這些值,亦即 Nothing <: T
。
範例
julia> Vector{Union{Nothing, String}}(nothing, 2)
2-element Vector{Union{Nothing, String}}:
nothing
nothing
Base.Vector
— 方法Vector{T}(missing, m)
建構一個長度為 m
的 Vector{T}
,並使用 missing
條目初始化。元素類型 T
必須能夠儲存這些值,亦即 Missing <: T
。
範例
julia> Vector{Union{Missing, String}}(missing, 2)
2-element Vector{Union{Missing, String}}:
missing
missing
Base.Matrix
— 類型Matrix{T} <: AbstractMatrix{T}
元素類型為 T
的二維密集陣列,通常用於表示數學矩陣。Array{T,2}
的別名。
Base.Matrix
— 方法Matrix{T}(undef, m, n)
建構一個大小為 m
×n
的未初始化 Matrix{T}
。
範例
julia> Matrix{Float64}(undef, 2, 3)
2×3 Array{Float64, 2}:
2.36365e-314 2.28473e-314 5.0e-324
2.26704e-314 2.26711e-314 NaN
julia> similar(ans, Int32, 2, 2)
2×2 Matrix{Int32}:
490537216 1277177453
1 1936748399
Base.Matrix
— 方法Matrix{T}(nothing, m, n)
建構一個大小為 m
×n
的 Matrix{T}
,並使用 nothing
條目初始化。元素類型 T
必須能夠儲存這些值,亦即 Nothing <: T
。
範例
julia> Matrix{Union{Nothing, String}}(nothing, 2, 3)
2×3 Matrix{Union{Nothing, String}}:
nothing nothing nothing
nothing nothing nothing
Base.Matrix
— 方法Matrix{T}(missing, m, n)
建構一個大小為 m
×n
的 Matrix{T}
,並以 missing
項目初始化。元素類型 T
必須能夠容納這些值,即 Missing <: T
。
範例
julia> Matrix{Union{Missing, String}}(missing, 2, 3)
2×3 Matrix{Union{Missing, String}}:
missing missing missing
missing missing missing
Base.VecOrMat
— 類型VecOrMat{T}
Vector{T}
和 Matrix{T}
的聯合類型,允許函數接受矩陣或向量。
範例
julia> Vector{Float64} <: VecOrMat{Float64}
true
julia> Matrix{Float64} <: VecOrMat{Float64}
true
julia> Array{Float64, 3} <: VecOrMat{Float64}
false
Core.DenseArray
— 類型DenseArray{T, N} <: AbstractArray{T,N}
元素類型為 T
的 N
維度稠密陣列。稠密陣列的元素在記憶體中是連續儲存的。
Base.DenseVector
— 類型DenseVector{T}
元素類型為 T
的一維 DenseArray
。DenseArray{T,1}
的別名。
Base.DenseMatrix
— 類型DenseMatrix{T}
元素類型為 T
的二維 DenseArray
。DenseArray{T,2}
的別名。
Base.DenseVecOrMat
— 類型DenseVecOrMat{T}
DenseVector{T}
和 DenseMatrix{T}
的聯合類型。
Base.StridedArray
— 類型StridedArray{T, N}
一個硬編碼的 Union
,由遵循 跨步陣列介面 的常見陣列類型組成,元素類型為 T
,維度為 N
。
如果 A
是 StridedArray
,則其元素會以偏移量儲存在記憶體中,偏移量在不同維度之間可能有所不同,但在同一個維度內則保持不變。例如,A
在維度 1 中的跨步為 2,在維度 2 中的跨步為 3。沿著維度 d
對 A
進行遞增,會在記憶體中跳過 [stride(A, d)
] 個槽位。跨步陣列特別重要且實用,因為它們有時可以直接作為指標傳遞給 BLAS 等外語函式庫。
Base.StridedVector
— 類型StridedVector{T}
一維 StridedArray
,元素類型為 T
。
Base.StridedMatrix
— 類型StridedMatrix{T}
二維 StridedArray
,元素類型為 T
。
Base.StridedVecOrMat
— 類型StridedVecOrMat{T}
聯合類型 StridedVector
和 StridedMatrix
,元素類型為 T
。
Base.Slices
— 類型Slices{P,SM,AX,S,N} <: AbstractSlices{S,N}
一個 AbstractArray
,用於對指定維度上的父陣列進行切片,返回從其他維度選擇所有數據的檢視。
這些通常應由 eachslice
、eachcol
或 eachrow
構建。
parent(s::Slices)
將返回父陣列。
Base.RowSlices
— 類型Base.ColumnSlices
— 類型Base.getindex
— 方法getindex(type[, elements...])
建構指定類型的 1 維陣列。這通常使用語法 Type[]
呼叫。元素值可以使用 Type[a,b,c,...]
指定。
範例
julia> Int8[1, 2, 3]
3-element Vector{Int8}:
1
2
3
julia> getindex(Int8, 1, 2, 3)
3-element Vector{Int8}:
1
2
3
Base.zeros
— 函數zeros([T=Float64,] dims::Tuple)
zeros([T=Float64,] dims...)
建立一個元素類型為 T
的 Array
,所有元素皆為 0,大小由 dims
指定。另請參閱 fill
、ones
、zero
。
範例
julia> zeros(1)
1-element Vector{Float64}:
0.0
julia> zeros(Int8, 2, 3)
2×3 Matrix{Int8}:
0 0 0
0 0 0
Base.ones
— 函數ones([T=Float64,] dims::Tuple)
ones([T=Float64,] dims...)
建立一個元素類型為 T
的 Array
,所有元素皆為 1,大小由 dims
指定。另請參閱 fill
、zeros
。
範例
julia> ones(1,2)
1×2 Matrix{Float64}:
1.0 1.0
julia> ones(ComplexF64, 2, 3)
2×3 Matrix{ComplexF64}:
1.0+0.0im 1.0+0.0im 1.0+0.0im
1.0+0.0im 1.0+0.0im 1.0+0.0im
Base.BitArray
— 類型BitArray{N} <: AbstractArray{Bool, N}
空間效率佳的 N
維布林陣列,每個布林值僅使用 1 個位元。
BitArray
將每個 8 個位元組封裝最多 64 個值,比 Array{Bool, N}
的空間效率高出 8 倍,並允許一些運算一次處理 64 個值。
預設情況下,Julia 會從產生布林元素(包括點狀比較,例如 .==
)的 廣播 運算,以及函數 trues
和 falses
回傳 BitArrays
。
由於其封裝儲存格式,同時存取 BitArray
元素(其中至少一個為寫入)並非執行緒安全。
Base.BitArray
— 方法BitArray(undef, dims::Integer...)
BitArray{N}(undef, dims::NTuple{N,Int})
建構一個具有給定維度的未定義 BitArray
。行為與 Array
建構函式相同。請參閱 undef
。
範例
julia> BitArray(undef, 2, 2)
2×2 BitMatrix:
0 0
0 0
julia> BitArray(undef, (3, 1))
3×1 BitMatrix:
0
0
0
Base.BitArray
— 方法BitArray(itr)
建構一個由給定的可迭代物件產生的 BitArray
。形狀由 itr
物件推斷。
範例
julia> BitArray([1 0; 0 1])
2×2 BitMatrix:
1 0
0 1
julia> BitArray(x+y == 3 for x = 1:2, y = 1:3)
2×3 BitMatrix:
0 1 0
1 0 0
julia> BitArray(x+y == 3 for x = 1:2 for y = 1:3)
6-element BitVector:
0
1
0
1
0
0
Base.trues
— 函式trues(dims)
建立一個所有值都設定為 true
的 BitArray
。
範例
julia> trues(2,3)
2×3 BitMatrix:
1 1 1
1 1 1
Base.falses
— 函式falses(dims)
建立一個所有值都設定為 false
的 BitArray
。
範例
julia> falses(2,3)
2×3 BitMatrix:
0 0 0
0 0 0
Base.fill
— 函式fill(value, dims::Tuple)
fill(value, dims...)
建立一個大小為 dims
的陣列,每個位置都設定為 value
。
例如,fill(1.0, (5,5))
會傳回一個 5×5 的浮點數陣列,陣列中每個位置都為 1.0
。
維度長度 dims
可以指定為元組或一系列引數。value
之後的一個 N
長度元組或 N
個引數指定一個 N
維陣列。因此,建立一個零維陣列,其唯一位置設定為 x
的常見慣用語法為 fill(x)
。
傳回陣列的每個位置都設定為(因此 ===
)傳遞的 value
;這表示如果 value
本身被修改,fill
ed 陣列的所有元素都會反映該修改,因為它們仍然是那個 value
。對於 fill(1.0, (5,5))
來說,這無需擔心,因為 value
1.0
是不可變的,本身無法修改,但對於可變值(最常見的是陣列)來說可能會出乎意料。例如,fill([], 3)
會將完全相同的空陣列放入傳回向量的所有三個位置
julia> v = fill([], 3)
3-element Vector{Vector{Any}}:
[]
[]
[]
julia> v[1] === v[2] === v[3]
true
julia> value = v[1]
Any[]
julia> push!(value, 867_5309)
1-element Vector{Any}:
8675309
julia> v
3-element Vector{Vector{Any}}:
[8675309]
[8675309]
[8675309]
若要建立一個包含許多獨立內部陣列的陣列,請改用 理解。這會在迴圈的每次反覆運算中建立一個新的、不同的陣列
julia> v2 = [[] for _ in 1:3]
3-element Vector{Vector{Any}}:
[]
[]
[]
julia> v2[1] === v2[2] === v2[3]
false
julia> push!(v2[1], 8675309)
1-element Vector{Any}:
8675309
julia> v2
3-element Vector{Vector{Any}}:
[8675309]
[]
[]
另請參閱:fill!
、zeros
、ones
、similar
。
範例
julia> fill(1.0, (2,3))
2×3 Matrix{Float64}:
1.0 1.0 1.0
1.0 1.0 1.0
julia> fill(42)
0-dimensional Array{Int64, 0}:
42
julia> A = fill(zeros(2), 2) # sets both elements to the same [0.0, 0.0] vector
2-element Vector{Vector{Float64}}:
[0.0, 0.0]
[0.0, 0.0]
julia> A[1][1] = 42; # modifies the filled value to be [42.0, 0.0]
julia> A # both A[1] and A[2] are the very same vector
2-element Vector{Vector{Float64}}:
[42.0, 0.0]
[42.0, 0.0]
Base.fill!
— 函式fill!(A, x)
用值 x
填滿陣列 A
。如果 x
是物件參考,所有元素都會參考同一個物件。fill!(A, Foo())
會傳回 A
,其中填滿了評估 Foo()
一次後的結果。
範例
julia> A = zeros(2,3)
2×3 Matrix{Float64}:
0.0 0.0 0.0
0.0 0.0 0.0
julia> fill!(A, 2.)
2×3 Matrix{Float64}:
2.0 2.0 2.0
2.0 2.0 2.0
julia> a = [1, 1, 1]; A = fill!(Vector{Vector{Int}}(undef, 3), a); a[1] = 2; A
3-element Vector{Vector{Int64}}:
[2, 1, 1]
[2, 1, 1]
[2, 1, 1]
julia> x = 0; f() = (global x += 1; x); fill!(Vector{Int}(undef, 3), f())
3-element Vector{Int64}:
1
1
1
Base.empty
— 函式empty(x::Tuple)
傳回一個空的元組,()
。
empty(v::AbstractVector, [eltype])
建立一個類似於 v
的空向量,選擇性地變更 eltype
。
另請參閱:empty!
、isempty
、isassigned
。
範例
julia> empty([1.0, 2.0, 3.0])
Float64[]
julia> empty([1.0, 2.0, 3.0], String)
String[]
empty(a::AbstractDict, [index_type=keytype(a)], [value_type=valtype(a)])
建立一個空的 AbstractDict
容器,它可以接受類型為 index_type
的索引和類型為 value_type
的值。第二和第三個參數是可選的,分別預設為輸入的 keytype
和 valtype
。(如果只指定了這兩種類型中的一種,則假設它是 value_type
,而 index_type
我們預設為 keytype(a)
)。
自訂的 AbstractDict
子類型可以選擇最適合於給定的索引和值類型傳回的特定字典類型,方法是針對三個參數簽章進行專門化。預設值是傳回一個空的 Dict
。
Base.similar
— 函數similar(A::AbstractSparseMatrixCSC{Tv,Ti}, [::Type{TvNew}, ::Type{TiNew}, m::Integer, n::Integer]) where {Tv,Ti}
根據給定的來源 SparseMatrixCSC
,建立一個未初始化的可變陣列,其元素類型、索引類型和大小與給定的一致。新的稀疏矩陣會維護原始稀疏矩陣的結構,除非輸出矩陣的維度與輸出不同。
輸出矩陣在與輸入相同的位置有零,但在非零位置有未初始化的值。
similar(array, [element_type=eltype(array)], [dims=size(array)])
根據給定的來源陣列,建立一個未初始化的可變陣列,其元素類型和大小與給定的一致。第二和第三個參數都是可選的,預設為給定陣列的 eltype
和 size
。維度可以指定為單一的元組參數或一系列整數參數。
自訂的 AbstractArray
子類型可以選擇最適合於給定的元素類型和維度傳回的特定陣列類型。如果它們沒有對此方法進行專門化,則預設值為 Array{element_type}(undef, dims...)
。
例如,similar(1:10, 1, 4)
傳回一個未初始化的 Array{Int,2}
,因為範圍既不可變也不支援 2 個維度
julia> similar(1:10, 1, 4)
1×4 Matrix{Int64}:
4419743872 4374413872 4419743888 0
相反地,similar(trues(10,10), 2)
傳回一個未初始化的 BitVector
,其中有兩個元素,因為 BitArray
既可變又可以支援 1 維陣列
julia> similar(trues(10,10), 2)
2-element BitVector:
0
0
由於 BitArray
只能儲存類型為 Bool
的元素,因此如果您要求不同的元素類型,它將建立一個常規的 Array
julia> similar(falses(10), Float64, 2, 4)
2×4 Matrix{Float64}:
2.18425e-314 2.18425e-314 2.18425e-314 2.18425e-314
2.18425e-314 2.18425e-314 2.18425e-314 2.18425e-314
另請參閱:undef
、isassigned
。
similar(storagetype, axes)
建立一個未初始化的可變陣列,類似於 storagetype
指定的陣列,但 axes
由最後一個參數指定。
範例:
similar(Array{Int}, axes(A))
建立一個「作用類似」於 Array{Int}
(並且可能確實由一個陣列支援)的陣列,但索引方式與 A
相同。如果 A
具有傳統索引,這將與 Array{Int}(undef, size(A))
相同,但如果 A
具有非傳統索引,則結果的索引將與 A
相符。
similar(BitArray, (axes(A, 2),))
將建立一個 1 維邏輯陣列,其索引與 A
的欄索引相符。
基本函數
Base.ndims
— 函數ndims(A::AbstractArray) -> Integer
傳回 A
的維度數。
範例
julia> A = fill(1, (3,4,5));
julia> ndims(A)
3
Base.size
— 函數size(A::AbstractArray, [dim])
傳回包含 A
維度的元組。您也可以指定一個維度,以取得該維度的長度。
請注意,對於具有非標準索引的陣列,size
可能未定義,在這種情況下,axes
可能有用。請參閱手冊章節 具有自訂索引的陣列。
另請參閱:length
、ndims
、eachindex
、sizeof
。
範例
julia> A = fill(1, (2,3,4));
julia> size(A)
(2, 3, 4)
julia> size(A, 2)
3
Base.axes
— 方法axes(A)
傳回陣列 A
的有效索引元組。
範例
julia> A = fill(1, (5,6,7));
julia> axes(A)
(Base.OneTo(5), Base.OneTo(6), Base.OneTo(7))
Base.axes
— 方法axes(A, d)
傳回陣列 A
沿著維度 d
的有效索引範圍。
另請參閱 size
,以及關於 具有自訂索引的陣列 的手冊章節。
範例
julia> A = fill(1, (5,6,7));
julia> axes(A, 2)
Base.OneTo(6)
julia> axes(A, 4) == 1:1 # all dimensions d > ndims(A) have size 1
true
使用注意事項
每個索引都必須是 AbstractUnitRange{<:Integer}
,但同時也可以是使用自訂索引的類型。因此,例如,如果您需要一個子集,請使用廣義索引建構,例如 begin
/end
或 firstindex
/lastindex
ix = axes(v, 1)
ix[2:end] # will work for eg Vector, but may fail in general
ix[(begin+1):end] # works for generalized indexes
Base.length
— 方法length(A::AbstractArray)
傳回陣列中的元素數量,預設為 prod(size(A))
。
範例
julia> length([1, 2, 3, 4])
4
julia> length([1 2; 3 4])
4
Base.keys
— 方法keys(a::AbstractArray)
傳回一個有效陣列,描述 a
的所有有效索引,排列成 a
本身的形狀。
1 維陣列(向量)的鍵是整數,而所有其他 N 維陣列則使用 CartesianIndex
來描述其位置。通常會使用特殊陣列類型 LinearIndices
和 CartesianIndices
來有效表示這些整數陣列和 CartesianIndex
es。
請注意,陣列的 keys
可能不是最有效的索引類型;若要獲得最佳效能,請改用 eachindex
。
範例
julia> keys([4, 5, 6])
3-element LinearIndices{1, Tuple{Base.OneTo{Int64}}}:
1
2
3
julia> keys([4 5; 6 7])
CartesianIndices((2, 2))
Base.eachindex
— 函數eachindex(A...)
eachindex(::IndexStyle, A::AbstractArray...)
建立一個可迭代物件,用有效率的方式拜訪 AbstractArray
A
的每個索引。對於已選擇快速線性索引的陣列類型(例如 Array
),如果它們使用以 1 為基礎的索引,則這只會是範圍 1:length(A)
。對於尚未選擇快速線性索引的陣列類型,通常會傳回一個專門的笛卡爾範圍,以使用為每個維度指定的索引有效率地對陣列進行索引。
一般而言,eachindex
接受任意可迭代物件,包括字串和字典,並傳回一個支援任意索引類型的迭代器物件(例如不均勻間隔或非整數索引)。
如果 A
是 AbstractArray
,則可以透過傳遞一個具有 IndexStyle
類型的值作為其第一個引數,來明確指定 eachindex
應該傳回的索引樣式(如果需要線性索引,通常為 IndexLinear()
;如果需要笛卡爾範圍,則為 IndexCartesian()
)。
如果您提供多個 AbstractArray
參數,eachindex
將建立一個可迭代物件,對所有參數來說都很快(通常是 UnitRange
,如果所有輸入都有快速的線性索引,否則為 CartesianIndices
)。如果陣列有不同的尺寸和/或維度,將會拋出 DimensionMismatch
例外。
另請參閱 pairs
(A)
以同時迭代索引和值,以及 axes
(A, 2)
以取得沿著一個維度的有效索引。
範例
julia> A = [10 20; 30 40];
julia> for i in eachindex(A) # linear indexing
println("A[", i, "] == ", A[i])
end
A[1] == 10
A[2] == 30
A[3] == 20
A[4] == 40
julia> for i in eachindex(view(A, 1:2, 1:1)) # Cartesian indexing
println(i)
end
CartesianIndex(1, 1)
CartesianIndex(2, 1)
Base.IndexStyle
— 類型IndexStyle(A)
IndexStyle(typeof(A))
IndexStyle
指定陣列 A
的「原生索引樣式」。當您定義一個新的 AbstractArray
類型時,您可以選擇實作線性索引(使用 IndexLinear
)或笛卡兒索引。如果您決定只實作線性索引,則必須為陣列類型設定此特質
Base.IndexStyle(::Type{<:MyArray}) = IndexLinear()
預設值為 IndexCartesian()
。
Julia 的內部索引機制會自動(且無形地)將所有索引操作重新計算為偏好的樣式。這允許使用者使用任何索引樣式存取陣列的元素,即使沒有提供明確的方法。
如果您為 AbstractArray
定義了兩種索引樣式,可以使用此特質來選擇效能最佳的索引樣式。有些方法會檢查輸入中的此特質,並根據最有效率的存取模式分派到不同的演算法。特別是,eachindex
會建立一個其類型取決於此特質設定的迭代器。
Base.IndexLinear
— 類型IndexLinear()
用於描述由一個線性索引最佳化索引的陣列的 IndexStyle
子類型。
線性索引樣式使用一個整數索引來描述陣列中的位置(即使它是多維陣列),並使用列優先順序來有效存取元素。這表示從 IndexLinear
陣列中要求 eachindex
會傳回一個簡單的一維範圍,即使它是多維的。
僅將其 IndexStyle
回報為 IndexLinear
的自訂陣列只需要使用單一 Int
索引來實作索引(和索引指定);所有其他索引表達式(包括多維存取)都將重新計算為線性索引。例如,如果 A
是具有線性索引的 2×3
自訂矩陣,而我們參照 A[1, 3]
,這將重新計算為等效的線性索引並呼叫 A[5]
,因為 1 + 2*(3 - 1) = 5
。
另請參閱 IndexCartesian
。
Base.IndexCartesian
— 類型IndexCartesian()
用於描述由笛卡兒索引最佳化索引的陣列的 IndexStyle
子類型。這是新的自訂 AbstractArray
子類型的預設值。
笛卡兒索引樣式使用多個整數索引來描述多維陣列中的位置,每個維度只有一個索引。這表示從 `IndexCartesian` 陣列中要求 eachindex
會傳回 CartesianIndices
範圍。
回報其 `IndexStyle` 為 `IndexCartesian` 的 `N` 維自訂陣列需要使用 `N` 個 `Int` 索引來實作索引(和索引指定),所有其他索引表達式(包括線性索引)將重新計算為等效的笛卡兒位置。例如,如果 `A` 是具有笛卡兒索引的 `2×3` 自訂矩陣,我們參考 `A[5]`,這將重新計算為等效的笛卡兒索引並呼叫 `A[1, 3]`,因為 `5 = 1 + 2*(3 - 1)`。
從線性索引計算笛卡兒索引比反向計算昂貴許多。前者運算需要除法(非常昂貴的運算),而後者只使用乘法和加法,而且本質上是免費的。這種不對稱表示使用 `IndexCartesian` 陣列的線性索引比使用 `IndexLinear` 陣列的笛卡兒索引昂貴許多。
另請參閱 IndexLinear
。
Base.conj!
— 函數conj!(A)
將陣列原地轉換為其複數共軛。
另請參閱 conj
。
範例
julia> A = [1+im 2-im; 2+2im 3+im]
2×2 Matrix{Complex{Int64}}:
1+1im 2-1im
2+2im 3+1im
julia> conj!(A);
julia> A
2×2 Matrix{Complex{Int64}}:
1-1im 2+1im
2-2im 3-1im
Base.stride
— 函數stride(A, k::Integer)
傳回維度 `k` 中相鄰元素在記憶體中的距離(以元素數量計)。
另請參閱:strides
。
範例
julia> A = fill(1, (3,4,5));
julia> stride(A,2)
3
julia> stride(A,3)
12
Base.strides
— 函數廣播和向量化
另請參閱函數向量化的點語法;例如,f.(args...)
會隱含呼叫broadcast(f, args...)
。與其依賴函數(如sin
)的「向量化」方法來對陣列進行運算,您應該使用sin.(a)
透過broadcast
進行向量化。
Base.Broadcast.broadcast
— 函數broadcast(f, As...)
對陣列、組、集合、Ref
和/或純量 As
廣播函數 f
。
廣播會對容器參數的元素和 As
中的純量本身套用函數 f
。單例和遺失的維度會透過實際重複該值來擴充,以符合其他參數的範圍。預設情況下,只有有限數量的類型被視為純量,包括 Number
、String
、Symbol
、Type
、Function
和一些常見的單例,例如 missing
和 nothing
。所有其他參數都會逐個元素進行反覆運算或索引。
結果容器類型由下列規則建立
- 如果所有參數都是純量或零維陣列,它會傳回一個未包裝的純量。
- 如果至少一個參數是組,而所有其他參數都是純量或零維陣列,它會傳回一個組。
- 所有其他參數組合預設傳回一個
Array
,但自訂容器類型可以定義自己的實作和類提升規則,以便在它們作為參數出現時自訂結果。
廣播有特殊的語法:f.(args...)
等同於 broadcast(f, args...)
,而巢狀的 f.(g.(args...))
呼叫會融合成單一的廣播迴圈。
範例
julia> A = [1, 2, 3, 4, 5]
5-element Vector{Int64}:
1
2
3
4
5
julia> B = [1 2; 3 4; 5 6; 7 8; 9 10]
5×2 Matrix{Int64}:
1 2
3 4
5 6
7 8
9 10
julia> broadcast(+, A, B)
5×2 Matrix{Int64}:
2 3
5 6
8 9
11 12
14 15
julia> parse.(Int, ["1", "2"])
2-element Vector{Int64}:
1
2
julia> abs.((1, -2))
(1, 2)
julia> broadcast(+, 1.0, (0, -2.0))
(1.0, -1.0)
julia> (+).([[0,2], [1,3]], Ref{Vector{Int}}([1,-1]))
2-element Vector{Vector{Int64}}:
[1, 1]
[2, 2]
julia> string.(("one","two","three","four"), ": ", 1:4)
4-element Vector{String}:
"one: 1"
"two: 2"
"three: 3"
"four: 4"
Base.Broadcast.broadcast!
— 函式broadcast!(f, dest, As...)
類似 broadcast
,但將 broadcast(f, As...)
的結果儲存在 dest
陣列中。請注意,dest
僅用於儲存結果,且不會提供引數給 f
,除非它也列在 As
中,例如 broadcast!(f, A, A, B)
會執行 A[:] = broadcast(f, A, B)
。
範例
julia> A = [1.0; 0.0]; B = [0.0; 0.0];
julia> broadcast!(+, B, A, (0, -2.0));
julia> B
2-element Vector{Float64}:
1.0
-2.0
julia> A
2-element Vector{Float64}:
1.0
0.0
julia> broadcast!(+, A, A, (0, -2.0));
julia> A
2-element Vector{Float64}:
1.0
-2.0
Base.Broadcast.@__dot__
— 巨集@. expr
將 expr
中的每個函式呼叫或運算子轉換為「點呼叫」(例如將 f(x)
轉換為 f.(x)
),並將 expr
中的每個賦值轉換為「點賦值」(例如將 +=
轉換為 .+=
)。
如果您想避免為 expr
中選定的函式呼叫加入點,請使用 $
將這些函式呼叫插入其中。例如,@. sqrt(abs($sort(x)))
等同於 sqrt.(abs.(sort(x)))
(sort
沒有點)。
(@.
等同於呼叫 @__dot__
。)
範例
julia> x = 1.0:3.0; y = similar(x);
julia> @. y = x + 3 * sin(x)
3-element Vector{Float64}:
3.5244129544236893
4.727892280477045
3.4233600241796016
若要針對自訂類型進行廣播專門化,請參閱
Base.Broadcast.BroadcastStyle
— 類型BroadcastStyle
是一種抽象類型和特質函式,用於確定物件在廣播下的行為。BroadcastStyle(typeof(x))
會傳回與 x
關聯的樣式。若要自訂類型的廣播行為,可以透過定義類型/方法配對來宣告樣式
struct MyContainerStyle <: BroadcastStyle end
Base.BroadcastStyle(::Type{<:MyContainer}) = MyContainerStyle()
然後撰寫方法(至少 similar
)來操作 Broadcasted{MyContainerStyle}
。BroadcastStyle
也有幾個預先定義的子類型,您可能可以使用;請參閱 介面章節 以取得更多資訊。
Base.Broadcast.AbstractArrayStyle
— 類型Broadcast.AbstractArrayStyle{N} <: BroadcastStyle
是與 AbstractArray
類型關聯的任何樣式的抽象超類型。N
參數是維度,這對於僅支援特定維度的 AbstractArray
類型非常方便
struct SparseMatrixStyle <: Broadcast.AbstractArrayStyle{2} end
Base.BroadcastStyle(::Type{<:SparseMatrixCSC}) = SparseMatrixStyle()
對於支援任意維度的 AbstractArray
類型,N
可以設定為 Any
struct MyArrayStyle <: Broadcast.AbstractArrayStyle{Any} end
Base.BroadcastStyle(::Type{<:MyArray}) = MyArrayStyle()
在您希望能夠混合多個 AbstractArrayStyle
並追蹤維度的案例中,您的樣式需要支援 Val
建構函式
struct MyArrayStyleDim{N} <: Broadcast.AbstractArrayStyle{N} end
(::Type{<:MyArrayStyleDim})(::Val{N}) where N = MyArrayStyleDim{N}()
請注意,如果兩個或多個 AbstractArrayStyle
子類型衝突,廣播機制將回退為產生 Array
。如果這是不希望的,您可能需要定義二進位 BroadcastStyle
規則來控制輸出類型。
Base.Broadcast.ArrayStyle
— 類型Broadcast.ArrayStyle{MyArrayType}()
是 BroadcastStyle
,表示物件在廣播時表現為陣列。它提供一種建構特定 AbstractArray
容器類型的 Broadcast.AbstractArrayStyle
的簡單方法。以這種方式建立的廣播樣式會失去維度的追蹤;如果追蹤對您的類型很重要,您應該建立您自己的自訂 Broadcast.AbstractArrayStyle
。
Base.Broadcast.DefaultArrayStyle
— 類型Broadcast.DefaultArrayStyle{N}()
是 BroadcastStyle
,表示物件在廣播時表現為 N
維陣列。具體來說,DefaultArrayStyle
用於任何尚未定義專門樣式的 AbstractArray
類型,並且在沒有其他 broadcast
參數覆寫的情況下,產生的輸出類型為 Array
。當有多個輸入到 broadcast
時,DefaultArrayStyle
會「輸給」任何其他 Broadcast.ArrayStyle
。
Base.Broadcast.broadcastable
— 函數Broadcast.broadcastable(x)
傳回 x
或類似 x
的物件,使其支援 axes
、索引,且其類型支援 ndims
。
如果 x
支援反覆運算,傳回的值應具有與 collect(x)
相同的 axes
和索引行為。
如果 x
不是 AbstractArray
,但它支援 axes
、索引,且其類型支援 ndims
,則可以實作 broadcastable(::typeof(x))
來傳回它自己。此外,如果 x
定義了自己的 BroadcastStyle
,則它必須定義其 broadcastable
方法,以傳回它自己,才能讓自訂樣式發揮作用。
範例
julia> Broadcast.broadcastable([1,2,3]) # like `identity` since arrays already support axes and indexing
3-element Vector{Int64}:
1
2
3
julia> Broadcast.broadcastable(Int) # Types don't support axes, indexing, or iteration but are commonly used as scalars
Base.RefValue{Type{Int64}}(Int64)
julia> Broadcast.broadcastable("hello") # Strings break convention of matching iteration and act like a scalar instead
Base.RefValue{String}("hello")
Base.Broadcast.combine_axes
— 函數combine_axes(As...) -> Tuple
針對 As
中所有值的廣播,決定結果軸。
julia> Broadcast.combine_axes([1], [1 2; 3 4; 5 6])
(Base.OneTo(3), Base.OneTo(2))
julia> Broadcast.combine_axes(1, 1, 1)
()
Base.Broadcast.combine_styles
— 函數combine_styles(cs...) -> BroadcastStyle
決定要對任意數量的值引數使用哪一個 BroadcastStyle
。使用 BroadcastStyle
取得每個引數的樣式,並使用 result_style
來結合樣式。
範例
julia> Broadcast.combine_styles([1], [1 2; 3 4])
Base.Broadcast.DefaultArrayStyle{2}()
Base.Broadcast.result_style
— 函數result_style(s1::BroadcastStyle[, s2::BroadcastStyle]) -> BroadcastStyle
取得一個或兩個 BroadcastStyle
,並使用 BroadcastStyle
將它們結合,以決定一個共通的 BroadcastStyle
。
範例
julia> Broadcast.result_style(Broadcast.DefaultArrayStyle{0}(), Broadcast.DefaultArrayStyle{3}())
Base.Broadcast.DefaultArrayStyle{3}()
julia> Broadcast.result_style(Broadcast.Unknown(), Broadcast.DefaultArrayStyle{1}())
Base.Broadcast.DefaultArrayStyle{1}()
索引和指派
Base.getindex
— 方法getindex(A, inds...)
傳回陣列 A
的子集,由 inds
指定,其中每個 ind
可能為 Int
、AbstractRange
或 Vector
。有關詳細資訊,請參閱手冊的 陣列索引 部分。
範例
julia> A = [1 2; 3 4]
2×2 Matrix{Int64}:
1 2
3 4
julia> getindex(A, 1)
1
julia> getindex(A, [2, 1])
2-element Vector{Int64}:
3
1
julia> getindex(A, 2:4)
3-element Vector{Int64}:
3
2
4
Base.setindex!
— 方法setindex!(A, X, inds...)
A[inds...] = X
將陣列 X
的值儲存在 A
的子集中,由 inds
指定。語法 A[inds...] = X
等於 (setindex!(A, X, inds...); X)
。
當任何已變異的引數與任何其他引數共用記憶體時,行為可能會出乎意料。
範例
julia> A = zeros(2,2);
julia> setindex!(A, [10, 20], [1, 2]);
julia> A[[3, 4]] = [30, 40];
julia> A
2×2 Matrix{Float64}:
10.0 30.0
20.0 40.0
Base.copyto!
— 方法copyto!(dest, Rdest::CartesianIndices, src, Rsrc::CartesianIndices) -> dest
將 src
中 Rsrc
範圍內的區塊複製到 dest
中 Rdest
範圍內的區塊。這兩個區域的大小必須相符。
範例
julia> A = zeros(5, 5);
julia> B = [1 2; 3 4];
julia> Ainds = CartesianIndices((2:3, 2:3));
julia> Binds = CartesianIndices(B);
julia> copyto!(A, Ainds, B, Binds)
5×5 Matrix{Float64}:
0.0 0.0 0.0 0.0 0.0
0.0 1.0 2.0 0.0 0.0
0.0 3.0 4.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0
Base.copy!
— 函數copy!(dst, src) -> dst
將 src
原地 複製
到 dst
中,捨棄 dst
中任何已存在的元素。如果 dst
和 src
的類型相同,呼叫後 dst == src
應成立。如果 dst
和 src
是多維陣列,它們必須有相等的 axes
。
當任何已變異的引數與任何其他引數共用記憶體時,行為可能會出乎意料。
另請參閱 copyto!
。
此方法至少需要 Julia 1.1。在 Julia 1.0 中,此方法可從 Future
標準函式庫中取得,為 Future.copy!
。
Base.isassigned
— 函式isassigned(array, i) -> Bool
測試給定的陣列是否與索引 i
有關聯的值。如果索引超出範圍或有未定義的參考,則傳回 false
。
範例
julia> isassigned(rand(3, 3), 5)
true
julia> isassigned(rand(3, 3), 3 * 3 + 1)
false
julia> mutable struct Foo end
julia> v = similar(rand(3), Foo)
3-element Vector{Foo}:
#undef
#undef
#undef
julia> isassigned(v, 1)
false
Base.Colon
— 類型Colon()
冒號 (:) 用於表示一次索引整個物件或維度。
在 Colons 上定義的運算很少;相反地,它們會由 to_indices
轉換成內部向量類型 (Base.Slice
),以表示它們在使用之前所跨越的索引集合。
Colon
的單例實例也是用於建構範圍的函式;請參閱 :
。
Base.IteratorsMD.CartesianIndex
— 類型CartesianIndex(i, j, k...) -> I
CartesianIndex((i, j, k...)) -> I
建立多維索引 I
,可用于索引多維陣列 A
。特別是,A[I]
等於 A[i,j,k...]
。可以自由地混合整數和 CartesianIndex
索引;例如,A[Ipre, i, Ipost]
(其中 Ipre
和 Ipost
是 CartesianIndex
索引,而 i
是 Int
)在撰寫沿任意維度運作的陣列演算法時,可能是一個有用的表達式。
CartesianIndex
有時是由 eachindex
產生,並且在使用明確的 CartesianIndices
進行反覆運算時總是產生。
I::CartesianIndex
被視為 broadcast
的「純量」(而非容器)。若要遍歷 CartesianIndex
的組成部分,請使用 Tuple(I)
將其轉換為元組。
範例
julia> A = reshape(Vector(1:16), (2, 2, 2, 2))
2×2×2×2 Array{Int64, 4}:
[:, :, 1, 1] =
1 3
2 4
[:, :, 2, 1] =
5 7
6 8
[:, :, 1, 2] =
9 11
10 12
[:, :, 2, 2] =
13 15
14 16
julia> A[CartesianIndex((1, 1, 1, 1))]
1
julia> A[CartesianIndex((1, 1, 1, 2))]
9
julia> A[CartesianIndex((1, 1, 2, 1))]
5
將 CartesianIndex
用作 broadcast
的「純量」需要 Julia 1.10;在之前的版本中,請使用 Ref(I)
。
Base.IteratorsMD.CartesianIndices
— 類型CartesianIndices(sz::Dims) -> R
CartesianIndices((istart:[istep:]istop, jstart:[jstep:]jstop, ...)) -> R
定義一個區域 R
,其跨越一個多維整數索引矩形範圍。這些最常在遍歷的背景下遇到,其中 for I in R ... end
會傳回 CartesianIndex
索引 I
,等於巢狀迴圈
for j = jstart:jstep:jstop
for i = istart:istep:istop
...
end
end
因此,這些對於撰寫在任意維度中運作的演算法很有用。
CartesianIndices(A::AbstractArray) -> R
方便的是,從陣列建構 CartesianIndices
會產生其索引的範圍。
步長範圍方法 CartesianIndices((istart:istep:istop, jstart:[jstep:]jstop, ...))
至少需要 Julia 1.6。
範例
julia> foreach(println, CartesianIndices((2, 2, 2)))
CartesianIndex(1, 1, 1)
CartesianIndex(2, 1, 1)
CartesianIndex(1, 2, 1)
CartesianIndex(2, 2, 1)
CartesianIndex(1, 1, 2)
CartesianIndex(2, 1, 2)
CartesianIndex(1, 2, 2)
CartesianIndex(2, 2, 2)
julia> CartesianIndices(fill(1, (2,3)))
CartesianIndices((2, 3))
線性索引和笛卡兒索引之間的轉換
線性索引轉換為笛卡兒索引利用了 CartesianIndices
是 AbstractArray
且可以線性索引的事實
julia> cartesian = CartesianIndices((1:3, 1:2))
CartesianIndices((1:3, 1:2))
julia> cartesian[4]
CartesianIndex(1, 2)
julia> cartesian = CartesianIndices((1:2:5, 1:2))
CartesianIndices((1:2:5, 1:2))
julia> cartesian[2, 2]
CartesianIndex(3, 2)
廣播
CartesianIndices
支援使用 CartesianIndex
進行廣播算術(+ 和 -)。
笛卡兒索引的廣播至少需要 Julia 1.1。
julia> CIs = CartesianIndices((2:3, 5:6))
CartesianIndices((2:3, 5:6))
julia> CI = CartesianIndex(3, 4)
CartesianIndex(3, 4)
julia> CIs .+ CI
CartesianIndices((5:6, 9:10))
有關笛卡兒索引轉換為線性索引的資訊,請參閱 LinearIndices
。
Base.Dims
— 類型Dims{N}
一個 NTuple
,包含 N
個 Int
,用於表示 AbstractArray
的維度。
Base.LinearIndices
— 類型LinearIndices(A::AbstractArray)
傳回一個具有與 A
相同形狀和 axes
的 LinearIndices
陣列,其中包含 A
中每個條目的線性索引。使用笛卡爾索引對此陣列進行索引,可以將其對應到線性索引。
對於具有傳統索引(索引從 1 開始)的陣列或任何多維陣列,線性索引範圍從 1 到 length(A)
。然而,對於 AbstractVector
,線性索引為 axes(A, 1)
,因此對於具有非常規索引的向量,不會從 1 開始。
呼叫此函式是撰寫利用線性索引的演算法的「安全」方式。
範例
julia> A = fill(1, (5,6,7));
julia> b = LinearIndices(A);
julia> extrema(b)
(1, 210)
LinearIndices(inds::CartesianIndices) -> R
LinearIndices(sz::Dims) -> R
LinearIndices((istart:istop, jstart:jstop, ...)) -> R
傳回一個具有指定形狀或 axes
的 LinearIndices
陣列。
範例
此建構式的主要目的是直觀地從笛卡爾索引轉換為線性索引
julia> linear = LinearIndices((1:3, 1:2))
3×2 LinearIndices{2, Tuple{UnitRange{Int64}, UnitRange{Int64}}}:
1 4
2 5
3 6
julia> linear[1,2]
4
Base.to_indices
— 函式to_indices(A, I::Tuple)
將元組 I
轉換為可用於對陣列 A
進行索引的索引元組。
傳回的元組只能包含 Int
或陣列 A
支援的標量索引的 AbstractArray
。遇到它不知道如何處理的新型索引類型時,它會出錯。
對於簡單的索引類型,它會遞延到未匯出的 Base.to_index(A, i)
來處理每個索引 i
。雖然此內部函式並非旨在直接呼叫,但自訂陣列或索引類型可以延伸 Base.to_index
以提供自訂索引行為。
更複雜的索引類型可能需要更多關於索引維度的背景資訊。為了支援這些情況,to_indices(A, I)
會呼叫 to_indices(A, axes(A), I)
,然後遞迴地同時遍歷給定的索引元組和 A
的維度索引。因此,並非所有索引類型都保證會傳遞到 Base.to_index
。
範例
julia> A = zeros(1,2,3,4);
julia> to_indices(A, (1,1,2,2))
(1, 1, 2, 2)
julia> to_indices(A, (1,1,2,20)) # no bounds checking
(1, 1, 2, 20)
julia> to_indices(A, (CartesianIndex((1,)), 2, CartesianIndex((3,4)))) # exotic index
(1, 2, 3, 4)
julia> to_indices(A, ([1,1], 1:2, 3, 4))
([1, 1], 1:2, 3, 4)
julia> to_indices(A, (1,2)) # no shape checking
(1, 2)
Base.checkbounds
— 函數checkbounds(Bool, A, I...)
如果指定的索引 I
在給定的陣列 A
的範圍內,則傳回 true
。AbstractArray
的子類型應專門化此方法,如果它們需要提供自訂的範圍檢查行為;然而,在許多情況下,可以依賴 A
的索引和 checkindex
。
另請參閱 checkindex
。
範例
julia> A = rand(3, 3);
julia> checkbounds(Bool, A, 2)
true
julia> checkbounds(Bool, A, 3, 4)
false
julia> checkbounds(Bool, A, 1:3)
true
julia> checkbounds(Bool, A, 1:3, 2:4)
false
checkbounds(A, I...)
如果指定的索引 I
不在給定的陣列 A
的範圍內,則擲回錯誤。
Base.checkindex
— 函數checkindex(Bool, inds::AbstractUnitRange, index)
如果給定的 index
在 inds
的範圍內,則傳回 true
。自訂類型,如果想對所有陣列表現為索引,可以延伸此方法以提供專門的範圍檢查實作。
另請參閱 checkbounds
。
範例
julia> checkindex(Bool, 1:20, 8)
true
julia> checkindex(Bool, 1:20, 21)
false
Base.elsize
— 函數elsize(type)
計算儲存在給定 type
內的連續 eltype
元素之間的記憶體跨距(以位元組為單位),如果陣列元素以均勻的線性跨距密集儲存。
範例
julia> Base.elsize(rand(Float32, 10))
4
檢視(子陣列和其他檢視類型)
「檢視」是一種資料結構,其作用類似陣列(它是 AbstractArray
的子類型),但底層資料實際上是另一個陣列的一部分。
例如,如果 x
是陣列且 v = @view x[1:10]
,則 v
會像 10 個元素的陣列一樣作用,但其資料實際上會存取 x
的前 10 個元素。寫入檢視,例如 v[3] = 2
,會直接寫入底層陣列 x
(在本例中會修改 x[3]
)。
預設情況下,像 x[1:10]
的切片操作會在 Julia 中建立一份副本。@view x[1:10]
會將其變更為建立檢視。@views
巨集可用於整段程式碼(例如 @views function foo() .... end
或 @views begin ... end
)中,以將該區塊中的所有切片操作變更為使用檢視。有時建立資料副本會比較快,而有時使用檢視會比較快,如 效能提示 中所述。
Base.view
— 函式view(A, inds...)
如同 getindex
,但會傳回一個輕量級陣列,它會延遲參考 (或實際上是檢視) 給定索引或索引 inds
的父陣列 A
,而不是急切地萃取元素或建構一個複製的子集。呼叫傳回值 (通常是 SubArray
) 上的 getindex
或 setindex!
會即時計算存取或修改父陣列的索引。如果在呼叫 view
之後改變父陣列的形狀,行為將是未定義的,因為沒有對父陣列進行邊界檢查;例如,它可能會導致分段錯誤。
某些不可變父陣列 (例如範圍) 可以在某些情況下選擇僅重新計算一個新陣列,而不是傳回 SubArray
,如果這樣做很有效率且提供相容的語意。
在 Julia 1.6 或更新版本中,可以在 AbstractString
上呼叫 view
,傳回 SubString
。
範例
julia> A = [1 2; 3 4]
2×2 Matrix{Int64}:
1 2
3 4
julia> b = view(A, :, 1)
2-element view(::Matrix{Int64}, :, 1) with eltype Int64:
1
3
julia> fill!(b, 0)
2-element view(::Matrix{Int64}, :, 1) with eltype Int64:
0
0
julia> A # Note A has changed even though we modified b
2×2 Matrix{Int64}:
0 2
0 4
julia> view(2:5, 2:3) # returns a range as type is immutable
3:4
Base.@view
— 巨集@view A[inds...]
將索引表達式 A[inds...]
轉換為等效的 view
呼叫。
這只能直接套用於單一索引表達式,對於包含特殊 begin
或 end
索引語法的表達式特別有幫助,例如 A[begin, 2:end-1]
(因為這些語法不受一般 view
函數支援)。
請注意,@view
不能用作一般指派的目標 (例如,@view(A[1, 2:end]) = ...
),未加裝飾的 索引指派 (A[1, 2:end] = ...
) 或廣播索引指派 (A[1, 2:end] .= ...
) 也不會建立副本。不過,它對於更新廣播指派很有用,例如 @view(A[1, 2:end]) .+= 1
,因為這是 @view(A[1, 2:end]) .= @view(A[1, 2:end]) + 1
的簡單語法,且右手邊的索引表達式在沒有 @view
的情況下會建立一個副本。
另請參閱 @views
,以切換整個程式碼區塊,以便對非純量索引使用檢視。
在索引表達式中使用 begin
來參照第一個索引需要至少 Julia 1.5。
範例
julia> A = [1 2; 3 4]
2×2 Matrix{Int64}:
1 2
3 4
julia> b = @view A[:, 1]
2-element view(::Matrix{Int64}, :, 1) with eltype Int64:
1
3
julia> fill!(b, 0)
2-element view(::Matrix{Int64}, :, 1) with eltype Int64:
0
0
julia> A
2×2 Matrix{Int64}:
0 2
0 4
Base.@views
— 巨集@views expression
將給定表達式(可能是 begin
/end
區塊、迴圈、函式等)中的每個陣列切片操作轉換為傳回檢視。純量索引、非陣列類型和明確的 getindex
呼叫(相對於 array[...]
)不受影響。
類似地,@views
將字串切片轉換為 SubString
檢視。
@views
巨集只會影響在給定 expression
中明確出現的 array[...]
表達式,而不是在該程式碼呼叫的函式中發生的陣列切片。
在索引表達式中使用 begin
來參照第一個索引需要至少 Julia 1.5。
範例
julia> A = zeros(3, 3);
julia> @views for row in 1:3
b = A[row, :]
b[:] .= row
end
julia> A
3×3 Matrix{Float64}:
1.0 1.0 1.0
2.0 2.0 2.0
3.0 3.0 3.0
Base.parent
— 函式parent(A)
傳回檢視的底層父物件。SubArray
、SubString
、ReshapedArray
或 LinearAlgebra.Transpose
類型的物件的父物件是在物件建立期間傳遞給 view
、reshape
、transpose
等作為引數的物件。如果輸入不是包裝物件,則傳回輸入本身。如果輸入被包裝多次,則只會移除最外層的包裝。
範例
julia> A = [1 2; 3 4]
2×2 Matrix{Int64}:
1 2
3 4
julia> V = view(A, 1:2, :)
2×2 view(::Matrix{Int64}, 1:2, :) with eltype Int64:
1 2
3 4
julia> parent(V)
2×2 Matrix{Int64}:
1 2
3 4
Base.parentindices
— 函式parentindices(A)
傳回對應於檢視 A
的 parent
中的索引。
範例
julia> A = [1 2; 3 4];
julia> V = view(A, 1, :)
2-element view(::Matrix{Int64}, 1, :) with eltype Int64:
1
2
julia> parentindices(V)
(1, Base.Slice(Base.OneTo(2)))
Base.selectdim
— 函式selectdim(A, d::Integer, i)
傳回 A
中所有資料的檢視,其中維度 d
的索引等於 i
。
等同於 view(A,:,:,...,i,:,:,...)
,其中 i
位於位置 d
。
另請參閱:eachslice
。
範例
julia> A = [1 2 3 4; 5 6 7 8]
2×4 Matrix{Int64}:
1 2 3 4
5 6 7 8
julia> selectdim(A, 2, 3)
2-element view(::Matrix{Int64}, :, 3) with eltype Int64:
3
7
julia> selectdim(A, 2, 3:4)
2×2 view(::Matrix{Int64}, :, 3:4) with eltype Int64:
3 4
7 8
Base.reinterpret
— 函數reinterpret(::Type{Out}, x::In)
將 isbits 值 x
中的二進位資料的類型詮釋變更為 isbits 類型 Out
。Out
的大小(忽略填補)必須與 x
的類型相同。例如,reinterpret(Float32, UInt32(7))
將對應於 UInt32(7)
的 4 個位元組詮釋為 Float32
。
julia> reinterpret(Float32, UInt32(7))
1.0f-44
julia> reinterpret(NTuple{2, UInt8}, 0x1234)
(0x34, 0x12)
julia> reinterpret(UInt16, (0x34, 0x12))
0x1234
julia> reinterpret(Tuple{UInt16, UInt8}, (0x01, 0x0203))
(0x0301, 0x02)
如果 Out
中的某些位元組組合不被視為有效,否則會被類型的建構函式和方法所阻止,請小心使用。未經額外驗證,可能會導致意外行為。
reinterpret(T::DataType, A::AbstractArray)
建立一個陣列檢視,其二進位資料與給定的陣列相同,但元素類型為 T
。
此函數也適用於「延遲」陣列,其元素在明確擷取之前不會計算。例如,範圍 1:6
上的 reinterpret
的作用類似於密集向量 collect(1:6)
julia> reinterpret(Float32, UInt32[1 2 3 4 5])
1×5 reinterpret(Float32, ::Matrix{UInt32}):
1.0f-45 3.0f-45 4.0f-45 6.0f-45 7.0f-45
julia> reinterpret(Complex{Int}, 1:6)
3-element reinterpret(Complex{Int64}, ::UnitRange{Int64}):
1 + 2im
3 + 4im
5 + 6im
reinterpret(reshape, T, A::AbstractArray{S}) -> B
在使用或新增「通道維度」的同時,變更 A
的類型詮釋。
如果 sizeof(T) = n*sizeof(S)
,其中 n>1
,則 A
的第一個維度大小必須為 n
,而 B
缺少 A
的第一個維度。相反,如果 sizeof(S) = n*sizeof(T)
,其中 n>1
,則 B
會取得一個新的第一個維度,大小為 n
。如果 sizeof(T) == sizeof(S)
,則維度不變。
此方法至少需要 Julia 1.6。
範例
julia> A = [1 2; 3 4]
2×2 Matrix{Int64}:
1 2
3 4
julia> reinterpret(reshape, Complex{Int}, A) # the result is a vector
2-element reinterpret(reshape, Complex{Int64}, ::Matrix{Int64}) with eltype Complex{Int64}:
1 + 3im
2 + 4im
julia> a = [(1,2,3), (4,5,6)]
2-element Vector{Tuple{Int64, Int64, Int64}}:
(1, 2, 3)
(4, 5, 6)
julia> reinterpret(reshape, Int, a) # the result is a matrix
3×2 reinterpret(reshape, Int64, ::Vector{Tuple{Int64, Int64, Int64}}) with eltype Int64:
1 4
2 5
3 6
Base.reshape
— 函數reshape(A, dims...) -> AbstractArray
reshape(A, dims) -> AbstractArray
傳回一個與 A
具有相同資料的陣列,但維度大小或維度數量不同。這兩個陣列共用相同的基礎資料,因此結果只有在 A
可變時才是可變的,而且設定一個陣列的元素會改變另一個陣列的值。
新維度可以指定為參數清單或形狀組。最多只能有一個維度使用 :
指定,這種情況下,其長度會計算為其與所有指定維度的乘積等於原始陣列 A
的長度。元素的總數不能改變。
範例
julia> A = Vector(1:16)
16-element Vector{Int64}:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
julia> reshape(A, (4, 4))
4×4 Matrix{Int64}:
1 5 9 13
2 6 10 14
3 7 11 15
4 8 12 16
julia> reshape(A, 2, :)
2×8 Matrix{Int64}:
1 3 5 7 9 11 13 15
2 4 6 8 10 12 14 16
julia> reshape(1:6, 2, 3)
2×3 reshape(::UnitRange{Int64}, 2, 3) with eltype Int64:
1 3 5
2 4 6
Base.dropdims
— 函數dropdims(A; dims)
傳回一個與 A
具有相同資料的陣列,但移除 dims
指定的維度。size(A,d)
必須等於 dims
中的每個 d
,且禁止重複的維度或 1:ndims(A)
之外的數字。
結果與 A
共享相同的基礎資料,因此,當且僅當 A
是可變時,結果才是可變的,且設定其中一個的元素會變更另一個的數值。
範例
julia> a = reshape(Vector(1:4),(2,2,1,1))
2×2×1×1 Array{Int64, 4}:
[:, :, 1, 1] =
1 3
2 4
julia> b = dropdims(a; dims=3)
2×2×1 Array{Int64, 3}:
[:, :, 1] =
1 3
2 4
julia> b[1,1,1] = 5; a
2×2×1×1 Array{Int64, 4}:
[:, :, 1, 1] =
5 3
2 4
Base.vec
— 函數vec(a::AbstractArray) -> AbstractVector
將陣列 a
重新塑造成一維欄向量。如果 a
已是 AbstractVector
,則傳回 a
。結果陣列與 a
共享相同的基礎資料,因此,只有當 a
是可變時,它才會是可變的,這種情況下,修改其中一個也會修改另一個。
範例
julia> a = [1 2 3; 4 5 6]
2×3 Matrix{Int64}:
1 2 3
4 5 6
julia> vec(a)
6-element Vector{Int64}:
1
4
2
5
3
6
julia> vec(1:3)
1:3
Base.SubArray
— 類型SubArray{T,N,P,I,L} <: AbstractArray{T,N}
N
維度檢視父陣列(類型為 P
),元素類型為 T
,受限於索引元組(類型為 I
)。L
對於支援快速線性索引的類型為真,否則為假。
使用 view
函數建構 SubArray
。
串接和排列
Base.cat
— 函數cat(A...; dims)
沿著 dims
中指定的維度串接輸入陣列。
沿著維度 d in dims
,輸出陣列的大小為 sum(size(a,d) for a in A)
。沿著其他維度,所有輸入陣列應具有相同大小,這也將是輸出陣列沿著這些維度的尺寸。
如果 dims
是單一數字,則不同的陣列將沿著該維度緊密封裝。如果 dims
是包含多個維度的可迭代物件,則這些維度上的位置會同時為每個輸入陣列增加,並在其他地方填入零。這允許將區塊對角矩陣建構為 cat(matrices...; dims=(1,2))
,及其高維類比。
特殊情況 dims=1
是 vcat
,而 dims=2
是 hcat
。另請參閱 hvcat
、hvncat
、stack
、repeat
。
關鍵字也接受 Val(dims)
。
對於多個維度 dims = Val(::Tuple)
已新增至 Julia 1.8。
範例
julia> cat([1 2; 3 4], [pi, pi], fill(10, 2,3,1); dims=2) # same as hcat
2×6×1 Array{Float64, 3}:
[:, :, 1] =
1.0 2.0 3.14159 10.0 10.0 10.0
3.0 4.0 3.14159 10.0 10.0 10.0
julia> cat(true, trues(2,2), trues(4)', dims=(1,2)) # block-diagonal
4×7 Matrix{Bool}:
1 0 0 0 0 0 0
0 1 1 0 0 0 0
0 1 1 0 0 0 0
0 0 0 1 1 1 1
julia> cat(1, [2], [3;;]; dims=Val(2))
1×3 Matrix{Int64}:
1 2 3
Base.vcat
— 函數vcat(A...)
垂直串接陣列或數字。等於 cat
(A...; dims=1)
,以及語法 [a; b; c]
。
若要串聯一個大型陣列向量,reduce(vcat, A)
會在 A isa AbstractVector{<:AbstractVecOrMat}
時,呼叫一個有效率的方法,而不是逐對處理。
另請參閱 hcat
、Iterators.flatten
、stack
。
範例
julia> v = vcat([1,2], [3,4])
4-element Vector{Int64}:
1
2
3
4
julia> v == vcat(1, 2, [3,4]) # accepts numbers
true
julia> v == [1; 2; [3,4]] # syntax for the same operation
true
julia> summary(ComplexF64[1; 2; [3,4]]) # syntax for supplying the element type
"4-element Vector{ComplexF64}"
julia> vcat(range(1, 2, length=3)) # collects lazy ranges
3-element Vector{Float64}:
1.0
1.5
2.0
julia> two = ([10, 20, 30]', Float64[4 5 6; 7 8 9]) # row vector and a matrix
([10 20 30], [4.0 5.0 6.0; 7.0 8.0 9.0])
julia> vcat(two...)
3×3 Matrix{Float64}:
10.0 20.0 30.0
4.0 5.0 6.0
7.0 8.0 9.0
julia> vs = [[1, 2], [3, 4], [5, 6]];
julia> reduce(vcat, vs) # more efficient than vcat(vs...)
6-element Vector{Int64}:
1
2
3
4
5
6
julia> ans == collect(Iterators.flatten(vs))
true
Base.hcat
— 函數hcat(A...)
水平串聯陣列或數字。等同於 cat
(A...; dims=2)
,以及語法 [a b c]
或 [a;; b;; c]
。
對於一個大型陣列向量,reduce(hcat, A)
會在 A isa AbstractVector{<:AbstractVecOrMat}
時,呼叫一個有效率的方法。對於一個向量的向量,這也可以寫成 stack
(A)
。
範例
julia> hcat([1,2], [3,4], [5,6])
2×3 Matrix{Int64}:
1 3 5
2 4 6
julia> hcat(1, 2, [30 40], [5, 6, 7]') # accepts numbers
1×7 Matrix{Int64}:
1 2 30 40 5 6 7
julia> ans == [1 2 [30 40] [5, 6, 7]'] # syntax for the same operation
true
julia> Float32[1 2 [30 40] [5, 6, 7]'] # syntax for supplying the eltype
1×7 Matrix{Float32}:
1.0 2.0 30.0 40.0 5.0 6.0 7.0
julia> ms = [zeros(2,2), [1 2; 3 4], [50 60; 70 80]];
julia> reduce(hcat, ms) # more efficient than hcat(ms...)
2×6 Matrix{Float64}:
0.0 0.0 1.0 2.0 50.0 60.0
0.0 0.0 3.0 4.0 70.0 80.0
julia> stack(ms) |> summary # disagrees on a vector of matrices
"2×2×3 Array{Float64, 3}"
julia> hcat(Int[], Int[], Int[]) # empty vectors, each of size (0,)
0×3 Matrix{Int64}
julia> hcat([1.1, 9.9], Matrix(undef, 2, 0)) # hcat with empty 2×0 Matrix
2×1 Matrix{Any}:
1.1
9.9
Base.hvcat
— 函數hvcat(blocks_per_row::Union{Tuple{Vararg{Int}}, Int}, values...)
一次呼叫水平和垂直串聯。此函數是針對區塊矩陣語法而呼叫。第一個參數指定在每個區塊列中要串聯的參數數量。如果第一個參數是單一整數 n
,則假設所有區塊列都有 n
個區塊欄。
範例
julia> a, b, c, d, e, f = 1, 2, 3, 4, 5, 6
(1, 2, 3, 4, 5, 6)
julia> [a b c; d e f]
2×3 Matrix{Int64}:
1 2 3
4 5 6
julia> hvcat((3,3), a,b,c,d,e,f)
2×3 Matrix{Int64}:
1 2 3
4 5 6
julia> [a b; c d; e f]
3×2 Matrix{Int64}:
1 2
3 4
5 6
julia> hvcat((2,2,2), a,b,c,d,e,f)
3×2 Matrix{Int64}:
1 2
3 4
5 6
julia> hvcat((2,2,2), a,b,c,d,e,f) == hvcat(2, a,b,c,d,e,f)
true
Base.hvncat
— 函數hvncat(dim::Int, row_first, values...)
hvncat(dims::Tuple{Vararg{Int}}, row_first, values...)
hvncat(shape::Tuple{Vararg{Tuple}}, row_first, values...)
一次呼叫多個 values
的水平、垂直和 n 維串聯。
這個函數會在區塊矩陣語法中被呼叫。第一個參數指定串接的形狀,類似於 hvcat
,作為一個元組的元組,或是指定每個軸上元素的關鍵數字的維度,並用於決定輸出維度。dims
表單效能較佳,且在串接操作在每個軸上具有相同數量的元素時預設使用(例如,[a b; c d;;; e f ; g h])。shape
表單用於每個軸上的元素數量不平衡時(例如,[a b ; c])。不平衡的語法需要額外的驗證開銷。dim
表單是針對僅沿一個維度串接的最佳化。row_first
指出 values
的排序方式。shape
的第一個和第二個元素的意義也根據 row_first
而交換。
範例
julia> a, b, c, d, e, f = 1, 2, 3, 4, 5, 6
(1, 2, 3, 4, 5, 6)
julia> [a b c;;; d e f]
1×3×2 Array{Int64, 3}:
[:, :, 1] =
1 2 3
[:, :, 2] =
4 5 6
julia> hvncat((2,1,3), false, a,b,c,d,e,f)
2×1×3 Array{Int64, 3}:
[:, :, 1] =
1
2
[:, :, 2] =
3
4
[:, :, 3] =
5
6
julia> [a b;;; c d;;; e f]
1×2×3 Array{Int64, 3}:
[:, :, 1] =
1 2
[:, :, 2] =
3 4
[:, :, 3] =
5 6
julia> hvncat(((3, 3), (3, 3), (6,)), true, a, b, c, d, e, f)
1×3×2 Array{Int64, 3}:
[:, :, 1] =
1 2 3
[:, :, 2] =
4 5 6
建構參數的範例
[a b c ; d e f ;;;
g h i ; j k l ;;;
m n o ; p q r ;;;
s t u ; v w x]
⇒ dims = (2, 3, 4)
[a b ; c ;;; d ;;;;]
___ _ _
2 1 1 = elements in each row (2, 1, 1)
_______ _
3 1 = elements in each column (3, 1)
_____________
4 = elements in each 3d slice (4,)
_____________
4 = elements in each 4d slice (4,)
⇒ shape = ((2, 1, 1), (3, 1), (4,), (4,)) with `row_first` = true
Base.stack
— 函數stack(iter; [dims])
將大小相等的陣列(或其他可迭代物件)集合組合成一個較大的陣列,透過沿著一個或多個新維度排列。
預設情況下,元素的軸會先放置,提供 size(result) = (size(first(iter))..., size(iter)...)
。這與 Iterators.flatten
(iter)
的元素順序相同。
使用關鍵字 dims::Integer
,iter
的第 i
個元素會變成切片 selectdim
(result, dims, i)
,以便 size(result, dims) == length(iter)
。在這種情況下,stack
會反轉具有相同 dims
的 eachslice
的動作。
各種 cat
函數也會合併陣列。不過,這些函數會延伸陣列現有的(可能微不足道的)維度,而不是將陣列置於新的維度中。它們也會接受陣列作為個別參數,而不是單一集合。
此函數至少需要 Julia 1.9。
範例
julia> vecs = (1:2, [30, 40], Float32[500, 600]);
julia> mat = stack(vecs)
2×3 Matrix{Float32}:
1.0 30.0 500.0
2.0 40.0 600.0
julia> mat == hcat(vecs...) == reduce(hcat, collect(vecs))
true
julia> vec(mat) == vcat(vecs...) == reduce(vcat, collect(vecs))
true
julia> stack(zip(1:4, 10:99)) # accepts any iterators of iterators
2×4 Matrix{Int64}:
1 2 3 4
10 11 12 13
julia> vec(ans) == collect(Iterators.flatten(zip(1:4, 10:99)))
true
julia> stack(vecs; dims=1) # unlike any cat function, 1st axis of vecs[1] is 2nd axis of result
3×2 Matrix{Float32}:
1.0 2.0
30.0 40.0
500.0 600.0
julia> x = rand(3,4);
julia> x == stack(eachcol(x)) == stack(eachrow(x), dims=1) # inverse of eachslice
true
高維度範例
julia> A = rand(5, 7, 11);
julia> E = eachslice(A, dims=2); # a vector of matrices
julia> (element = size(first(E)), container = size(E))
(element = (5, 11), container = (7,))
julia> stack(E) |> size
(5, 11, 7)
julia> stack(E) == stack(E; dims=3) == cat(E...; dims=3)
true
julia> A == stack(E; dims=2)
true
julia> M = (fill(10i+j, 2, 3) for i in 1:5, j in 1:7);
julia> (element = size(first(M)), container = size(M))
(element = (2, 3), container = (5, 7))
julia> stack(M) |> size # keeps all dimensions
(2, 3, 5, 7)
julia> stack(M; dims=1) |> size # vec(container) along dims=1
(35, 2, 3)
julia> hvcat(5, M...) |> size # hvcat puts matrices next to each other
(14, 15)
stack(f, args...; [dims])
將函數套用至集合中的每個元素,並將結果「堆疊」起來。或將多個集合使用 zip
函數串接在一起。
此函數應傳回大小相同的陣列(或元組,或其他迭代器)。這些陣列會成為結果的切片,每個切片沿著 dims
(如果已提供)或預設沿著最後的維度分隔。
範例
julia> stack(c -> (c, c-32), "julia")
2×5 Matrix{Char}:
'j' 'u' 'l' 'i' 'a'
'J' 'U' 'L' 'I' 'A'
julia> stack(eachrow([1 2 3; 4 5 6]), (10, 100); dims=1) do row, n
vcat(row, row .* n, row ./ n)
end
2×9 Matrix{Float64}:
1.0 2.0 3.0 10.0 20.0 30.0 0.1 0.2 0.3
4.0 5.0 6.0 400.0 500.0 600.0 0.04 0.05 0.06
Base.vect
— 函數vect(X...)
建立一個 Vector
,其元素類型由參數的 promote_typeof
計算而得,包含參數清單。
範例
julia> a = Base.vect(UInt8(1), 2.5, 1//2)
3-element Vector{Float64}:
1.0
2.5
0.5
Base.circshift
— 函數circshift(A, shifts)
在陣列中循環位移,亦即旋轉資料。第二個參數是元組或向量,指出在每個維度中位移的量,或整數,指出僅在第一個維度中位移。
另請參閱:circshift!
、circcopy!
、bitrotate
、<<
。
範例
julia> b = reshape(Vector(1:16), (4,4))
4×4 Matrix{Int64}:
1 5 9 13
2 6 10 14
3 7 11 15
4 8 12 16
julia> circshift(b, (0,2))
4×4 Matrix{Int64}:
9 13 1 5
10 14 2 6
11 15 3 7
12 16 4 8
julia> circshift(b, (-1,0))
4×4 Matrix{Int64}:
2 6 10 14
3 7 11 15
4 8 12 16
1 5 9 13
julia> a = BitArray([true, true, false, false, true])
5-element BitVector:
1
1
0
0
1
julia> circshift(a, 1)
5-element BitVector:
1
1
1
0
0
julia> circshift(a, -1)
5-element BitVector:
1
0
0
1
1
Base.circshift!
— 函數circshift!(dest, src, shifts)
在 src
中循環位移,亦即旋轉資料,將結果儲存在 dest
中。shifts
指定在每個維度中位移的量。
當任何已變異的引數與任何其他引數共用記憶體時,行為可能會出乎意料。
另請參閱 circshift
。
Base.circcopy!
— 函數circcopy!(dest, src)
將 src
複製到 dest
,將每個維度的索引設定為其長度的模數。src
和 dest
必須具有相同的大小,但其索引可以有偏移;任何偏移都會導致(循環)折返。如果陣列具有重疊的索引,則在重疊的網域上 dest
會與 src
相同。
當任何已變異的引數與任何其他引數共用記憶體時,行為可能會出乎意料。
另請參閱:circshift
。
範例
julia> src = reshape(Vector(1:16), (4,4))
4×4 Array{Int64,2}:
1 5 9 13
2 6 10 14
3 7 11 15
4 8 12 16
julia> dest = OffsetArray{Int}(undef, (0:3,2:5))
julia> circcopy!(dest, src)
OffsetArrays.OffsetArray{Int64,2,Array{Int64,2}} with indices 0:3×2:5:
8 12 16 4
5 9 13 1
6 10 14 2
7 11 15 3
julia> dest[1:3,2:4] == src[1:3,2:4]
true
Base.findall
— 方法findall(A)
傳回 A
的 true
索引或鍵的向量 I
。如果 A
沒有此類元素,則傳回一個空陣列。若要搜尋其他類型的值,請將謂詞作為第一個引數傳遞。
索引或鍵與 keys(A)
和 pairs(A)
傳回的索引或鍵類型相同。
另請參閱:findfirst
、searchsorted
。
範例
julia> A = [true, false, false, true]
4-element Vector{Bool}:
1
0
0
1
julia> findall(A)
2-element Vector{Int64}:
1
4
julia> A = [true false; false true]
2×2 Matrix{Bool}:
1 0
0 1
julia> findall(A)
2-element Vector{CartesianIndex{2}}:
CartesianIndex(1, 1)
CartesianIndex(2, 2)
julia> findall(falses(3))
Int64[]
Base.findall
— 方法findall(f::Function, A)
傳回 A
的索引或鍵的向量 I
,其中 f(A[I])
傳回 true
。如果 A
沒有此類元素,則傳回一個空陣列。
索引或鍵與 keys(A)
和 pairs(A)
傳回的索引或鍵類型相同。
範例
julia> x = [1, 3, 4]
3-element Vector{Int64}:
1
3
4
julia> findall(isodd, x)
2-element Vector{Int64}:
1
2
julia> A = [1 2 0; 3 4 0]
2×3 Matrix{Int64}:
1 2 0
3 4 0
julia> findall(isodd, A)
2-element Vector{CartesianIndex{2}}:
CartesianIndex(1, 1)
CartesianIndex(2, 1)
julia> findall(!iszero, A)
4-element Vector{CartesianIndex{2}}:
CartesianIndex(1, 1)
CartesianIndex(2, 1)
CartesianIndex(1, 2)
CartesianIndex(2, 2)
julia> d = Dict(:A => 10, :B => -1, :C => 0)
Dict{Symbol, Int64} with 3 entries:
:A => 10
:B => -1
:C => 0
julia> findall(x -> x >= 0, d)
2-element Vector{Symbol}:
:A
:C
Base.findfirst
— 方法findfirst(A)
傳回 A
中第一個 true
值的索引或鍵。如果找不到此類值,傳回 nothing
。若要搜尋其他類型的值,請將謂詞作為第一個參數傳遞。
索引或鍵與 keys(A)
和 pairs(A)
傳回的索引或鍵類型相同。
另見:findall
、findnext
、findlast
、searchsortedfirst
。
範例
julia> A = [false, false, true, false]
4-element Vector{Bool}:
0
0
1
0
julia> findfirst(A)
3
julia> findfirst(falses(3)) # returns nothing, but not printed in the REPL
julia> A = [false false; true false]
2×2 Matrix{Bool}:
0 0
1 0
julia> findfirst(A)
CartesianIndex(2, 1)
Base.findfirst
— 方法findfirst(predicate::Function, A)
傳回 A
中第一個元素的索引或鍵,而 predicate
會傳回 true
。如果沒有此類元素,傳回 nothing
。
索引或鍵與 keys(A)
和 pairs(A)
傳回的索引或鍵類型相同。
範例
julia> A = [1, 4, 2, 2]
4-element Vector{Int64}:
1
4
2
2
julia> findfirst(iseven, A)
2
julia> findfirst(x -> x>10, A) # returns nothing, but not printed in the REPL
julia> findfirst(isequal(4), A)
2
julia> A = [1 4; 2 2]
2×2 Matrix{Int64}:
1 4
2 2
julia> findfirst(iseven, A)
CartesianIndex(2, 1)
Base.findlast
— 方法findlast(A)
傳回 A
中最後一個 true
值的索引或鍵。如果 A
中沒有 true
值,傳回 nothing
。
索引或鍵與 keys(A)
和 pairs(A)
傳回的索引或鍵類型相同。
另見:findfirst
、findprev
、findall
。
範例
julia> A = [true, false, true, false]
4-element Vector{Bool}:
1
0
1
0
julia> findlast(A)
3
julia> A = falses(2,2);
julia> findlast(A) # returns nothing, but not printed in the REPL
julia> A = [true false; true false]
2×2 Matrix{Bool}:
1 0
1 0
julia> findlast(A)
CartesianIndex(2, 1)
Base.findlast
— 方法findlast(predicate::Function, A)
傳回 A
中最後一個元素的索引或鍵,而 predicate
會傳回 true
。如果沒有此類元素,傳回 nothing
。
索引或鍵與 keys(A)
和 pairs(A)
傳回的索引或鍵類型相同。
範例
julia> A = [1, 2, 3, 4]
4-element Vector{Int64}:
1
2
3
4
julia> findlast(isodd, A)
3
julia> findlast(x -> x > 5, A) # returns nothing, but not printed in the REPL
julia> A = [1 2; 3 4]
2×2 Matrix{Int64}:
1 2
3 4
julia> findlast(isodd, A)
CartesianIndex(2, 1)
Base.findnext
— 方法findnext(A, i)
尋找 A
中 true
元素的 i
之後(包含 i
)的下一個索引,如果找不到,傳回 nothing
。
索引與 keys(A)
和 pairs(A)
傳回的索引類型相同。
範例
julia> A = [false, false, true, false]
4-element Vector{Bool}:
0
0
1
0
julia> findnext(A, 1)
3
julia> findnext(A, 4) # returns nothing, but not printed in the REPL
julia> A = [false false; true false]
2×2 Matrix{Bool}:
0 0
1 0
julia> findnext(A, CartesianIndex(1, 1))
CartesianIndex(2, 1)
Base.findnext
— 方法findnext(predicate::Function, A, i)
尋找元素 A
中大於或等於 i
的下一個索引,其中 predicate
傳回 true
,如果找不到則傳回 nothing
。
索引與 keys(A)
和 pairs(A)
傳回的索引類型相同。
範例
julia> A = [1, 4, 2, 2];
julia> findnext(isodd, A, 1)
1
julia> findnext(isodd, A, 2) # returns nothing, but not printed in the REPL
julia> A = [1 4; 2 2];
julia> findnext(isodd, A, CartesianIndex(1, 1))
CartesianIndex(1, 1)
Base.findprev
— 方法findprev(A, i)
尋找元素 A
中大於或等於 i
的前一個索引,其中 true
,如果找不到則傳回 nothing
。
索引與 keys(A)
和 pairs(A)
傳回的索引類型相同。
另請參閱:findnext
、findfirst
、findall
。
範例
julia> A = [false, false, true, true]
4-element Vector{Bool}:
0
0
1
1
julia> findprev(A, 3)
3
julia> findprev(A, 1) # returns nothing, but not printed in the REPL
julia> A = [false false; true true]
2×2 Matrix{Bool}:
0 0
1 1
julia> findprev(A, CartesianIndex(2, 1))
CartesianIndex(2, 1)
Base.findprev
— 方法findprev(predicate::Function, A, i)
尋找元素 A
中大於或等於 i
的前一個索引,其中 predicate
傳回 true
,如果找不到則傳回 nothing
。
索引與 keys(A)
和 pairs(A)
傳回的索引類型相同。
範例
julia> A = [4, 6, 1, 2]
4-element Vector{Int64}:
4
6
1
2
julia> findprev(isodd, A, 1) # returns nothing, but not printed in the REPL
julia> findprev(isodd, A, 3)
3
julia> A = [4 6; 1 2]
2×2 Matrix{Int64}:
4 6
1 2
julia> findprev(isodd, A, CartesianIndex(1, 2))
CartesianIndex(2, 1)
Base.permutedims
— 函數permutedims(A::AbstractArray, perm)
排列陣列 A
的維度。perm
是長度為 ndims(A)
的向量或元組,指定排列。
另請參閱 permutedims!
、PermutedDimsArray
、transpose
、invperm
。
範例
julia> A = reshape(Vector(1:8), (2,2,2))
2×2×2 Array{Int64, 3}:
[:, :, 1] =
1 3
2 4
[:, :, 2] =
5 7
6 8
julia> perm = (3, 1, 2); # put the last dimension first
julia> B = permutedims(A, perm)
2×2×2 Array{Int64, 3}:
[:, :, 1] =
1 2
5 6
[:, :, 2] =
3 4
7 8
julia> A == permutedims(B, invperm(perm)) # the inverse permutation
true
對於 B = permutedims(A, perm)
的每個維度 i
,其對應的 A
維度將為 perm[i]
。這表示等式 size(B, i) == size(A, perm[i])
成立。
julia> A = randn(5, 7, 11, 13);
julia> perm = [4, 1, 3, 2];
julia> B = permutedims(A, perm);
julia> size(B)
(13, 5, 11, 7)
julia> size(A)[perm] == ans
true
permutedims(m::AbstractMatrix)
通過翻轉矩陣對角線上的元素來排列矩陣 m
的維度。與 LinearAlgebra
的 transpose
不同,因為該操作不是遞迴的。
範例
julia> a = [1 2; 3 4];
julia> b = [5 6; 7 8];
julia> c = [9 10; 11 12];
julia> d = [13 14; 15 16];
julia> X = [[a] [b]; [c] [d]]
2×2 Matrix{Matrix{Int64}}:
[1 2; 3 4] [5 6; 7 8]
[9 10; 11 12] [13 14; 15 16]
julia> permutedims(X)
2×2 Matrix{Matrix{Int64}}:
[1 2; 3 4] [9 10; 11 12]
[5 6; 7 8] [13 14; 15 16]
julia> transpose(X)
2×2 transpose(::Matrix{Matrix{Int64}}) with eltype Transpose{Int64, Matrix{Int64}}:
[1 3; 2 4] [9 11; 10 12]
[5 7; 6 8] [13 15; 14 16]
permutedims(v::AbstractVector)
將向量 v
重新塑造成 1 × length(v)
行矩陣。與 LinearAlgebra
的 transpose
不同,因為該操作不是遞迴的。
範例
julia> permutedims([1, 2, 3, 4])
1×4 Matrix{Int64}:
1 2 3 4
julia> V = [[[1 2; 3 4]]; [[5 6; 7 8]]]
2-element Vector{Matrix{Int64}}:
[1 2; 3 4]
[5 6; 7 8]
julia> permutedims(V)
1×2 Matrix{Matrix{Int64}}:
[1 2; 3 4] [5 6; 7 8]
julia> transpose(V)
1×2 transpose(::Vector{Matrix{Int64}}) with eltype Transpose{Int64, Matrix{Int64}}:
[1 3; 2 4] [5 7; 6 8]
Base.permutedims!
— 函數permutedims!(dest, src, perm)
排列陣列 src
的維度,並將結果儲存在陣列 dest
中。perm
是指定長度為 ndims(src)
的排列的向量。預先配置的陣列 dest
應具有 size(dest) == size(src)[perm]
,且會被完全覆寫。不支援原地排列,如果 src
和 dest
有重疊的記憶體區域,將會發生無法預期的結果。
另請參閱 permutedims
。
Base.PermutedDimsArrays.PermutedDimsArray
— 類型PermutedDimsArray(A, perm) -> B
給定一個抽象陣列 A
,建立一個檢視 B
,讓維度看起來是已排列的。類似於 permutedims
,但不會發生複製(B
與 A
共享儲存空間)。
另請參閱 permutedims
、invperm
。
範例
julia> A = rand(3,5,4);
julia> B = PermutedDimsArray(A, (3,1,2));
julia> size(B)
(4, 3, 5)
julia> B[3,1,2] == A[1,2,3]
true
Base.promote_shape
— 函數promote_shape(s1, s2)
檢查兩個陣列形狀的相容性,允許尾隨的單例維度,並傳回維度較多的形狀。
範例
julia> a = fill(1, (3,4,1,1,1));
julia> b = fill(1, (3,4));
julia> promote_shape(a,b)
(Base.OneTo(3), Base.OneTo(4), Base.OneTo(1), Base.OneTo(1), Base.OneTo(1))
julia> promote_shape((2,3,1,4), (2, 3, 1, 4, 1))
(2, 3, 1, 4, 1)
陣列函數
Base.accumulate
— 函數accumulate(op, A; dims::Integer, [init])
沿著 A
的維度 dims
進行累積運算 op
(對於向量,提供 dims
是可選的)。初始值 init
可以選擇透過關鍵字參數提供。另請參閱 accumulate!
以使用預先配置的輸出陣列,同時提升效能和控制輸出的精度(例如避免溢位)。
對於常見的運算,有專門的 accumulate
變體,請參閱 cumsum
、cumprod
。對於延遲版本,請參閱 Iterators.accumulate
。
非陣列迭代器上的 accumulate
至少需要 Julia 1.5。
範例
julia> accumulate(+, [1,2,3])
3-element Vector{Int64}:
1
3
6
julia> accumulate(min, (1, -2, 3, -4, 5), init=0)
(0, -2, -2, -4, -4)
julia> accumulate(/, (2, 4, Inf), init=100)
(50.0, 12.5, 0.0)
julia> accumulate(=>, i^2 for i in 1:3)
3-element Vector{Any}:
1
1 => 4
(1 => 4) => 9
julia> accumulate(+, fill(1, 3, 4))
3×4 Matrix{Int64}:
1 4 7 10
2 5 8 11
3 6 9 12
julia> accumulate(+, fill(1, 2, 5), dims=2, init=100.0)
2×5 Matrix{Float64}:
101.0 102.0 103.0 104.0 105.0
101.0 102.0 103.0 104.0 105.0
Base.accumulate!
— 函式accumulate!(op, B, A; [dims], [init])
沿著維度 dims
對 A
進行累積運算 op
,將結果儲存在 B
中。對於向量,提供 dims
是可選的。如果給定關鍵字參數 init
,則其值用於建立累積。
當任何已變異的引數與任何其他引數共用記憶體時,行為可能會出乎意料。
另請參閱 accumulate
、cumsum!
、cumprod!
。
範例
julia> x = [1, 0, 2, 0, 3];
julia> y = rand(5);
julia> accumulate!(+, y, x);
julia> y
5-element Vector{Float64}:
1.0
1.0
3.0
3.0
6.0
julia> A = [1 2 3; 4 5 6];
julia> B = similar(A);
julia> accumulate!(-, B, A, dims=1)
2×3 Matrix{Int64}:
1 2 3
-3 -3 -3
julia> accumulate!(*, B, A, dims=2, init=10)
2×3 Matrix{Int64}:
10 20 60
40 200 1200
Base.cumprod
— 函式cumprod(A; dims::Integer)
沿著維度 dim
進行累積乘積。另請參閱 cumprod!
以使用預先配置的輸出陣列,既可提升效能,又能控制輸出的精度(例如避免溢位)。
範例
julia> a = Int8[1 2 3; 4 5 6];
julia> cumprod(a, dims=1)
2×3 Matrix{Int64}:
1 2 3
4 10 18
julia> cumprod(a, dims=2)
2×3 Matrix{Int64}:
1 2 6
4 20 120
cumprod(itr)
迭代器的累積乘積。
另請參閱 cumprod!
、accumulate
、cumsum
。
非陣列迭代器上的 cumprod
至少需要 Julia 1.5。
範例
julia> cumprod(fill(1//2, 3))
3-element Vector{Rational{Int64}}:
1//2
1//4
1//8
julia> cumprod((1, 2, 1, 3, 1))
(1, 2, 2, 6, 6)
julia> cumprod("julia")
5-element Vector{String}:
"j"
"ju"
"jul"
"juli"
"julia"
Base.cumprod!
— 函式cumprod!(B, A; dims::Integer)
沿著維度 dims
對 A
進行累積乘積,將結果儲存在 B
中。另請參閱 cumprod
。
當任何已變異的引數與任何其他引數共用記憶體時,行為可能會出乎意料。
cumprod!(y::AbstractVector, x::AbstractVector)
向量 x
的累積乘積,將結果儲存在 y
中。另請參閱 cumprod
。
當任何已變異的引數與任何其他引數共用記憶體時,行為可能會出乎意料。
Base.cumsum
— 函式cumsum(A; dims::Integer)
沿著維度 dims
進行累積總和。另請參閱 cumsum!
以使用預先配置的輸出陣列,既可提升效能,又能控制輸出的精度(例如避免溢位)。
範例
julia> a = [1 2 3; 4 5 6]
2×3 Matrix{Int64}:
1 2 3
4 5 6
julia> cumsum(a, dims=1)
2×3 Matrix{Int64}:
1 2 3
5 7 9
julia> cumsum(a, dims=2)
2×3 Matrix{Int64}:
1 3 6
4 9 15
回傳陣列的 eltype
為小於系統字元組大小的帶正負號整數的 Int
,以及小於系統字元組大小的無正負號整數的 UInt
。若要保留具有小帶正負號或無正負號整數的陣列的 eltype
,應使用 accumulate(+, A)
。
julia> cumsum(Int8[100, 28])
2-element Vector{Int64}:
100
128
julia> accumulate(+,Int8[100, 28])
2-element Vector{Int8}:
100
-128
在前一種情況下,整數會擴充為系統字元組大小,因此結果為 Int64[100, 128]
。在後一種情況下,不會發生此類擴充,且整數溢位會導致 Int8[100, -128]
。
cumsum(itr)
反覆運算器的累加總和。
另請參閱 accumulate
以套用 +
以外的函數。
非陣列反覆運算器的 cumsum
至少需要 Julia 1.5。
範例
julia> cumsum(1:3)
3-element Vector{Int64}:
1
3
6
julia> cumsum((true, false, true, false, true))
(1, 1, 2, 2, 3)
julia> cumsum(fill(1, 2) for i in 1:3)
3-element Vector{Vector{Int64}}:
[1, 1]
[2, 2]
[3, 3]
Base.cumsum!
— 函數cumsum!(B, A; dims::Integer)
沿著維度 dims
對 A
進行累加總和,將結果儲存在 B
中。另請參閱 cumsum
。
當任何已變異的引數與任何其他引數共用記憶體時,行為可能會出乎意料。
Base.diff
— 函數diff(A::AbstractVector)
diff(A::AbstractArray; dims::Integer)
向量或多維陣列 A
上的有限差分運算子。在後一種情況下,需要使用 dims
關鍵字參數指定要運算的維度。
維度大於 2 的陣列的 diff
至少需要 Julia 1.1。
範例
julia> a = [2 4; 6 16]
2×2 Matrix{Int64}:
2 4
6 16
julia> diff(a, dims=2)
2×1 Matrix{Int64}:
2
10
julia> diff(vec(a))
3-element Vector{Int64}:
4
-2
12
Base.repeat
— 函數repeat(A::AbstractArray, counts::Integer...)
透過在每個維度中重複陣列 A
指定次數來建構陣列,由 counts
指定。
另請參閱:fill
、Iterators.repeated
、Iterators.cycle
。
範例
julia> repeat([1, 2, 3], 2)
6-element Vector{Int64}:
1
2
3
1
2
3
julia> repeat([1, 2, 3], 2, 3)
6×3 Matrix{Int64}:
1 1 1
2 2 2
3 3 3
1 1 1
2 2 2
3 3 3
repeat(A::AbstractArray; inner=ntuple(Returns(1), ndims(A)), outer=ntuple(Returns(1), ndims(A)))
透過重複 A
的項目來建構陣列。inner
的第 i 個元素指定 A
的第 i 個維度的個別項目應重複的次數。outer
的第 i 個元素指定沿著 A
的第 i 個維度重複切片的次數。如果省略 inner
或 outer
,則不執行重複。
範例
julia> repeat(1:2, inner=2)
4-element Vector{Int64}:
1
1
2
2
julia> repeat(1:2, outer=2)
4-element Vector{Int64}:
1
2
1
2
julia> repeat([1 2; 3 4], inner=(2, 1), outer=(1, 3))
4×6 Matrix{Int64}:
1 2 1 2 1 2
1 2 1 2 1 2
3 4 3 4 3 4
3 4 3 4 3 4
Base.rot180
— 函式rot180(A)
將矩陣 A
旋轉 180 度。
範例
julia> a = [1 2; 3 4]
2×2 Matrix{Int64}:
1 2
3 4
julia> rot180(a)
2×2 Matrix{Int64}:
4 3
2 1
rot180(A, k)
將矩陣 A
旋轉 180 度,整數 k
次。如果 k
為偶數,這等效於 copy
。
範例
julia> a = [1 2; 3 4]
2×2 Matrix{Int64}:
1 2
3 4
julia> rot180(a,1)
2×2 Matrix{Int64}:
4 3
2 1
julia> rot180(a,2)
2×2 Matrix{Int64}:
1 2
3 4
Base.rotl90
— 函式rotl90(A)
將矩陣 A
向左旋轉 90 度。
範例
julia> a = [1 2; 3 4]
2×2 Matrix{Int64}:
1 2
3 4
julia> rotl90(a)
2×2 Matrix{Int64}:
2 4
1 3
rotl90(A, k)
將矩陣 A
向左旋轉 90 度,逆時針旋轉,整數 k
次。如果 k
是四的倍數(包括零),這等效於 copy
。
範例
julia> a = [1 2; 3 4]
2×2 Matrix{Int64}:
1 2
3 4
julia> rotl90(a,1)
2×2 Matrix{Int64}:
2 4
1 3
julia> rotl90(a,2)
2×2 Matrix{Int64}:
4 3
2 1
julia> rotl90(a,3)
2×2 Matrix{Int64}:
3 1
4 2
julia> rotl90(a,4)
2×2 Matrix{Int64}:
1 2
3 4
Base.rotr90
— 函式rotr90(A)
將矩陣 A
向右旋轉 90 度。
範例
julia> a = [1 2; 3 4]
2×2 Matrix{Int64}:
1 2
3 4
julia> rotr90(a)
2×2 Matrix{Int64}:
3 1
4 2
rotr90(A, k)
將矩陣 A
向右旋轉 90 度,順時針旋轉,整數 k
次。如果 k
是四的倍數(包括零),這等效於 copy
。
範例
julia> a = [1 2; 3 4]
2×2 Matrix{Int64}:
1 2
3 4
julia> rotr90(a,1)
2×2 Matrix{Int64}:
3 1
4 2
julia> rotr90(a,2)
2×2 Matrix{Int64}:
4 3
2 1
julia> rotr90(a,3)
2×2 Matrix{Int64}:
2 4
1 3
julia> rotr90(a,4)
2×2 Matrix{Int64}:
1 2
3 4
Base.mapslices
— 函式mapslices(f, A; dims)
透過對陣列 A
的每個切片套用函式 f
,來轉換陣列 A
的給定維度,切片形式為 A[..., :, ..., :, ...]
,在 dims
中的每個 d
處有一個冒號。結果會沿著其餘維度串接。
例如,如果 dims = [1,2]
且 A
是 4 維,則 f
會在所有 i
和 j
上呼叫 x = A[:,:,i,j]
,且 f(x)
會在結果 R
中變成 R[:,:,i,j]
。
另請參閱 eachcol
或 eachslice
,與 map
或 stack
搭配使用。
範例
julia> A = reshape(1:30,(2,5,3))
2×5×3 reshape(::UnitRange{Int64}, 2, 5, 3) with eltype Int64:
[:, :, 1] =
1 3 5 7 9
2 4 6 8 10
[:, :, 2] =
11 13 15 17 19
12 14 16 18 20
[:, :, 3] =
21 23 25 27 29
22 24 26 28 30
julia> f(x::Matrix) = fill(x[1,1], 1,4); # returns a 1×4 matrix
julia> B = mapslices(f, A, dims=(1,2))
1×4×3 Array{Int64, 3}:
[:, :, 1] =
1 1 1 1
[:, :, 2] =
11 11 11 11
[:, :, 3] =
21 21 21 21
julia> f2(x::AbstractMatrix) = fill(x[1,1], 1,4);
julia> B == stack(f2, eachslice(A, dims=3))
true
julia> g(x) = x[begin] // x[end-1]; # returns a number
julia> mapslices(g, A, dims=[1,3])
1×5×1 Array{Rational{Int64}, 3}:
[:, :, 1] =
1//21 3//23 1//5 7//27 9//29
julia> map(g, eachslice(A, dims=2))
5-element Vector{Rational{Int64}}:
1//21
3//23
1//5
7//27
9//29
julia> mapslices(sum, A; dims=(1,3)) == sum(A; dims=(1,3))
true
請注意,在 eachslice(A; dims=2)
中,指定的維度是切片中沒有冒號的那個。這是 view(A,:,i,:)
,而 mapslices(f, A; dims=(1,3))
使用 A[:,i,:]
。函數 f
可以變異切片中的值,而不會影響 A
。
Base.eachrow
— 函數eachrow(A::AbstractVecOrMat) <: AbstractVector
建立一個 RowSlices
物件,它是矩陣或向量 A
的一列列向量。列切片會以 A
的 AbstractVector
檢視回傳。
對於反函數,請參閱 stack
(rows; dims=1)
。
另請參閱 eachcol
、eachslice
和 mapslices
。
此函數需要至少 Julia 1.1。
在 Julia 1.9 之前,這會回傳一個迭代器。
範例
julia> a = [1 2; 3 4]
2×2 Matrix{Int64}:
1 2
3 4
julia> s = eachrow(a)
2-element RowSlices{Matrix{Int64}, Tuple{Base.OneTo{Int64}}, SubArray{Int64, 1, Matrix{Int64}, Tuple{Int64, Base.Slice{Base.OneTo{Int64}}}, true}}:
[1, 2]
[3, 4]
julia> s[1]
2-element view(::Matrix{Int64}, 1, :) with eltype Int64:
1
2
Base.eachcol
— 函數eachcol(A::AbstractVecOrMat) <: AbstractVector
建立一個 ColumnSlices
物件,它是矩陣或向量 A
的一列列向量。列切片會以 A
的 AbstractVector
檢視回傳。
對於反函數,請參閱 stack
(cols)
或 reduce(
hcat
, cols)
。
另請參閱 eachrow
、eachslice
和 mapslices
。
此函數需要至少 Julia 1.1。
在 Julia 1.9 之前,這會回傳一個迭代器。
範例
julia> a = [1 2; 3 4]
2×2 Matrix{Int64}:
1 2
3 4
julia> s = eachcol(a)
2-element ColumnSlices{Matrix{Int64}, Tuple{Base.OneTo{Int64}}, SubArray{Int64, 1, Matrix{Int64}, Tuple{Base.Slice{Base.OneTo{Int64}}, Int64}, true}}:
[1, 3]
[2, 4]
julia> s[1]
2-element view(::Matrix{Int64}, :, 1) with eltype Int64:
1
3
Base.eachslice
— 函數eachslice(A::AbstractArray; dims, drop=true)
建立一個 Slices
物件,它是 A
的維度 dims
上的一系列切片,回傳檢視,選取 A
中其他維度中的所有資料。dims
可以是整數或整數的元組。
如果 drop = true
(預設),外部 Slices
會捨棄內部維度,且維度的順序會與 dims
中的順序相符。如果 drop = false
,則 Slices
會具有與底層陣列相同的維度,且內部維度的長度為 1。
請參閱 stack
(slices; dims)
,了解 eachslice(A; dims::Integer)
的反函數。
另請參閱 eachrow
、eachcol
、mapslices
和 selectdim
。
此函數需要至少 Julia 1.1。
在 Julia 1.9 之前,這會傳回一個迭代器,且僅支援單一維度 dims
。
範例
julia> m = [1 2 3; 4 5 6; 7 8 9]
3×3 Matrix{Int64}:
1 2 3
4 5 6
7 8 9
julia> s = eachslice(m, dims=1)
3-element RowSlices{Matrix{Int64}, Tuple{Base.OneTo{Int64}}, SubArray{Int64, 1, Matrix{Int64}, Tuple{Int64, Base.Slice{Base.OneTo{Int64}}}, true}}:
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
julia> s[1]
3-element view(::Matrix{Int64}, 1, :) with eltype Int64:
1
2
3
julia> eachslice(m, dims=1, drop=false)
3×1 Slices{Matrix{Int64}, Tuple{Int64, Colon}, Tuple{Base.OneTo{Int64}, Base.OneTo{Int64}}, SubArray{Int64, 1, Matrix{Int64}, Tuple{Int64, Base.Slice{Base.OneTo{Int64}}}, true}, 2}:
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
組合
Base.invperm
— 函數invperm(v)
傳回 v
的反置換。如果 B = A[v]
,則 A == B[invperm(v)]
。
另請參閱 sortperm
、invpermute!
、isperm
、permutedims
。
範例
julia> p = (2, 3, 1);
julia> invperm(p)
(3, 1, 2)
julia> v = [2; 4; 3; 1];
julia> invperm(v)
4-element Vector{Int64}:
4
1
3
2
julia> A = ['a','b','c','d'];
julia> B = A[v]
4-element Vector{Char}:
'b': ASCII/Unicode U+0062 (category Ll: Letter, lowercase)
'd': ASCII/Unicode U+0064 (category Ll: Letter, lowercase)
'c': ASCII/Unicode U+0063 (category Ll: Letter, lowercase)
'a': ASCII/Unicode U+0061 (category Ll: Letter, lowercase)
julia> B[invperm(v)]
4-element Vector{Char}:
'a': ASCII/Unicode U+0061 (category Ll: Letter, lowercase)
'b': ASCII/Unicode U+0062 (category Ll: Letter, lowercase)
'c': ASCII/Unicode U+0063 (category Ll: Letter, lowercase)
'd': ASCII/Unicode U+0064 (category Ll: Letter, lowercase)
Base.isperm
— 函數isperm(v) -> Bool
如果 v
是有效的置換,則傳回 true
。
範例
julia> isperm([1; 2])
true
julia> isperm([1; 3])
false
Base.permute!
— 方法permute!(v, p)
根據置換 p
,就地置換向量 v
。不會執行任何檢查,以驗證 p
是否為置換。
要傳回新的排列,請使用 v[p]
。這通常比 permute!(v, p)
快;使用 u .= @view v[p]
寫入預先配置的輸出陣列會更快。(即使 permute!
會覆寫 v
,它在內部仍需要一些配置才能追蹤哪些元素已被移動。)
當任何已變異的引數與任何其他引數共用記憶體時,行為可能會出乎意料。
另請參閱 invpermute!
。
範例
julia> A = [1, 1, 3, 4];
julia> perm = [2, 4, 3, 1];
julia> permute!(A, perm);
julia> A
4-element Vector{Int64}:
1
4
3
1
Base.invpermute!
— 函數invpermute!(v, p)
與 permute!
相同,但套用給定排列的逆排列。
請注意,如果您有預先配置的輸出陣列(例如 u = similar(v)
),改用 u[p] = v
會更快。(invpermute!
在內部配置資料的副本。)
當任何已變異的引數與任何其他引數共用記憶體時,行為可能會出乎意料。
範例
julia> A = [1, 1, 3, 4];
julia> perm = [2, 4, 3, 1];
julia> invpermute!(A, perm);
julia> A
4-element Vector{Int64}:
4
1
3
1
Base.reverse
— 方法reverse(A; dims=:)
沿著維度 dims
反轉 A
,它可以是整數(單一維度)、整數的元組(維度的元組)或 :
(沿著所有維度反轉,為預設值)。另請參閱 reverse!
以進行就地反轉。
範例
julia> b = Int64[1 2; 3 4]
2×2 Matrix{Int64}:
1 2
3 4
julia> reverse(b, dims=2)
2×2 Matrix{Int64}:
2 1
4 3
julia> reverse(b)
2×2 Matrix{Int64}:
4 3
2 1
在 Julia 1.6 之前,reverse
中只支援單一整數 dims
。
Base.reverseind
— 函數reverseind(v, i)
給定 reverse(v)
中的索引 i
,傳回 v
中對應的索引,使得 v[reverseind(v,i)] == reverse(v)[i]
。(在 v
包含非 ASCII 字元的案例中,這可能不簡單。)
範例
julia> s = "Julia🚀"
"Julia🚀"
julia> r = reverse(s)
"🚀ailuJ"
julia> for i in eachindex(s)
print(r[reverseind(r, i)])
end
Julia🚀
Base.reverse!
— 函數reverse!(v [, start=firstindex(v) [, stop=lastindex(v) ]]) -> v
reverse
的就地版本。
範例
julia> A = Vector(1:5)
5-element Vector{Int64}:
1
2
3
4
5
julia> reverse!(A);
julia> A
5-element Vector{Int64}:
5
4
3
2
1