接口

Julia的很多能力和扩展性都来自于一些非正式的接口。 通过为自定义的类型及其对象扩展一些特定的方法, 可以不但获得它们的功能, 而且使它们能够被用到其它的基于它们的行为而定义的通用方法中。

Iteration

必须方法 简短描述
iterate(iter) 通常返回由第一项及其其初始状态组成的元组,但如果是空,则要么返回nothing
iterate(iter, state) 通常返回由下一项及其状态组成的元组,或者在没有下一项存在时返回nothing
重要可选方法 默认定义 简短描述
IteratorSize(IterType) HasLength() HasLength()HasShape{N}()IsInfinite() , 或者 SizeUnknown() 作为合适的
IteratorEltype(IterType) HasEltype() EltypeUnknown()HasEltype() 都是可接受的
eltype(IterType) Any 元组中第一个条目的类型由 iterate() 返回。
length(iter) (未定义) The number of items, if known
size(iter, [dim…]) (未定义) 在各个维度上条目的数量,如果知道
IteratorSize(IterType) 返回的值。 必须方法
HasLength() length(iter)
HasShape{N}() length(iter) and size(iter, [dim…])
IsInfinite() (none)
SizeUnknown() (none)
IteratorEltype(IterType) 返回的值 必须方法
HasEltype() eltype(IterType)
EltypeUnknown() (none)

顺序迭代由 iterate 函数实现. Julia的迭代器可以从目标外部跟踪迭代状态,而不是在迭代过程中改变目标本身。 迭代过程中的返回一个包含了当前迭代值及其状态的元组,或者在没有元素存在的情况下返回 nothing 。 状态对象将在下一次迭代时传递回迭代函数 并且通常被认为是可迭代对象的私有实现细节。

任何定义了这个函数的兑现个都是可迭代的,并且可以被应用到 many functions that rely upon iteration 。 也可以直接被应用到 for 循环中,因为根据语法:

  1. for i in iter # or "for i = iter"
  2. # body
  3. end

被解释为:

  1. next = iterate(iter)
  2. while next !== nothing
  3. (i, state) = next
  4. # body
  5. next = iterate(iter, state)
  6. end

一个简单的例子是一组定长数据的平方数迭代序列:

  1. julia> struct Squares
  2. count::Int
  3. end
  4. julia> Base.iterate(S::Squares, state=1) = state > S.count ? nothing : (state*state, state+1)

仅仅定义了 iterate 函数的 Squares 类型就已经很强大了。 我们现在可以迭代所有的元素了:

  1. julia> for i in Squares(7)
  2. println(i)
  3. end
  4. 1
  5. 4
  6. 9
  7. 16
  8. 25
  9. 36
  10. 49

我们可以利用许多内置方法来处理迭代,比如标准库 Statistics 中的 inmeanstd

  1. julia> 25 in Squares(10)
  2. true
  3. julia> using Statistics
  4. julia> mean(Squares(100))
  5. 3383.5
  6. julia> std(Squares(100))
  7. 3024.355854282583
  8. 我们可以扩展一些其它的方法,为Julia提供有关此可迭代集合的更多信息。
  9. 我们知道 `Squares` 序列中的元素总是 `Int` 型的。
  10. 通过扩展 [`eltype`](@ref) 方法, 我们可以给Julia过多的信息来帮助其在更复杂的方法中产生更加具体的代码。
  11. 我们同时也知道我们序列中的元素数目,所以我们也同样可以扩展 [`length`](@ref)

jldoctest squaretype julia> Base.eltype(::Type{Squares}) = Int # Note that this is defined for the type

julia> Base.length(S::Squares) = S.count

  1. 现在,当我们让Julia [`collect`](@ref) 所有元素到一个array中时,Julia可以预分配一个 适当大小的 `Vector{Int}` ,而不是盲目地 [`push!`](@ref) 每一个元素到 `Vector{Any}`

jldoctest squaretype julia> collect(Squares(4)) 4-element Array{Int64,1}: 1 4 9 16

  1. While we can rely upon generic implementations, we can also extend specific methods where we know
  2. there is a simpler algorithm. For example, there's a formula to compute the sum of squares, so
  3. we can override the generic iterative version with a more performant solution:

jldoctest squaretype julia> Base.sum(S::Squares) = (n = S.count; return n(n+1)(2n+1)÷6)

