@@ -40,6 +40,7 @@ const AdjOrTransSparseVectorUnion{Tv,Ti} = LinearAlgebra.AdjOrTrans{Tv, <:Sparse
40
40
41
41
# ## Basic properties
42
42
43
+ length (x:: SparseVector ) = getfield (x, :n )
43
44
size (x:: SparseVector ) = (getfield (x, :n ),)
44
45
count (f, x:: SparseVector ) = count (f, nonzeros (x)) + f (zero (eltype (x)))* (length (x) - nnz (x))
45
46
@@ -1075,20 +1076,27 @@ const _Annotated_SparseConcatArrays = Union{_Triangular_SparseConcatArrays, _Sym
1075
1076
const _SparseConcatGroup = Union{_DenseConcatGroup, _SparseConcatArrays, _Annotated_SparseConcatArrays}
1076
1077
1077
1078
# Concatenations involving un/annotated sparse/special matrices/vectors should yield sparse arrays
1079
+
1080
+ # the output array type is determined by the first element of the to be concatenated objects
1081
+ # if this is a Number, the output would be dense by the fallback abstractarray.jl code (see cat_similar)
1082
+ # so make sure that if that happens, the "array" is sparse (if more sparse arrays are involved, of course)
1083
+ _sparse (x:: Number ) = sparsevec ([1 ], [x], 1 )
1084
+ _sparse (A) = _makesparse (A)
1078
1085
_makesparse (x:: Number ) = x
1079
- _makesparse (x:: AbstractArray ) = SparseMatrixCSC (issparse (x) ? x : sparse (x))
1086
+ _makesparse (x:: AbstractVector ) = convert (SparseVector, issparse (x) ? x : sparse (x)):: SparseVector
1087
+ _makesparse (x:: AbstractMatrix ) = convert (SparseMatrixCSC, issparse (x) ? x : sparse (x)):: SparseMatrixCSC
1080
1088
1081
1089
function Base. _cat (dims, Xin:: _SparseConcatGroup... )
1082
- X = map (_makesparse, Xin)
1090
+ X = ( _sparse ( first (Xin)), map (_makesparse, Base . tail ( Xin)) ... )
1083
1091
T = promote_eltype (Xin... )
1084
1092
Base. cat_t (T, X... ; dims= dims)
1085
1093
end
1086
1094
function hcat (Xin:: _SparseConcatGroup... )
1087
- X = map (_makesparse, Xin)
1095
+ X = ( _sparse ( first (Xin)), map (_makesparse, Base . tail ( Xin)) ... )
1088
1096
return cat (X... , dims= Val (2 ))
1089
1097
end
1090
1098
function vcat (Xin:: _SparseConcatGroup... )
1091
- X = map (_makesparse, Xin)
1099
+ X = ( _sparse ( first (Xin)), map (_makesparse, Base . tail ( Xin)) ... )
1092
1100
return cat (X... , dims= Val (1 ))
1093
1101
end
1094
1102
hvcat (rows:: Tuple{Vararg{Int}} , X:: _SparseConcatGroup... ) =
@@ -1122,9 +1130,9 @@ Concatenate along dimension 2. Return a SparseMatrixCSC object.
1122
1130
the concatenation with specialized "sparse" matrix types from LinearAlgebra.jl
1123
1131
automatically yielded sparse output even in the absence of any SparseArray argument.
1124
1132
"""
1125
- sparse_hcat (Xin:: Union{AbstractVecOrMat,Number} ...) = cat (map (_makesparse, Xin)... , dims= Val (2 ))
1133
+ sparse_hcat (Xin:: Union{AbstractVecOrMat,Number} ...) = cat (_sparse ( first (Xin)), map (_makesparse, Base . tail ( Xin) )... , dims= Val (2 ))
1126
1134
function sparse_hcat (X:: Union{AbstractVecOrMat,UniformScaling,Number} ...)
1127
- LinearAlgebra. _hcat (X ... ; array_type = SparseMatrixCSC)
1135
+ LinearAlgebra. _hcat (_sparse ( first (X)), map (_makesparse, Base . tail (X)) ... ; array_type = SparseMatrixCSC)
1128
1136
end
1129
1137
1130
1138
"""
@@ -1137,9 +1145,9 @@ Concatenate along dimension 1. Return a SparseMatrixCSC object.
1137
1145
the concatenation with specialized "sparse" matrix types from LinearAlgebra.jl
1138
1146
automatically yielded sparse output even in the absence of any SparseArray argument.
1139
1147
"""
1140
- sparse_vcat (Xin:: Union{AbstractVecOrMat,Number} ...) = cat (map (_makesparse, Xin)... , dims= Val (1 ))
1148
+ sparse_vcat (Xin:: Union{AbstractVecOrMat,Number} ...) = cat (_sparse ( first (Xin)), map (_makesparse, Base . tail ( Xin) )... , dims= Val (1 ))
1141
1149
function sparse_vcat (X:: Union{AbstractVecOrMat,UniformScaling,Number} ...)
1142
- LinearAlgebra. _vcat (X ... ; array_type = SparseMatrixCSC)
1150
+ LinearAlgebra. _vcat (_sparse ( first (X)), map (_makesparse, Base . tail (X)) ... ; array_type = SparseMatrixCSC)
1143
1151
end
1144
1152
1145
1153
"""
@@ -1155,10 +1163,10 @@ arguments to concatenate in each block row.
1155
1163
automatically yielded sparse output even in the absence of any SparseArray argument.
1156
1164
"""
1157
1165
function sparse_hvcat (rows:: Tuple{Vararg{Int}} , Xin:: Union{AbstractVecOrMat,Number} ...)
1158
- hvcat (rows, map (_makesparse, Xin)... )
1166
+ hvcat (rows, _sparse ( first (Xin)), map (_makesparse, Base . tail ( Xin) )... )
1159
1167
end
1160
1168
function sparse_hvcat (rows:: Tuple{Vararg{Int}} , X:: Union{AbstractVecOrMat,UniformScaling,Number} ...)
1161
- LinearAlgebra. _hvcat (rows, X ... ; array_type = SparseMatrixCSC)
1169
+ LinearAlgebra. _hvcat (rows, _sparse ( first (X)), map (_makesparse, Base . tail (X)) ... ; array_type = SparseMatrixCSC)
1162
1170
end
1163
1171
1164
1172
# ## math functions
@@ -1434,7 +1442,12 @@ for (fun, comp, word) in ((:findmin, :(<), "minimum"), (:findmax, :(>), "maximum
1434
1442
# we try to avoid findfirst(iszero, x)
1435
1443
sindex = findfirst (iszero, nzvals) # first stored zero, if any
1436
1444
zindex = findfirst (i -> i < nzinds[i], eachindex (nzinds)) # first non-stored zero
1437
- index = isnothing (sindex) ? zindex : min (sindex, zindex)
1445
+ index = if isnothing (sindex)
1446
+ # non-stored zero are contiguous and at the end
1447
+ isnothing (zindex) && last (nzinds) < lastindex (x) ? last (nzinds) + 1 : zindex
1448
+ else
1449
+ min (sindex, zindex)
1450
+ end
1438
1451
return zeroval, index
1439
1452
end
1440
1453
end
@@ -2120,8 +2133,8 @@ function subvector_shifter!(R::AbstractVector, V::AbstractVector, start::Integer
2120
2133
end
2121
2134
end
2122
2135
# ...but rowval should be sorted within columns
2123
- circshift! (@view (R[start: fin]), split- start+ 1 )
2124
- circshift! (@view (V[start: fin]), split- start+ 1 )
2136
+ circshift! (@view (R[start: fin]), (CIRCSHIFT_WRONG_DIRECTION ? ( + ) : ( - ))( split- start+ 1 ) )
2137
+ circshift! (@view (V[start: fin]), (CIRCSHIFT_WRONG_DIRECTION ? ( + ) : ( - ))( split- start+ 1 ) )
2125
2138
end
2126
2139
2127
2140
function circshift! (O:: SparseVector , X:: SparseVector , (r,):: Base.DimsInteger{1} )
0 commit comments