# Antisymmetry of a Matrix

A matrix is antisymmetric, or skew-symmetric, if its transpose equals its negative.

The transpose of a matrix can be obtained by reflecting its elements across the main diagonal. Examples of transpositions can be seen here:

$$\\begin{pmatrix}11&12&13\\21&22&23\end{pmatrix}\rightarrow\begin{pmatrix}11&21\\12&22\\13&23\end{pmatrix}\$$

$$\\begin{pmatrix}11&12&13\\21&22&23\\31&32&33\end{pmatrix}\rightarrow\begin{pmatrix}11&21&31\\12&22&32\\13&23&33\end{pmatrix}\$$

This matrix is antisymmetric because it equals its transpose when multiplied by -1:

$$\\begin{pmatrix}0&2&-1\\-2&0&0\\1&0&0\end{pmatrix}\$$

All antisymmetric matrices exhibit certain characteristics:

• Antisymmetry can only be found on square matrices, because otherwise the matrix and its transpose would be of different dimensions.

• Elements which lie on the main diagonal must equal zero because they do not move and consequently must be their own negatives, and zero is the only number which satisfies $$\x=-x\$$.

• The sum of two antisymmetric matrices is also antisymmetric.

# The Challenge

Given a square, non-empty matrix which contains only integers, check whether it is antisymmetric or not.

# Rules

• This is so the shortest program in bytes wins.

• Input and output can assume whatever forms are most convenient as long as they are self-consistent (including output which is not truthy or falsy, or is truthy for non-antisymmetry and falsy for antisymmetry, etc).

• Assume only valid input will be given.

# Test Cases

In:
1 1 1
1 1 1
1 1 1

Out: False

In:
0 0 1
0 0 0
-1 0 0

Out: True

In:
0 -2
2  0

Out: True

• Speaking of skew-symmetry... That's totally different though since that one is in the CA sense. – null Aug 3 '20 at 0:40
• What type of outputs can be used? Any two consistent values? Any truthy and falsy values? Can we choose falsy for antisymmetric and truthy for symmetric? – Luis Mendo Aug 3 '20 at 9:30
• Will the input ever contain complex numbers? Only contain real numbers? Only integers? – Luis Mendo Aug 3 '20 at 10:20
• @LuisMendo I do believe your first comment is addressed by rule 2, but examples were added anyway. Additionally, only integers will be present (also added). For the record I do want to delete this question but I can't. – golf69 Aug 3 '20 at 20:17
• @user That's Do X Without Y, obviously, and is thus deprecated. – null Aug 4 '20 at 3:55

# APL (Dyalog Unicode), 3 bytes

-≡⍉


Try it online!

This is exactly an APLcart entry on "antisymmetric". Basically it checks if the input's negative - matches ≡ the input's transpose ⍉.

# Python 2, 45 bytes

lambda A:A==[[-x for x in R]for R in zip(*A)]


Try it online!

# R, 23 bytes

function(m)!any(m+t(m))


Try it online!

Checks whether there are any non-zero elements in $$\M+M^T\$$.

# C (gcc), 67 64 bytes

-3 thanks to AZTECCO

i,j;f(m,s)int**m;{for(i=j=0;i=i?:s--;)j|=m[s][--i]+m[i][s];m=j;}


Try it online!

Returns 0 if the matrix is antisymmetric, and a nonzero value otherewise.

# Octave, 19 bytes

