@@ -1108,7 +1108,9 @@ function hcat(Xin::AbstractSparseVector...)
1108
1108
X = map (_unsafe_unfix, Xin)
1109
1109
Tv = promote_type (map (eltype, X)... )
1110
1110
Ti = promote_type (map (indtype, X)... )
1111
- r = _absspvec_hcat (map (x -> convert (SparseVector{Tv,Ti}, x), X)... )
1111
+ r = (function (:: Type{SV} ) where SV
1112
+ _absspvec_hcat (map (x -> convert (SV, x), X)... )
1113
+ end )(SparseVector{Tv,Ti})
1112
1114
return @if_move_fixed Xin... r
1113
1115
end
1114
1116
function _absspvec_hcat (X:: AbstractSparseVector{Tv,Ti} ...) where {Tv,Ti}
@@ -1144,7 +1146,9 @@ function vcat(Xin::AbstractSparseVector...)
1144
1146
X = map (_unsafe_unfix, Xin)
1145
1147
Tv = promote_type (map (eltype, X)... )
1146
1148
Ti = promote_type (map (indtype, X)... )
1147
- r = _absspvec_vcat (map (x -> convert (SparseVector{Tv,Ti}, x), X)... )
1149
+ r = (function (:: Type{SV} ) where SV
1150
+ _absspvec_vcat (map (x -> convert (SV, x), X)... )
1151
+ end )(SparseVector{Tv,Ti})
1148
1152
return @if_move_fixed Xin... r
1149
1153
end
1150
1154
function _absspvec_vcat (X:: AbstractSparseVector{Tv,Ti} ...) where {Tv,Ti}
@@ -1194,13 +1198,14 @@ anysparse() = false
1194
1198
anysparse (X) = X isa AbstractArray && issparse (X)
1195
1199
anysparse (X, Xs... ) = anysparse (X) || anysparse (Xs... )
1196
1200
1197
- function hcat (X:: Union{Vector, AbstractSparseVector} ...)
1201
+ const _SparseVecConcatGroup = Union{Vector, AbstractSparseVector}
1202
+ function hcat (X:: _SparseVecConcatGroup... )
1198
1203
if anysparse (X... )
1199
1204
X = map (sparse, X)
1200
1205
end
1201
1206
return cat (X... ; dims= Val (2 ))
1202
1207
end
1203
- function vcat (X:: Union{Vector, AbstractSparseVector} ...)
1208
+ function vcat (X:: _SparseVecConcatGroup ... )
1204
1209
if anysparse (X... )
1205
1210
X = map (sparse, X)
1206
1211
end
@@ -1213,30 +1218,30 @@ end
1213
1218
const _SparseConcatGroup = Union{AbstractVecOrMat{<: Number },Number}
1214
1219
1215
1220
# `@constprop :aggressive` allows `dims` to be propagated as constant improving return type inference
1216
- Base. @constprop :aggressive function Base. _cat (dims, X:: _SparseConcatGroup... )
1217
- T = promote_eltype (X... )
1218
- if anysparse (X... )
1219
- X = ( _sparse (first (X)) , map (_makesparse, Base . tail (X)) ... )
1221
+ Base. @constprop :aggressive function Base. _cat (dims, X1 :: _SparseConcatGroup , X:: _SparseConcatGroup... )
1222
+ T = promote_eltype (X1, X... )
1223
+ if anysparse (X1) || anysparse ( X... )
1224
+ X1, X = _sparse (X1) , map (_makesparse, X )
1220
1225
end
1221
- return Base. _cat_t (dims, T, X... )
1226
+ return Base. _cat_t (dims, T, X1, X... )
1222
1227
end
1223
- function hcat (X:: _SparseConcatGroup... )
1224
- if anysparse (X... )
1225
- X = ( _sparse (first (X)) , map (_makesparse, Base . tail (X)) ... )
1228
+ function hcat (X1 :: _SparseConcatGroup , X:: _SparseConcatGroup... )
1229
+ if anysparse (X1) || anysparse ( X... )
1230
+ X1, X = _sparse (X1) , map (_makesparse, X )
1226
1231
end
1227
- return cat (X... , dims= Val (2 ))
1232
+ return cat (X1, X... , dims= Val (2 ))
1228
1233
end
1229
- function vcat (X:: _SparseConcatGroup... )
1230
- if anysparse (X... )
1231
- X = ( _sparse (first (X)) , map (_makesparse, Base . tail (X)) ... )
1234
+ function vcat (X1 :: _SparseConcatGroup , X:: _SparseConcatGroup... )
1235
+ if anysparse (X1) || anysparse ( X... )
1236
+ X1, X = _sparse (X1) , map (_makesparse, X )
1232
1237
end
1233
- return cat (X... , dims= Val (1 ))
1238
+ return cat (X1, X... , dims= Val (1 ))
1234
1239
end
1235
- function hvcat (rows:: Tuple{Vararg{Int}} , X:: _SparseConcatGroup... )
1236
- if anysparse (X... )
1237
- vcat (_hvcat_rows (rows, X... )... )
1240
+ function hvcat (rows:: Tuple{Vararg{Int}} , X1 :: _SparseConcatGroup , X:: _SparseConcatGroup... )
1241
+ if anysparse (X1) || anysparse ( X... )
1242
+ vcat (_hvcat_rows (rows, X1, X... )... )
1238
1243
else
1239
- Base. typed_hvcat (Base. promote_eltypeof (X... ), rows, X... )
1244
+ Base. typed_hvcat (Base. promote_eltypeof (X1, X... ), rows, X1 , X... )
1240
1245
end
1241
1246
end
1242
1247
function _hvcat_rows ((row1, rows... ):: Tuple{Vararg{Int}} , X:: _SparseConcatGroup... )
@@ -1254,6 +1259,15 @@ function _hvcat_rows((row1, rows...)::Tuple{Vararg{Int}}, X::_SparseConcatGroup.
1254
1259
end
1255
1260
_hvcat_rows (:: Tuple{} , X:: _SparseConcatGroup... ) = ()
1256
1261
1262
+ # disambiguation for type-piracy problems created above
1263
+ hcat (n1:: Number , ns:: Vararg{Number} ) = invoke (hcat, Tuple{Vararg{Number}}, n1, ns... )
1264
+ vcat (n1:: Number , ns:: Vararg{Number} ) = invoke (vcat, Tuple{Vararg{Number}}, n1, ns... )
1265
+ hcat (n1:: Type{N} , ns:: Vararg{N} ) where {N<: Number } = invoke (hcat, Tuple{Vararg{Number}}, n1, ns... )
1266
+ vcat (n1:: Type{N} , ns:: Vararg{N} ) where {N<: Number } = invoke (vcat, Tuple{Vararg{Number}}, n1, ns... )
1267
+ hvcat (rows:: Tuple{Vararg{Int}} , n1:: Number , ns:: Vararg{Number} ) = invoke (hvcat, Tuple{typeof (rows), Vararg{Number}}, rows, n1, ns... )
1268
+ hvcat (rows:: Tuple{Vararg{Int}} , n1:: N , ns:: Vararg{N} ) where {N<: Number } = invoke (hvcat, Tuple{typeof (rows), Vararg{N}}, rows, n1, ns... )
1269
+
1270
+
1257
1271
# make sure UniformScaling objects are converted to sparse matrices for concatenation
1258
1272
promote_to_array_type (A:: Tuple{Vararg{Union{_SparseConcatGroup,UniformScaling}}} ) = anysparse (A... ) ? SparseMatrixCSC : Matrix
1259
1273
promote_to_arrays_ (n:: Int , :: Type{SparseMatrixCSC} , J:: UniformScaling ) = sparse (J, n, n)
0 commit comments