Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
yurivish opened this issue Jun 13, 2018 · 4 comments
Labels
sparse Sparse arrays

Comments

@yurivish
Copy link

yurivish commented Jun 13, 2018

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.

@yurivish yurivish changed the title Difference between sparse and dense matrix multiplication Sparse matrix multiplication error; difference between sparse and dense matrix multiplication Jun 13, 2018
@stevengj
Copy link
Member

matmul uses promote_op(matprod, eltype(A), eltype(B)), whereas spmatmul does not.

@stevengj
Copy link
Member

stevengj commented Jun 13, 2018

On a related note, mixed-type sparse-matrix operations copy the whole input matrices to promoted types (using type-unstable assignments), which seems wasteful.

@KristofferC
Copy link
Member

For mixed see JuliaLang/julia#24045

@ViralBShah
Copy link
Member

Works now.

@KristofferC KristofferC transferred this issue from JuliaLang/julia Nov 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
sparse Sparse arrays
Projects
None yet
Development

No branches or pull requests

4 participants