Skip to content

Commit 5392618

Browse files
committed
relax type constraints - this is not C++!
1 parent a869f33 commit 5392618

File tree

5 files changed

+48
-80
lines changed

5 files changed

+48
-80
lines changed

src/extendable.jl

Lines changed: 19 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ $(SIGNATURES)
3030
3131
Create empty ExtendableSparseMatrix.
3232
"""
33-
function ExtendableSparseMatrix{Tv,Ti}(m::Integer, n::Integer) where{Tv,Ti<:Integer}
33+
function ExtendableSparseMatrix{Tv,Ti}(m, n) where{Tv,Ti<:Integer}
3434
ExtendableSparseMatrix{Tv,Ti}(spzeros(Tv,Ti,m,n),nothing,time())
3535
end
3636

@@ -39,7 +39,7 @@ $(SIGNATURES)
3939
4040
Create empty ExtendableSparseMatrix.
4141
"""
42-
function ExtendableSparseMatrix(valuetype::Type{Tv},indextype::Type{Ti},m::Integer, n::Integer) where{Tv,Ti<:Integer}
42+
function ExtendableSparseMatrix(valuetype::Type{Tv},indextype::Type{Ti},m, n) where{Tv,Ti<:Integer}
4343
ExtendableSparseMatrix{Tv,Ti}(m,n)
4444
end
4545

@@ -49,7 +49,7 @@ $(SIGNATURES)
4949
Create empty ExtendablSparseMatrix.
5050
This is a pendant to spzeros.
5151
"""
52-
ExtendableSparseMatrix(valuetype::Type{Tv},m::Integer, n::Integer) where{Tv}=ExtendableSparseMatrix{Tv,Int}(m,n)
52+
ExtendableSparseMatrix(valuetype::Type{Tv},m, n) where{Tv}=ExtendableSparseMatrix{Tv,Int}(m,n)
5353

5454

5555
"""
@@ -58,7 +58,7 @@ $(SIGNATURES)
5858
Create empty ExtendableSparseMatrix.
5959
This is a pendant to spzeros.
6060
"""
61-
ExtendableSparseMatrix(m::Integer, n::Integer)=ExtendableSparseMatrix{Float64,Int}(m,n)
61+
ExtendableSparseMatrix(m, n)=ExtendableSparseMatrix{Float64,Int}(m,n)
6262

6363

6464
"""
@@ -99,7 +99,7 @@ updateindex!(A,+,0.1,1,2)
9999
A
100100
```
101101
"""
102-
function updateindex!(ext::ExtendableSparseMatrix{Tv,Ti}, op,v, i,j) where{Tv,Ti<:Integer}
102+
function updateindex!(ext::ExtendableSparseMatrix{Tv,Ti}, op,v, i,j) where {Tv,Ti<:Integer}
103103
k=findindex(ext.cscmatrix,i,j)
104104
if k>0
105105
ext.cscmatrix.nzval[k]=op(ext.cscmatrix.nzval[k],v)
@@ -120,7 +120,7 @@ Find index in CSC matrix and set value if it exists. Otherwise,
120120
set index in extension.
121121
"""
122122

123-
function Base.setindex!(ext::ExtendableSparseMatrix{Tv,Ti}, v, i,j) where{Tv,Ti<:Integer}
123+
function Base.setindex!(ext::ExtendableSparseMatrix{Tv,Ti}, v, i,j) where {Tv,Ti}
124124
k=findindex(ext.cscmatrix,i,j)
125125
if k>0
126126
ext.cscmatrix.nzval[k]=v
@@ -140,7 +140,7 @@ $(SIGNATURES)
140140
Find index in CSC matrix and return value, if it exists.
141141
Otherwise, return value from extension.
142142
"""
143-
function Base.getindex(ext::ExtendableSparseMatrix{Tv,Ti},i::Integer, j::Integer) where{Tv,Ti<:Integer}
143+
function Base.getindex(ext::ExtendableSparseMatrix{Tv,Ti},i, j) where{Tv,Ti<:Integer}
144144
k=findindex(ext.cscmatrix,i,j)
145145
if k>0
146146
return ext.cscmatrix.nzval[k]
@@ -185,7 +185,7 @@ $(SIGNATURES)
185185
If there are new entries in extension, create new CSC matrix by adding the
186186
cscmatrix and linked list matrix and reset the linked list based extension.
187187
"""
188-
function flush!(ext::ExtendableSparseMatrix{Tv,Ti}) where {Tv, Ti<:Integer}
188+
function flush!(ext::ExtendableSparseMatrix)
189189
if ext.lnkmatrix!=nothing && nnz(ext.lnkmatrix)>0
190190
ext.cscmatrix=ext.lnkmatrix+ext.cscmatrix
191191
ext.lnkmatrix=nothing
@@ -261,35 +261,25 @@ function LinearAlgebra.lu(ext::ExtendableSparseMatrix)
261261
return LinearAlgebra.lu(ext.cscmatrix)
262262
end
263263

264-
"""
265-
$(SIGNATURES)
266-
267-
[`flush!`](@ref) and multiply with ext.cscmatrix
268-
"""
269-
function LinearAlgebra.mul!(r::AbstractArray{T,1} where T, ext::ExtendableSparse.ExtendableSparseMatrix, x::AbstractArray{T,1} where T)
270-
@inbounds flush!(ext)
271-
return LinearAlgebra.mul!(r,ext.cscmatrix,x)
272-
end
273-
274264

275265

276266
"""
277267
$(SIGNATURES)
278268
279269
[`\\`](@ref) for extmatrix
280270
"""
281-
function LinearAlgebra.:\(ext::ExtendableSparseMatrix,X::Union{AbstractArray{T,1}, AbstractArray{T,2}} where T)
271+
function LinearAlgebra.:\(ext::ExtendableSparseMatrix,B::AbstractVecOrMat{T} where T)
282272
flush!(ext)
283-
ext.cscmatrix\X
273+
ext.cscmatrix\B
284274
end
285275

286276

287277
"""
288278
$(SIGNATURES)
289279
290-
[`\\`](@ref) for Symmetric{ExtenableSparse}
280+
[`\\`](@ref) for Symmetric{ExtendableSparse}
291281
"""
292-
function LinearAlgebra.:\(symm_ext::Symmetric{Tm, ExtendableSparseMatrix{Tm, Ti}}, B::Union{AbstractArray{T,1}, AbstractArray{T,2}} where T) where{Tm,Ti}
282+
function LinearAlgebra.:\(symm_ext::Symmetric{Tm, ExtendableSparseMatrix{Tm, Ti}}, B::AbstractVecOrMat{T} where T) where{Tm,Ti}
293283
flush!(symm_ext.data)
294284
symm_csc=Symmetric(symm_ext.data.cscmatrix,Symbol(symm_ext.uplo))
295285
symm_csc\B
@@ -299,23 +289,21 @@ end
299289
"""
300290
$(SIGNATURES)
301291
302-
[`\\`](@ref) for Hermitian{ExtenableSparse}
292+
[`\\`](@ref) for Hermitian{ExtendableSparse}
303293
"""
304-
function LinearAlgebra.:\(symm_ext::Hermitian{Tm, ExtendableSparseMatrix{Tm, Ti}}, B::Union{AbstractArray{T,1}, AbstractArray{T,2}} where T) where{Tm,Ti}
294+
function LinearAlgebra.:\(symm_ext::Hermitian{Tm, ExtendableSparseMatrix{Tm, Ti}}, B::AbstractVecOrMat{T} where T) where{Tm,Ti}
305295
flush!(symm_ext.data)
306296
symm_csc=Hermitian(symm_ext.data.cscmatrix,Symbol(symm_ext.uplo))
307297
symm_csc\B
308298
end
309299

310300

311-
312-
313301
"""
314302
$(SIGNATURES)
315303
316304
[`flush!`](@ref) and ldiv with ext.cscmatrix
317305
"""
318-
function LinearAlgebra.ldiv!(r::AbstractArray{T,1} where T, ext::ExtendableSparse.ExtendableSparseMatrix, x::AbstractArray{T,1} where T)
306+
function LinearAlgebra.ldiv!(r, ext::ExtendableSparse.ExtendableSparseMatrix, x)
319307
@inbounds flush!(ext)
320308
return LinearAlgebra.ldiv!(r,ext.cscmatrix,x)
321309
end
@@ -325,24 +313,12 @@ $(SIGNATURES)
325313
326314
[`flush!`](@ref) and multiply with ext.cscmatrix
327315
"""
328-
function LinearAlgebra.mul!(r::AbstractArray{T,2} where T, ext::ExtendableSparse.ExtendableSparseMatrix, x::AbstractArray{T,2} where T)
316+
function LinearAlgebra.mul!(r,ext::ExtendableSparse.ExtendableSparseMatrix, x)
329317
@inbounds flush!(ext)
330318
return LinearAlgebra.mul!(r,ext.cscmatrix,x)
331319
end
332320

333321

334-
"""
335-
$(SIGNATURES)
336-
337-
[`flush!`](@ref) and ldiv with ext.cscmatrix
338-
"""
339-
function LinearAlgebra.ldiv!(r::AbstractArray{T,2} where T, ext::ExtendableSparse.ExtendableSparseMatrix, x::AbstractArray{T,2} where T)
340-
@inbounds flush!(ext)
341-
return LinearAlgebra.ldiv!(r,ext.cscmatrix,x)
342-
end
343-
344-
345-
346322
"""
347323
$(SIGNATURES)
348324
@@ -380,7 +356,7 @@ $(SIGNATURES)
380356
381357
Add SparseMatrixCSC matrix and [`ExtendableSparseMatrix`](@ref) ext.
382358
"""
383-
function Base.:+(ext::ExtendableSparseMatrix{Tv,Ti},csc::SparseMatrixCSC{Tv,Ti}) where {Tv,Ti<:Integer}
359+
function Base.:+(ext::ExtendableSparseMatrix,csc::SparseMatrixCSC)
384360
@inbounds flush!(ext)
385361
return ext.cscmatrix+csc
386362
end
@@ -390,7 +366,7 @@ $(SIGNATURES)
390366
391367
Subtract SparseMatrixCSC matrix from [`ExtendableSparseMatrix`](@ref) ext.
392368
"""
393-
function Base.:-(ext::ExtendableSparseMatrix{Tv,Ti},csc::SparseMatrixCSC{Tv,Ti}) where {Tv,Ti<:Integer}
369+
function Base.:-(ext::ExtendableSparseMatrix,csc::SparseMatrixCSC)
394370
@inbounds flush!(ext)
395371
return ext.cscmatrix-csc
396372
end
@@ -400,7 +376,7 @@ $(SIGNATURES)
400376
401377
Subtract [`ExtendableSparseMatrix`](@ref) ext from SparseMatrixCSC.
402378
"""
403-
function Base.:-(csc::SparseMatrixCSC{Tv,Ti},ext::ExtendableSparseMatrix{Tv,Ti}) where {Tv,Ti<:Integer}
379+
function Base.:-(csc::SparseMatrixCSC,ext::ExtendableSparseMatrix)
404380
@inbounds flush!(ext)
405381
return csc - ext.cscmatrix
406382
end

src/sparsematrixcsc.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ $(SIGNATURES)
44
Return index corresponding to entry [i,j] in the array of nonzeros,
55
if the entry exists, otherwise, return 0.
66
"""
7-
function findindex(csc::SparseMatrixCSC{T}, i::Integer, j::Integer) where T
7+
function findindex(csc::SparseMatrixCSC{T}, i, j) where T
88
if !(1 <= i <= csc.m && 1 <= j <= csc.n); throw(BoundsError()); end
99
r1 = Int(csc.colptr[j])
1010
r2 = Int(csc.colptr[j+1]-1)
@@ -43,7 +43,7 @@ updateindex!(A,+,0.1,1,2)
4343
A
4444
```
4545
"""
46-
function updateindex!(csc::SparseMatrixCSC{Tv,Ti}, op, v,i::Integer, j::Integer) where{Tv,Ti<:Integer}
46+
function updateindex!(csc::SparseMatrixCSC{Tv,Ti}, op, v,i, j) where{Tv,Ti<:Integer}
4747
k=findindex(csc,i,j)
4848
if k>0 # update existing value
4949
csc.nzval[k]=op(csc.nzval[k],v)

src/sparsematrixlnk.jl

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -79,28 +79,28 @@ $(SIGNATURES)
7979
8080
Constructor of empty matrix.
8181
"""
82-
SparseMatrixLNK{Tv,Ti}(m::Integer, n::Integer) where {Tv,Ti<:Integer} = SparseMatrixLNK{Tv,Ti}(m,n,0,n,zeros(Ti,n),zeros(Ti,n),zeros(Tv,n))
82+
SparseMatrixLNK{Tv,Ti}(m, n) where {Tv,Ti<:Integer} = SparseMatrixLNK{Tv,Ti}(m,n,0,n,zeros(Ti,n),zeros(Ti,n),zeros(Tv,n))
8383

