Search code examples
juliasparse-matrixdiagonal

Julia: Diagonal times Sparse Matrix


A = sparse([4 0 0; 0 0 0; 4 0 0])
D = Diagonal([1;3;4])
D*A

Gives the error that there is no method * for performing this operation. Is there a simple way for performing the operation?


Solution

  • With convert:

    julia> convert(SparseMatrixCSC{Int64,Int64},D)*A
    3x3 sparse matrix with 2 Int64 entries:
        [1, 1]  =  4
        [3, 1]  =  16
    

    General solution, from the error message:

    julia> f(a,b)
    ERROR: MethodError: `f` has no method matching f(::T1, ::T2)
    

    Do:

    julia> f(convert(T2,a),b)