Skip to content

Commit

Permalink
Merge branch 'main' into dk/matmul
Browse files Browse the repository at this point in the history
  • Loading branch information
dkarrasch authored May 8, 2024
2 parents ea7c7e3 + 3b30333 commit 24b2cf8
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 11 deletions.
28 changes: 26 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,38 @@ jobs:
- run: julia --color=yes .ci/test_and_change_uuid.jl
- uses: julia-actions/julia-buildpkg@v1
- uses: julia-actions/julia-runtest@v1
env:
SPARSEARRAYS_AQUA_TEST: true
- uses: julia-actions/julia-processcoverage@v1
- uses: codecov/codecov-action@v4
with:
file: lcov.info
token: ${{ secrets.CODECOV_TOKEN }}
fail_ci_if_error: true
aqua-test:
runs-on: ${{ matrix.os }}
timeout-minutes: 60
permissions: # needed to allow julia-actions/cache to proactively delete old caches that it has created
actions: write
contents: read
strategy:
matrix:
julia-version:
- 'nightly'
os:
- ubuntu-latest
julia-arch:
- x64
steps:
- uses: actions/checkout@v4
- uses: julia-actions/setup-julia@v2
with:
version: ${{ matrix.julia-version }}
arch: ${{ matrix.julia-arch }}
- uses: julia-actions/cache@v1
- run: julia --color=yes .ci/test_and_change_uuid.jl
- uses: julia-actions/julia-buildpkg@v1
- uses: julia-actions/julia-runtest@v1
env:
SPARSEARRAYS_AQUA_TEST: true
docs:
runs-on: ubuntu-latest
steps:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
Manifest.toml
Manifest-v*.*.toml
docs/build
2 changes: 1 addition & 1 deletion src/SparseArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import Base: +, -, *, \, /, ==, zero
import LinearAlgebra: mul!, ldiv!, rdiv!, cholesky, adjoint!, diag, eigen, dot,
issymmetric, istril, istriu, lu, tr, transpose!, tril!, triu!, isbanded,
cond, diagm, factorize, ishermitian, norm, opnorm, lmul!, rmul!, tril, triu,
matprod_dest, generic_matvecmul!, generic_matmatmul!
matprod_dest, generic_matvecmul!, generic_matmatmul!, copytrito!

import Base: adjoint, argmin, argmax, Array, broadcast, circshift!, complex, Complex,
conj, conj!, convert, copy, copy!, copyto!, count, diff, findall, findmax, findmin,
Expand Down
25 changes: 25 additions & 0 deletions src/sparsematrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,12 @@ SparseMatrixCSC(m, n, colptr::ReadOnly, rowval::ReadOnly, nzval::Vector) =

"""
SparseMatrixCSC{Tv,Ti}(::UndefInitializer, m::Integer, n::Integer)
SparseMatrixCSC{Tv,Ti}(::UndefInitializer, (m,n)::NTuple{2,Integer})
Creates an empty sparse matrix with element type `Tv` and integer type `Ti` of size `m × n`.
"""
SparseMatrixCSC{Tv,Ti}(::UndefInitializer, m::Integer, n::Integer) where {Tv, Ti} = spzeros(Tv, Ti, m, n)
SparseMatrixCSC{Tv,Ti}(::UndefInitializer, mn::NTuple{2,Integer}) where {Tv, Ti} = spzeros(Tv, Ti, mn...)

