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

Add support for kron #180

Open
devon-research opened this issue Jun 16, 2020 · 3 comments
Open

Add support for kron #180

devon-research opened this issue Jun 16, 2020 · 3 comments

Comments

@devon-research
Copy link

Do people think this would be appropriate / useful to include?

And do people have ideas for how to make it efficient?

Here is a rudimentary and inefficient but seemingly functional implementation I used to get something working:

function Base.kron(a::BandedMatrix{T}, b::BandedMatrix{T}) where T
    R = BandedMatrix{T}(undef, (size(a,1) * size(b,1), size(a,2) * size(b,2)), (bandwidth(a,1) * size(b,1) + bandwidth(b,1), bandwidth(a,2) * size(b,2) + bandwidth(b,2)))
    m = 0
    @inbounds for j = 1:size(a,2), l = 1:size(b,2), i = 1:size(a,1)
        aij = a[i,j]
        for k = 1:size(b,1)
            R[m += 1] = aij * b[k,l]
        end
    end
    R
end
@dlfivefifty
Copy link
Member

Yep! Note it currently exists in LazyBandedMatrices.jl, but would be nice here.

Ideally it would support an arbitrary number of matrices

@dlfivefifty
Copy link
Member

Note quick and dirty for more matrices:

kron(A::BandedMatrix, B::BandedMatrix, C::AbstractMatrix...) = kron(kron(A,B), C...)

@dlfivefifty
Copy link
Member

Also, no need to restrict to same type, do:

function Base.kron(a::BandedMatrix{T}, b::BandedMatrix{V}) where {T,V}
   R = BandedMatrix{Base.promote_op(*,T,V)}(undef, (size(a,1) * size(b,1), size(a,2) * size(b,2)), (bandwidth(a,1) * size(b,1) + bandwidth(b,1), bandwidth(a,2) * size(b,2) + bandwidth(b,2)))
   ...
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants