@@ -85,15 +85,17 @@ _unsafe_unfix(s::FixedSparseVector) = SparseVector(length(s), parent(nonzeroinds
85
85
86
86
# Define an alias for a view of a whole column of a SparseMatrixCSC. Many methods can be written for the
87
87
# union of such a view and a SparseVector so we define an alias for such a union as well
88
- const SparseColumnView{Tv,Ti} = SubArray{Tv ,1 ,<: AbstractSparseMatrixCSC{Tv,Ti} ,Tuple{Base. Slice{Base. OneTo{Int}},Int},false }
89
- const SparseVectorView{Tv,Ti} = SubArray{Tv ,1 ,<: AbstractSparseVector{Tv,Ti} ,Tuple{Base. Slice{Base. OneTo{Int}}},false }
90
- const SparseVectorUnion{Tv,Ti} = Union{AbstractCompressedVector{Tv,Ti}, SparseColumnView{Tv,Ti}, SparseVectorView{Tv,Ti} }
91
- const AdjOrTransSparseVectorUnion{Tv,Ti} = LinearAlgebra . AdjOrTrans{Tv, <: SparseVectorUnion{Tv,Ti} }
92
- const SVorFSV{Tv,Ti} = Union{SparseVector{Tv,Ti},FixedSparseVector{Tv,Ti}}
88
+ const SparseColumnView = SubArray{<: Any ,1 ,<: AbstractSparseMatrixCSC ,Tuple{Base. Slice{Base. OneTo{Int}},Int},false }
89
+ const SparseVectorView = SubArray{<: Any ,1 ,<: AbstractSparseVector ,Tuple{Base. Slice{Base. OneTo{Int}}},false }
90
+ const SparseVectorUnion = Union{AbstractCompressedVector, SparseColumnView, SparseVectorView}
91
+ const AdjOrTransSparseVectorUnion = AdjOrTrans{<: Any , <: SparseVectorUnion }
92
+
93
93
# ## Basic properties
94
94
95
- length (x:: SVorFSV ) = getfield (x, :n )
96
- size (x:: SVorFSV ) = (getfield (x, :n ),)
95
+ length (x:: SparseVector ) = getfield (x, :n )
96
+ length (x:: FixedSparseVector ) = getfield (x, :n )
97
+ size (x:: SparseVector ) = (getfield (x, :n ),)
98
+ size (x:: FixedSparseVector ) = (getfield (x, :n ),)
97
99
98
100
function Base. _simple_count (f, x:: AbstractCompressedVector , init:: T ) where T
99
101
init + T (count (f, nonzeros (x)) + f (zero (eltype (x)))* (length (x) - nnz (x)))
@@ -118,7 +120,8 @@ function nzrange(x::SparseVectorUnion, j::Integer)
118
120
j == 1 ? (1 : nnz (x)) : throw (BoundsError (x, (" :" , j)))
119
121
end
120
122
121
- nonzeros (x:: SVorFSV ) = getfield (x, :nzval )
123
+ nonzeros (x:: SparseVector ) = getfield (x, :nzval )
124
+ nonzeros (x:: FixedSparseVector ) = getfield (x, :nzval )
122
125
function nonzeros (x:: SparseColumnView )
123
126
rowidx, colidx = parentindices (x)
124
127
A = parent (x)
@@ -127,7 +130,8 @@ function nonzeros(x::SparseColumnView)
127
130
end
128
131
nonzeros (x:: SparseVectorView ) = nonzeros (parent (x))
129
132
130
- nonzeroinds (x:: SVorFSV ) = getfield (x, :nzind )
133
+ nonzeroinds (x:: SparseVector ) = getfield (x, :nzind )
134
+ nonzeroinds (x:: FixedSparseVector ) = getfield (x, :nzind )
131
135
function nonzeroinds (x:: SparseColumnView )
132
136
rowidx, colidx = parentindices (x)
133
137
A = parent (x)
@@ -802,12 +806,12 @@ function findall(x::SparseVectorUnion)
802
806
return findall (identity, x)
803
807
end
804
808
805
- function findall (p:: F , x:: SparseVectorUnion{<:Any,Ti} ) where {Ti, F<: Function }
809
+ function findall (p:: F , x:: SparseVectorUnion ) where {F<: Function }
806
810
if p (zero (eltype (x)))
807
811
return invoke (findall, Tuple{Function, Any}, p, x)
808
812
end
809
813
numnz = nnz (x)
810
- I = Vector {Ti } (undef, numnz)
814
+ I = Vector {indtype(x) } (undef, numnz)
811
815
812
816
nzind = nonzeroinds (x)
813
817
nzval = nonzeros (x)
@@ -827,7 +831,7 @@ function findall(p::F, x::SparseVectorUnion{<:Any,Ti}) where {Ti,F<:Function}
827
831
828
832
return I
829
833
end
830
- findall (p:: Base.Fix2{typeof(in)} , x:: SparseVectorUnion{<:Any,Ti} ) where {Ti} =
834
+ findall (p:: Base.Fix2{typeof(in)} , x:: SparseVectorUnion ) =
831
835
invoke (findall, Tuple{Base. Fix2{typeof (in)}, AbstractArray}, p, x)
832
836
833
837
"""
@@ -849,11 +853,11 @@ julia> findnz(x)
849
853
([1, 4, 6, 8], [1, 2, 4, 3])
850
854
```
851
855
"""
852
- function findnz (x:: SparseVectorUnion{Tv,Ti} ) where {Tv,Ti}
856
+ function findnz (x:: SparseVectorUnion )
853
857
numnz = nnz (x)
854
858
855
- I = Vector {Ti } (undef, numnz)
856
- V = Vector {Tv } (undef, numnz)
859
+ I = Vector {indtype(x) } (undef, numnz)
860
+ V = Vector {eltype(x) } (undef, numnz)
857
861
858
862
nzind = nonzeroinds (x)
859
863
nzval = nonzeros (x)
@@ -1095,13 +1099,13 @@ complex(x::AbstractSparseVector) =
1095
1099
1096
1100
# ## Concatenation
1097
1101
1098
- # Without the first of these methods, horizontal concatenations of SparseVectors fall
1099
- # back to the horizontal concatenation method that ensures that combinations of
1100
- # sparse/special/dense matrix/vector types concatenate to SparseMatrixCSCs, instead
1101
- # of _absspvec_hcat below. The <:Integer qualifications are necessary for correct dispatch.
1102
- hcat (X :: SparseVector{Tv,Ti} ...) where {Tv,Ti <: Integer } = _absspvec_hcat (X ... )
1103
- hcat (X :: FixedSparseVector{Tv,Ti} ...) where {Tv,Ti <: Integer } = _absspvec_hcat (X ... )
1104
- hcat (X :: AbstractSparseVector{Tv,Ti} ...) where {Tv,Ti <: Integer } = _absspvec_hcat (X ... )
1102
+ function hcat (Xin :: AbstractSparseVector... )
1103
+ X = map (_unsafe_unfix, Xin)
1104
+ Tv = promote_type ( map (eltype, X) ... )
1105
+ Ti = promote_type ( map (indtype, X) ... )
1106
+ r = _absspvec_hcat ( map (x -> convert ( SparseVector{Tv,Ti}, x), X) ... )
1107
+ return @if_move_fixed Xin ... r
1108
+ end
1105
1109
function _absspvec_hcat (X:: AbstractSparseVector{Tv,Ti} ...) where {Tv,Ti}
1106
1110
# check sizes
1107
1111
n = length (X)
@@ -1128,24 +1132,15 @@ function _absspvec_hcat(X::AbstractSparseVector{Tv,Ti}...) where {Tv,Ti}
1128
1132
roff += length (xnzind)
1129
1133
end
1130
1134
colptr[n+ 1 ] = roff
1131
- r = SparseMatrixCSC {Tv,Ti} (m, n, colptr, nzrow, nzval)
1132
- return @if_move_fixed X... r
1135
+ return SparseMatrixCSC {Tv,Ti} (m, n, colptr, nzrow, nzval)
1133
1136
end
1134
1137
1135
- # Without the first of these methods, vertical concatenations of SparseVectors fall
1136
- # back to the vertical concatenation method that ensures that combinations of
1137
- # sparse/special/dense matrix/vector types concatenate to SparseMatrixCSCs, instead
1138
- # of _absspvec_vcat below. The <:Integer qualifications are necessary for correct dispatch.
1139
- vcat (X:: SparseVector{Tv,Ti} ...) where {Tv,Ti<: Integer } = _absspvec_vcat (X... )
1140
- vcat (X:: AbstractSparseVector{Tv,Ti} ...) where {Tv,Ti<: Integer } = _absspvec_vcat (X... )
1141
- function vcat (X:: SparseVector... )
1142
- commeltype = promote_type (map (eltype, X)... )
1143
- commindtype = promote_type (map (indtype, X)... )
1144
- return vcat (map (x -> SparseVector {commeltype,commindtype} (x), X)... )
1145
- end
1146
- function vcat (X:: SVorFSV... )
1147
- r = vcat (map (_unsafe_unfix, X)... )
1148
- return @if_move_fixed X r
1138
+ function vcat (Xin:: AbstractSparseVector... )
1139
+ X = map (_unsafe_unfix, Xin)
1140
+ Tv = promote_type (map (eltype, X)... )
1141
+ Ti = promote_type (map (indtype, X)... )
1142
+ r = _absspvec_vcat (map (x -> convert (SparseVector{Tv,Ti}, x), X)... )
1143
+ return @if_move_fixed Xin... r
1149
1144
end
1150
1145
function _absspvec_vcat (X:: AbstractSparseVector{Tv,Ti} ...) where {Tv,Ti}
1151
1146
# check sizes
@@ -1177,22 +1172,18 @@ end
1177
1172
1178
1173
hcat (Xin:: Union{Vector, AbstractSparseVector} ...) = hcat (map (sparse, Xin)... )
1179
1174
vcat (Xin:: Union{Vector, AbstractSparseVector} ...) = vcat (map (sparse, Xin)... )
1180
- # Without the following method, vertical concatenations of SparseVectors with Vectors
1181
- # fall back to the vertical concatenation method that ensures that combinations of
1182
- # sparse/special/dense matrix/vector types concatenate to SparseMatrixCSCs (because
1183
- # the vcat method immediately above is less specific, being defined in AbstractSparseVector
1184
- # rather than SparseVector).
1185
- vcat (X:: Union{Vector,AbstractCompressedVector} ...) = vcat (map (sparse, X)... )
1186
-
1187
1175
1188
1176
# ## Concatenation of un/annotated sparse/special/dense vectors/matrices
1189
1177
1190
- const _SparseArrays = Union{SparseVector, AbstractSparseMatrixCSC, Adjoint{<: Any ,<: SparseVector }, Transpose{<: Any ,<: SparseVector }}
1178
+ const _SparseArrays = Union{AbstractSparseVector,
1179
+ AbstractSparseMatrixCSC,
1180
+ Adjoint{<: Any ,<: AbstractSparseVector },
1181
+ Transpose{<: Any ,<: AbstractSparseVector }}
1191
1182
const _SparseConcatArrays = Union{_SpecialArrays, _SparseArrays}
1192
1183
1193
- const _Symmetric_SparseConcatArrays{T,A <: _SparseConcatArrays } = Symmetric{T,A }
1194
- const _Hermitian_SparseConcatArrays{T,A <: _SparseConcatArrays } = Hermitian{T,A }
1195
- const _Triangular_SparseConcatArrays{T,A <: _SparseConcatArrays } = UpperOrLowerTriangular{T,A }
1184
+ const _Symmetric_SparseConcatArrays = Symmetric{<: Any , <: _SparseConcatArrays }
1185
+ const _Hermitian_SparseConcatArrays = Hermitian{<: Any , <: _SparseConcatArrays }
1186
+ const _Triangular_SparseConcatArrays = UpperOrLowerTriangular{<: Any , <: _SparseConcatArrays }
1196
1187
const _Annotated_SparseConcatArrays = Union{_Triangular_SparseConcatArrays, _Symmetric_SparseConcatArrays, _Hermitian_SparseConcatArrays}
1197
1188
# It's important that _SparseConcatGroup is a larger union than _DenseConcatGroup to make
1198
1189
# sparse cat-methods less specific and to kick in only if there is some sparse array present
@@ -1548,7 +1539,8 @@ end
1548
1539
Base. reducedim_initarray (A:: SparseVectorUnion , region, v0, :: Type{R} ) where {R} =
1549
1540
fill! (Array {R} (undef, Base. to_shape (Base. reduced_indices (A, region))), v0)
1550
1541
1551
- function Base. _mapreduce (f, op, :: IndexCartesian , A:: SparseVectorUnion{T} ) where {T}
1542
+ function Base. _mapreduce (f, op, :: IndexCartesian , A:: SparseVectorUnion )
1543
+ T = eltype (A)
1552
1544
isempty (A) && return Base. mapreduce_empty (f, op, T)
1553
1545
z = nnz (A)
1554
1546
rest, ini = if z == 0
@@ -1671,26 +1663,26 @@ end
1671
1663
(/ )(x:: SparseVectorUnion , a:: Number ) =
1672
1664
@if_move_fixed x SparseVector (length (x), copy (nonzeroinds (x)), nonzeros (x) / a)
1673
1665
# dot
1674
- function dot (x:: AbstractVector{Tx} , y:: SparseVectorUnion{Ty} ) where {Tx <: Number ,Ty <: Number }
1666
+ function dot (x:: AbstractVector , y:: SparseVectorUnion )
1675
1667
require_one_based_indexing (x, y)
1676
1668
n = length (x)
1677
1669
length (y) == n || throw (DimensionMismatch ())
1678
1670
nzind = nonzeroinds (y)
1679
1671
nzval = nonzeros (y)
1680
- s = dot (zero (Tx) , zero (Ty ))
1672
+ s = dot (zero (eltype (x)) , zero (eltype (y) ))
1681
1673
@inbounds for i = 1 : length (nzind)
1682
1674
s += dot (x[nzind[i]], nzval[i])
1683
1675
end
1684
1676
return s
1685
1677
end
1686
1678
1687
- function dot (x:: SparseVectorUnion{Tx} , y:: AbstractVector{Ty} ) where {Tx <: Number ,Ty <: Number }
1679
+ function dot (x:: SparseVectorUnion , y:: AbstractVector )
1688
1680
require_one_based_indexing (x, y)
1689
1681
n = length (y)
1690
1682
length (x) == n || throw (DimensionMismatch ())
1691
1683
nzind = nonzeroinds (x)
1692
1684
nzval = nonzeros (x)
1693
- s = dot (zero (Tx) , zero (Ty ))
1685
+ s = dot (zero (eltype (x)) , zero (eltype (y) ))
1694
1686
@inbounds for i = 1 : length (nzind)
1695
1687
s += dot (nzval[i], y[nzind[i]])
1696
1688
end
@@ -1718,7 +1710,7 @@ function _spdot(f::Function,
1718
1710
s
1719
1711
end
1720
1712
1721
- function dot (x:: SparseVectorUnion{<:Number} , y:: SparseVectorUnion{<:Number} )
1713
+ function dot (x:: SparseVectorUnion , y:: SparseVectorUnion )
1722
1714
x === y && return sum (abs2, x)
1723
1715
n = length (x)
1724
1716
length (y) == n || throw (DimensionMismatch ())
0 commit comments