8484
"""
8585
$(SIGNATURES)
8686
8787
Constructor of empty matrix.
8888
"""
89-
SparseMatrixLNK(valuetype::Type{Tv},indextype::Type{Ti},m::Integer, n::Integer) where {Tv,Ti<:Integer} =SparseMatrixLNK{Tv,Ti}(m,n)
89+
SparseMatrixLNK(valuetype::Type{Tv},indextype::Type{Ti},m, n) where {Tv,Ti<:Integer} =SparseMatrixLNK{Tv,Ti}(m,n)
9090

9191
"""
9292
$(SIGNATURES)
9393
9494
Constructor of empty matrix.
9595
"""
96-
SparseMatrixLNK(valuetype::Type{Tv},m::Integer, n::Integer) where {Tv} =SparseMatrixLNK(Tv,Int,m,n)
96+
SparseMatrixLNK(valuetype::Type{Tv},m, n) where {Tv} =SparseMatrixLNK(Tv,Int,m,n)
9797

9898
"""
9999
$(SIGNATURES)
100100
101101
Constructor of empty matrix.
102102
"""
103-
SparseMatrixLNK(m::Integer, n::Integer) where {Tv} =SparseMatrixLNK(Float64,m,n)
103+
SparseMatrixLNK(m, n) where {Tv} =SparseMatrixLNK(Float64,m,n)
104104