julia> sum(Squares(1803)) 1955361914

  1. This is a very common pattern throughout Julia Base: a small set of required methods
  2. define an informal interface that enable many fancier behaviors. In some cases, types will want
  3. to additionally specialize those extra behaviors when they know a more efficient algorithm can
  4. be used in their specific case.
  5. It is also often useful to allow iteration over a collection in *reverse order*
  6. by iterating over [`Iterators.reverse(iterator)`](@ref). To actually support
  7. reverse-order iteration, however, an iterator
  8. type `T` needs to implement `iterate` for `Iterators.Reverse{T}`.
  9. (Given `r::Iterators.Reverse{T}`, the underling iterator of type `T` is `r.itr`.)
  10. In our `Squares` example, we would implement `Iterators.Reverse{Squares}` methods:

jldoctest squaretype julia> Base.iterate(rS::Iterators.Reverse{Squares}, state=rS.itr.count) = state < 1 ? nothing : (state*state, state-1)

julia> collect(Iterators.reverse(Squares(4))) 4-element Array{Int64,1}: 16 9 4 1

  1. ## Indexing
  2. | Methods to implement | Brief description |
  3. |:-------------------- |:-------------------------------- |
  4. | `getindex(X, i)` | `X[i]`, indexed element access |
  5. | `setindex!(X, v, i)` | `X[i] = v`, indexed assignment |
  6. | `firstindex(X)` | The first index |
  7. | `lastindex(X)` | The last index, used in `X[end]` |
  8. For the `Squares` iterable above, we can easily compute the `i`th element of the sequence by squaring
  9. it. We can expose this as an indexing expression `S[i]`. To opt into this behavior, `Squares`
  10. simply needs to define [`getindex`](@ref):

jldoctest squaretype julia> function Base.getindex(S::Squares, i::Int) 1 <= i <= S.count || throw(BoundsError(S, i)) return i*i end

julia> Squares(100)[23] 529

  1. Additionally, to support the syntax `S[end]`, we must define [`lastindex`](@ref) to specify the last
  2. valid index. It is recommended to also define [`firstindex`](@ref) to specify the first valid index:

jldoctest squaretype julia> Base.firstindex(S::Squares) = 1

julia> Base.lastindex(S::Squares) = length(S)

julia> Squares(23)[end] 529

  1. Note, though, that the above *only* defines [`getindex`](@ref) with one integer index. Indexing with
  2. anything other than an `Int` will throw a [`MethodError`](@ref) saying that there was no matching method.
  3. In order to support indexing with ranges or vectors of `Int`s, separate methods must be written:

jldoctest squaretype julia> Base.getindex(S::Squares, i::Number) = S[convert(Int, i)]

julia> Base.getindex(S::Squares, I) = [S[i] for i in I]

julia> Squares(10)[[3,4.,5]] 3-element Array{Int64,1}: 9 16 25

  1. While this is starting to support more of the [indexing operations supported by some of the builtin types](@ref man-array-indexing),
  2. there's still quite a number of behaviors missing. This `Squares` sequence is starting to look
  3. more and more like a vector as we've added behaviors to it. Instead of defining all these behaviors
  4. ourselves, we can officially define it as a subtype of an [`AbstractArray`](@ref).
  5. ## [Abstract Arrays](@id man-interface-array)
  6. | Methods to implement | | 简短描述 |
  7. |:----------------------------------------------- |:-------------------------------------- |:------------------------------------------------------------------------------------- |
  8. | `size(A)` | | Returns a tuple containing the dimensions of `A` |
  9. | `getindex(A, i::Int)` | | (if `IndexLinear`) Linear scalar indexing |
  10. | `getindex(A, I::Vararg{Int, N})` | | (if `IndexCartesian`, where `N = ndims(A)`) N-dimensional scalar indexing |
  11. | `setindex!(A, v, i::Int)` | | (if `IndexLinear`) Scalar indexed assignment |
  12. | `setindex!(A, v, I::Vararg{Int, N})` | | (if `IndexCartesian`, where `N = ndims(A)`) N-dimensional scalar indexed assignment |
  13. | **Optional methods** | **默认定义** | **简短描述** |
  14. | `IndexStyle(::Type)` | `IndexCartesian()` | Returns either `IndexLinear()` or `IndexCartesian()`. See the description below. |
  15. | `getindex(A, I...)` | defined in terms of scalar `getindex` | [Multidimensional and nonscalar indexing](@ref man-array-indexing) |
  16. | `setindex!(A, I...)` | defined in terms of scalar `setindex!` | [Multidimensional and nonscalar indexed assignment](@ref man-array-indexing) |
  17. | `iterate` | defined in terms of scalar `getindex` | Iteration |
  18. | `length(A)` | `prod(size(A))` | Number of elements |
  19. | `similar(A)` | `similar(A, eltype(A), size(A))` | Return a mutable array with the same shape and element type |
  20. | `similar(A, ::Type{S})` | `similar(A, S, size(A))` | Return a mutable array with the same shape and the specified element type |
  21. | `similar(A, dims::NTuple{Int})` | `similar(A, eltype(A), dims)` | Return a mutable array with the same element type and size *dims* |
  22. | `similar(A, ::Type{S}, dims::NTuple{Int})` | `Array{S}(undef, dims)` | Return a mutable array with the specified element type and size |
  23. | **Non-traditional indices** | **默认定义** | **简短描述** |
  24. | `axes(A)` | `map(OneTo, size(A))` | Return the `AbstractUnitRange` of valid indices |
  25. | `Base.similar(A, ::Type{S}, inds::NTuple{Ind})` | `similar(A, S, Base.to_shape(inds))` | Return a mutable array with the specified indices `inds` (see below) |
  26. | `Base.similar(T::Union{Type,Function}, inds)` | `T(Base.to_shape(inds))` | Return an array similar to `T` with the specified indices `inds` (see below) |
  27. If a type is defined as a subtype of `AbstractArray`, it inherits a very large set of rich behaviors
  28. including iteration and multidimensional indexing built on top of single-element access. See
  29. the [arrays manual page](@ref man-multi-dim-arrays) and the [Julia Base section](@ref lib-arrays) for more supported methods.
  30. A key part in defining an `AbstractArray` subtype is [`IndexStyle`](@ref). Since indexing is
  31. such an important part of an array and often occurs in hot loops, it's important to make both
  32. indexing and indexed assignment as efficient as possible. Array data structures are typically
  33. defined in one of two ways: either it most efficiently accesses its elements using just one index
  34. (linear indexing) or it intrinsically accesses the elements with indices specified for every dimension.
  35. These two modalities are identified by Julia as `IndexLinear()` and `IndexCartesian()`.
  36. Converting a linear index to multiple indexing subscripts is typically very expensive, so this
  37. provides a traits-based mechanism to enable efficient generic code for all array types.
  38. This distinction determines which scalar indexing methods the type must define. `IndexLinear()`
  39. arrays are simple: just define `getindex(A::ArrayType, i::Int)`. When the array is subsequently
  40. indexed with a multidimensional set of indices, the fallback `getindex(A::AbstractArray, I...)()`
  41. efficiently converts the indices into one linear index and then calls the above method. `IndexCartesian()`
  42. arrays, on the other hand, require methods to be defined for each supported dimensionality with
  43. `ndims(A)` `Int` indices. For example, [`SparseMatrixCSC`](@ref) from the `SparseArrays` standard
  44. library module, only supports two dimensions, so it just defines
  45. `getindex(A::SparseMatrixCSC, i::Int, j::Int)`. The same holds for [`setindex!`](@ref).
  46. Returning to the sequence of squares from above, we could instead define it as a subtype of an
  47. `AbstractArray{Int, 1}`:

jldoctest squarevectype julia> struct SquaresVector <: AbstractArray{Int, 1} count::Int end

julia> Base.size(S::SquaresVector) = (S.count,)

julia> Base.IndexStyle(::Type{<:SquaresVector}) = IndexLinear()

julia> Base.getindex(S::SquaresVector, i::Int) = i*i

  1. Note that it's very important to specify the two parameters of the `AbstractArray`; the first
  2. defines the [`eltype`](@ref), and the second defines the [`ndims`](@ref). That supertype and those three
  3. methods are all it takes for `SquaresVector` to be an iterable, indexable, and completely functional
  4. array:

jldoctest squarevectype julia> s = SquaresVector(4) 4-element SquaresVector: 1 4 9 16

julia> s[s .> 8] 2-element Array{Int64,1}: 9 16

julia> s + s 4-element Array{Int64,1}: 2 8 18 32

