Skip to main content
new test cases
Source Link
Dennis
  • 211.7k
  • 41
  • 380
  • 830
edited body
Source Link
Dennis
  • 211.7k
  • 41
  • 380
  • 830
LŒ!s€2Ṣ€QŒịHP€Ss€2Ṣ€QœịHP€S 

Try it online!Try it online!

LŒ!s€2Ṣ€QŒiHP€Ss€2Ṣ€QœiHP€S Main link. Argument: M (matrix / 2D array) L Take the length, yielding 2n. Œ! Generate all permutations of [1, ..., 2n]. s€2 Split each permutation into pairs. Ṣ€ Sort the pair arrays. Q Unique; deduplicate the array of pair arrays. This avoids dividing by n! at the end. H Halve; yield M, with all of its elements divided by 2. This avoids dividing by 2**n at the end. Œịœị At-index (n-dimensional); take each pair of indices [i, j] and yield M[i][j]. P€ Take the product the results corresponding the same permutation. S Take the sum of the products. 

The 19-byte version works in a similar fashion; it just has to implement Œịœị itself.

LŒ!s€2Ṣ€QŒịHP€S 

Try it online!

LŒ!s€2Ṣ€QŒiHP€S Main link. Argument: M (matrix / 2D array) L Take the length, yielding 2n. Œ! Generate all permutations of [1, ..., 2n]. s€2 Split each permutation into pairs. Ṣ€ Sort the pair arrays. Q Unique; deduplicate the array of pair arrays. This avoids dividing by n! at the end. H Halve; yield M, with all of its elements divided by 2. This avoids dividing by 2**n at the end. Œị At-index (n-dimensional); take each pair of indices [i, j] and yield M[i][j]. P€ Take the product the results corresponding the same permutation. S Take the sum of the products. 

The 19-byte version works in a similar fashion; it just has to implement Œị itself.

LŒ!s€2Ṣ€QœịHP€S 

Try it online!

LŒ!s€2Ṣ€QœiHP€S Main link. Argument: M (matrix / 2D array) L Take the length, yielding 2n. Œ! Generate all permutations of [1, ..., 2n]. s€2 Split each permutation into pairs. Ṣ€ Sort the pair arrays. Q Unique; deduplicate the array of pair arrays. This avoids dividing by n! at the end. H Halve; yield M, with all of its elements divided by 2. This avoids dividing by 2**n at the end. œị At-index (n-dimensional); take each pair of indices [i, j] and yield M[i][j]. P€ Take the product the results corresponding the same permutation. S Take the sum of the products. 

The 19-byte version works in a similar fashion; it just has to implement œị itself.

added 1632 characters in body
Source Link
Dennis
  • 211.7k
  • 41
  • 380
  • 830

How it works

LŒ!s€2Ṣ€QŒiHP€S Main link. Argument: M (matrix / 2D array) L Take the length, yielding 2n. Œ! Generate all permutations of [1, ..., 2n]. s€2 Split each permutation into pairs. Ṣ€ Sort the pair arrays. Q Unique; deduplicate the array of pair arrays. This avoids dividing by n! at the end. H Halve; yield M, with all of its elements divided by 2. This avoids dividing by 2**n at the end. Œị At-index (n-dimensional); take each pair of indices [i, j] and yield M[i][j]. P€ Take the product the results corresponding the same permutation. S Take the sum of the products. 

The 19-byte version works in a similar fashion; it just has to implement Œị itself.

...ḅL_LịFH... Return value: Array of arrays of index pairs. Argument: M L Length; yield 2n. ḅ Convert each pair of indices [i, j] from base 2n to integer, yielding ((2n)i + j). _L Subtract 2n, yielding ((2n)(i - 1) + j). This is necessary because indexing is 1-based in Jelly, so the index pair [1, 1] must map to index 1. F Yield M, flattened. ị Take the indices to the left and get the element at these indices from the array to the right. H Halve; divide all retrieved elements by 2. 

How it works

LŒ!s€2Ṣ€QŒiHP€S Main link. Argument: M (matrix / 2D array) L Take the length, yielding 2n. Œ! Generate all permutations of [1, ..., 2n]. s€2 Split each permutation into pairs. Ṣ€ Sort the pair arrays. Q Unique; deduplicate the array of pair arrays. This avoids dividing by n! at the end. H Halve; yield M, with all of its elements divided by 2. This avoids dividing by 2**n at the end. Œị At-index (n-dimensional); take each pair of indices [i, j] and yield M[i][j]. P€ Take the product the results corresponding the same permutation. S Take the sum of the products. 

The 19-byte version works in a similar fashion; it just has to implement Œị itself.

...ḅL_LịFH... Return value: Array of arrays of index pairs. Argument: M L Length; yield 2n. ḅ Convert each pair of indices [i, j] from base 2n to integer, yielding ((2n)i + j). _L Subtract 2n, yielding ((2n)(i - 1) + j). This is necessary because indexing is 1-based in Jelly, so the index pair [1, 1] must map to index 1. F Yield M, flattened. ị Take the indices to the left and get the element at these indices from the array to the right. H Halve; divide all retrieved elements by 2. 
Source Link
Dennis
  • 211.7k
  • 41
  • 380
  • 830
Loading