#MATL, 20 bytes
XJZyXyi:"!J21ドル!*s1$e
###Explanation
This avoids the matrix multiplication by doing it manually, using element-wise multiplication with broadcast followed by vectorized sum. Specifically, to multiply matrices M and N, both of size s×ばつs:
- Transpose
M. Call the resulting matrixP. - Permute the dimensions of
Nsuch thatNis "turned" with a rotation axis along the first dimension, giving an s×ばつs 3D array, sayQ. - Multiply each element of
Ptimes each element ofQ, with implicit broadcast. This means thatPis automatically replicated s times along the third dimension, andQis replicated s times along the second, to make them both s×ばつs×ばつs, before the actual element-wise multiplication takes place. - Sum along the first dimension to yield a ×ばつs×ばつs array.
- Squeeze the leading singleton dimension out, to produce an s×ばつs result.
Commented code:
XJ % take matrix A. Copy to clipboard J
ZyXy % generate identity matrix of the same size
i: % take exponent n. Generate range [1 2 ... n] (possibly empty)
" % for each element in that range
! % transpose original A, or matrix with product accumulated productup to now (this is step 1)
J % paste A
21ドル! % permute dimensions: rotation along first-dimension axis (step 2)
* % element-wise multiplication with broadcast (step 3)
s % sum along first dimension (step 4)
1$e % squeeze out singleton dimension, i.e. the first dimension (step 5)
% end for. Display
#MATL, 20 bytes
XJZyXyi:"!J21ドル!*s1$e
###Explanation
This avoids the matrix multiplication by doing it manually, using element-wise multiplication with broadcast followed by vectorized sum. Specifically, to multiply matrices M and N, both of size s×ばつs:
- Transpose
M. Call the resulting matrixP. - Permute the dimensions of
Nsuch thatNis "turned" with a rotation axis along the first dimension, giving an s×ばつs 3D array, sayQ. - Multiply each element of
Ptimes each element ofQ, with implicit broadcast. This means thatPis automatically replicated s times along the third dimension, andQis replicated s times along the second, to make them both s×ばつs×ばつs, before the actual element-wise multiplication takes place. - Sum along the first dimension to yield a ×ばつs×ばつs array.
- Squeeze the leading singleton dimension out, to produce an s×ばつs result.
Commented code:
XJ % take matrix A. Copy to clipboard J
ZyXy % generate identity matrix of the same size
i: % take exponent n. Generate range [1 2 ... n] (possibly empty)
" % for each element in that range
! % transpose original A, or matrix with accumulated product (this is step 1)
J % paste A
21ドル! % permute dimensions: rotation along first-dimension axis (step 2)
* % element-wise multiplication with broadcast (step 3)
s % sum along first dimension (step 4)
1$e % squeeze out singleton dimension, i.e. the first (step 5)
% end for. Display
#MATL, 20 bytes
XJZyXyi:"!J21ドル!*s1$e
###Explanation
This avoids the matrix multiplication by doing it manually, using element-wise multiplication with broadcast followed by vectorized sum. Specifically, to multiply matrices M and N, both of size s×ばつs:
- Transpose
M. Call the resulting matrixP. - Permute the dimensions of
Nsuch thatNis "turned" with a rotation axis along the first dimension, giving an s×ばつs 3D array, sayQ. - Multiply each element of
Ptimes each element ofQ, with implicit broadcast. This means thatPis automatically replicated s times along the third dimension, andQis replicated s times along the second, to make them both s×ばつs×ばつs, before the actual element-wise multiplication takes place. - Sum along the first dimension to yield a ×ばつs×ばつs array.
- Squeeze the leading singleton dimension out, to produce an s×ばつs result.
Commented code:
XJ % take matrix A. Copy to clipboard J
ZyXy % generate identity matrix of the same size
i: % take exponent n. Generate range [1 2 ... n] (possibly empty)
" % for each element in that range
! % transpose matrix with product accumulated up to now (this is step 1)
J % paste A
21ドル! % permute dimensions: rotation along first-dimension axis (step 2)
* % element-wise multiplication with broadcast (step 3)
s % sum along first dimension (step 4)
1$e % squeeze out singleton dimension, i.e. first dimension (step 5)
% end for. Display
#MATL, 20 bytes
XJZyXyi:"!J21ドル!*s1$e
###Explanation
This avoids the matrix multiplication by doing it manually, using element-wise multiplication with broadcast followed by vectorized sum. Specifically, to multiply matrices M and N, both of size s×ばつs:
- Transpose
M. Call the resulting matrixP. - Permute the dimensions of
Nsuch thatNis "turned" with a rotation axis along the first dimension, giving an s×ばつs 3D array, sayQ. - Multiply each element of
Ptimes each element ofQ, with implicit broadcast. This means thatPis automatically replicated s times along the third dimension, andQis replicated s times along the second, to make them both s×ばつs×ばつs, before the actual element-wise multiplication takes place. - Sum along the first dimension to yield a ×ばつs×ばつs array.
- Squeeze the leading singleton dimension out, to produce an s×ばつs result.
Commented code:
XJ % take matrix A. Copy to clipboard J
ZyXy % generate identity matrix of the same size
i: % take exponent n. Generate range [1 2 ... n] (possibly empty)
" % for each element in that range
! % transpose original A, or matrix with accumulated product (this is step 1)
J % paste A
21ドル! % permute dimensions: rotation along first-dimension axis (step 2)
* % element-wise multiplication with broadcast (step 3)
s % sum along first dimension (step 4)
1$e % squeeze out singleton dimension, i.e. the first (step 5)
% end for. Display
#MATL, 20 bytes
XJZyXyi:"!J21ドル!*s1$e
###Explanation
This avoids the matrix multiplication by doing it manually, using element-wise multiplication with broadcast followed by vectorized sum. Specifically, to multiply matrices M and N, both of size s×ばつs:
- Transpose
M. Call the resulting matrixP. - Permute the dimensions of
Nsuch thatNis "turned" with a rotation axis along the first dimension, giving an s×ばつs 3D array, sayQ. - Multiply each element of
Ptimes each element ofQ, with implicit broadcast. This means thatPis automatically replicated s times along the third dimension, andQis replicated s times along the second, to make them both s×ばつs×ばつs, before the actual element-wise multiplication takes place. - Sum along the first dimension to yield a ×ばつs×ばつs array.
- Squeeze the leading singleton dimension out, to produce an s×ばつs result.
Commented code:
XJ % take matrix A. Copy to clipboard J
ZyXy % generate identity matrix of the same size
i: % take exponent n. Generate range [1 2 ... n] (possibly empty)
" % for each element in that range
! % transpose original A, or matrix with accumulated product (this is step 1)
J % paste A
21ドル! % permute dimensions: rotation along firstdimension axis (step 2)
* % element-wise multiplication with broadcast (step 3)
s % sum along first dimension (step 4)
1$e % squeeze out singleton dimension, i.e. the first (step 5)
% end for. Display
#MATL, 20 bytes
XJZyXyi:"!J21ドル!*s1$e
###Explanation
This avoids the matrix multiplication by doing it manually, using element-wise multiplication with broadcast followed by vectorized sum. Specifically, to multiply matrices M and N, both of size s×ばつs:
- Transpose
M. Call the resulting matrixP. - Permute the dimensions of
Nsuch thatNis "turned" with a rotation axis along the first dimension, giving an s×ばつs 3D array, sayQ. - Multiply each element of
Ptimes each element ofQ, with implicit broadcast. This means thatPis automatically replicated s times along the third dimension, andQis replicated s times along the second, to make them both s×ばつs×ばつs, before the actual element-wise multiplication takes place. - Sum along the first dimension to yield a ×ばつs×ばつs array.
- Squeeze the leading singleton dimension out, to produce an s×ばつs result.
Commented code:
XJ % take matrix A. Copy to clipboard J
ZyXy % generate identity matrix of the same size
i: % take exponent n. Generate range [1 2 ... n] (possibly empty)
" % for each element in that range
! % transpose original A, or matrix with accumulated product (this is step 1)
J % paste A
21ドル! % permute dimensions: rotation along first-dimension axis (step 2)
* % element-wise multiplication with broadcast (step 3)
s % sum along first dimension (step 4)
1$e % squeeze out singleton dimension, i.e. the first (step 5)
% end for. Display
#MATL, 20 bytes
XJZyXyi:"!J21ドル!*s1$e
###Explanation
This avoids the matrix multiplication by doing it manually, by means ofusing element-wise multiplication with broadcast followed by vectorized sum. Specifically, to multiply matrices AM and BN, both of size s×ばつs:
- Transpose
AM. Call the resulting matrixP. - Permute the dimensions of
BNsuch thatBNis "turned" with a rotation axis along the first dimension, giving an s×ばつs 3D array, sayCQ. - Multiply each element of
APtransposed times each element ofCQ, with implicit broadcast. This means thatAPis automatically replicated s times along the third dimension, andCQis replicated s times along the second, to make them both s×ばつs×ばつs, before the actual element-wise multiplication takes place. - Sum along the first dimension to yield a ×ばつs×ばつs array.
- Squeeze the leading singleton dimension out, to produce an s×ばつs result.
Commented code:
XJ % take matrix A. Copy to clipboard J
ZyXy % generate identity matrix of the same size
i: % take exponent n. Generate range [1 2 ... n] (possibly empty)
" % for each element in that range
! % transpose original A, or matrix with accumulated product (this is step 1)
J % paste A
21ドル! % permute dimensions: rotation along first dimension axis (step 2)
* % element-wise multiplication with broadcast (step 3)
s1$es % sum along first dimension (step 4)
1$e % squeeze out singleton dimension, i.e. the first (step 5)
% end for. Display
#MATL, 20 bytes
XJZyXyi:"!J21ドル!*s1$e
###Explanation
This avoids the matrix multiplication doing it manually, by means of element-wise multiplication with broadcast. Specifically, to multiply matrices A and B, both of size s×ばつs:
- Transpose
A. - Permute the dimensions of
Bsuch thatBis "turned" with a rotation axis along the first dimension, giving an s×ばつs 3D array, sayC. - Multiply each element of
Atransposed times each element ofC, with implicit broadcast. This means thatAis automatically replicated s times along the third dimension, andCis replicated s times along the second, to make them both s×ばつs×ばつs before the actual element-wise multiplication - Sum along the first dimension to yield a ×ばつs×ばつs array.
- Squeeze the leading singleton dimension out, to produce an s×ばつs result.
Commented code:
XJ % take matrix A. Copy to clipboard J
ZyXy % generate identity matrix of the same size
i: % take exponent n. Generate range [1 2 ... n] (possibly empty)
" % for each element in that range
! % transpose original A, or matrix with accumulated product
J % paste A
21ドル! % permute dimensions: rotation along first dimension axis
* % element-wise multiplication with broadcast
s1$e % squeeze out singleton dimension
% end for. Display
#MATL, 20 bytes
XJZyXyi:"!J21ドル!*s1$e
###Explanation
This avoids the matrix multiplication by doing it manually, using element-wise multiplication with broadcast followed by vectorized sum. Specifically, to multiply matrices M and N, both of size s×ばつs:
- Transpose
M. Call the resulting matrixP. - Permute the dimensions of
Nsuch thatNis "turned" with a rotation axis along the first dimension, giving an s×ばつs 3D array, sayQ. - Multiply each element of
Ptimes each element ofQ, with implicit broadcast. This means thatPis automatically replicated s times along the third dimension, andQis replicated s times along the second, to make them both s×ばつs×ばつs, before the actual element-wise multiplication takes place. - Sum along the first dimension to yield a ×ばつs×ばつs array.
- Squeeze the leading singleton dimension out, to produce an s×ばつs result.
Commented code:
XJ % take matrix A. Copy to clipboard J
ZyXy % generate identity matrix of the same size
i: % take exponent n. Generate range [1 2 ... n] (possibly empty)
" % for each element in that range
! % transpose original A, or matrix with accumulated product (this is step 1)
J % paste A
21ドル! % permute dimensions: rotation along first dimension axis (step 2)
* % element-wise multiplication with broadcast (step 3)
s % sum along first dimension (step 4)
1$e % squeeze out singleton dimension, i.e. the first (step 5)
% end for. Display