julia> sin.(s) 4-element Array{Float64,1}: 0.8414709848078965 -0.7568024953079282 0.4121184852417566 -0.2879033166650653

  1. As a more complicated example, let's define our own toy N-dimensional sparse-like array type built
  2. on top of [`Dict`](@ref):

jldoctest squarevectype julia> struct SparseArray{T,N} <: AbstractArray{T,N} data::Dict{NTuple{N,Int}, T} dims::NTuple{N,Int} end

julia> SparseArray(::Type{T}, dims::Int…) where {T} = SparseArray(T, dims);

julia> SparseArray(::Type{T}, dims::NTuple{N,Int}) where {T,N} = SparseArray{T,N}(Dict{NTuple{N,Int}, T}(), dims);

julia> Base.size(A::SparseArray) = A.dims

julia> Base.similar(A::SparseArray, ::Type{T}, dims::Dims) where {T} = SparseArray(T, dims)

julia> Base.getindex(A::SparseArray{T,N}, I::Vararg{Int,N}) where {T,N} = get(A.data, I, zero(T))

julia> Base.setindex!(A::SparseArray{T,N}, v, I::Vararg{Int,N}) where {T,N} = (A.data[I] = v)

  1. Notice that this is an `IndexCartesian` array, so we must manually define [`getindex`](@ref) and [`setindex!`](@ref)
  2. at the dimensionality of the array. Unlike the `SquaresVector`, we are able to define [`setindex!`](@ref),
  3. and so we can mutate the array:

jldoctest squarevectype julia> A = SparseArray(Float64, 3, 3) 3×3 SparseArray{Float64,2}: 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0

julia> fill!(A, 2) 3×3 SparseArray{Float64,2}: 2.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0

julia> A[:] = 1:length(A); A 3×3 SparseArray{Float64,2}: 1.0 4.0 7.0 2.0 5.0 8.0 3.0 6.0 9.0

  1. The result of indexing an `AbstractArray` can itself be an array (for instance when indexing by
  2. an `AbstractRange`). The `AbstractArray` fallback methods use [`similar`](@ref) to allocate an `Array`
  3. of the appropriate size and element type, which is filled in using the basic indexing method described
  4. above. However, when implementing an array wrapper you often want the result to be wrapped as
  5. well:

jldoctest squarevectype julia> A[1:2,:] 2×3 SparseArray{Float64,2}: 1.0 4.0 7.0 2.0 5.0 8.0

  1. In this example it is accomplished by defining `Base.similar{T}(A::SparseArray, ::Type{T}, dims::Dims)`
  2. to create the appropriate wrapped array. (Note that while `similar` supports 1- and 2-argument
  3. forms, in most case you only need to specialize the 3-argument form.) For this to work it's important
  4. that `SparseArray` is mutable (supports `setindex!`). Defining `similar`, `getindex` and
  5. `setindex!` for `SparseArray` also makes it possible to [`copy`](@ref) the array:

jldoctest squarevectype julia> copy(A) 3×3 SparseArray{Float64,2}: 1.0 4.0 7.0 2.0 5.0 8.0 3.0 6.0 9.0

  1. In addition to all the iterable and indexable methods from above, these types can also interact
  2. with each other and use most of the methods defined in Julia Base for `AbstractArrays`:

jldoctest squarevectype julia> A[SquaresVector(3)] 3-element SparseArray{Float64,1}: 1.0 4.0 9.0

julia> sum(A) 45.0

  1. If you are defining an array type that allows non-traditional indexing (indices that start at
  2. something other than 1), you should specialize [`axes`](@ref). You should also specialize [`similar`](@ref)
  3. so that the `dims` argument (ordinarily a `Dims` size-tuple) can accept `AbstractUnitRange` objects,
  4. perhaps range-types `Ind` of your own design. For more information, see
  5. [Arrays with custom indices](@ref man-custom-indices).
  6. ## [Strided Arrays](@id man-interface-strided-arrays)
  7. | Methods to implement | | Brief description |
  8. |:----------------------------------------------- |:-------------------------------------- |:------------------------------------------------------------------------------------- |
  9. | `strides(A)` | | Return the distance in memory (in number of elements) between adjacent elements in each dimension as a tuple. If `A` is an `AbstractArray{T,0}`, this should return an empty tuple. |
  10. | `Base.unsafe_convert(::Type{Ptr{T}}, A)` | | Return the native address of an array. |
  11. | **Optional methods** | **Default definition** | **Brief description** |
  12. | `stride(A, i::Int)` | `strides(A)[i]` | Return the distance in memory (in number of elements) between adjacent elements in dimension k. |
  13. A strided array is a subtype of `AbstractArray` whose entries are stored in memory with fixed strides.
  14. Provided the element type of the array is compatible with BLAS, a strided array can utilize BLAS and LAPACK routines
  15. for more efficient linear algebra routines. A typical example of a user-defined strided array is one
  16. that wraps a standard `Array` with additional structure.
  17. Warning: do not implement these methods if the underlying storage is not actually strided, as it
  18. may lead to incorrect results or segmentation faults.
  19. Here are some examples to demonstrate which type of arrays are strided and which are not:

