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

21 Answers 21

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 .

| improve this answer | |
\$\endgroup\$
11
\$\begingroup\$

Python 2, 45 bytes

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

Try it online!

| improve this answer | |
\$\endgroup\$
10
\$\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\$.

| improve this answer | |
\$\endgroup\$
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.

| improve this answer | |
\$\endgroup\$
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

| improve this answer | |
\$\endgroup\$
5
\$\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
| improve this answer | |
\$\endgroup\$
  • \$\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. Aug 4 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 Aug 5 at 3:01
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!

| improve this answer | |
\$\endgroup\$
4
\$\begingroup\$

Pyth, 5 bytes

qC_MM

Try it online!

Explanation

qC_MM
q      : Check if input equals
 C     : Transpose of
  _MM  : Negated input
| improve this answer | |
\$\endgroup\$
4
\$\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?
)
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Hi "new contributor" +1~~~ \$\endgroup\$ – null Aug 3 at 1:29
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
| improve this answer | |
\$\endgroup\$
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?
| improve this answer | |
\$\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.

| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ I don't think the transpose sign (Unicode: F3C7) should count as one byte? \$\endgroup\$ – M. Stern Aug 3 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\$ – polfosol ఠ_ఠ Aug 3 at 18:15
  • 4
    \$\begingroup\$ @polfosolఠ_ఠ APL has a custom code page. The character for Transpose is , which is 3 bytes. Try it online! \$\endgroup\$ – att Aug 3 at 18:59
3
\$\begingroup\$

Julia 1.0, 9 bytes

A->A==-A'

A straightforward anonymous function checking the equality.

Try it online!

| improve this answer | |
\$\endgroup\$
2
\$\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

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

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

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

Google Sheets, 90 88

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.

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

Ruby, 40 bytes

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

Try it online!

| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ I learned a bit of Ruby golfing and fixed it. \$\endgroup\$ – Razetime Aug 4 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 Aug 5 at 0:41
1
\$\begingroup\$

Java (JDK), 89 87 bytes

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

Try it online!

I cheated a bit by returning 0 for false and 1 for true instead of the actual boolean/Boolean values.

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

Haskell, 49 bytes

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

Try it online!

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

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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