@@ -1146,6 +1146,129 @@ for (dep, f, op) in [(:sumabs!, :sum!, :abs),
1146
1146
end
1147
1147
end
1148
1148
1149
+ # # Deprecate broadcast_zpreserving[!] (wasn't exported, but might as well be friendly)
1150
+ function gen_broadcast_function_sparse (genbody:: Function , f:: Function , is_first_sparse:: Bool )
1151
+ body = genbody (f, is_first_sparse)
1152
+ @eval let
1153
+ local _F_
1154
+ function _F_ {Tv,Ti} (B:: SparseMatrixCSC{Tv,Ti} , A_1, A_2)
1155
+ $ body
1156
+ end
1157
+ _F_
1158
+ end
1159
+ end
1160
+ function gen_broadcast_body_zpreserving (f:: Function , is_first_sparse:: Bool )
1161
+ F = Expr (:quote , f)
1162
+ if is_first_sparse
1163
+ A1 = :(A_1)
1164
+ A2 = :(A_2)
1165
+ op1 = :(val1)
1166
+ op2 = :(val2)
1167
+ else
1168
+ A1 = :(A_2)
1169
+ A2 = :(A_1)
1170
+ op1 = :(val2)
1171
+ op2 = :(val1)
1172
+ end
1173
+ quote
1174
+ Base. Broadcast. check_broadcast_indices (indices (B), $ A1)
1175
+ Base. Broadcast. check_broadcast_indices (indices (B), $ A2)
1176
+
1177
+ nnzB = isempty (B) ? 0 :
1178
+ nnz ($ A1) * div (B. n, ($ A1). n) * div (B. m, ($ A1). m)
1179
+ if length (B. rowval) < nnzB
1180
+ resize! (B. rowval, nnzB)
1181
+ end
1182
+ if length (B. nzval) < nnzB
1183
+ resize! (B. nzval, nnzB)
1184
+ end
1185
+ z = zero (Tv)
1186
+
1187
+ ptrB = 1
1188
+ B. colptr[1 ] = 1
1189
+
1190
+ @inbounds for col = 1 : B. n
1191
+ ptr1:: Int = ($ A1). n == 1 ? ($ A1). colptr[1 ] : ($ A1). colptr[col]
1192
+ stop1:: Int = ($ A1). n == 1 ? ($ A1). colptr[2 ] : ($ A1). colptr[col+ 1 ]
1193
+ col2 = size ($ A2, 2 ) == 1 ? 1 : col
1194
+ row = 1
1195
+ while ptr1 < stop1 && row <= B. m
1196
+ if ($ A1). m != 1
1197
+ row = ($ A1). rowval[ptr1]
1198
+ end
1199
+ row2 = size ($ A2, 1 ) == 1 ? 1 : row
1200
+ val1 = ($ A1). nzval[ptr1]
1201
+ val2 = ($ A2)[row2,col2]
1202
+ res = ($ F)($ op1, $ op2)
1203
+ if res != z
1204
+ B. rowval[ptrB] = row
1205
+ B. nzval[ptrB] = res
1206
+ ptrB += 1
1207
+ end
1208
+ if ($ A1). m != 1
1209
+ ptr1 += 1
1210
+ else
1211
+ row += 1
1212
+ end
1213
+ end
1214
+ B. colptr[col+ 1 ] = ptrB
1215
+ end
1216
+ deleteat! (B. rowval, B. colptr[end ]: length (B. rowval))
1217
+ deleteat! (B. nzval, B. colptr[end ]: length (B. nzval))
1218
+ nothing
1219
+ end
1220
+ end
1221
+ for (Bsig, A1sig, A2sig, gbb, funcname) in
1222
+ (
1223
+ (SparseMatrixCSC , SparseMatrixCSC , Array, :gen_broadcast_body_zpreserving , :_broadcast_zpreserving! ),
1224
+ (SparseMatrixCSC , Array , SparseMatrixCSC, :gen_broadcast_body_zpreserving , :_broadcast_zpreserving! ),
1225
+ (SparseMatrixCSC , Number , SparseMatrixCSC, :gen_broadcast_body_zpreserving , :_broadcast_zpreserving! ),
1226
+ (SparseMatrixCSC , SparseMatrixCSC , Number, :gen_broadcast_body_zpreserving , :_broadcast_zpreserving! ),
1227
+ (SparseMatrixCSC , BitArray , SparseMatrixCSC, :gen_broadcast_body_zpreserving , :_broadcast_zpreserving! ),
1228
+ (SparseMatrixCSC , SparseMatrixCSC , BitArray, :gen_broadcast_body_zpreserving , :_broadcast_zpreserving! ),
1229
+ )
1230
+ @eval let cache = Dict {Function,Function} ()
1231
+ global $ funcname
1232
+ function $funcname (f:: Function , B:: $Bsig , A1:: $A1sig , A2:: $A2sig )
1233
+ func = @get! cache f gen_broadcast_function_sparse ($ gbb, f, ($ A1sig) <: SparseMatrixCSC )
1234
+ # need eval because func was just created by gen_broadcast_function_sparse
1235
+ # TODO : convert this to a generated function
1236
+ eval (current_module (), Expr (:body , Expr (:return , Expr (:call , QuoteNode (func), QuoteNode (B), QuoteNode (A1), QuoteNode (A2)))))
1237
+ return B
1238
+ end
1239
+ end # let broadcast_cache
1240
+ end
1241
+ _broadcast_zpreserving! (args... ) = broadcast! (args... )
1242
+ _broadcast_zpreserving (args... ) = Base. Broadcast. broadcast_elwise_op (args... )
1243
+ _broadcast_zpreserving {Tv1,Ti1,Tv2,Ti2} (f:: Function , A_1:: SparseMatrixCSC{Tv1,Ti1} , A_2:: SparseMatrixCSC{Tv2,Ti2} ) =
1244
+ _broadcast_zpreserving! (f, spzeros (promote_type (Tv1, Tv2), promote_type (Ti1, Ti2), Base. to_shape (Base. Broadcast. broadcast_indices (A_1, A_2))), A_1, A_2)
1245
+ _broadcast_zpreserving {Tv,Ti} (f:: Function , A_1:: SparseMatrixCSC{Tv,Ti} , A_2:: Union{Array,BitArray,Number} ) =
1246
+ _broadcast_zpreserving! (f, spzeros (promote_eltype (A_1, A_2), Ti, Base. to_shape (Base. Broadcast. broadcast_indices (A_1, A_2))), A_1, A_2)
1247
+ _broadcast_zpreserving {Tv,Ti} (f:: Function , A_1:: Union{Array,BitArray,Number} , A_2:: SparseMatrixCSC{Tv,Ti} ) =
1248
+ _broadcast_zpreserving! (f, spzeros (promote_eltype (A_1, A_2), Ti, Base. to_shape (Base. Broadcast. broadcast_indices (A_1, A_2))), A_1, A_2)
1249
+
1250
+ function _depstring_bczpres ()
1251
+ return string (" broadcast_zpreserving[!] is deprecated. Generic sparse broadcast[!] " ,
1252
+ " provides most of broadcast_zpreserving[!]'s functionality. If you have a use case " ,
1253
+ " that generic sparse broadcast[!] does not cover, please describe your use case in " ,
1254
+ " issue #19533 (https://github.com/JuliaLang/julia/issues/19533)." )
1255
+ end
1256
+ function _depwarn_bczpres (f, args... )
1257
+ depwarn (_depstring_bczpres (), :broadcast_zpreserving )
1258
+ return _broadcast_zpreserving (f, args... )
1259
+ end
1260
+ function _depwarn_bczpres! (f, args... )
1261
+ depwarn (_depstring_bczpres (), :broadcast_zpreserving! )
1262
+ return _broadcast_zpreserving! (f, args... )
1263
+ end
1264
+ eval (SparseArrays, :(broadcast_zpreserving (f, args... ) = Base. _depwarn_bczpres (f, args... )))
1265
+ eval (SparseArrays, :(broadcast_zpreserving (f, A:: SparseMatrixCSC , B:: SparseMatrixCSC ) = Base. _depwarn_bczpres (f, A, B)))
1266
+ eval (SparseArrays, :(broadcast_zpreserving (f, A:: SparseMatrixCSC , B:: Union{Array,BitArray,Number} ) = Base. _depwarn_bczpres (f, A, B)))
1267
+ eval (SparseArrays, :(broadcast_zpreserving (f, A:: Union{Array,BitArray,Number} , B:: SparseMatrixCSC ) = Base. _depwarn_bczpres (f, A, B)))
1268
+ eval (SparseArrays, :(broadcast_zpreserving! (f, args... ) = Base. _depwarn_bczpres! (f, args... )))
1269
+ eval (SparseArrays, :(broadcast_zpreserving! (f, C:: SparseMatrixCSC , A:: SparseMatrixCSC , B:: Union{Array,BitArray,Number} ) = Base. _depwarn_bczpres! (f, C, A, B)))
1270
+ eval (SparseArrays, :(broadcast_zpreserving! (f, C:: SparseMatrixCSC , A:: Union{Array,BitArray,Number} , B:: SparseMatrixCSC ) = Base. _depwarn_bczpres! (f, C, A, B)))
1271
+
1149
1272
# #19719
1150
1273
@deprecate getindex (t:: Tuple , r:: AbstractArray ) getindex (t, vec (r))
1151
1274
@deprecate getindex (t:: Tuple , b:: AbstractArray{Bool} ) getindex (t, vec (b))
0 commit comments