26

MATL, 2 bytes Xy A translation of my Octave answer. Try it online. A 4 byte version with no built-ins (thanks to Luis Mendo): :t!= : take input n and a generate row array [1,2,...n] t duplicate ! zip = thread compare over the result


25

Jelly, 15 bytes LŒ!ðŒcIṠ;ị"Pð€S Try it online! How it works LŒ!ðŒcIṠ;ị"Pð€S input L length Œ! all_permutations ð ð€ for each permutation: Œc take all unordered pairs I calculate the difference between the two integers of each pair Ṡ ...


20

TI-BASIC, 2 bytes identity(Ans Fun fact: The shortest way to get a list {N,N} is dim(identity(N. Here's the shortest way without the builtin, in 8 bytes: randM(Ans,Ans)^0 randM( creates a random matrix with entries all integers between -9 and 9 inclusive (that sounds oddly specific because it is). We then take this matrix to the 0th power.


19

Julia, 9 3 bytes eye This is just a built-in function that accepts an integer n and returns an nxn Array{Float64,2} (i.e. a 2D array). Call it like eye(n). Note that submissions of this form are acceptable per this policy.


18

MATL, 54 bytes th3LZ)t,3:q&XdpswP]w-lw/GtY*tXdsGXdsUw-IXy*2/+GtXds*-* Try it online! Just to keep it interesting, doesn't use the inbuilt matrix division or determinant functions to do it. Instead, computes the determinant using the Rule of Sarrus. And the adjugate (transposed cofactor matrix) using Cayley–Hamilton formula. $$ {\displaystyle \...


17

Python 3, 67 66 bytes, 53 bytes def f(p,v,q,w):p-=q;d=((v-w)/p).real*p;return v-d,w+d Try it online! -1 byte thanks to @ngn -13 bytes thanks to @Neil This function f takes four complex numbers as input and returns two complex numbers. The ungolfed version is shown in the following. Ungolfed def elastic_collision_complex(p1, v1, p2, v2): p12 = p1 - p2 ...


16

APL, 5 bytes ∘.=⍨⍳ This is a monadic function train that accepts an integer on the right and returns the identity matrix. Try it here


14

Octave, 10 4 bytes @eye Returns an anonymous function that takes a number n and returns the identity matrix.


14

Jelly, 14 13 12 bytes ;"s€2U×¥/ḅ-U Try it online! How it works ;"s€2U×¥/ḅ-U Main link. Input: [a1, a2, a3], [b1, b2, b3] ;" Concatenate each [x1, x2, x3] with itself. Yields [a1, a2, a3, a1, a2, a3], [b1, b2, b3, b1, b2, b3]. s€2 Split each array into pairs. Yields [[a1, a2], [a3, a1], [a2, a3]], [[b1, b2], [...


14

Haskell import Control.Parallel.Strategies import qualified Data.Vector.Unboxed as V import qualified Data.Vector as VB type Poly = V.Vector Int type Matrix = VB.Vector ( VB.Vector Poly ) constpoly :: Int -> Int -> Poly constpoly n c = V.generate (n+1) (\i -> if i==0 then c else 0) add :: Poly -> Poly -> Poly add = V.zipWith (+) ...


13

Mathematica, 10 bytes #2.#==#3#& Takes input like {vector, matrix, scalar} and returns a boolean.


13

Jelly, 6 bytes ẸÐfÆrE Try it online! How it works ẸÐfÆrE Main link. Argument: M (2D array) ẸÐf Filter by any, removing rows of zeroes. Ær Interpret each row as coefficients of a polynomial and solve it over the complex numbers. E Test if all results are equal. Precision Ær uses numerical methods, so its results are usually ...


13

APL(Dyalog Classic),1 byte ⌹ Try it online! if a flat lis is required this is 8 bytes ,∘⌹3 3∘⍴ Try it online!


12

R, 4 bytes diag When given a matrix, diag returns the diagonal of the matrix. However, when given an integer n, diag(n) returns the identity matrix. Try it online


12

Python 2, 42 bytes lambda n:zip(*[iter(([1]+[0]*n)*n)]*n)[:n] An anonymous function, produces output like [(1, 0, 0), (0, 1, 0), (0, 0, 1)], First, creates the list ([1]+[0]*n)*n, which for n=3 looks like [1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0] Using the zip/iter trick zip(*[iter(_)]*n to make groups of n gives [(1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 0)] ...


12

R, 3 bytes Trivial Solution det Try it online! R, 94 92 89 bytes re-implemented solution outgolfed by Jarko Dubbeldam d=function(m)"if"(x<-nrow(m),m[,1]%*%sapply(1:x,function(y)(-1)^y*-d(m[-y,-1,drop=F])),1) Try it online! Recursively uses expansion by minors down the first column of the matrix. f <- function(m){ x <- nrow(m) ...


12

Haskell, 576 554 532 507 bytes No built-ins! import Data.Complex s=sum l=length m=magnitude i=fromIntegral (&)=zip t=zipWith (x!a)b=x*a+b a#b=[[s$t(*)x y|y<-foldr(t(:))([]<$b)b]|x<-a] f a|let c=[1..l a];g(u,d)k|m<-[t(+)a b|(a,b)<-a#u&[[s[d|x==y]|y<-c]|x<-c]]=(m,-s[s[b|(n,b)<-c&a,n==m]|(a,m)<-a#m&c]/i k)=snd<$&...


11

LISP, 128 122 bytes Hi! This is my code: (defmacro D(x y)`(list(*(cadr,x)(caddr,y))(*(caddr,x)(car,y))(*(car,x)(cadr,y))))(defun c(a b)(mapcar #'- (D a b)(D b a))) I know that it isn't the shortest solution, but nobody has provided one in Lisp, until now :) Copy and paste the following code here to try it! (defmacro D(x y)`(list(*(cadr,x)(caddr,y))(*(...


11

MATL, 7 bytes *i2GY*= Inputs in order: l,v,A. Explanation: * % implicitly get l and v, multiply. i % get A 2G % get second input, i.e., v again Y* % perform matrix multiplication = % test equality of both multiplications Surprisingly long answer, if you ask me, mostly because I needed a way to get all the input correctly. I do not think that less than 5 ...


11

Jelly, 5 bytes æ.⁵⁼× This is a triadic, full program. Try it online! How it works æ.⁵⁼× Main link Left argument: v (eigenvector) Right argument: λ (eigenvalue) Third argument: A (matrix) ⁵ Third; yield A. æ. Take the dot product of v and A, yielding Av. × Multiply v and λ component by component, yielding λv. ⁼ ...


11

SageMath, 3 bytes 5 bytes saved thanks to @Mego fcp Try it online! Takes a Matrix as input. fcp stands for factorization of the characteristic polynomial, which is shorter than the normal builtin charpoly.


11

JavaScript (Node.js), 90 88 bytes (m,n,o,p,q,r,s,t,u=(q-=m)*q+(r-=n)*r,v=o*q+p*r-s*q-t*r)=>[o-(q*=v/u),p-(v*=r/u),s+q,t+v] Try it online! Link includes test suite. Explanation: q,r are repurposed as the difference vector between the centres, and u is the square of its length. v is the difference in the dot products of o,p and s,t with q,r, so v/u is the ...


10

Jelly, 4 bytes R=€R Doesn't use a built-in. Try it online! How it works R=€R Main link. Input: n R Range; yield [1, ..., n]. R Range; yield [1, ..., n]. =€ Compare each. This compares each element of the left list with the right list, so it yields [1 = [1, ..., n], ..., n = [1, ..., n]], where comparison is ...


10

J, 4 bytes =@i. This is a function that takes an integer and returns the matrix.


10

Jelly, 9 bytes Zṙ"JC$µ2¡ Try it online! The coordinates are as in the answer. Explanation µ2¡ Twice: Z Transpose, then ṙ" Rotate rows left by JC$ 0, -1, -2, -3, …, 1-n units. This wrap-shears the matrix in one direction, then the other.


10

Jelly, 16 15 12 10 bytes Ḣ×Zß-Ƥ$Ṛḅ- Uses Laplace expansion. Thanks to @miles for golfing off 3 5 bytes! Try it online! How it works Ḣ×Zß-Ƥ$Ṛḅ- Main link. Argument: M (matrix / 2D array) Ḣ Head; pop and yield the first row of M. $ Combine the two links to the left into a monadic chain. Z Zip/transpose the matrix (M without ...


9

Python 2, 108 89 87 86 bytes x=y=0 for m in map(int,raw_input()):x+=m*y and(m-y)%3*3/2;y^=m print"--i"[~x%4::2]+`y` (Thanks to @grc and @xnor for the help) Explanation Let's split up the coefficient and the base matrix. If we focus on the base matrix only, we get this multiplication table (e.g. 13 is -i2, so we put 2): 0123 0 0123 1 1032 2 2301 3 ...


9

Haskell, 43 37 bytes f n=[[0^abs(x-y)|y<-[1..n]]|x<-[1..n]] Pretty straightforward, though I think one can do better (without a language that already has this function built in, as many have done). Edit: dropped some bytes thanks to Ørjan Johansen


9

J, 27 14 bytes 2|.v~-v=.*2&|. This is a dyadic verb that accepts arrays on the left and right and returns their cross product. Explanation: *2&|. NB. Dyadic verb: Left input * twice-rotated right input v=. NB. Locally assign to v v~- NB. Commute arguments, negate left 2|. NB. Left rotate ...


9

Dyalog APL, 12 bytes 2⌽p⍨-p←⊣×2⌽⊢ Based on @AlexA.'s J answer and (coincidentally) equivalent to @randomra's improvement in that answer's comment section. Try it online on TryAPL. How it works 2⌽p⍨-p←⊣×2⌽⊢ Dyadic function. Left argument: a = [a1, a2, a3]. Right argument: b = [b1, b2, b3]. 2⌽⊢ Rotate b 2 units to the left. ...


Only top voted, non community-wiki answers of a minimum length are eligible