"""
FixedSparseCSC{Tv,Ti<:Integer} <: AbstractSparseMatrixCSC{Tv,Ti}
Expand Down Expand Up @@ -4538,3 +4540,26 @@ function _reverse!(A::SparseMatrixCSC, dims::Tuple{Integer,Integer})
dims == (1,2) || dims == (2,1) || throw(ArgumentError("invalid dimension $dims in reverse"))
_reverse!(A, :)
end

function copytrito!(M::AbstractMatrix, S::AbstractSparseMatrixCSC, uplo::Char)
Base.require_one_based_indexing(M, S)
if !(uplo == 'U' || uplo == 'L')
throw(ArgumentError(lazy"uplo argument must be 'U' (upper) or 'L' (lower), got '$uplo'"))
end
m,n = size(S)
m1,n1 = size(M)
(m1 < m || n1 < n) && throw(DimensionMismatch("dest of size ($m1,$n1) should have at least the same number of rows and columns than src of size ($m,$n)"))

rv = rowvals(S)
nz = nonzeros(S)
for col in axes(S,2)
trirange = uplo == 'U' ? (1:min(col, size(S,1))) : (col:size(S,1))
fill!(view(M, trirange, col), zero(eltype(S)))
for i in nzrange(S, col)
row = rv[i]
(uplo == 'U' && row <= col) || (uplo == 'L' && row >= col) || continue
M[row, col] = nz[i]
end
end
return M
end
1 change: 1 addition & 0 deletions src/sparsevector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ SparseVector(n::Integer, nzind::Vector{Ti}, nzval::Vector{Tv}) where {Tv,Ti} =
SparseVector{Tv,Ti}(n, nzind, nzval)

SparseVector{Tv, Ti}(::UndefInitializer, n::Integer) where {Tv, Ti} = SparseVector{Tv, Ti}(n, Ti[], Tv[])
SparseVector{Tv, Ti}(::UndefInitializer, (n,)::Tuple{Integer}) where {Tv, Ti} = SparseVector{Tv, Ti}(n, Ti[], Tv[])

"""
FixedSparseVector{Tv,Ti<:Integer} <: AbstractCompressedVector{Tv,Ti}
Expand Down
11 changes: 7 additions & 4 deletions test/sparsematrix_constructors_indexing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,13 @@ end
@test sparse([1, 1, 2, 2, 2], [1, 2, 1, 2, 2], -1.0, 2, 2, *) == sparse([1, 1, 2, 2], [1, 2, 1, 2], [-1.0, -1.0, -1.0, 1.0], 2, 2)
@test sparse(sparse(Int32.(1:5), Int32.(1:5), trues(5))') isa SparseMatrixCSC{Bool,Int32}
# undef initializer
m = SparseMatrixCSC{Float32, Int16}(undef, 3, 4)
@test size(m) == (3, 4)
@test eltype(m) === Float32
@test m == spzeros(3, 4)
sz = (3, 4)
for m in (SparseMatrixCSC{Float32, Int16}(undef, sz...), SparseMatrixCSC{Float32, Int16}(undef, sz),
similar(SparseMatrixCSC{Float32, Int16}, sz))
@test size(m) == sz
@test eltype(m) === Float32
@test m == spzeros(sz...)
end
end

@testset "spzeros for pattern creation (structural zeros)" begin
Expand Down
25 changes: 25 additions & 0 deletions test/sparsematrix_ops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -601,4 +601,29 @@ end
end
end

@testset "copytrito!" begin
S = sparse([1,2,2,2,3], [1,1,2,2,4], [5, -19, 73, 12, -7])
M = fill(Inf, size(S))
copytrito!(M, S, 'U')
for col in axes(S, 2)
for row in 1:min(col, size(S,1))
@test M[row, col] == S[row, col]
end
for row in min(col, size(S,1))+1:size(S,1)
@test isinf(M[row, col])
end
end
M .= Inf
copytrito!(M, S, 'L')
for col in axes(S, 2)
for row in 1:col-1
@test isinf(M[row, col])
end
for row in col:size(S, 1)
@test M[row, col] == S[row, col]
end
end
@test_throws ArgumentError copytrito!(M, S, 'M')
end

end # module
12 changes: 8 additions & 4 deletions test/sparsevector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,14 @@ end
end

@testset "Undef initializer" begin
v = SparseVector{Float32, Int16}(undef, 4)
@test size(v) == (4, )
@test eltype(v) === Float32
@test v == spzeros(Float32, 4)
sz = (4,)
for v in (SparseVector{Float32, Int16}(undef, sz),
SparseVector{Float32, Int16}(undef, sz...),
similar(SparseVector{Float32, Int16}, sz))
@test size(v) == sz
@test eltype(v) === Float32
@test v == spzeros(Float32, sz...)
end
end
end
### Element access
Expand Down

0 comments on commit 24b2cf8

Please sign in to comment.