25
\$\begingroup\$

Inspired by a question (now closed) at Stack Overflow.

Given a square matrix, let its double trace be defined as the sum of the entries from its main diagonal and its anti-diagonal. These are marked with X in the following examples:

X · · X
· X X · 
· X X ·
X · · X
X · · · X
· X · X ·
· · X · ·
· X · X ·
X · · · X

Note that for odd n the central entry, which belongs to both diagonals, is counted only once.

Rules

  • The matrix size can be any positive integer.
  • The matrix will only contain non-negative integers.
  • Any reasonable input format can be used. If the matrix is taken as an array (even a flat one) its size cannot be taken as a separate input.
  • Input and output means are flexible as usual. Programs or functions are allowed. Standard loopholes are forbidden.
  • Shortest wins.

Test cases

5
 -> 5
3 5
4 0
 -> 12
 7 6 10
20 13 44
 5 0 1
 -> 36
4 4 4 4
4 4 4 4
4 4 4 4
4 4 4 4
 -> 32
23 4 21 5
24 7 0 7
14 22 24 16
 4 7 9 12
 -> 97
22 12 10 11 1
 8 9 0 5 17
 5 7 15 4 3
 5 3 7 0 25
 9 15 19 3 21
 -> 85

Inputs in other formats:

[[5]]
[[3,5],[4,0]]
[[7,6,10],[20,13,44],[5,0,1]]
[[4,4,4,4],[4,4,4,4],[4,4,4,4],[4,4,4,4]]
[[23,4,21,5],[24,7,0,7],[14,22,24,16],[4,7,9,12]]
[[22,12,10,11,1],[8,9,0,5,17],[5,7,15,4,3],[5,3,7,0,25],[9,15,19,3,21]]
[5]
[3 5; 4 0]
[7 6 10; 20 13 44; 5 0 1]
[4 4 4 4; 4 4 4 4; 4 4 4 4; 4 4 4 4]
[23 4 21 5; 24 7 0 7; 14 22 24 16; 4 7 9 12]
[22 12 10 11 1; 8 9 0 5 17; 5 7 15 4 3; 5 3 7 0 25; 9 15 19 3 21]
[5]
[3,5,4,0]
[7,6,10,20,13,44,5,0,1]
[4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4]
[23,4,21,5,24,7,0,7,14,22,24,16,4,7,9,12]
[22,12,10,11,1,8,9,0,5,17,5,7,15,4,3,5,3,7,0,25,9,15,19,3,21]
asked Oct 24, 2021 at 10:39
\$\endgroup\$
6
  • \$\begingroup\$ Loosely related: Sum the diagonals, Transposes and diagonals, Generalized matrix trace \$\endgroup\$ Commented Oct 24, 2021 at 10:39
  • \$\begingroup\$ I suggest adding a test case for [[4,4,4,4],[4,4,4,4],[4,4,4,4],[4,4,4,4]]. In J, (+.|.)@= works to create a mask of the traces for most arrays, except for arrays with repeated rows. \$\endgroup\$ Commented Oct 26, 2021 at 0:08
  • \$\begingroup\$ @ConorO'Brien Added \$\endgroup\$ Commented Oct 26, 2021 at 11:29
  • 1
    \$\begingroup\$ @qwr The second one you propose sounds interesting! \$\endgroup\$ Commented Oct 27, 2021 at 9:13
  • 1
    \$\begingroup\$ Related: Output diagonal positions of me squared \$\endgroup\$ Commented Nov 5 at 18:11

19 Answers 19

8
\$\begingroup\$

APL (Dyalog 18.0), (削除) 17 (削除ここまで) 16 bytes

−1 thanks to Vadim Tukaev.

Anonymous lambda.

×ばつ∨∘⌽⍨∘.=⍨⍳≢⍵}

Try it online!

{...} dfn; argument is

⍴⍵ shape (number of rows and columns) in the argument

indices of an array of that size

=/ ̈ equality-reduction of each row-column pair (gives identity matrix)

∨∘⌽⍨ OR with mirrored self (indicates both diagonals)

×ばつ multiply with argument

≢⍸ sum (lit. length of list of indices where each index is repeated as many times as its corresponding number in that)

answered Oct 24, 2021 at 11:13
\$\endgroup\$
2
  • \$\begingroup\$ Minus one byte: {≢⍸⍵×∨∘⌽⍨=/¨⍳⍴⍵} \$\endgroup\$ Commented Jul 14, 2022 at 19:36
  • 1
    \$\begingroup\$ @VadimTukaev Nice. Alternative: ∘.=⍨⍋⍵ \$\endgroup\$ Commented Jul 14, 2022 at 20:37