@(a)isequal(a',-a);


Try it online!

The semicolon doesn't need to be there, but it outputs the function otherwise, so I'll take the one-byte hit to my score for now.

# Explanation

It's pretty straightforward - it checks to see if the matrix of the transpose is equal to the negative matrix

# Brachylog, 5 bytes

5 bytes seems to be the right length for this (unless you're Jelly). Actually, this would be three bytes if Brachylog implicitly vectorized predicates like negation.

\ṅᵐ²?


Try it online!

### Explanation

\      Transpose
ṅᵐ²   Map negation at depth 2
?  Assert that the result is the same as the input

• How do you program in this language without using something like windows character map or a special keyboard with 5000 keys? – Daniel W. Aug 4 '20 at 19:09
• @DanielW. Good question! I have two methods: copying and pasting from the codepage chart, and using a bookmarklet of Adám's language bar (also available for several other languages). – DLosc Aug 5 '20 at 3:01

# JavaScript (ES6), 42 bytes

Returns false for antisymmetric or true for non-antisymmetric.

m=>m.some((r,y)=>r.some((v,x)=>m[x][y]+v))


Try it online!

# Io, 67 bytes

method(~,~map(i,\,\map(I,V,V+x at(I)at(i)))flatten unique==list(0))


Try it online!

## Explanation

For all a[x][y], it checks whether all a[x][y]+a[y][x]==0.

method(~,                                 // Input x.
~ map(i,\,                            // Map all x's rows (index i):
\ map(I,V,                        //     Foreach the rows (index I):
V+x at(I)at(i)                //         x[i][I] + x[I][i]
)
) flatten                             // Flatten the resulting list
unique                                // Uniquify the list
==list(0)                             // Does this resulting list *only* contain the item 0?
)

• Hi "new contributor" +1~~~ – null Aug 3 '20 at 1:29

# Pyth, 5 bytes

qC_MM


Try it online!

## Explanation

qC_MM
q      : Check if input equals
C     : Transpose of
_MM  : Negated input


# MATL, 5 bytes

!_GX=


Try it online!

## Explanation

!_GX=
// Implicit input on top of stack
!       // Replace top stack element with its transpose
_      // Replace top stack element with its negative
G     // Push input onto stack
X=   // Check for equality


# Charcoal, 10 bytes

⁼θＥθＥθ±§λκ


Try it online! Link is to verbose version of code. Outputs a Charcoal boolean, i.e. - if the matrix is antisymmetric, nothing if not. Explanation:

  Ｅθ        Map over input matrix rows (should be columns, but it's square)
Ｅθ      Map over input matrix rows
§λκ  Cell of transpose
±     Negated
⁼θ          Does matrix equal its negated transpose?


# Wolfram Mathematica, 20, 7 bytes

There is a built-in function for this task:

AntisymmetricMatrixQ

But one can simply write a script with less byte counts:

#==-#ᵀ&

The ᵀ character, as it is displayed in notebooks, stands for transpose. But if you copy this into tio, it won't be recognized because these characters are only supported by Mathematica notebooks.

• I don't think the transpose sign (Unicode: F3C7) should count as one byte? – M. Stern Aug 3 '20 at 18:14
• @M.Stern Maybe I am wrong, but I look at it the same way as ⍉ in the highest voted answer counts as one byte. – polfosol ఠ_ఠ Aug 3 '20 at 18:15
• @polfosolఠ_ఠ APL has a custom code page. The character for Transpose is , which is 3 bytes. Try it online! – att Aug 3 '20 at 18:59

# Julia 1.0, 9 bytes

A->A==-A'


A straightforward anonymous function checking the equality.

Try it online!

# Japt, 5 bytes

eUy®n


Try it

e       compare input with :
Uy       columns of input
®n     with each element negated


Previous version ÕeËËn didn't work, corrected using the ® symbol

# Scala, 32 bytes

l=>l.transpose==l.map(_.map(-1*))


Finally, something that Scala has a builtin for!

The function's pretty straightforward - it compares the transpose of a List[List[Int]](doesn't have to be a List, could be any Iterable) to the negative, found by mapping each list inside l and using - to make it negative.

Try it in Scastie

Closing parens discounted.

Input matrix starts at A2:

• A1: =COUNTA(2:2), gets number of columns (assume square)
• A2: =SUM(ArrayFormula(OFFSET(A2,,,A1,A1)+TRANSPOSE(ArrayFormula(OFFSET(A2,,,A1,A1)))))

That was fun!

## How it Works:

Add the matrix to its negative transpose. If the resulting matrix is all 0's, then the sum of all elements is 0, which means we the two are equal.

Return 0 if equal, some positive number otherwise.

# Pip, 5 bytes

Z_=-_


A function submission; pass a nested list as its argument. Try it online!

### Explanation

Z_     The argument, zipped together
=    Equals
-_  The argument, negated


# gorbitsa-ROM, 8 bytes

r1 R A1 B0 T


This is an awful abuse of rule

Input and output can assume whatever forms are most convenient.

If input takes form of "arr[i][j] arr[j][i]", the problem becomes "is sum = 0?".
This code takes pairs of values and outputs their sum if it's not 0

Thus if you provide matrix as previously mentioned pairs, code will return some value for not-anti-symmetric ones and will not return anything for anti-symmetric ones.

r1 R A1 B0 T
r1           #store first number
T #else output the sum


# Ruby, 40 bytes

->a{a==a.transpose.map{|r|r.map{|c|-c}}}


Try it online!

• I learned a bit of Ruby golfing and fixed it. – Razetime Aug 4 '20 at 13:43
• Probably this is what you're referring to, but Tips for golfing in Ruby is a good resource if you haven't seen it. – Dingus Aug 5 '20 at 0:41

import Data.List
f x=x==transpose(map(map(0-))x)


Try it online!

Function tacking a matrix and checking if input is equal to input mapped to (0-value) and transposed

# Java (JDK), 8987 86 bytes

• -2 bytes thanks to Calculuswhiz!
m->{int i=0,j,r=1;for(;++i<m.length;)for(j=0;++j<i;)r=m[i][j]!=-m[j][i]?0:r;return r;}


Try it online!

Returns 0 for false and 1 for true.

• How about int i=0,j then j=0 in the inner loop? – Calculuswhiz Aug 8 '20 at 1:01
• @Calculuswhiz Thanks! I almost never use commas for assignments and totally forgot about them – user Aug 8 '20 at 15:30
• Oh, I found something better at 76: m->{int l=m.length,i=l*l;while(--i>=0&&m[i%l][i/l]==-m[i/l][i%l]);return i;}. This returns -1 only if antisymmetric, something bigger otherwise. Your original code might also have needed to start at -1's instead of 0's. – Calculuswhiz Aug 8 '20 at 17:19
• @Calculuswhiz That's really cool, you should post your own answer! – user Aug 8 '20 at 17:32

# Husk, 5 bytes

§=T†_


Try it online!

# Jelly, 3 bytes

N⁼Z


Try it online!

Posting before caird coinheringaahing finds this question.

# 05AB1E, 3 bytes

ø(Q


Explanation:

ø    # Zip/transpose the (implicit) input-matrix; swapping rows/columns
(   # Negate each value in this transposed matrix
Q  # And check if it's equal to the (implicit) input-matrix
# (after which the result is output implicitly)