# [Jelly], 19 bytes LŒ!s€2Ṣ€QḅL_LịFHP€S [Try it online!][TIO-je9yvhnf] ### Alternate version, 15 bytes, postdates challenge LŒ!s€2Ṣ€QœịHP€S Jelly *finally* got n-dimensional array indexing. [Try it online!][TIO-je9z0j2i] ### 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. [Jelly]: https://github.com/DennisMitchell/jelly [TIO-je9yvhnf]: https://tio.run/##y0rNyan8/9/n6CTF4kdNa4we7lwEpAIf7mj1ifd5uLvbzSMAyA/@f7gdSEX@/x/NpRAdbaCjYKJnGqujEA2kdBQMYmN14MLmIMIMSQFQAChhpGcIpPVMIGJmMAEdBUM9I4RBeiZgAYSRhnrGIKMMYcJ6BiDCEiQGNcoQxDYCmWcOVwQ2xRwkbwiXMoHoArOAhhhD5MEGghWBpYxALGOQnYZ6FhB5S6iBUF3GIK4pkv0Qu8AGgnWBpUBcI5AXYgE "Jelly – Try It Online" [TIO-je9z0j2i]: https://tio.run/##TY89DsIwDIV3ThH2yKqTtKU3YGAAMaEoIwvqxsTKwsBWbsDMBRAj6kHKRYLt9G@Jn9@zPyunY11fYty0zfL8u75M935S2bWP7nNfb0nu4/dG5RCjXyjvM60c5EErT0WrLAQ92iU/xWyADAoMIFVwySsGQysEM4HAiTEhESyjcLAh46dir0cha8O8chwSSsk5jpFLW6IIYlMuQBmSyLCyfBNhlfKqB/Zbltt8dj/dEqBsScSt4S@EPw "Jelly – Try It Online"