Between Matrix{Matrix{Any}} and Matrix{Any}

function ⊗(A::T,B::T) where T<:Array
    [i*B for i in A]
end

a = ⊗(rand(2,2),rand(3,3))
typeof(a)
Matrix{Matrix{Float64}} (alias for Array{Array{Float64, 2}, 2})

I know:

a = kron(rand(2,2),rand(3,3))

But I’m trying to get something like:

reduce(hcat,[collect(1:3) for i in 1:3)

which works on a Vector{Vector{Any}} but not on Matrix{Matrix{Any}}.
I tried to play with function cat() and I failed.
I tried cartesian index and I failed.

I mean, it is how our minds treat a matrix right? we can ‘blockrize’ or ‘flatten’ a matrix with very little effort. Can I do that in Julia?

hcat only works for vectors AFAIK. You can flatten a matrix with A[:], then cat it and finally use reshape to get it back into the shape you want. Maybe an easier way is using something like https://github.com/JuliaArrays/BlockArrays.jl

1 Like

Pardon my English but by “flatten” I mean Matrix{Any}
However I figure something out:

function ⊗(A::T,B::T) where T<:Array
    a = [i*B for i in A] # blocked
    c = reduce(hcat,[vcat(i...) for i in eachcol(a)])# flatten
    a,c
end

A = rand(20,20)
B = rand(30,30)
@btime  ⊗(A,B) 
@btime  kron(A,B) 
1 Like

I don’t think it’s very efficient, but just to show Julia’s many possibilities …

hvcat(size(A),.*(A, [B])...)
reshape(vcat(.*(A, [B])...),size(A).*size(B))

reshape(reduce(vcat, A.*[B]), size(A).*size(B))

I can’t recommend the package TensorCast.jl highly enough for this kind of array index wrangling:

A, B = rand(20,20), rand(30,30)

using TensorCast
@cast C[m⊗i,n⊗j] := A[i,j] * B[m,n]

C == kron(A,B)      # true

In this case there is a built-in kron function available, but in most cases it is more complicated for regular users to find an effective solution.