julia 1:5 # not strided (there is no storage associated with this array.) Vector(1:5) # is strided with strides (1,) A = [1 5; 2 6; 3 7; 4 8] # is strided with strides (1,4) V = view(A, 1:2, :) # is strided with strides (1,4) V = view(A, 1:2:3, 1:2) # is strided with strides (2,4) V = view(A, [1,2,4], :) # is not strided, as the spacing between rows is not fixed.

  1. ## [Customizing broadcasting](@id man-interfaces-broadcasting)
  2. | Methods to implement | 简短描述 |
  3. |:-------------------- |:----------------- |
  4. | `Base.BroadcastStyle(::Type{SrcType}) = SrcStyle()` | Broadcasting behavior of `SrcType` |
  5. | `Base.similar(bc::Broadcasted{DestStyle}, ::Type{ElType})` | Allocation of output container |
  6. | **Optional methods** | | |
  7. | `Base.BroadcastStyle(::Style1, ::Style2) = Style12()` | Precedence rules for mixing styles |
  8. | `Base.broadcast_axes(x)` | Declaration of the indices of `x` for broadcasting purposes (defaults to [`axes(x)`](@ref)) |
  9. | `Base.broadcastable(x)` | Convert `x` to an object that has `axes` and supports indexing |
  10. | **Bypassing default machinery** | |
  11. | `Base.copy(bc::Broadcasted{DestStyle})` | Custom implementation of `broadcast` |
  12. | `Base.copyto!(dest, bc::Broadcasted{DestStyle})` | Custom implementation of `broadcast!`, specializing on `DestStyle` |
  13. | `Base.copyto!(dest::DestType, bc::Broadcasted{Nothing})` | Custom implementation of `broadcast!`, specializing on `DestType` |
  14. | `Base.Broadcast.broadcasted(f, args...)` | Override the default lazy behavior within a fused expression |
  15. | `Base.Broadcast.instantiate(bc::Broadcasted{DestStyle})` | Override the computation of the lazy broadcast's axes |
  16. [Broadcasting](@ref) is triggered by an explicit call to `broadcast` or `broadcast!`, or implicitly by
  17. "dot" operations like `A .+ b` or `f.(x, y)`. Any object that has [`axes`](@ref) and supports
  18. indexing can participate as an argument in broadcasting, and by default the result is stored
  19. in an `Array`. This basic framework is extensible in three major ways:
  20. * Ensuring that all arguments support broadcast
  21. * Selecting an appropriate output array for the given set of arguments
  22. * Selecting an efficient implementation for the given set of arguments
  23. Not all types support `axes` and indexing, but many are convenient to allow in broadcast.
  24. The [`Base.broadcastable`](@ref) function is called on each argument to broadcast, allowing
  25. it to return something different that supports `axes` and indexing. By
  26. default, this is the identity function for all `AbstractArray`s and `Number`s — they already
  27. support `axes` and indexing. For a handful of other types (including but not limited to
  28. types themselves, functions, special singletons like [`missing`](@ref) and [`nothing`](@ref), and dates),
  29. `Base.broadcastable` returns the argument wrapped in a `Ref` to act as a 0-dimensional
  30. "scalar" for the purposes of broadcasting. Custom types can similarly specialize
  31. `Base.broadcastable` to define their shape, but they should follow the convention that
  32. `collect(Base.broadcastable(x)) == collect(x)`. A notable exception is `AbstractString`;
  33. strings are special-cased to behave as scalars for the purposes of broadcast even though
  34. they are iterable collections of their characters (see [Strings](@ref) for more).
  35. The next two steps (selecting the output array and implementation) are dependent upon
  36. determining a single answer for a given set of arguments. Broadcast must take all the varied
  37. types of its arguments and collapse them down to just one output array and one
  38. implementation. Broadcast calls this single answer a "style." Every broadcastable object
  39. each has its own preferred style, and a promotion-like system is used to combine these
  40. styles into a single answer — the "destination style".
  41. ### Broadcast Styles
  42. `Base.BroadcastStyle` is the abstract type from which all broadcast styles are derived. When used as a
  43. function it has two possible forms, unary (single-argument) and binary. The unary variant states
  44. that you intend to implement specific broadcasting behavior and/or output type, and do not wish to
  45. rely on the default fallback [`Broadcast.DefaultArrayStyle`](@ref).
  46. To override these defaults, you can define a custom `BroadcastStyle` for your object:

