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]
19 Answers 19
APL (Dyalog 18.0), (削除) 17 (削除ここまで) 16 bytes
−1 thanks to Vadim Tukaev.
Anonymous lambda.
×ばつ∨∘⌽⍨∘.=⍨⍳≢⍵}
{...} 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)
-
\$\begingroup\$ Minus one byte: {≢⍸⍵×∨∘⌽⍨=/¨⍳⍴⍵} \$\endgroup\$Vadim Tukaev– Vadim Tukaev2022年07月14日 19:36:50 +00:00Commented Jul 14, 2022 at 19:36
-
1\$\begingroup\$ @VadimTukaev Nice. Alternative:
∘.=⍨⍋⍵\$\endgroup\$Adám– Adám2022年07月14日 20:37:53 +00:00Commented Jul 14, 2022 at 20:37
Jelly, 8 bytes
TżU$Ṭḋ8S
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
K (ngn/k), 15 bytes
{+//x*|/|:\=#x}
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
Python 3.10, 58 bytes
lambda m,i=0:sum(r[i:=i-1]+r[~i]*(~i!=len(m)+i)for r in m)
Python 3, 62 bytes
lambda m:sum(r[i]+r[~i]*(i!=len(m)+~i)for i,r in enumerate(m))
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
-
1\$\begingroup\$ Interestingly this alternative using a set also ties at 62 \$\endgroup\$xnor– xnor2021年10月25日 06:48:54 +00:00Commented 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\$Mohammad– Mohammad2021年10月26日 13:10:20 +00:00Commented 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\$Mukundan314– Mukundan3142021年10月26日 13:17:34 +00:00Commented Oct 26, 2021 at 13:17
-
1\$\begingroup\$ ato.pxeger.com/run?l=python \$\endgroup\$emanresu A– emanresu A2022年07月14日 10:10:23 +00:00Commented Jul 14, 2022 at 10:10
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))
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
Vyxal, 8 bytes
LÞ□しろいしかくḂ⋎*∑∑
Þ□しろいしかく # Identity matrix of size...
L # len(input)
⋎ # OR with...
Ḃ # itself reversed
* # Multiply by input
∑∑ # Sum
05AB1E, 9 bytes
āDδQÂ~* ̃O
-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
-
\$\begingroup\$ ovs is right, which is why all those other answers are awkwardly long. (Otherwise Jelly would be 4 bytes) \$\endgroup\$Bubbler– Bubbler2021年10月25日 07:45:51 +00:00Commented 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\$Bubbler– Bubbler2021年10月25日 07:56:57 +00:00Commented Oct 25, 2021 at 7:56
-
1\$\begingroup\$ @ovs fixed by now \$\endgroup\$avarice– avarice2021年10月25日 08:43:49 +00:00Commented Oct 25, 2021 at 8:43
-
\$\begingroup\$ @Bubbler yeah check now it is the same long looking answer now \$\endgroup\$avarice– avarice2021年10月25日 08:44:17 +00:00Commented Oct 25, 2021 at 8:44
-
\$\begingroup\$ @KevinCruijssen thanks for the 1 byte save! new builtin learned \$\endgroup\$avarice– avarice2021年10月25日 08:44:47 +00:00Commented Oct 25, 2021 at 8:44
Ruby, 64 bytes
->m{(r=0...l=m.size).sum{|y|r.sum{|x|x==y||x-~y==l ?m[y][x]:0}}}
->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
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;}
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.
-
\$\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\$att– att2021年10月25日 18:08:38 +00:00Commented Oct 25, 2021 at 18:08 -
\$\begingroup\$ @att Oh, that's sweet - thanks! :D \$\endgroup\$Noodle9– Noodle92021年10月25日 18:17:39 +00:00Commented Oct 25, 2021 at 18:17
-
-
\$\begingroup\$ 59 Bytes by using
*m+=i%~l&&i%~-l?..\$\endgroup\$AZTECCO– AZTECCO2021年10月26日 07:55:28 +00:00Commented Oct 26, 2021 at 7:55 -
\$\begingroup\$ @AZTECCO Fantastic - thanks! :D \$\endgroup\$Noodle9– Noodle92021年10月26日 10:39:05 +00:00Commented Oct 26, 2021 at 10:39
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
-23 bytes thanks to DLosc.
-
\$\begingroup\$ @DLosc Thanks! I must have been thinking about a different problem when I did this. \$\endgroup\$Giuseppe– Giuseppe2022年02月03日 19:54:59 +00:00Commented Feb 3, 2022 at 19:54
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]]
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)
Japt -mx, 11 bytes
gV +J ́gUhVT
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
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
Raku, 43 bytes
{sum .kv.flatmap:{@^v[unique $^k,@v-$k-1]}}
A function which takes its input in $_ as a list-of-lists.
.kvreturns 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$kand the row itself in@v, each argument declared with a "twigil"$^/@^on its first lexical appearance.$kand@v - $k - 1are 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).uniqueselects 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.
Pyth, 18 bytes
s.e+@bk&-hyklb@_bk
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))
JavaScript (ES6), 51 bytes
m=>m.reduce((t,r,i)=>t+r[i]+r.reverse(r[i]=0)[i],0)
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.)
-
\$\begingroup\$ You can give
Array.prototype.ata try which is introduced by ES2022:m=>m.reduce((t,r,i)=>t+r[i]+r.at(~i,r[i]=0),0)\$\endgroup\$tsh– tsh2021年10月26日 02:01:31 +00:00Commented Oct 26, 2021 at 2:01
[[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\$