New answers tagged

2

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,...


1

Java (JDK), 89 bytes m->{for(int i=0;++i<m.length;)for(int 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.


0

Ruby, 91 bytes ->a{[*1..a.length-1].map{|n|("%0#{a.length}b"%2**n).reverse.split(//).map(&:to_i)}.push(a)} Try it online!


0

Google Sheets, 52 Closing Parens discounted. Input cells is Row 1, starting in column B. A2 - =COUNTA(1:1). Rules say that we can take this as input too, so I have discounted this as well. (Our "k") A3 - =ArrayFormula(IFERROR(0^MOD(SEQUENCE(A2-1,A2)-1,A2+1))) The output matrix starts in B1. How it works Since this is a spreadsheet, the input ...


0

R, 179 bytes (or 175 bytes without labelling 'T' and 'L') function(m,n,t,z=function(l,t){while(t>1){F=c(F,g<-l%/%t);l=l-g;t=t-1};list(l=l,f=F[F>0])}) list(T=z(m,b<-order(sapply(1:t,function(f)z(m,f)$l*z(n,t%/%f)$l))[1])$f,L=z(n,t%/%b)$f) Try it online! Outputs a list of ['T' = horizontal folds from top, 'L' = vertical folds from left]. How does ...


0

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


1

05AB1E, 7 bytes āDδQ`\) Outputs reversed in both dimensions. Try it online or verify all test cases. Explanation: ā # Push a list in the range [1,length] (without popping the implicit input-list) D # Duplicate it δ # Apply double-vectorized: Q # Check if it's equal # (this results in an L by L matrix filled with 0s, ...


3

Julia 1.0, 9 bytes A->A==-A' A straightforward anonymous function checking the equality. Try it online!


2

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


3

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 ...


2

R, 425 bytes p=function(m,t){ d=dim(m);r=d[1];c=d[2] l=apply(matrix(c(seq(l=r-1),rep(0,r+c-2),seq(l=c-1)),,2),1,function(f){n=array(0,pmax(g<-(f-1)%%d+1,h<-(d-f-1)%%d+1)) `if`(f,n[1:g[1],]<-m[g[1]:1,],n[,1:g[2]]<-m[,g[2]:1]) n[1:h[1],1:h[2]]=n[1:h[1],1:h[2]]+m[(i=g%%d+1)[1]:r,i[2]:c] if(max(n)<=t)cbind(c(T=f[1],L=f[2]),p(n,t))}) if(!is.null(l)...


1

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, ...


3

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 ...


5

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!


9

R, 23 bytes function(m)!any(m+t(m)) Try it online! Checks whether there are any non-zero elements in \$M+M^T\$.


7

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.


1

Ruby, 40 bytes ->a{a==a.transpose.map{|r|r.map{|c|-c}}} Try it online!


5

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


1

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


4

Pyth, 5 bytes qC_MM Try it online! Explanation qC_MM q : Check if input equals C : Transpose of _MM : Negated input


6

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


3

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


2

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


10

Python 2, 45 bytes lambda A:A==[[-x for x in R]for R in zip(*A)] Try it online!


4

Io, 69 bytes method(x,x map(i,v,v 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(x, // Input x. x map(i,v, // Map all x's rows (index i): v map(I,V, // Foreach the ...


13

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 ⍉.


4

Jelly, 3 bytes Z⁼N Try it online! Explanation Z Transposition ⁼ Equals N Negative


4

05AB1E (legacy), 3 bytes ø(Q Try it online! Explanation ø The input transposed, ( Negated, Q Is equal to the orginal input


2

Python 2, 46 bytes lambda l,k:[l]+zip(*[iter(([1]+[0]*k)*~-k)]*k) Try it online! Takes input as a tuple l and number of terms k, and outputs with both rows and columns reversed. The idea is to use the zip/iter trick to create an identity-like matrix by splitting a repeating list into chunks. The is similar to my solution to construct the identity matrix ...


1

R, 34 bytes function(r,k)rbind(diag(k)[-1,],r) Try it online! Takes the length as well; the TIO link has a k=length(r) argument so you can just input the recurrence relation.


2

APL (Dyalog Unicode), 10 bytes ⊢⍪¯1↓⍋∘.=⍋ Try it online! Tacit function taking the list of coefficients on the right. Explanation ⊢⍪¯1↓⍋∘.=⍋ ⍋ ⍋ ⍝ Grade up to obtain a list of k distinct values ∘.= ⍝ Outer product with operation `equals` (identity matrix) ¯1↓ ⍝ Drop the last row ⊢⍪ ⍝ Prepend the list of coefficients


2

Io, 56 bytes method(a,a map(i,v,if(i<1,a,a map(I,v,if(I==i-1,1,0))))) Try it online! Explanation method(a, ) // Input an array. a map(i,v, ) // Map. i = index, v = value if(i<1, ) // If the indice is 0, ...


0

C (gcc), 90 89 80 bytes Saved 9 bytes thanks to ceilingcat!!! i;j;f(a,k)int*a;{for(i=k;i--;puts(""))for(j=k;j--;)printf("%d ",i?i-1==j:a[j]);} Try it online! Inputs an array of coefficients (in forward order) along with its length. Prints a matrix that represents the recurrence relation.


5

JavaScript (ES6), 36 bytes a=>a.map((_,i)=>i?a.map(_=>+!--i):a) Try it online! Returns: $$ \begin{bmatrix} a_1 & a_2 & a_3 & \cdots & a_{k-1} & a_k \\ 1 & 0 & 0 & \cdots & 0 & 0 \\ 0 & 1 & 0 & \cdots & 0 & 0 \\ \vdots & \vdots & \vdots & & \vdots & \vdots \\ 0 & 0 &...


0

Jelly, 8 bytes W;J⁼þṖ$$ A monadic Link accepting a list which yields a list of lists in the reversed rows & columns permutation. Try it online! How? W;J⁼þṖ$$ - Link: list A e.g. [5,2,5,4] W - wrap (A) in a list [[5,2,5,4]] $ - last two links as a monad - f(A): J - range of length (A) ...


8

MATL, 8 7 bytes -1 byte thanks to @LuisMendo Xy4LY)i Takes the coefficients in reverse order Try it online! Explanation Xy4LY)i Xy : Create an identity matrix of size equal to input 4LY) : Remove the first row i : Insert input onto the stack


1

Charcoal, 12 bytes IEθ⎇κEθ⁼⊖κμθ Try it online! Link is to verbose version of code. Produces the "reversed in both directions" output. Works by replacing the first row of a shifted identity matrix with the input. Explanation: Eθ Map over input list ⎇κ If this is not the first row then Eθ Map over input list ...


6

J, 10 8 bytes Returns the matrix reversed in both dimensions. ,}:@=@/: Try it online! How it works ,}:@=@/: input: 3 _1 19 /: indices that sort: 1 0 2 (just to get k different numbers) =@ self-classify: 1 0 0 0 1 0 0 0 1 }:@ drop last row: ...


1

Python 3, 60 58 bytes -2 bytes thanks to @JonathanAllan lambda a,k:[map(i.__eq__,range(k))for i in range(1,k)]+[a] Try it online! Takes the coefficients in reverse order


3

APL (Dyalog Unicode), 37 35 bytes {⌽∘⍉@⍺⊢⍵}/⌽(⊂,{(4|⎕)/,⊢∘⊂⌺2 2⍳⍴⍵})⎕ Try it online! Switched to a more straightforward method after I realized @ also accepts a matrix of coordinates. Then we don't need to fiddle with the coordinate order; we extract the submatrix coordinates with ⊢∘⊂⌺2 2, and just rotate them directly using ⌽∘⍉. APL (Dyalog Unicode), 37 ...


2

APL (Dyalog Unicode), 70 66 bytes D←⊢-1∘⌽ +/,{16=×/|D⍵/⍨×⍵×D⍵}¨(⍉1↓⌽)⍣4×(⊢-{⊂(,⍵)[8(-,⊢)3,⍳3]}⌺3 3)⎕ Try it online! -4 bytes thanks to @Bubbler and @ngn Full program that requires ⎕IO←0 ⍝ Helper function D: cyclic differences of a list D←⊢-1∘⌽ ⊢ ⍝ Each element - ⍝ Subtract 1∘⌽ ⍝ The one after ⍝ Main code +/,{16=×/|D⍵/⍨×⍵×D⍵}¨¯2 ¯2↓1⊖1⌽×(⊢-{⊂...


0

Charcoal, 81 bytes FθFι⊞υκUMθκ≔LθηFυF⁺⁺⪪EυληEθ⁺λ×θη⟦×θ⊕η×⊕θ⊖η⟧«≔Eκ§υλι¿⁼¹№ι⁰§≔υ§κ⌕ι⁰⁻÷×⊕×ηηη²Σι»I⪪υη Try it online! Link is to verbose version of code. Uses zero as the "blank" marker. Explanation: FθFι⊞υκ Flatten the input array. UMθκ Replace the original array with a range from 0 to n-1. ≔Lθη Also the length of the array is used a lot so ...


1

Jelly, 25 bytes ZṚ,⁸;Jị"$€$§FE ²Œ!ṁ€ÇƇ=ÐṀ A full program taking n and a list-of-lists-formatted representation of the incomplete square which prints the result in the same format. Try it online! - too slow for TIO's 60s limit ...so, Try a limited space one which only considers the first 150K permutations - three magic squares two of which match at two ...


3

R, 169 180 142 135 bytes Edits: +11 bytes to rotate the magic square back to it's original orientation, -38 bytes by wrapping "replace-only-missing-element" into a function, -7 bytes by various golf obfuscations function(m,n){while(F%%4|sum(!m)){m[n:1,]=apply(m,1,f<-function(v){if(sum(!v)<2)v[!v]=(n^3+n)/2-sum(v);v}) m[d]=f(m[d<-!0:n]) F=...


4

JavaScript (ES7),  143 142  140 bytes Expects (n)(m), where unknown cells in m are filled with 0's. n=>g=m=>[0,1,2,3].some(d=>m.some((r,i)=>m.map((R,j)=>t^(t-=(v=d?R:r)[x=[j,i,j,n+~j][d]])||(e--,X=x,V=v),e=1,t=n**3+n>>1)&&!e))?g(m,V[X]=t):m Try it online! Commented n => // outer function taking n g ...


4

APL (Dyalog Unicode), 60 bytes {(⍵,m+.×1+⍺*2)⌹(∘.(×⊢×=)⍨⍵)⍪2×m←(⍪↑c(⌽c))⍪(⊢⍪⍴⍴⍉)⍺/c←∘.=⍨⍳⍺} Try it online! Not likely the shortest approach, but anyway here is one with Matrix Divide ⌹, a.k.a. Solve Linear Equation. This works because all the cells are uniquely determined by the horizontal/vertical/diagonal sums when joined with the givens. ⌹ has no ...


2

Wolfram Language (Mathematica), 100 bytes #/.Solve[Tr/@Flatten[{#,Thread@#,{(d=Diagonal)@#,d@Reverse@#}},1]==Table[(l^3+l)/2,2(l=Tr[1^#])+2]]& Try it online!


6

JavaScript, 559 551 bytes Fast and methodical. B=Boolean,f=((e,r)=>(v=r*((r**2+1)/2),e.forEach(e=>e.filter(B).length==r-1?e[e.findIndex(e=>!e)]=v-e.reduce((e,f)=>!(e+=f)||e):0),e[0].reduce((f,l,n)=>!(f[0].push(e[n][n])+f[1].push(e[n][r-1-n]))||f,[[],[]]).forEach((f,l)=>{f.filter(B).length==r-1&&(z=f.findIndex(e=>!e),e[z][l?r-1-z:...


9

MATL, 36 bytes `nZ@[]etGg)GXz-yt!hs&ytXdwPXdhsh&-ha The input is an \$ n \times n\$ matrix, with \$0\$ for the unknown numbers. The code keeps generating random \$ n \times n\$ matrices formed by the numbers \$1, \dots, n^2\$ until one such matrix meets the required conditions. This procedure is guaranteed to finish with probability one. This is a ...


2

05AB1E, 43 41 30 bytes -2 bytes by replacing Dgt with ¹ to get first input back -11 bytes thanks to Kevin Cruijssen! nLœʒ¹ôD©ø®Å\®Å/)O˜Ë}ʒøε¬_sË~}P Try it online! Takes input as (n, flattened square), where zeros represent blanks, like 3 [4,9,2,3,0,0,0,0,0] Works by generating all permutations of the numbers from 1 to n2, filtering to only keep those that ...


Top 50 recent answers are included