New answers tagged

3

Brachylog, 23 bytes {ḍ\⟨+ᵐ↰₁ᵐ-ᵐ⟩c/₂ᵐ|}ᵐ\↰₁ᵐ Input: A list of columns Output: A list of rows How: It uses the Fast Hadamard Transformation. AFAIK Brachylog doesn't have matrix multiplication, nor binary operations so I doubt using the "evil numbers technique" will help shorten the code. {ḍ\⟨+ᵐ↰₁ᵐ-ᵐ⟩c/₂ᵐ|}ᵐ\↰₁ᵐ # {ḍ\⟨+ᵐ↰₁ᵐ-ᵐ⟩c/₂ᵐ|}ᵐ # Fast ...


1

Jelly, 18 bytes J’&þ`B§-*ðæ×⁺@÷L$⁺ A monadic Link accepting a list of lists of integers (any numbers really), the square input matrix of side \$2^n\$, which yields a list of lists of floats. Try it online! How? It feels like one should be able to outer-product the dot-product of the binary representations of the 0-indexed coordinates, however to do ...


1

R, 80 78 74 bytes function(M){for(i in 1:log2(nrow(M)))T=T%x%matrix(1-2*!3:0,2)/2 T%*%M%*%T} Try it online! Calculates \$\frac{1}{2^n}H_nX\frac{1}{2^n}H_n\$; constructs \$\frac{1}{2^n}H_n\$ by Kronecker product %x%. Looks like I can still golf, recalling that 1-2*!3:0 is shorter than c(1,1,1,-1) as I realized in the comments to this answer.


4

Octave, 93 79 bytes @(x)(r=f(f=@(f)@(x){@()[x=f(f)(x-1) x;x -x],1}{1+~x}())(log2(r=rows(x)))/r)*x*r This computes \$\frac{1}{2^n}H_nX\frac{1}{2^n}H_n\$ Try it online! Octave, 47 33 bytes (uses builtin) @(x)(r=hadamard(r=rows(x))/r)*x*r Try it online!


7

MATL, 12 bytes &nKYLw/Gy,Y* Try it online! Or verify all test cases. Explanation Consider input [2,3; 2,5] as an example. &n % Input (implicit). Push number of rows and number of columns % STACK: 2, 2 KYL % Hadamard matrix of specified size % STACK: 2, [1 1; 1 -1] w/ % Swap, divide element-wise % STACK: [0.5 0.5; ...


11

Haskell, 111 102 97 bytes import Data.Matrix t x|Right m<-inverse$h$nrows x=m*x*m h 1=1 h i|m<-h$div i 2=m<->m<|>(m<->(-m)) This uses the Data.Matrix module. The input matrix has to be of type Matrix Float or Matrix Double. TIO doesn't provide Data.Matrix, so no link. Edit: -9 bytes thanks to @ceased to turn counterclockwis, -5 ...


7

JavaScript (ES6), 114 bytes This version is golfed a bit further by embedding the function \$h\$ within the callback of the deepest loop. M=>(g=z=>M=M.map((r,y)=>r.map((_,x)=>r.map(v=>s+=(h=m=>m?-h(m&~-m):z?v:M[i-1][x])(i++&(z?x:y)),i=s=0)&&s/i)))(g()) Try it online! JavaScript (ES6),  161 ... 117  116 bytes M=...


3

Haskell, 45 bytes map.(foldr1(z(+)).).flip(z$map.(*)) z=zipWith Try it online! Takes arguments in reversed order.


0

Ruby, 59 bytes ->m,n{m.map{|r|n.transpose.map{|c|r.zip(c).sum{|i,j|i*j}}}} Try it online!


1

Haskell, 49 bytes z=zipWith m a=map(\x->foldr1(z(+))$z(map.(*))x a) Try it online! Input and output are lists of columns. Maps each column of the second matrix to that row, zipped with the columns of the first matrix and scaling each, summed as a vector. I feel that there's gotta be a nice way to make this pointfree and save a handful of bytes, but I'...


3

Brain-Flak (BrainHack), 382 375 337 bytes No comments! ( <( <>)<> ><>) ({}) {}{} {( )()<({}<{}<><>>{}<><>{}) ( <>) ({}<><( [ ]({}<{}( )<({}()<{}<><>>){} ><><{}<< ><> ( [ ]( <>)<>)>{}<>>>)){}>)> ( ){...


0

Zsh, 75 bytes <<<${(F)@} fa< o[< r+$ +{ si( ;]F i+) ==a &$} &s r[ ei p+ e1 a] t $ # s TIO: Normal Transposed The garbage below the main print is harmless, it prints an error when encountering a newline after fa< and exits. 86 bytes to remove that error. The transposed version is here. After printing it errors on seeing < ...


0

Cjam, 13 bytes qo ~ z ` Try it online! Transposed version: q~z` o Try it online! Input format The input format is the standard CJam array format: [[1 2] [3 4]] No crash version, 12 bytes The normal version crashes after printing the array. A version which doesn't crash would be: qo{ ~ z ` }; Try it online! or transposed: q~z` o { }; Try it ...


1

Clean, 64 bytes //i$ //mp $ p=p //ot //rr //ta // n //Ss //tp //do //Ls //ie //b Try it online! //$////////// // ////////// import StdLib $p=transpose p Try it transposed!


1

Python 3, 91 bytes lambda a,u:[[[a[y][x]for x in t]for y in t]for t in[u,[u.index(i)for i in range(len(u))]]] Try it online! Takes parameters as a 2D and 1D list and returns a list containing two 2D lists B and C. I'm not sure if there's a cleaner way to do all the for-loops.


5

Haskell, 185 161 bytes t i = i af n m u a e i l p s d lu h=u ( h e a d a ) t h e n [ ] e l s e m a p h e a d a : t ( m a p t a i l a ) Try it online! Transposed: t a =if null(head a)then[]else map head a:t(map tail a) i u map h es= u id Try it online! No comments,...


2

Haskell, 153 144 bytes (thanks, Sriotchilism O'Zaic) f [ ]= [ ]; f( x: l) =( :) x l -- : z $ -- f i f -- [ p -- ] W -- i -- t -- h Try it online! Try it transposed!


1

Ruby, 35 bytes #-sz## ->hip{ hip } #{f #ht* #..h Try it online! Hip to be square! (Almost) Accepts ruby matrices as input (arrays of arrays)


2

JavaScript (Node.js), 62 bytes //m0()m ] //=](=a ) //>.a>p( ) //mm,m/aa //[ap./=[ p =>p Try it online! Improved with a different layout ///// ///// m=>m[ 0].map ((a,p )=>m. map//= (a=> a[p ]))


1

05AB1E, 3 bytes øø q Try it online. øq ø Try it transposed. Explanation: Unlike some of the other languages, newlines are simply ignored in 05AB1E, so I don't think a 2-byter is possible (although I'd love to be proven wrong). ø # Transpose the (implicit) input ø # Transpose it back q # Stop the program (and output the top of the stack ...


0

Wolfram Language (Mathematica), 13 bytes #&(*  *) \ Try it online! / Transposed


1

Clean, 91 bytes import StdEnv $a u=map(\l={{a.[i,j]\\j<-l}\\i<-l})[u,[k\\i<-[0..]&_<-u,j<-u&k<-[0..]|j==i]] Try it online! Defines $ :: {{a}} [Int] -> [{{a}}] (used with a = Int) taking an array of arrays and a list of zero-based indices, returning a list of arrays of arrays containing B and C.


9

C (gcc), 209 205 203 201 bytes Normal f(n,L,r,c)char**L;{for(c=0;0?L+ c:c<n;c+=puts(""))for(r=0;0?r :L[c][r];r++)putchar(L[ c ][ r ]);}/* \\\ \\ 1 [0][] \ \\\ \ \\ 1 <n \ \\\\\\ r+-c c+-r */ Try it online! Transposed f ( n , L , r , c ) c\ h\ a\ r * * L ; { f\ o\ r ( c = 0 ; 01 ...


7

Python 3, 51 bytes lambda\ a:a mz= ''' bi' ' dp' ' a(' * a \)''' Try it online! Based on Joel's approach. The input is a tuple of tuples, and the output is a zip (converted to a tuple over TIO for better visibility). Transposed: lambda \ a:zip(*a) ma=''' ' b ' d ' ' a ' \ ''' Try it online!


0

Python 3, 118 95 bytes Input and result are lists of tuples. f=lambda i:i;""" = " l " a m b d a i : [ * z i p ( * i ) ] ; " " """ Try it online!


26

Python 3 + numpy, 45 bytes lambda\ a:a ma= """ b. " dT" " a " \ """ Try it online! Thanks to @EriktheOutgolfer pointing out a bug of the previous version Transposed: lambda\ a:a.T ma= """ b " d " " a " \ """ Try it online! The functions take a numpy matrix as input and output a numpy matrix. The solution does not rely on comments ...


8

Haskell, 51 bytes This version is valid, but will not halt when given [] as input. f --(:[|,<zabf=]f --abx(y-i$] -- ):x) pf;x[:x y =y Try it online! Transposed, 75 bytes f--- ---y (a :b)= [x:y |(x ,y) <- zip a$f b]; f x = [ ] : f x Try it online! Haskell, 51 bytes This version is valid, but crashes with [] as input for the ...


5

Perl 6, 20 11 bytes #& *[ #Z ] Try it online! Transposed #*# &[Z] Try it online! Both functions work on list of lists. *[ ] is a WhateverCode returning the zen slice of its argument, effectively the identity function. &[Z] is the zip operator.


13

R, 5 4 bytes #t I Try it online! An R function, either the identity function I or the transpose function t when transposed. Footer on TIO shows output of both. Thanks to @RobinRyder for saving a byte!


2

Japt, 2 bytes ÕU Try it | Transposed


4

PHP (7.4), 114 86 70 bytes My first ever experience doing something like this in PHP, there must be a better way I cannot see! Input is an array of arrays like [[1,2],[3,4],[5,6]]. Normal: fn($a)=>$a/* nu / (l $l a, ). =. >. a$ ra r) a/ y* _ m a p (*/ Try it online! Transposed (space padded): fn($a)=>array_map( null,...$a)/* * ( ...


3

Charcoal, 19 bytes A¿⁰« ‖⁰¿ ↗⁰ ¿⁰ ⁰¿ « Try it online! Takes input as an array of strings. Explanation: A implicitly prints the explicit input, while ¿⁰ is a conditional, with 0 as the condition, which is therefore always false. « then starts a block of meaningless Charcoal code which never gets executed. (It might be possible to remove some of those bytes ...


15

Jelly, 2 bytes Input is a list of lists. Thanks to Luis Mendo and Nick Kennedy for improving the answer. ZZ Try it online! Try it transposed! The program transposes the input twice, returning the original input. The transposed version ignores the first line and only transposes once.


2

APL (Dyalog Extended), 12 bytesSBCS Full program. Prompts for \$u\$ and then \$A\$. Prints \$C\$ next to \$B\$, separated by two spaces ⌷∘⎕¨⍋¨⍛⍮⍨⍮⍨⎕ Try it online! ⎕ prompt for \$u\$; [3,1,2] ⍮⍨ juxtaposition-selfie; [[3,1,2],[3,1,2]] ⍋¨ permutation-inversion of each; [[2,3,1],[2,3,1]] ⍛ then ⍮⍨ juxtapose with itself [[[2,3,1],[2,3,1]],[[3,1,2],[3,1,2]]...


1

q, 26 bytes {Y:iasc y;(x[y;y];x[Y;Y])} iasc returns indexes to sort it's argument.


3

JavaScript (Node.js), 77 70 68 bytes a=>g=(u,v=[])=>[u.map((i,x)=>u.map(j=>a[i][j],v[i]=x)),v&&g(v,0)[0]] Try it online!


2

APL+WIN, 21 bytes Prompts for input of u followed by a. Outputs b immediately over the top of c with no separator: (a←⎕)[u;u←⎕]⋄a[⍋u;⍋u] Try it online! Courtesy of Dyalog Classic


3

J, 17 16 15 14 bytes -1 thanks to @Jonah ([{"1{)~(,:/:) Try it online!


1

C++ (gcc), 148 bytes #import<vector> #define q[o[i/z]*z+o[i%z]] using V=std::vector<int>;int f(V m,V o,V&r,V&R,int z){int i=z*z;for(r=V(i),R=V(i);i--;r[i]=m q)R q=m[i];} Try it online!


4

PowerShell, 78 73 71 bytes ($A,$u=$args)|%{$A[$u]|%{''+$_[$u]} $u=1..$u.count|%{$u.indexof($_-1)}} Try it online.


1

Jelly,  12 11  13 bytes +2 :( to fix cases when B = C ṭþ`œị¥@Ƭị@2,0 A dyadic Link accepting a list of lists, A (n by n), on the left and a list of the first n integers on the right, u, which yields a list of lists of lists, [B, C]. Try it online! How? ṭþ`œị¥@Ƭị@2,0 - Link: A, u Ƭ - collect up while the results are no longer unique,...


1

Perl 5, 79 bytes sub{$[=1;($A,$u)=@_;@$v[@$u]=(1..@$u);map{$x=$_;[map[@$_[@$x]],@$A[@$x]]}$u,$v} Try it online!


5

Python 3 with numpy, 51 45 bytes lambda m,p:[m[x][:,x]for x in(p,p.argsort())] Try it online! -6 bytes thanks to @xnor The function takes two arguments: a numpy matrix and a permutation vector having values from \$0\$ to \$n-1\$.


3

J, 19 bytes (]/:~"1/:)"_ 1],:/: Try it online! Main verb ]/:~"1/: The right most /: sorts the left arg (matrix) according to the order that would sort the right arg (specified order). This sorts rows. Now that result get sorted /:~"1 again according to the order specified ]. But this time we're sorting with rank 1, ie, we're sorting each row, which has ...


2

Kotlin, 213 bytes {a:List<List<Int>>,u:List<Int>->val s=u.size for(l in List(s){r->List(s){c->a[u[r]][u[c]]}})println(l.joinToString(" ")) for(l in List(s){r->List(s){c->a[u.indexOf(r)][u.indexOf(c)]}})println(l.joinToString(" "))} Try it online!


6

R, 42 bytes function(A,o)list(A[o,o],A[I<-order(o),I]) Try it online! Takes A as a matrix and 1-based indexes o.


3

Jelly, 13 bytes ŒJṁ⁸ịⱮ⁹,Ụ¤œị⁸ Try it online!


4

Wolfram Language (Mathematica), 30 bytes ii[[#,#]]&/@{#,Ordering@#}& Try it online! Input as f[A][u].


2

Charcoal, 24 bytes E⟦ηEη⌕ηκ⟧Eθ⪫E觧θ§ιμ§ιξ Try it online! Link is to verbose version of code. 0-indexed. Note: Trailing space. Explanation: η Input `u` E Map over elements ⌕ Index of κ Current index in η Input `u` η ...


5

Octave, 33 bytes @(A,u){A(u,u) A([~,v]=sort(u),v)} Try it online! Thanks to Luis for correcting an error and saving several bytes! Basic indexing works here for both tasks, by defining a vector \$ v \$ equal to the permutation that undoes \$ u \$. That is, if \$ u = ( 3, 1, 2 ) \$ then the first element of \$ v \$ is 2, since 1 is in the second position ...


Top 50 recent answers are included