Julia matrix operation

Viewed 91

I am looking for a matrix operation. But not sure if there is an existing operation for it.

Ex:P=[1 2 ; 3 4] and Q=[5 6 ; 7 8]

[P ; Q] @ [P ; Q] => [P*P ; P*Q ; Q*P ; Q*Q]

@ is the operation that I am looking for. Thanks!

1 Answers

You can just define your custom operator such as:

function ⊗(a::Matrix,b::Matrix)
    h1 = Int(size(a,1)/2)
    P1 = @view a[1:h1,:]
    Q1 = @view a[h1+1:end,:]
    h2 = Int(size(b,1)/2)
    P2 = @view b[1:h2,:]
    Q2 = @view b[h2+1:end,:]
    [P1*P2 ; P1*Q2 ; Q1*P2 ; Q1*Q2]
end

And now use it!

julia> [P ; Q] ⊗ [P ; Q] == [P*P ; P*Q ; Q*P ; Q*Q]
true

Perhaps you need to add checking the sizes etc. You might also want to have and additional operator function ⊗(a::Tuple{Matrix,Matrix},b::Tuple{Matrix,Matrix}) so you do not need to merge P and Q matrices and then later decomposing them.

Related