SparseMatricesCSR.jl icon indicating copy to clipboard operation
SparseMatricesCSR.jl copied to clipboard

Convenience functions like `spdiagm`

Open vchuravy opened this issue 2 years ago • 1 comments

Would be lovely to have so that one could more easily experiment across CSC/CSR

vchuravy avatar Feb 28 '23 06:02 vchuravy

spdiagm(kv::Pair{<:Integer,<:AbstractVector}...) = _spdiagm(nothing, kv...)
spdiagm(m::Integer, n::Integer, kv::Pair{<:Integer,<:AbstractVector}...) = _spdiagm((Int(m),Int(n)), kv...)
spdiagm(v::AbstractVector) = _spdiagm(nothing, 0 => v)
spdiagm(m::Integer, n::Integer, v::AbstractVector) = _spdiagm((Int(m), Int(n)), 0 => v)

function _spdiagm(size, kv::Pair{<:Integer,<:AbstractVector}...)
    I, J, V, mmax, nmax = SparseArrays.spdiagm_internal(kv...)
    mnmax = max(mmax, nmax)
    m, n = something(size, (mnmax,mnmax))
    (m ≥ mmax && n ≥ nmax) || throw(DimensionMismatch("invalid size=$size"))
    return sparsecsr(I, J, V, m, n)
end

Seems to be okay

vchuravy avatar Feb 28 '23 06:02 vchuravy