29
\$\begingroup\$

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
\$\endgroup\$
9
  • 2
    \$\begingroup\$ Speaking of skew-symmetry... That's totally different though since that one is in the CA sense. \$\endgroup\$ Commented Aug 3, 2020 at 0:40
  • 3
    \$\begingroup\$ 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? \$\endgroup\$
    – Luis Mendo
    Commented Aug 3, 2020 at 9:30
  • 7
    \$\begingroup\$ Will the input ever contain complex numbers? Only contain real numbers? Only integers? \$\endgroup\$
    – Luis Mendo
    Commented Aug 3, 2020 at 10:20
  • 1
    \$\begingroup\$ @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. \$\endgroup\$
    – golf69
    Commented Aug 3, 2020 at 20:17
  • 1
    \$\begingroup\$ @user That's Do X Without Y, obviously, and is thus deprecated. \$\endgroup\$ Commented Aug 4, 2020 at 3:55

33 Answers 33

14
\$\begingroup\$

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 .

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

Python 2, 45 bytes

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

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ 44 bytes \$\endgroup\$
    – xnor
    Commented Sep 26, 2022 at 22:53
12
\$\begingroup\$

R, 23 bytes

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

Try it online!

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

\$\endgroup\$
7
\$\begingroup\$

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
\$\endgroup\$
2
  • \$\begingroup\$ How do you program in this language without using something like windows character map or a special keyboard with 5000 keys? \$\endgroup\$
    – Daniel W.
    Commented Aug 4, 2020 at 19:09
  • 1
    \$\begingroup\$ @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). \$\endgroup\$
    – DLosc
    Commented Aug 5, 2020 at 3:01
7
\$\begingroup\$

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.

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

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

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

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!

\$\endgroup\$
5
\$\begingroup\$

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?
)
\$\endgroup\$
1
  • \$\begingroup\$ Hi "new contributor" +1~~~ \$\endgroup\$ Commented Aug 3, 2020 at 1:29
4
\$\begingroup\$

Pyth, 5 bytes

qC_MM

Try it online!

Explanation

qC_MM
q      : Check if input equals
 C     : Transpose of
  _MM  : Negated input
\$\endgroup\$
3
\$\begingroup\$

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
\$\endgroup\$
3
\$\begingroup\$

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

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

Charcoal, 10 bytes

⁼θEθE豧λκ

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:

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

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.

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

Julia 1.0, 9 bytes

A->A==-A'

A straightforward anonymous function checking the equality.

Try it online!

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

Husk, 5 bytes

§=T†_

Try it online!

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

Google Sheets, 90 88 41

(Now with lambdas)

Closing parens discounted.

Named Functions (name, args, and formula text all count):

Name args Formula
P a, b a+b
Q a MAP(a,TRANSPOSE(a),P)

Formula:

=SUM(Q(A1:C3))

No, I cannot currently use ADD instead of P as it is not a lambda.

How it Works:

Add the matrix to its 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.

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

Haskell, 40 bytes

(==)<*>foldr(zipWith(:).map(0-))z
z=[]:z

Try it online!

Uses this tip for shorter transpose and the idiom of (==)<*> to check invariance under an operation.

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

Haskell, 49 47 bytes

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

Try it online!

  • saved 2 thanks to @Unrelated String

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

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

Scala, 32 bytes

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

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

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

Ruby 2.7, 40 bytes

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

Try it online!

\$\endgroup\$
4
  • 1
    \$\begingroup\$ I learned a bit of Ruby golfing and fixed it. \$\endgroup\$
    – Razetime
    Commented Aug 4, 2020 at 13:43
  • 1
    \$\begingroup\$ Probably this is what you're referring to, but Tips for golfing in Ruby is a good resource if you haven't seen it. \$\endgroup\$
    – Dingus
    Commented Aug 5, 2020 at 0:41
  • \$\begingroup\$ Not saying much but using _1 for either of the map blocks saves two bytes. \$\endgroup\$
    – south
    Commented Jun 22, 2023 at 6:41
  • \$\begingroup\$ it's on tio which used 2.7 or something. guess i'll just add the version number \$\endgroup\$
    – Razetime
    Commented Jun 26, 2023 at 6:07
1
\$\begingroup\$

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
\$\endgroup\$
1
\$\begingroup\$

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
   R         #read second number
     A1      #add first number
        B0   #if sum==0, jump to the beginning
           T #else output the sum
\$\endgroup\$
1
\$\begingroup\$

Jelly, 3 bytes

N⁼Z

Try it online!

Posting before caird coinheringaahing finds this question.

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

Java (JDK), 89 87 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.

\$\endgroup\$
4
  • 1
    \$\begingroup\$ How about int i=0,j then j=0 in the inner loop? \$\endgroup\$ Commented Aug 8, 2020 at 1:01
  • \$\begingroup\$ @Calculuswhiz Thanks! I almost never use commas for assignments and totally forgot about them \$\endgroup\$
    – user
    Commented Aug 8, 2020 at 15:30
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Aug 8, 2020 at 17:19
  • \$\begingroup\$ @Calculuswhiz That's really cool, you should post your own answer! \$\endgroup\$
    – user
    Commented Aug 8, 2020 at 17:32
1
\$\begingroup\$

Nekomata + -e, 3 bytes

Ť_=

Attempt This Online!

Ť     Transpose
 _    Negate
  =   Check equality
\$\endgroup\$
1
\$\begingroup\$

Vyxal, 3 bytes

∩N⁼

Try it Online!

∩   # Transpose
 N  # Negate
  ⁼ # Equal to original?
\$\endgroup\$
1
\$\begingroup\$

Uiua, 4 bytes

≍¯⊸⍉

Try it!

Does the ¯ negated ⍉ transposition ≍ match the input ()?

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

05AB1E, 3 bytes

ø(Q

Try it online or verify all test cases.

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)
\$\endgroup\$
0
\$\begingroup\$

Factor + math.matrices, 19 bytes

[ dup flip mneg = ]

Try it online!

  • dup make a copy of the input
  • flip transpose it
  • mneg negate it
  • = are they equal?
\$\endgroup\$
0
\$\begingroup\$

Raku, 26 bytes

{none flat $_ »+«[Z] $_}

Try it online!

  • $_ is the input matrix, in the form of a list of lists of integers.
  • [Z] $_ is the transpose of the input matrix.
  • »+« adds the input matrix to its transpose.
  • flat flattens the matrix into a single list of numbers.
  • none converts that list of numbers into a junction which is true if and only if none of the numbers is truthy, that is, nonzero.

The none-junction is returned. It is truthy only if adding the matrix to its transpose results in a matrix containing all zeroes.

\$\endgroup\$

Your Answer

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

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