10
\$\begingroup\$

This challenge is very simple. You are given as input a square matrix, represented in any sane way, and you have to output the dot product of the diagonals of the matrix.

The diagonals in specific are the diagonal running from top-left to bottom-right and from top-right to bottom-left.

Test Cases

[[-1, 1], [-2, 1]]  ->  -3
[[824, -65], [-814, -741]]  ->  549614
[[-1, -8, 4], [4, 0, -5], [-3, 5, 2]]  ->  -10
[[0, -1, 0], [1, 0, 2], [1, 0, 1]]  ->  1
\$\endgroup\$
0

14 Answers 14

5
\$\begingroup\$

Jelly, 8 6 bytes

×UŒDḢS

Try it online! or verify all test cases

How it works

×UŒDḢS  Main link. Argument: M (matrix)

 U      Upend M, i.e., reverse each row.
,       Pair M and upended M.
  ŒD    Yield all diagonals.
    Ḣ   Head; extract the first, main diagonal.
     S  Reduce by sum.
\$\endgroup\$
0
3
\$\begingroup\$

MATL, 8 bytes

t!P!*Xds

Input format is

[-1, -8, 4; 4, 0 -5; -3, 5, 2]

Try it online! Or verify all test cases.

Explanation

t       % Take input matrix implicitly. Duplicate
!P!     % Flip matrix horizontally
*       % Element-wise product
Xd      % Extract main diagonal as a column vector
s       % Sum. Display implicitly
\$\endgroup\$
0
2
\$\begingroup\$

Julia, 25 bytes

~=diag
!x=~x⋅~rotl90(x)

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ rot90, good idea! \$\endgroup\$
    – Luis Mendo
    Jun 11, 2016 at 0:26
2
\$\begingroup\$

Python, 47 bytes

lambda x:sum(r[i]*r[~i]for i,r in enumerate(x))

Test it on Ideone.

\$\endgroup\$
2
\$\begingroup\$

J, 21 19 bytes

[:+/(<0 1)|:(*|."1)

Straight-forward approach.

Saved 2 bytes thanks to @Lynn.

Usage

The input array is shaped using dimensions $ values.

   f =: [:+/(<0 1)|:(*|."1)
   f (2 2 $ _1 1 _2 1)
_3
   f (2 2 $ 824 _65 _814 _741)
549614
   f (3 3 $ _1 _8 4 4 0 _5 _3 5 2)
_10
   f (3 3 $ 0 _1 0 1 0 2 1 0 1)
1

Explanation

[:+/(<0 1)|:(*|."1)    Input: matrix M
              |."1     Reverse each row of M
             *         Multiply element-wise M and the row-reversed M
    (<0 1)|:           Take the diagonal of that matrix
[:+/                   Sum that diagonal and return it=
\$\endgroup\$
1
  • \$\begingroup\$ [:+/(<0 1)|:(*|."1) is 19 bytes \$\endgroup\$
    – Lynn
    Jun 11, 2016 at 14:24
2
\$\begingroup\$

APL (Dyalog), 15 9 bytes

+/1 1⍉⌽×⊢

Try it online!

How?

+/ - sum

1 1⍉ - diagonal of

⌽×⊢ - element wise multiplication of the matrix with it's reverse

\$\endgroup\$
1
\$\begingroup\$

JavaScript (ES6), 45 bytes

a=>a.reduce((r,b,i)=>r+b[i]*b.slice(~i)[0],0)
a=>a.reduce((r,b,i)=>r+b[i]*b[b.length+~i],0)
\$\endgroup\$
1
\$\begingroup\$

R, 26 bytes

sum(diag(A*A[,ncol(A):1]))
\$\endgroup\$
1
\$\begingroup\$

Mathematica, 17 bytes

Tr[#~Reverse~2#]&
\$\endgroup\$
0
\$\begingroup\$

Clojure, 57 bytes

#(apply +(map(fn[i r](*(r i)(nth(reverse r)i)))(range)%))
\$\endgroup\$
0
\$\begingroup\$

Haskell, 80 48 bytes

I liked my previous solution more, but this is much shorter (basically does the same as the Python solution):

f m=sum[r!!i*r!!(length m-i-1)|(i,r)<-zip[0..]m]

Try it online!

\$\endgroup\$
0
\$\begingroup\$

J, 18 Bytes

<:@#{+//.@:(*|."1)

Explaination:

           (     ) | Monadic hook
            *      | Argument times...
             |."1  | The argument mirrored around the y axis
     +//.@:        | Make a list by summing each of the diagonals of the matrix
    {              | Takes element number...
<:@#               | Calculates the correct index (size of the array - 1)
\$\endgroup\$
0
\$\begingroup\$

05AB1E, 5 bytes

í*Å\O

Try it online or verify all test cases.

Explanation:

í        # Reverse each row of the (implicit) input-matrix
         #  i.e. [[-1,-8,4],[4,0,-5],[-3,5,2]] → [[4,-8,-1],[-5,0,4],[2,5,-3]]
 *       # Multiply it with the (implicit) input-matrix (at the same positions)
         #  i.e. [[-1,-8,4],[4,0,-5],[-3,5,2]] and [[4,-8,-1],[-5,0,4],[2,5,-3]]
         #   → [[-4,64,-4],[-20,0,-20],[-6,25,-6]]
  Å\     # Get the diagonal-list from the top-left corner towards the bottom-right
         #  i.e. [[-4,64,-4],[-20,0,-20],[-6,25,-6]] → [-4,0,-6]
    O    # Sum it (and output implicitly)
         #  i.e. [-4,0,-6] → -10
\$\endgroup\$
0
\$\begingroup\$

Kamilalisp, 46 bytes

$(⌿⊙← +)∘∊∘[* #0 [$(⌼ =) #0 #0]∘⍳∘⍴]∘[* #0 :⌽]

Attempt This Online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.