# Tag Info

27

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

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

19

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.

18

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

15

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

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

14

Python 2, 42 bytes lambda n:zip(*[iter((+*n)*n)]*n)[:n] An anonymous function, produces output like [(1, 0, 0), (0, 1, 0), (0, 0, 1)], First, creates the list (+*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)] ...

14

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

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 (+) ...

14

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

13

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

13

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<$&... 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 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) ... 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 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 ...

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, 6 bytes œc2IFP œc2 gets all combinations without replacement of length 2. I computes the difference list of each of those pairs, yielding a list like [, , , ..., ]. We Flatten and take the Product. Try it here!

10

Mathematica, 30 bytes 1##&@@(#2-#&@@@#~Subsets~{2})& This is an anonymous function. Expanded by Mathematica, it is equivalent to (1 ##1 & ) @@ Apply[#2 - #1 & , Subsets[#1, {2}], {1}] &. 1##& is an equivalent for Times (thanks tips page), which is applied to each distinct pair of elements from the input list, generated by Subsets[...

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.

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

Python 2, 198 bytes a,b,c,d=input() H=(a+d)/2 D=(H*H-a*d+b*c)**.5 X,Y=H+D,H-D p,q,r,s=[[1,0,0,1],[b,X-a,b,Y-a],[X-d,c,Y-d,c]][2*(c!=0)or(b!=0)] A=abs V=A(A(p)+A(q)*1j) W=A(A(r)+A(s)*1j) print[X,Y],[[p/V,q/V],[r/W,s/W]] Input is a flat list of 4 complex numbers via STDIN, e.g. [0.0+0j, 0.4+0j, -0.1+0j, -0.4+0j] Note that Python uses j instead of i for ...

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