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