@@ -87,39 +87,48 @@ Broadcast.BroadcastStyle(S::LinearAlgebra.StructuredMatrixStyle, ::ZerosStyle{2}
87
87
Broadcast. BroadcastStyle (S:: LinearAlgebra.StructuredMatrixStyle , :: ZerosStyle{1} ) = S
88
88
Broadcast. BroadcastStyle (S:: LinearAlgebra.StructuredMatrixStyle , :: ZerosStyle{0} ) = S
89
89
90
- _getindex_value (f:: AbstractFill ) = getindex_value (f)
91
- _getindex_value (x:: Number ) = x
92
- _getindex_value (x:: Ref ) = x[]
93
- function _getindex_value (bc:: Broadcast.Broadcasted )
94
- bc. f (map (_getindex_value, bc. args)... )
90
+ # Obtain the fill value of a broadcasted object by recursively evaluating the fill components
91
+ broadcast_getindex_value (f:: AbstractFill ) = getindex_value (f)
92
+ broadcast_getindex_value (f:: Transpose{<:Any,<:AbstractFill} ) = getindex_value (parent (f))
93
+ broadcast_getindex_value (f:: Adjoint{<:Any,<:AbstractFill} ) = getindex_value (parent (f))
94
+ broadcast_getindex_value (x:: Number ) = x
95
+ broadcast_getindex_value (x:: Ref ) = x[]
96
+ function broadcast_getindex_value (bc:: Broadcast.Broadcasted )
97
+ bc. f (map (broadcast_getindex_value, bc. args)... )
95
98
end
96
99
97
100
has_static_value (x) = false
98
101
has_static_value (x:: Union{AbstractZeros, AbstractOnes} ) = true
99
102
has_static_value (x:: Broadcast.Broadcasted ) = all (has_static_value, x. args)
100
103
104
+ # _iszeros and _isones are conservative checks for zeros and ones,
105
+ # which are used to determine if a broadcasted object is a Fill, Zeros or Ones.
101
106
function _iszeros (bc:: Broadcast.Broadcasted )
102
- all (has_static_value, bc. args) && _iszero (_getindex_value (bc))
107
+ all (has_static_value, bc. args) && _iszero (broadcast_getindex_value (bc))
103
108
end
104
109
# conservative check for zeros. In most cases, there isn't a zero element to compare with
105
110
_iszero (x:: Union{Number, AbstractArray} ) = iszero (x)
106
111
_iszero (_) = false
107
112
108
113
function _isones (bc:: Broadcast.Broadcasted )
109
- all (has_static_value, bc. args) && _isone (_getindex_value (bc))
114
+ all (has_static_value, bc. args) && _isone (broadcast_getindex_value (bc))
110
115
end
111
116
# conservative check for ones. In most cases, there isn't a unit element to compare with
112
117
_isone (x:: Union{Number, AbstractArray} ) = isone (x)
113
118
_isone (_) = false
114
119
115
- _isfill (bc:: Broadcast.Broadcasted ) = all (_isfill, bc. args)
116
- _isfill (f:: AbstractFill ) = true
117
- _isfill (f:: Number ) = true
118
- _isfill (f:: Ref ) = true
119
- _isfill (:: Any ) = false
120
+ # wrappers that are equivalent to an `AbstractFill` may opt in to the broadcasting behavior
121
+ # of `AbstractFill` by specializing `isfill` and `broadcast_getindex_value`
122
+ isfill (bc:: Broadcast.Broadcasted ) = all (isfill, bc. args)
123
+ isfill (f:: AbstractFill ) = true
124
+ isfill (f:: Transpose ) = isfill (parent (f))
125
+ isfill (f:: Adjoint ) = isfill (parent (f))
126
+ isfill (f:: Number ) = true
127
+ isfill (f:: Ref ) = true
128
+ isfill (:: Any ) = false
120
129
121
130
function _copy_fill (bc)
122
- v = _getindex_value (bc)
131
+ v = broadcast_getindex_value (bc)
123
132
if _iszeros (bc)
124
133
return Zeros (typeof (v), axes (bc))
125
134
elseif _isones (bc)
130
139
131
140
# recursively copy the purely fill components
132
141
function _preprocess_fill (bc:: Broadcast.Broadcasted{<:AbstractFillStyle} )
133
- _isfill (bc) ? _copy_fill (bc) : Broadcast. broadcasted (bc. f, map (_preprocess_fill, bc. args)... )
142
+ isfill (bc) ? _copy_fill (bc) : Broadcast. broadcasted (bc. f, map (_preprocess_fill, bc. args)... )
134
143
end
135
144
_preprocess_fill (bc:: Broadcast.Broadcasted ) = Broadcast. broadcasted (bc. f, map (_preprocess_fill, bc. args)... )
136
145
_preprocess_fill (x) = x
@@ -144,7 +153,7 @@ function _fallback_copy(bc)
144
153
end
145
154
146
155
function Base. copy (bc:: Broadcast.Broadcasted{<:AbstractFillStyle} )
147
- _isfill (bc) ? _copy_fill (bc) : _fallback_copy (bc)
156
+ isfill (bc) ? _copy_fill (bc) : _fallback_copy (bc)
148
157
end
149
158
# make the zero-dimensional case consistent with Base
150
159
Base. copy (bc:: Broadcast.Broadcasted{<:AbstractFillStyle{0}} ) = _fallback_copy (bc)
0 commit comments