ITADN
SciML/EllipsisNotation.jl
SciML/EllipsisNotation.jl · 文件 下载 ZIP
文件最后提交记录最后更新时间
README.md
以下内容由 AI 翻译,如有问题请点此提交 issue 反馈

EllipsisNotation.jl

Join the chat at https://julialang.zulipchat.com #sciml-bridged Global Docs

codecov Build Status

ColPrac: Contributor's Guide on Collaborative Practices for Community Packages SciML Code Style

该实现使用了 .. 作为数组索引的符号。它与 Python 中的 ... 类似,含义均为“(位于)之前的(或之后的)所有列”。

安装

Pkg.add("EllipsisNotation")
using EllipsisNotation

示例用法

julia> A = Array{Int}(undef, 2, 4, 2)

julia> A[.., 1] = [2 1 4 5
                   2 2 3 6]

julia> A[.., 2] = [3 2 6 5
                   3 2 6 6]

julia> A[:, :, 1] == [2 1 4 5
                      2 2 3 6]
true

julia> A[1, ..] = reshape([3 4
                           5 6
                           4 5
                           6 7], 1, 4, 2) # drops singleton dimension

julia> B = [3 4
            5 6
            4 5
            6 7]

julia> B == reshape(A[1, ..], 4, 2)
true

julia> A[.., 1, 2] # Can do as many integers as you want on the end!

为避免因切片而挤压维度。

julia> C = ones(3, 3, 3, 3, 3);

julia> size(C[1:1, .., 1:1])
(1, 3, 3, 3, 1)

注意:.. 会贪婪地消耗维度,这意味着在索引表达式中,.. 的第一次出现会生成尽可能多的切片。之后的其他..实例则仅被视为普通切片。通常,为避免可能的混淆,您应在索引表达式中仅使用一个..实例。

警告: 当同时使用endbegin进行索引时,..将无法正常工作。例如,A = randn(2,3,4); A[.., 1:end]不会产生预期的结果。这是众所周知的局限性,除非 Julia 语言本身进行必要的修改,否则不太可能得到解决。更多详情请参见 https://github.com/ChrisRackauckas/EllipsisNotation.jl/issues/19。

致谢

我要感谢 M. Schauer 实现了..符号系统。他提出了最初的构想,而我则因其对我而言的巨大实用性而对其进行了扩展,并将其封装成了一个包。