An equation upon 2 matrices which carries out matrix multiplication on rows

47 Views Asked by At

This may not be the best way to start (but please stick with me I'll explain more).

I am relatively inexperienced in linear algebra and unfamiliar with tensors (I beleive that's the right term, an 'n-dimensional array' in programming terms, I'm primarily a programmer).

I need a function (or some alternative method of using my inputs to produce the output) of doing the following:

I have 2 matrices, A, a GxP matrix and B, a GxQ matrix.

$A: \begin{bmatrix} a_{1,1}&a_{1,2}&\dots \\ a_{2,1}&a_{2,2}&\dots \\ a_{3,1}&a_{3,2}&\dots \\ \vdots&\vdots&\ddots \\ \end{bmatrix} $op $B: \begin{bmatrix} b_{1,1}&b_{1,2}&b_{1,3}&\dots \\ b_{2,1}&b_{2,2}&b_{2,3}&\dots \\ b_{3,1}&b_{3,2}&b_{3,3}&\dots \\ \vdots&\vdots&\vdots&\ddots \\ \end{bmatrix} $ = $ C:\begin{bmatrix} \begin{bmatrix} a_1^T\cdot b_1 \end{bmatrix} \begin{bmatrix} a_2^T\cdot b_2 \end{bmatrix} \dots \begin{bmatrix} a_G^T\cdot b_G \end{bmatrix} \end{bmatrix} $

$ = C: \begin{bmatrix}\begin{bmatrix} a_{1,1}\cdot b_{1,1}&a_{1,1}\cdot b_{1,2}&\dots \\ a_{1,2}\cdot b_{1,1}&a_{1,2}\cdot b_{1,2}&\dots \\ \vdots&\vdots&\ddots \\ \end{bmatrix}\dots\end{bmatrix}$

($a_1^T\cdot b_1$ being the matrix multiplication of the column vector $a_1^T$ ($[a_{1,1},a_{1,2},\dots,a_{1,P}]^T$) (transpose ($^T$) of the row vector $a_1$) with the row vector $b_1$ ($[b_{1,1},b_{1,2},\dots,b_{1,Q}]$).

$a_1^T\cdot b_1 = \begin{bmatrix} a_{1,1}b_{1,1}&a_{1,1}b_{1,2}&\dots\\ a_{1,2}b_{1,1}&a_{1,2}b_{1,2}&\dots\\ \vdots&\vdots&\ddots\\ \end{bmatrix} $

Effectively making a 3 dimensional tensor (I beleive this is correct terminology, please correct if I'm wrong) out of layers which equal the matrix multiplication of each of the rows in the 2 original matrices.

I have been stuck on this for quite a while, I am trying to learn more about the topic, but any help would be greatly appreciated.