Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Answer

added 6 characters in body
Source Link
Luis Mendo
  • 106.7k
  • 10
  • 139
  • 382

#MATL, 20 bytes

XJZyXyi:"!J21ドル!*s1$e

Try it online!

###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:

  1. Transpose M. Call the resulting matrix P.
  2. Permute the dimensions of N such that N is "turned" with a rotation axis along the first dimension, giving an s×ばつs 3D array, say Q.
  3. Multiply each element of P times each element of Q, with implicit broadcast. This means that P is automatically replicated s times along the third dimension, and Q is replicated s times along the second, to make them both s×ばつs×ばつs, before the actual element-wise multiplication takes place.
  4. Sum along the first dimension to yield a ×ばつs×ばつs array.
  5. 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

Try it online!

###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:

  1. Transpose M. Call the resulting matrix P.
  2. Permute the dimensions of N such that N is "turned" with a rotation axis along the first dimension, giving an s×ばつs 3D array, say Q.
  3. Multiply each element of P times each element of Q, with implicit broadcast. This means that P is automatically replicated s times along the third dimension, and Q is replicated s times along the second, to make them both s×ばつs×ばつs, before the actual element-wise multiplication takes place.
  4. Sum along the first dimension to yield a ×ばつs×ばつs array.
  5. 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

Try it online!

###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:

  1. Transpose M. Call the resulting matrix P.
  2. Permute the dimensions of N such that N is "turned" with a rotation axis along the first dimension, giving an s×ばつs 3D array, say Q.
  3. Multiply each element of P times each element of Q, with implicit broadcast. This means that P is automatically replicated s times along the third dimension, and Q is replicated s times along the second, to make them both s×ばつs×ばつs, before the actual element-wise multiplication takes place.
  4. Sum along the first dimension to yield a ×ばつs×ばつs array.
  5. 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
edited body
Source Link
Luis Mendo
  • 106.7k
  • 10
  • 139
  • 382

#MATL, 20 bytes

XJZyXyi:"!J21ドル!*s1$e

Try it online!

###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:

  1. Transpose M. Call the resulting matrix P.
  2. Permute the dimensions of N such that N is "turned" with a rotation axis along the first dimension, giving an s×ばつs 3D array, say Q.
  3. Multiply each element of P times each element of Q, with implicit broadcast. This means that P is automatically replicated s times along the third dimension, and Q is replicated s times along the second, to make them both s×ばつs×ばつs, before the actual element-wise multiplication takes place.
  4. Sum along the first dimension to yield a ×ばつs×ばつs array.
  5. 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

Try it online!

###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:

  1. Transpose M. Call the resulting matrix P.
  2. Permute the dimensions of N such that N is "turned" with a rotation axis along the first dimension, giving an s×ばつs 3D array, say Q.
  3. Multiply each element of P times each element of Q, with implicit broadcast. This means that P is automatically replicated s times along the third dimension, and Q is replicated s times along the second, to make them both s×ばつs×ばつs, before the actual element-wise multiplication takes place.
  4. Sum along the first dimension to yield a ×ばつs×ばつs array.
  5. 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

Try it online!

###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:

  1. Transpose M. Call the resulting matrix P.
  2. Permute the dimensions of N such that N is "turned" with a rotation axis along the first dimension, giving an s×ばつs 3D array, say Q.
  3. Multiply each element of P times each element of Q, with implicit broadcast. This means that P is automatically replicated s times along the third dimension, and Q is replicated s times along the second, to make them both s×ばつs×ばつs, before the actual element-wise multiplication takes place.
  4. Sum along the first dimension to yield a ×ばつs×ばつs array.
  5. 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
added 624 characters in body
Source Link
Luis Mendo
  • 106.7k
  • 10
  • 139
  • 382

#MATL, 20 bytes

XJZyXyi:"!J21ドル!*s1$e

Try it online!

###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:

  1. Transpose AM. Call the resulting matrix P.
  2. Permute the dimensions of BN such that BN is "turned" with a rotation axis along the first dimension, giving an s×ばつs 3D array, say CQ.
  3. Multiply each element of AP transposed times each element of CQ, with implicit broadcast. This means that AP is automatically replicated s times along the third dimension, and CQ is replicated s times along the second, to make them both s×ばつs×ばつs, before the actual element-wise multiplication takes place.
  4. Sum along the first dimension to yield a ×ばつs×ばつs array.
  5. 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

Try it online!

###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:

  1. Transpose A.
  2. Permute the dimensions of B such that B is "turned" with a rotation axis along the first dimension, giving an s×ばつs 3D array, say C.
  3. Multiply each element of A transposed times each element of C, with implicit broadcast. This means that A is automatically replicated s times along the third dimension, and C is replicated s times along the second, to make them both s×ばつs×ばつs before the actual element-wise multiplication
  4. Sum along the first dimension to yield a ×ばつs×ばつs array.
  5. 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

Try it online!

###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:

  1. Transpose M. Call the resulting matrix P.
  2. Permute the dimensions of N such that N is "turned" with a rotation axis along the first dimension, giving an s×ばつs 3D array, say Q.
  3. Multiply each element of P times each element of Q, with implicit broadcast. This means that P is automatically replicated s times along the third dimension, and Q is replicated s times along the second, to make them both s×ばつs×ばつs, before the actual element-wise multiplication takes place.
  4. Sum along the first dimension to yield a ×ばつs×ばつs array.
  5. 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
added 624 characters in body
Source Link
Luis Mendo
  • 106.7k
  • 10
  • 139
  • 382
Loading
added 624 characters in body
Source Link
Luis Mendo
  • 106.7k
  • 10
  • 139
  • 382
Loading
Source Link
Luis Mendo
  • 106.7k
  • 10
  • 139
  • 382
Loading

AltStyle によって変換されたページ (->オリジナル) /