105105

106106
"""
@@ -121,7 +121,7 @@ end
121121

122122

123123

124-
function findindex(lnk::SparseMatrixLNK{Tv,Ti},i::Integer, j::Integer) where {Tv,Ti<:Integer}
124+
function findindex(lnk::SparseMatrixLNK,i,j)
125125
if !((1 <= i <= lnk.m) & (1 <= j <= lnk.n))
126126
throw(BoundsError(lnk, (i,j)))
127127
end
@@ -144,9 +144,8 @@ $(SIGNATURES)
144144
145145
Return value stored for entry or zero if not found
146146
"""
147-
function Base.getindex(lnk::SparseMatrixLNK{Tv,Ti},i::Integer, j::Integer) where {Tv,Ti<:Integer}
148-
149-
k,k0=findindex(lnk,i,j)
147+
function Base.getindex(lnk::SparseMatrixLNK{Tv,Ti},i,j) where {Tv,Ti}
148+
k, k0=findindex(lnk,i,j)
150149
if k==0
151150
return zero(Tv)
152151
else
@@ -155,18 +154,17 @@ function Base.getindex(lnk::SparseMatrixLNK{Tv,Ti},i::Integer, j::Integer) where
155154
end
156155

157156

158-
function addentry!(lnk::SparseMatrixLNK{Tv,Ti},i,j,k,k0) where {Tv,Ti<:Integer}
157+
function addentry!(lnk::SparseMatrixLNK,i,j,k,k0)
159158
# increase number of entries
160159
lnk.nentries+=1
161160
if length(lnk.nzval)<lnk.nentries
162-
newsize=Int64(ceil(5.0*lnk.nentries/4.0))
161+
newsize=Int(ceil(5.0*lnk.nentries/4.0))
163162
resize!(lnk.nzval,newsize)
164163
resize!(lnk.rowval,newsize)
165164
resize!(lnk.colptr,newsize)
166165
end
167166

168167
# Append entry if not found
169-
# lnk.nzval[lnk.nentries]=v
170168
lnk.rowval[lnk.nentries]=i
171169

