数组
构造函数和类型
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}元素类型为 T 的 N 维稠密数组。
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.0Core.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 nothingCore.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 missingCore.UndefInitializer — 类型UndefInitializer在数组初始化中使用的单例类型,指示数组构造函数调用者希望得到一个未初始化的数组。另请参阅 undef,它是 UndefInitializer() 的别名。
示例
julia> Array{Float64, 1}(UndefInitializer(), 3)
3-element Array{Float64, 1}:
2.2752528595e-314
2.202942107e-314
2.275252907e-314Core.undef — 常量undefUndefInitializer() 的别名,它构造单例类型 UndefInitializer 的实例,在数组初始化中用于指示数组构造函数调用者希望得到一个未初始化的数组。
示例
julia> Array{Float64, 1}(undef, 3)
3-element Vector{Float64}:
2.2752528595e-314
2.202942107e-314
2.275252907e-314Base.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-310Base.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
nothingBase.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
missingBase.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 1936748399Base.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 nothingBase.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 missingBase.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}
falseCore.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,包含对父数组在指定维度(s)上的切片,返回选择其他维度(s)中所有数据的视图。
这些通常应该由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
3Base.zeros — 函数zeros([T=Float64,] dims::Tuple)
zeros([T=Float64,] dims...)创建一个Array,元素类型为T,所有元素都为零,大小由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 0Base.ones — 函数ones([T=Float64,] dims::Tuple)
ones([T=Float64,] dims...)创建一个Array,元素类型为T,所有元素都为一,大小由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.0imBase.BitArray — 类型BitArray{N} <: AbstractArray{Bool, N}空间效率高的N维布尔数组,每个布尔值只使用一个比特。
BitArray将最多64个值打包到每个8字节中,这使得空间效率比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
0Base.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
0Base.trues — 函数trues(dims)创建一个所有值都设置为true的BitArray。
示例
julia> trues(2,3)
2×3 BitMatrix:
1 1 1
1 1 1Base.falses — 函数falses(dims)创建一个所有值都设置为false的BitArray。
示例
julia> falses(2,3)
2×3 BitMatrix:
0 0 0
0 0 0Base.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本身被修改,则filled数组的所有元素都将反映该修改,因为它们仍然是那个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())将返回用Foo()一次计算结果填充的A。
示例
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
1Base.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既可变又可以支持一维数组
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指定的未初始化的可变数组,但轴由最后一个参数指定。
示例:
similar(Array{Int}, axes(A))创建一个“类似于”Array{Int}的数组(实际上可能由它支持),但其索引与A相同。如果A具有常规索引,则这将与Array{Int}(undef, size(A))相同,但如果A具有非常规索引,则结果的索引将与A匹配。
similar(BitArray, (axes(A, 2),))将创建一个一维逻辑数组,其索引与A的列的索引匹配。
基本函数
Base.ndims — 函数ndims(A::AbstractArray) -> Integer返回A的维度数。
示例
julia> A = fill(1, (3,4,5));
julia> ndims(A)
3Base.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)
3Base.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 indexesBase.length — 方法length(A::AbstractArray)返回数组中元素的数量,默认为 prod(size(A))。
示例
julia> length([1, 2, 3, 4])
4
julia> length([1 2; 3 4])
4Base.keys — 方法keys(a::AbstractArray)返回一个高效的数组,描述 a 的所有有效索引,并以 a 本身形状排列。
一维数组(向量)的键是整数,而所有其他 N 维数组使用 CartesianIndex 来描述它们的位置。通常,特殊数组类型 LinearIndices 和 CartesianIndices 分别用于高效地表示这些整数数组和 CartesianIndex 数组。
请注意,数组的 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 类型的值的第一个参数(通常是 IndexLinear()(如果需要线性索引)或 IndexCartesian()(如果需要笛卡尔范围))来显式指定 eachindex 应返回的索引样式。
如果提供多个 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-1imBase.stride — 函数stride(A, k::Integer)返回维度 k 中相邻元素之间的内存距离(以元素数表示)。
另请参阅:strides。
示例
julia> A = fill(1, (3,4,5));
julia> stride(A,2)
3
julia> stride(A,3)
12Base.strides — 函数广播和矢量化
另请参阅 用于矢量化函数的点语法;例如,f.(args...) 隐式调用 broadcast(f, args...)。您应该使用 sin.(a) 通过 broadcast 进行矢量化,而不是依赖于像 sin 这样的函数的“矢量化”方法来对数组进行操作。
Base.Broadcast.broadcast — 函数broadcast(f, As...)在数组、元组、集合、Ref 和/或标量 As 上广播函数 f。
广播将函数 f 应用于容器参数的元素以及 As 中的标量本身。通过虚拟重复值,将单例和缺失维度扩展以匹配其他参数的范围。默认情况下,只有有限数量的类型被认为是标量,包括 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 仅用于存储结果,除非它也列在 As 中,否则不会向 f 提供参数,例如 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.0Base.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...)返回由 inds 指定的数组 A 的子集,其中每个 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
4Base.setindex! — 方法setindex!(A, X, inds...)
A[inds...] = X将来自数组 X 的值存储在由 inds 指定的 A 的某个子集中。语法 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.0Base.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.0Base.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)
falseBase.Colon — 类型Colon()冒号 (:) 用于表示一次性索引整个对象或维度。
很少有操作直接在冒号上定义;相反,它们由 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 进行迭代时始终生成。
对于 broadcast,I::CartesianIndex 被视为“标量”(而不是容器)。为了迭代 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 进行广播运算(+ 和 -)。
CartesianIndices 的广播需要至少 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}一个包含 N 个 Int 的 NTuple,用于表示 AbstractArray 的维度。
Base.LinearIndices — 类型LinearIndices(A::AbstractArray)返回一个 LinearIndices 数组,其形状和 axes 与 A 相同,包含 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]
4Base.to_indices — 函数to_indices(A, I::Tuple)将元组 I 转换为用于索引数组 A 的索引元组。
返回的元组必须仅包含 Int 或数组支持的标量索引的 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)
falsecheckbounds(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)
falseBase.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,但返回一个轻量级数组,该数组以延迟方式引用(或有效地作为父数组A在给定索引或索引inds处的视图),而不是急切地提取元素或构建已复制的子集。在返回值(通常是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:4Base.@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 4Base.@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.0Base.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 4Base.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 8Base.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)上使用reinterpret的效果类似。
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 + 6imreinterpret(reshape, T, A::AbstractArray{S}) -> B更改A的类型解释,同时使用或添加“通道维度”。
如果对于n>1,sizeof(T) = n*sizeof(S),则A的第一维的大小必须为n,而B缺少A的第一维。相反,如果对于n>1,sizeof(S) = n*sizeof(T),则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 6Base.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 6Base.dropdims — 函数dropdims(A; dims)返回一个与A具有相同数据的数组,但已删除由dims指定的维度。对于dims中的每个d,size(A,d)必须等于1,并且不允许重复的维度或超出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 4Base.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:3Base.SubArray — 类型SubArray{T,N,P,I,L} <: AbstractArray{T,N}对父数组(类型为P)的N维视图,元素类型为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 3Base.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))
trueBase.hcat — 函数hcat(A...)水平连接数组或数字。等效于cat(A...; dims=2),以及语法[a b c]或[a;; b;; c]。
对于大型数组向量,当A isa AbstractVector{<:AbstractVecOrMat}时,reduce(hcat, A)会调用一种高效的方法。对于向量,这也可以写成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.9Base.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)
trueBase.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,作为元组的元组,要么指定沿每个轴指定关键元素数量的维度,并用于确定输出维度。当连接操作沿每个轴具有相同数量的元素时(例如,[a b; c d;;; e f ; g h]),dims形式的性能更高,并且默认使用。当沿每个轴的元素数量不平衡时(例如,[a b ; c]),使用shape形式。不平衡的语法需要额外的验证开销。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` = trueBase.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])将函数应用于集合的每个元素,并stack结果。或者应用于几个一起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.06Base.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.5Base.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
1Base.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]
trueBase.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的真元素的下一个索引(包括或在i之后),如果未找到则返回nothing。
示例
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。
示例
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之前),如果未找到则返回nothing。
另请参阅: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。
示例
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
truepermutedims(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给定一个AbstractArray 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]
trueBase.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.0Base.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 1200Base.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 120cumprod(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
12Base.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 3repeat(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 4repeat(c::AbstractChar, r::Integer) -> String重复字符 r 次。这可以通过调用 c^r 等效地实现。
示例
julia> repeat('A', 3)
"AAA"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 1rot180(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 4Base.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 3rotl90(A, k)将矩阵 A 逆时针旋转 90 度整数 k 次。如果 k 是 4 的倍数(包括 0),则等效于 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 4Base.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 2rotr90(A, k)将矩阵 A 顺时针旋转 90 度整数 k 次。如果 k 是 4 的倍数(包括 0),则等效于 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 4Base.mapslices — 函数mapslices(f, A; dims)通过对表单 A[..., :, ..., :, ...] 的每个切片应用函数 f 来转换数组 A 的给定维度,在每个 d 中都有一个冒号 dims。结果沿剩余维度连接。
例如,如果 dims = [1,2] 且 A 是 4 维的,则对所有 i 和 j 调用 x = A[:,:,i,j] 上的 f,并且 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
2Base.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
3Base.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])
falseBase.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
1Base.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
1Base.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) ]]) -> vreverse 的就地版本。
示例
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