7
\$\begingroup\$

Jelly, 8 bytes

TżU$Ṭḋ8S

Try it online!

T -- truthy indices of the argument, since every row is non-empty, this is [1 .. len(z)]
 żU$ -- zip with its reverse
 Ṭ -- for each pair of integers, create a list with 1's at those two indices
 ḋ8 -- for each resulting list, take the dot product with the corresponding row vector of the input matrix
 S -- sum the results
answered Oct 24, 2021 at 12:42
\$\endgroup\$
6
\$\begingroup\$

R, 47 bytes

function(m)sum(m[(z=diag(y<-nrow(m)))|z[y:1,]])

Try it online!

answered Oct 24, 2021 at 11:30
\$\endgroup\$
6
\$\begingroup\$

K (ngn/k), 15 bytes

{+//x*|/|:\=#x}

Try it online!

Useful to have an identity matrix primitive.

Explanation:

{+//x*|/|:\=#x}
 =#x / identity matrix with size of x
 |:\ / append reverse 
 |/ / OR both matrices
 x* / multiply by original matrix
 +// / sum all elements
answered Oct 24, 2021 at 13:45
\$\endgroup\$
5
\$\begingroup\$

Python 3.10, 58 bytes

lambda m,i=0:sum(r[i:=i-1]+r[~i]*(~i!=len(m)+i)for r in m)

Attempt This Online!


Python 3, 62 bytes

lambda m:sum(r[i]+r[~i]*(i!=len(m)+~i)for i,r in enumerate(m))

Try it online!

Explanation

lambda m:
 for i,r in enumerate(m) # iterate m with i = row index and r = row
 +r[~i]*(i!=len(m)+~i) # add r[len(m) - i - 1] if the index does not equal i
 r[i] # add r[i]
 sum( ) # sum numbers generated by the loop
answered Oct 25, 2021 at 4:41
\$\endgroup\$
4
  • 1
    \$\begingroup\$ Interestingly this alternative using a set also ties at 62 \$\endgroup\$ Commented Oct 25, 2021 at 6:48
  • \$\begingroup\$ Wouldn't the 2nd program work for python 3.8+. I am not sure which feature requires only python 3.10. \$\endgroup\$ Commented Oct 26, 2021 at 13:10
  • 1
    \$\begingroup\$ @Mohammad, The use of assignment expressions in sequence indexes was only introduced in python 3.10 \$\endgroup\$ Commented Oct 26, 2021 at 13:17
  • 1
    \$\begingroup\$ ato.pxeger.com/run?l=python \$\endgroup\$ Commented Jul 14, 2022 at 10:10
4
\$\begingroup\$

Octave, (削除) 36 (削除ここまで) 34 bytes

This uses logical indexing. As an index we use an identity matrix of the size of the input and or it with it's flipped version. Thanks @LuisMendo for -2 bytes!

@(x)sum(x(flip(e=eye(size(x)))|e))

Try it online!

answered Oct 24, 2021 at 11:30
\$\endgroup\$
0
4
\$\begingroup\$

MATL, (削除) 13 (削除ここまで) 11 bytes

This uses logical indexing. As an index we use an identity matrix of the size of the input and or it with it's flipped version, just like in my Octave answer. Thanks @LuisMendo for -2 bytes:)

tZyXytPY|)s

Explanation

t push input twice to stack
 Zy get size of matrix
 Xy get an identity matrix of that size
 t duplicate the identity matrix
 P flip one of the identty matrices
 Y| logical OR the two identity matrices
 ) perform (logical) indexing to the original matrix
 s compute the sum

Try it online!

answered Oct 24, 2021 at 11:59
\$\endgroup\$
3
\$\begingroup\$

Vyxal, 8 bytes

しろいしかくḂ⋎*∑∑

Try it Online!

 Þしろいしかく # Identity matrix of size...
L # len(input)
 ⋎ # OR with...
 Ḃ # itself reversed
 * # Multiply by input
 ∑∑ # Sum
answered Oct 24, 2021 at 23:28
\$\endgroup\$
2
\$\begingroup\$

Pari/GP, 42 bytes