172170
# Shift the end of the list
@@ -181,13 +179,9 @@ end
181179
"""
182180
$(SIGNATURES)
183181
184-
Update value of existing entry, otherwise extend matrix if _v is nonzero.
182+
Update value of existing entry, otherwise extend matrix if v is nonzero.
185183
"""
186-
function Base.setindex!(lnk::SparseMatrixLNK{Tv,Ti}, _v, _i::Integer, _j::Integer) where {Tv,Ti<:Integer}
187-
v = convert(Tv, _v)
188-
i = convert(Ti, _i)
189-
j = convert(Ti, _j)
190-
184+
function Base.setindex!(lnk::SparseMatrixLNK,v,i,j)
191185
if !((1 <= i <= lnk.m) & (1 <= j <= lnk.n))
192186
throw(BoundsError(lnk, (i,j)))
193187
end
@@ -219,12 +213,7 @@ $(SIGNATURES)
219213
Update element of the matrix with operation `op`.
220214
It assumes that `op(0,0)==0`
221215
"""
222-
function updateindex!(lnk::SparseMatrixLNK{Tv,Ti},op, _v, _i::Integer, _j::Integer) where {Tv,Ti<:Integer}
223-
v = convert(Tv, _v)
224-
i = convert(Ti, _i)
225-
j = convert(Ti, _j)
226-
227-
216+
function updateindex!(lnk::SparseMatrixLNK{Tv,Ti},op, v, i,j) where {Tv,Ti}
228217
# Set the first column entry if it was not yet set.
229218
if lnk.rowval[j]==0 && !iszero(v)
230219
lnk.rowval[j]=i
@@ -284,21 +273,22 @@ struct ColEntry{Tv,Ti<:Integer}
284273
end
285274

286275
# Comparison method for sorting
287-
Base.isless(x::ColEntry{Tv, Ti},y::ColEntry{Tv, Ti}) where {Tv,Ti<:Integer} = (x.rowval<y.rowval)
276+
Base.isless(x::ColEntry, y::ColEntry)= (x.rowval<y.rowval)
288277

289278
"""
290279
$(SIGNATURES)
291280
292281
Add SparseMatrixCSC matrix and [`SparseMatrixLNK`](@ref) lnk, returning a SparseMatrixCSC
293282
"""
294-
function Base.:+(lnk::SparseMatrixLNK{Tv,Ti},csc::SparseMatrixCSC{Tv,Ti})::SparseMatrixCSC{Tv,Ti} where {Tv,Ti<:Integer}
283+
function Base.:+(lnk::SparseMatrixLNK{Tv,Ti},csc::SparseMatrixCSC)::SparseMatrixCSC where {Tv,Ti<:Integer}
295284
@assert(csc.m==lnk.m)
296285
@assert(csc.n==lnk.n)
297286

298287
xnnz=nnz(csc)+nnz(lnk)
299288
colptr=Vector{Ti}(undef,csc.n+1)
300289
rowval=Vector{Ti}(undef,xnnz)
301290
nzval=Vector{Tv}(undef,xnnz)
291+
302292
# Detect the maximum column length of lnk
303293
lnk_maxcol=0
304294
for j=1:csc.n
@@ -314,12 +304,12 @@ function Base.:+(lnk::SparseMatrixLNK{Tv,Ti},csc::SparseMatrixCSC{Tv,Ti})::Spars
314304
# pre-allocate column data
315305
col=[ColEntry{Tv,Ti}(0,0) for i=1:lnk_maxcol]
316306

317-
318307

319308
inz=1 # counts the nonzero entries in the new matrix
320309

321310

322311
in_csc_col(jcsc,j)=(nnz(csc)>zero(Ti)) && (jcsc<csc.colptr[j+1])
312+
323313
in_lnk_col(jlnk,l_lnk_col)=(jlnk<=l_lnk_col)
324314

325315
# loop over all columns
@@ -338,6 +328,8 @@ function Base.:+(lnk::SparseMatrixLNK{Tv,Ti},csc::SparseMatrixCSC{Tv,Ti})::Spars
338328

339329

340330
# jointly sort lnk and csc entries into new matrix data
331+
# this could be replaced in a more transparent manner by joint sorting
332+
341333
colptr[j]=inz
342334
jlnk=one(Ti) # counts the entries in col
343335
jcsc=csc.colptr[j] # counts entries in csc
@@ -372,15 +364,15 @@ function Base.:+(lnk::SparseMatrixLNK{Tv,Ti},csc::SparseMatrixCSC{Tv,Ti})::Spars
372364
SparseMatrixCSC{Tv,Ti}(csc.m,csc.n,colptr,rowval,nzval)
373365
end
374366

375-
Base.:+(csc::SparseMatrixCSC{Tv,Ti},lnk::SparseMatrixLNK{Tv,Ti}) where {Tv,Ti<:Integer}=lnk+csc
367+
Base.:+(csc::SparseMatrixCSC,lnk::SparseMatrixLNK)=lnk+csc
376368

377369
"""
378370
$(SIGNATURES)
379371
380-
Constructor from SparseMatrixCSC.
372+
Constructor from SparseMatrixLNK.
381373
382374
"""
383-
function SparseArrays.SparseMatrixCSC{Tv,Ti}(lnk::SparseMatrixLNK{Tv,Ti}) where {Tv,Ti<:Integer}
375+
function SparseArrays.SparseMatrixCSC(lnk::SparseMatrixLNK)::SparseMatrixCSC
384376
csc=spzeros(lnk.m,lnk.n)
385377
lnk+csc
386378
end

0 commit comments

Comments
 (0)