julia struct MyStyle <: Broadcast.BroadcastStyle end Base.BroadcastStyle(::Type{<:MyType}) = MyStyle()

  1. In some cases it might be convenient not to have to define `MyStyle`, in which case you can
  2. leverage one of the general broadcast wrappers:
  3. - `Base.BroadcastStyle(::Type{<:MyType}) = Broadcast.Style{MyType}()` can be
  4. used for arbitrary types.
  5. - `Base.BroadcastStyle(::Type{<:MyType}) = Broadcast.ArrayStyle{MyType}()` is preferred
  6. if `MyType` is an `AbstractArray`.
  7. - For `AbstractArrays` that only support a certain dimensionality, create a subtype of `Broadcast.AbstractArrayStyle{N}` (see below).
  8. When your broadcast operation involves several arguments, individual argument styles get
  9. combined to determine a single `DestStyle` that controls the type of the output container.
  10. For more details, see [below](@ref writing-binary-broadcasting-rules).
  11. ### 选择合适的输出数组
  12. The broadcast style is computed for every broadcasting operation to allow for
  13. dispatch and specialization. The actual allocation of the result array is
  14. handled by `similar`, using the Broadcasted object as its first argument.

julia Base.similar(bc::Broadcasted{DestStyle}, ::Type{ElType})

  1. The fallback definition is

julia similar(bc::Broadcasted{DefaultArrayStyle{N}}, ::Type{ElType}) where {N,ElType} = similar(Array{ElType}, axes(bc))

  1. However, if needed you can specialize on any or all of these arguments. The final argument
  2. `bc` is a lazy representation of a (potentially fused) broadcast operation, a `Broadcasted`
  3. object. For these purposes, the most important fields of the wrapper are
  4. `f` and `args`, describing the function and argument list, respectively. Note that the argument
  5. list can and often does include other nested `Broadcasted` wrappers.
  6. For a complete example, let's say you have created a type, `ArrayAndChar`, that stores an
  7. array and a single character:

jldoctest ArrayAndChar; output = false struct ArrayAndChar{T,N} <: AbstractArray{T,N} data::Array{T,N} char::Char end Base.size(A::ArrayAndChar) = size(A.data) Base.getindex(A::ArrayAndChar{T,N}, inds::Vararg{Int,N}) where {T,N} = A.data[inds…] Base.setindex!(A::ArrayAndChar{T,N}, val, inds::Vararg{Int,N}) where {T,N} = A.data[inds…] = val Base.showarg(io::IO, A::ArrayAndChar, toplevel) = print(io, typeof(A), " with char '", A.char, "'")

output

  1. You might want broadcasting to preserve the `char` "metadata." First we define

jldoctest ArrayAndChar; output = false Base.BroadcastStyle(::Type{<:ArrayAndChar}) = Broadcast.ArrayStyle{ArrayAndChar}()

output

  1. This means we must also define a corresponding `similar` method:

jldoctest ArrayAndChar; output = false function Base.similar(bc::Broadcast.Broadcasted{Broadcast.ArrayStyle{ArrayAndChar}}, ::Type{ElType}) where ElType # Scan the inputs for the ArrayAndChar: A = find_aac(bc) # Use the char field of A to create the output ArrayAndChar(similar(Array{ElType}, axes(bc)), A.char) end

"A = find_aac(As) returns the first ArrayAndChar among the arguments." find_aac(bc::Base.Broadcast.Broadcasted) = find_aac(bc.args) find_aac(args::Tuple) = find_aac(find_aac(args[1]), Base.tail(args)) find_aac(x) = x find_aac(a::ArrayAndChar, rest) = a find_aac(::Any, rest) = find_aac(rest)

