Skip to content

Sparse matrix multiplication error; difference between sparse and dense matrix multiplication #533

@yurivish

Description

@yurivish
julia> a = ones(Bool, 3, 3);

julia> b = ones(Bool, 3, 3);

julia> a * b
3×3 Array{Int64,2}:
 3  3  3
 3  3  3
 3  3  3

julia> using SparseArrays

julia> sparse(a) * sparse(b)
ERROR: InexactError: Bool(Bool, 2)
Stacktrace:
 [1] Type at ./float.jl:73 [inlined]
 [2] convert at ./number.jl:7 [inlined]
 [3] setindex! at ./array.jl:745 [inlined]
 [4] #spmatmul#60(::Symbol, ::Function, ::SparseMatrixCSC{Bool,Int64}, ::SparseMatrixCSC{Bool,Int64}) at /Users/yurivish/Documents/julia/usr/share/julia/stdlib/v0.7/SparseArrays/src/linalg.jl:186
 [5] spmatmul at /Users/yurivish/Documents/julia/usr/share/julia/stdlib/v0.7/SparseArrays/src/linalg.jl:151 [inlined]
 [6] *(::SparseMatrixCSC{Bool,Int64}, ::SparseMatrixCSC{Bool,Int64}) at /Users/yurivish/Documents/julia/usr/share/julia/stdlib/v0.7/SparseArrays/src/linalg.jl:141
 [7] top-level scope at none:0

I would have expected sparse and dense matrix multiplication to work the same way, but instead the sparse version seems to require the resulting array to be of the same type as the input arrays.

I believe the behavior on dense matrices is correct; true + true + true == 3.

Metadata

Metadata

Assignees

No one assigned

    Labels

    sparseSparse arrays

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions