Matrix multiplication in summation form

How to write an explicit one-line code for matrix multiplication A*B, each of size n by n, for example using the summation definition of matrix multiplication (here, eq 14)?

I know how to do it for matrix-vector multiplication: if A is n by n and x is n by 1, then

A*x =sum(A[i,j]*x[j]) for j in 1:n)

A = [1 2; 3 4]
2×2 Matrix{Int64}:
 1  2
 3  4
x = [1 2 3; 1 2 3]
2×3 Matrix{Int64}:
 1  2  3
 1  2  3
[sum(A[i, :] .* x[:, j]) for i in 1:size(A, 1), j in 1:size(x, 2)]
2×3 Matrix{Int64}:
 3   6   9
 7  14  21

Are you looking for something like above !?
where, i lops over each row of A and j loops over each column of B.

Maybe tullio.jl is something worth checking out:

I’ve never used it personally but it seems quite powerful and fast.


I think you just solved a homework problem for the OP.


Just in case someone stumbles onto this answer: the performance can be improved a lot by avoiding copies in the slices and dot product. But of course the best implementation in most cases will be built-in multiplication

Thanks. I have thought of this but it did not help me because I wanted use it as efficient way to incorporate matrices with sparse structures.

This post was temporarily hidden by the community for possibly being off-topic, inappropriate, or spammy.

What you originally posted sounded very much like a request for solving a homework problem. It would have sounded less like that if you had provided some context/motivation for the request or shown what you had already tried.

Yes, you are right :slight_smile:
I was just solving the problem by keeping in mind the constraints posted in OP (using summation and element-wise multiplication) !!