output

find_aac (generic function with 5 methods)

  1. From these definitions, one obtains the following behavior:

jldoctest ArrayAndChar julia> a = ArrayAndChar([1 2; 3 4], 'x') 2×2 ArrayAndChar{Int64,2} with char 'x': 1 2 3 4

julia> a .+ 1 2×2 ArrayAndChar{Int64,2} with char 'x': 2 3 4 5

julia> a .+ [5,10] 2×2 ArrayAndChar{Int64,2} with char 'x': 6 7 13 14

  1. ### [Extending broadcast with custom implementations](@id extending-in-place-broadcast)
  2. In general, a broadcast operation is represented by a lazy `Broadcasted` container that holds onto
  3. the function to be applied alongside its arguments. Those arguments may themselves be more nested
  4. `Broadcasted` containers, forming a large expression tree to be evaluated. A nested tree of
  5. `Broadcasted` containers is directly constructed by the implicit dot syntax; `5 .+ 2.*x` is
  6. transiently represented by `Broadcasted(+, 5, Broadcasted(*, 2, x))`, for example. This is
  7. invisible to users as it is immediately realized through a call to `copy`, but it is this container
  8. that provides the basis for broadcast's extensibility for authors of custom types. The built-in
  9. broadcast machinery will then determine the result type and size based upon the arguments, allocate
  10. it, and then finally copy the realization of the `Broadcasted` object into it with a default
  11. `copyto!(::AbstractArray, ::Broadcasted)` method. The built-in fallback `broadcast` and
  12. `broadcast!` methods similarly construct a transient `Broadcasted` representation of the operation
  13. so they can follow the same codepath. This allows custom array implementations to
  14. provide their own `copyto!` specialization to customize and
  15. optimize broadcasting. This is again determined by the computed broadcast style. This is such
  16. an important part of the operation that it is stored as the first type parameter of the
  17. `Broadcasted` type, allowing for dispatch and specialization.
  18. For some types, the machinery to "fuse" operations across nested levels of broadcasting
  19. is not available or could be done more efficiently incrementally. In such cases, you may
  20. need or want to evaluate `x .* (x .+ 1)` as if it had been
  21. written `broadcast(*, x, broadcast(+, x, 1))`, where the inner operation is evaluated before
  22. tackling the outer operation. This sort of eager operation is directly supported by a bit
  23. of indirection; instead of directly constructing `Broadcasted` objects, Julia lowers the
  24. fused expression `x .* (x .+ 1)` to `Broadcast.broadcasted(*, x, Broadcast.broadcasted(+, x, 1))`. Now,
  25. by default, `broadcasted` just calls the `Broadcasted` constructor to create the lazy representation
  26. of the fused expression tree, but you can choose to override it for a particular combination
  27. of function and arguments.
  28. As an example, the builtin `AbstractRange` objects use this machinery to optimize pieces
  29. of broadcasted expressions that can be eagerly evaluated purely in terms of the start,
  30. step, and length (or stop) instead of computing every single element. Just like all the
  31. other machinery, `broadcasted` also computes and exposes the combined broadcast style of its
  32. arguments, so instead of specializing on `broadcasted(f, args...)`, you can specialize on
  33. `broadcasted(::DestStyle, f, args...)` for any combination of style, function, and arguments.
  34. For example, the following definition supports the negation of ranges:

julia broadcasted(::DefaultArrayStyle{1}, ::typeof(-), r::OrdinalRange) = range(-first(r), step=-step(r), length=length(r))

  1. ### [Extending in-place broadcasting](@id extending-in-place-broadcast)
  2. In-place broadcasting can be supported by defining the appropriate `copyto!(dest, bc::Broadcasted)`
  3. method. Because you might want to specialize either on `dest` or the specific subtype of `bc`,
  4. to avoid ambiguities between packages we recommend the following convention.
  5. If you wish to specialize on a particular style `DestStyle`, define a method for

julia copyto!(dest, bc::Broadcasted{DestStyle})

  1. Optionally, with this form you can also specialize on the type of `dest`.
  2. If instead you want to specialize on the destination type `DestType` without specializing
  3. on `DestStyle`, then you should define a method with the following signature:

julia copyto!(dest::DestType, bc::Broadcasted{Nothing})

  1. This leverages a fallback implementation of `copyto!` that converts the wrapper into a
  2. `Broadcasted{Nothing}`. Consequently, specializing on `DestType` has lower precedence than
  3. methods that specialize on `DestStyle`.
  4. Similarly, you can completely override out-of-place broadcasting with a `copy(::Broadcasted)`
  5. method.
  6. #### Working with `Broadcasted` objects
  7. In order to implement such a `copy` or `copyto!`, method, of course, you must
  8. work with the `Broadcasted` wrapper to compute each element. There are two main
  9. ways of doing so:
  10. * `Broadcast.flatten` recomputes the potentially nested operation into a single
  11. function and flat list of arguments. You are responsible for implementing the
  12. broadcasting shape rules yourself, but this may be helpful in limited situations.
  13. * Iterating over the `CartesianIndices` of the `axes(::Broadcasted)` and using
  14. indexing with the resulting `CartesianIndex` object to compute the result.
  15. ### [Writing binary broadcasting rules](@id writing-binary-broadcasting-rules)
  16. The precedence rules are defined by binary `BroadcastStyle` calls:

julia Base.BroadcastStyle(::Style1, ::Style2) = Style12()

  1. where `Style12` is the `BroadcastStyle` you want to choose for outputs involving
  2. arguments of `Style1` and `Style2`. For example,

julia Base.BroadcastStyle(::Broadcast.Style{Tuple}, ::Broadcast.AbstractArrayStyle{0}) = Broadcast.Style{Tuple}()

  1. indicates that `Tuple` "wins" over zero-dimensional arrays (the output container will be a tuple).
  2. It is worth noting that you do not need to (and should not) define both argument orders
  3. of this call; defining one is sufficient no matter what order the user supplies the arguments in.
  4. For `AbstractArray` types, defining a `BroadcastStyle` supersedes the fallback choice,
  5. [`Broadcast.DefaultArrayStyle`](@ref). `DefaultArrayStyle` and the abstract supertype, `AbstractArrayStyle`, store the dimensionality as a type parameter to support specialized
  6. array types that have fixed dimensionality requirements.
  7. `DefaultArrayStyle` "loses" to any other
  8. `AbstractArrayStyle` that has been defined because of the following methods:

julia BroadcastStyle(a::AbstractArrayStyle{Any}, ::DefaultArrayStyle) = a BroadcastStyle(a::AbstractArrayStyle{N}, ::DefaultArrayStyle{N}) where N = a BroadcastStyle(a::AbstractArrayStyle{M}, ::DefaultArrayStyle{N}) where {M,N} = typeof(a)(_max(Val(M),Val(N)))

  1. You do not need to write binary `BroadcastStyle`
  2. rules unless you want to establish precedence for
  3. two or more non-`DefaultArrayStyle` types.
  4. If your array type does have fixed dimensionality requirements, then you should
  5. subtype `AbstractArrayStyle`. For example, the sparse array code has the following definitions:

julia struct SparseVecStyle <: Broadcast.AbstractArrayStyle{1} end struct SparseMatStyle <: Broadcast.AbstractArrayStyle{2} end Base.BroadcastStyle(::Type{<:SparseVector}) = SparseVecStyle() Base.BroadcastStyle(::Type{<:SparseMatrixCSC}) = SparseMatStyle()

  1. Whenever you subtype `AbstractArrayStyle`, you also need to define rules for combining
  2. dimensionalities, by creating a constructor for your style that takes a `Val(N)` argument.
  3. For example:

julia SparseVecStyle(::Val{0}) = SparseVecStyle() SparseVecStyle(::Val{1}) = SparseVecStyle() SparseVecStyle(::Val{2}) = SparseMatStyle() SparseVecStyle(::Val{N}) where N = Broadcast.DefaultArrayStyle{N}() ```

These rules indicate that the combination of a SparseVecStyle with 0- or 1-dimensional arrays yields another SparseVecStyle, that its combination with a 2-dimensional array yields a SparseMatStyle, and anything of higher dimensionality falls back to the dense arbitrary-dimensional framework. These rules allow broadcasting to keep the sparse representation for operations that result in one or two dimensional outputs, but produce an Array for any other dimensionality.

原文: https://juliacn.github.io/JuliaZH.jl/latest/manual/interfaces/