m->sum(i=1,#m,m[i,i]+m[i,j=#m+1-i]*(i!=j))

Try it online!

answered Oct 24, 2021 at 12:34
\$\endgroup\$
2
\$\begingroup\$

05AB1E, 9 bytes

āDδQÂ~* ̃O

Try it online!

-1 thanks to @KevinCruijssen

Wish to have the identity matrix builtin

g Length
 L Range
 D Dup
 δ Outer product with
 Q Equals?
 Â Bifurcate
 ~ Bitwise OR
 * Multiply with input
 ̃ Flatten
 O Sum
answered Oct 25, 2021 at 7:19
\$\endgroup\$
6
  • \$\begingroup\$ ovs is right, which is why all those other answers are awkwardly long. (Otherwise Jelly would be 4 bytes) \$\endgroup\$ Commented Oct 25, 2021 at 7:45
  • 2
    \$\begingroup\$ (To clarify: the one that doesn't work is the 7-byter at the top. The 10-byter works fine with odd sizes.) \$\endgroup\$ Commented Oct 25, 2021 at 7:56
  • 1
    \$\begingroup\$ @ovs fixed by now \$\endgroup\$ Commented Oct 25, 2021 at 8:43
  • \$\begingroup\$ @Bubbler yeah check now it is the same long looking answer now \$\endgroup\$ Commented Oct 25, 2021 at 8:44
  • \$\begingroup\$ @KevinCruijssen thanks for the 1 byte save! new builtin learned \$\endgroup\$ Commented Oct 25, 2021 at 8:44
2
\$\begingroup\$

Ruby, 64 bytes

->m{(r=0...l=m.size).sum{|y|r.sum{|x|x==y||x-~y==l ?m[y][x]:0}}}

Try it online!

->m{ # lambda taking a 2d array and returning diagonal sum
(r=0...l=m.size) # range 0..length 
.sum{|y|r.sum{|x| # sum the result of passing each coordinates
x==y||x-~y==l ? # if on diagonals 
m[y][x]:0 # take value else 0
answered Oct 25, 2021 at 15:54
\$\endgroup\$
2
\$\begingroup\$

C (gcc), (削除) 81 (削除ここまで) \$\cdots\$ (削除) 71 (削除ここまで) 59 bytes

i;f(m,l)int*m;{for(i=l*l;--i;)*m+=i%~l&&i%~-l?0:m[i];l=*m;}

Try it online!

Saved a bytes thanks to att!!!
Saved 12 bytes thanks to AZTECCO!!!

Inputs a pointer to a flattened square array and the number of rows (because pointers in C carry no length info).
Returns its double trace.

answered Oct 24, 2021 at 13:28
\$\endgroup\$
5
  • \$\begingroup\$ i;s;f(m,l)int*m;{for(s=i=-l;i;)s+=m[i-i*l]-~m[++i*~l];s-=l%2*m[l*l/2];} for 71 \$\endgroup\$ Commented Oct 25, 2021 at 18:08
  • \$\begingroup\$ @att Oh, that's sweet - thanks! :D \$\endgroup\$ Commented Oct 25, 2021 at 18:17
  • \$\begingroup\$ 61 bytes by using my Ruby approach \$\endgroup\$ Commented Oct 25, 2021 at 20:44
  • \$\begingroup\$ 59 Bytes by using *m+=i%~l&&i%~-l?.. \$\endgroup\$ Commented Oct 26, 2021 at 7:55
  • \$\begingroup\$ @AZTECCO Fantastic - thanks! :D \$\endgroup\$ Commented Oct 26, 2021 at 10:39
2
\$\begingroup\$

tinylisp, (削除) 132 (削除ここまで) 109 bytes

(load library
(d L length
(d f(q((M)(s(a(trace M)(trace(reverse M)))(*(odd?(L M))(nth(nth M(/(L M)2))(/(L M)2

Try it online!

-23 bytes thanks to DLosc.

answered Feb 3, 2022 at 16:24
\$\endgroup\$
1
  • \$\begingroup\$ @DLosc Thanks! I must have been thinking about a different problem when I did this. \$\endgroup\$ Commented Feb 3, 2022 at 19:54
2
\$\begingroup\$

Pip -x, (削除) 24 (削除ここまで) (削除) 21 (削除ここまで) 20 bytes

YEY#a$+$+a*HV:y+1+Ry

Attempt This Online! The matrix should be given as a command-line argument in the following form:

[[7;6;10];[20;13;44];[5;0;1]]

Or, verify all test cases.

Explanation

YEY#a$+$+a*HV:y+1+Ry
 #a Size of the matrix
 EY Identity matrix of that size
Y Yank into the y variable
 Ry y reversed (backward identity matrix)
 1+ Add 1 to each value
 y+ Add to y (identity matrix)
 HV: Each value halved (rounded down)
 This creates a matrix where both diagonals are 1's and
 everything else is 0's
 a* Multiply the input matrix itemwise by the 1/0 matrix
 $+ Fold on addition (sum each column)
 $+ Fold on addition (sum that list of sums)
answered Oct 26, 2021 at 17:08
\$\endgroup\$
2
\$\begingroup\$

Japt -mx, 11 bytes

gV +J ́gUhVT

Try it

gV +J ́gUhVT :Implicit map of each row U at 0-based index V in implicit input array
gV :Index V into U
 + :Add
 J : Initially -1
 ́ : Postfix decrement
 g : Index into
 UhV : U with the character at index V replaced with
 T : 0
 :Implicit output of sum of resulting array
answered May 2, 2023 at 9:13
\$\endgroup\$
1
\$\begingroup\$

Charcoal, 18 bytes

IΣEθΣΦι∨=κμ=+κμ⊖Lθ

Try it online! Link is to verbose version of code. Explanation: Filters out elements not on either main diagonal, then takes the sum.

 θ Input matrix
 E Map over rows
 ι Current row
 Φ Filtered where
 κ Row index
 = Equals
 μ Column index
 ∨ Logical Or
 κ Row index
 + Plus
 μ Column index
 = Equals
 θ Input matrix
 L Length
 ⊖ Decremented
 Σ Take the sum
 Σ Take the sum
I Cast to string
 Implicitly print
answered Oct 24, 2021 at 13:32
\$\endgroup\$
1
\$\begingroup\$

Raku, 43 bytes

{sum .kv.flatmap:{@^v[unique $^k,@v-$k-1]}}

Try it online!

A function which takes its input in $_ as a list-of-lists.

  • .kv returns an interleaved sequence of each row's index and the row, ie: 0, the first row, 1, the second row, etc.
  • .flatmap: { ... } passes the key and value to the brace-delimited anonymous function and flattens the return values. That function takes the index of each row in $k and the row itself in @v, each argument declared with a "twigil" $^/@^ on its first lexical appearance.
  • $k and @v - $k - 1 are the indices of the elements of each row that go into the double trace. @v, the row, evaluates to its number of elements in a numerical context (the subtraction operator).
  • unique selects only the unique indices. This eliminates the double index at the center of the matrix, if it has an odd dimension.
  • @v[...] is a slice that returns the row elements at the unique indices.
answered Oct 25, 2021 at 18:54
\$\endgroup\$
1
\$\begingroup\$

Pyth, 18 bytes

s.e+@bk&-hyklb@_bk

Try it online!

Explanation

s.e+@bk&-hyklb@_bk
 .e # Iterate through implicit input with (b = row, k = index)
 @bk # b[k]
 &-hyklb # if ((2 * k) + 1) - len(b) != 0
 + @_bk # plus b[::-1][k]
s # sum results from the iteration

The ((2 * k) + 1) - len(b) is derived from rearranging k - (len(b) - (k + 1))

answered Oct 25, 2021 at 16:45
\$\endgroup\$
1
\$\begingroup\$

JavaScript (ES6), 51 bytes

m=>m.reduce((t,r,i)=>t+r[i]+r.reverse(r[i]=0)[i],0)

Try it online!

Commented

m => // m[] = matrix
m.reduce( // reduce():
 ( t, // t = sum
 r, // r[] = current row
 i // i = row index, used as a column index
 ) => //
 t + r[i] + // add r[i] to t
 r.reverse( // reverse r[] ...
 r[i] = 0 // ... but only once r[i] has been cleared
 // so that it's not counted twice
 )[i], // add r[i] again
 0 // start with t = 0
) // end of reduce()

JavaScript ES2022, 46 bytes

Suggested by @tsh:

m=>m.reduce((t,r,i)=>t+r[i]+r.at(~i,r[i]=0),0)

(Doesn't work on TIO.)

answered Oct 24, 2021 at 11:46
\$\endgroup\$
1
  • \$\begingroup\$ You can give Array.prototype.at a try which is introduced by ES2022: m=>m.reduce((t,r,i)=>t+r[i]+r.at(~i,r[i]=0),0) \$\endgroup\$ Commented Oct 26, 2021 at 2:01

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.