เอกลักษณ์เมทริกซ์ N มิติ


30

รับค่าจำนวนเต็มบวกnเอาท์พุทN"มิติ" เอกลักษณ์มิติซึ่งเป็นN^Nอาร์เรย์1ที่มีส่วนประกอบทั้งหมดของดัชนีเท่ากันและ0อย่างอื่น N^Nหมายถึง N-by-N-by-N-by-...

1 -> [1]

2 -> [[1,0],[0,1]]

3 -> [[[1,0,0],[0,0,0],[0,0,0]],[[0,0,0],[0,1,0],[0,0,0]],[[0,0,0],[0,0,0],[0,0,1]]]

4 -> [[[[1,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]],[[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]],[[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]],[[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]],[[[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]],[[0,0,0,0],[0,1,0,0],[0,0,0,0],[0,0,0,0]],[[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]],[[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]],[[[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]],[[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]],[[0,0,0,0],[0,0,0,0],[0,0,1,0],[0,0,0,0]],[[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]],[[[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]],[[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]],[[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]],[[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,1]]]]

ตัวอย่างเช่นถ้าaเป็น4มิติตัวตน "เมทริกซ์" จากนั้นรายการเท่านั้นที่มี1จะเป็นa[0][0][0][0], a[1][1][1][1], และa[2][2][2][2]a[3][3][3][3]

นี่คือรหัสกอล์ฟคำตอบที่สั้นที่สุดในการชนะไบต์ ช่องโหว่มาตรฐานใช้


1
ที่เกี่ยวข้อง , ที่เกี่ยวข้อง
Leun Nun

11
ที่นี่คำตอบของ MATL กับ builtin กำลังทำเพื่อคุณ ...
caird coinheringaahing

คำตอบ:



9

เยลลี่ขนาด 8 ไบต์

×=¥þ’¡`Ṡ

ลองออนไลน์!

Ooh ดูเหมือนว่าฉันจะชนะ @Dennis ในภาษาของเขาเองอีกครั้ง :-)

นี่คือฟังก์ชั่น 1 ข้อโต้แย้ง (เนื่องจากรูปแบบเอาต์พุตเริ่มต้นของเยลลี่สำหรับรายการที่ซ้อนกันนั้นมีความคลุมเครือเล็กน้อยซึ่งหมายความว่าเนื้อหาไม่ตรงตามข้อกำหนดในฐานะโปรแกรมเต็มรูปแบบ)

คำอธิบาย

×=¥þ’¡`Ṡ
     ¡    Repeatedly apply the following operation,
    ’     {input-1} times in total:
   þ        For each element of the current value {perhaps made into a range}
      `     and of {the range from 1 to the} {input}:
 =            Compare corresponding elements, giving 0 for equal or 1 for unequal
× ¥           then multiply by one of the elements
       Ṡ  then replace each element with its sign

เพื่อที่จะเข้าใจสิ่งนี้มันช่วยในการดูขั้นตอนกลาง สำหรับอินพุต 3 เราได้รับขั้นตอนกลางดังต่อไปนี้:

  1. [1,2,3](อินพุตทำในช่วงโดยปริยายโดยþ)
  2. [[1,0,0],[0,2,0],[0,0,3]](ทำตารางด้วย[1,2,3]เปรียบเทียบความเท่าเทียมกันเพื่อรับ[[1,0,0],[0,1,0],[0,0,1]]แล้วคูณด้วยหนึ่งในค่าที่เราเปรียบเทียบ)
  3. [[[1,0,0],[0,0,0],[0,0,0]],[[0,0,0],[0,2,0],[0,0,0]],[[0,0,0],[0,0,0],[0,0,3]]] (ความคิดเดียวกันอีกครั้ง)
  4. [[[1,0,0],[0,0,0],[0,0,0]],[[0,0,0],[0,1,0],[0,0,0]],[[0,0,0],[0,0,0],[0,0,1]]](แทนที่แต่ละองค์ประกอบด้วยเครื่องหมายโดยใช้)

โปรดสังเกตความจริงที่ว่าอินพุตเริ่มต้นจาก 1 มิติหมายความว่าเราต้องวนซ้ำ (อินพุท -1) เพื่อเพิ่มมิติ (อินพุท -1) เพื่อสร้างรายการอินพุตมิติ

Fun fact: this program contains five quicks in a row, ¥þ’¡`. (A quick is a modifier to a "link", or builtin, used to modify its behaviour or combine it with another link.)


+!, just because you beat Dennis in Jelly.
Zacharý

7

Mathematica, 30 bytes

Array[Boole@*Equal,#~Table~#]&

1
@FryAmTheEggman an integer parameter as the second argument to Table is a recent addition. Mathics still needs a singleton list there: tio.run/##y00sychMLv7/P83WsagosTLaKT8/…
Martin Ender

1
@FryAmTheEggman Looks like you need to change it to Array[Boole@*Equal,#~Table~{#}]& to work on Mathics. Older versions of Mathematica don't support an integer as the second argument to Table, and I guess Mathics is based off that.
ngenisis

1
@MartinEnder Pinch, poke, you owe me a Coke :)
ngenisis

6

APL (Dyalog), 10 bytes

1=≢∘∪¨⍳⍴⍨⎕

Try it online!

1= [is] 1 equal to

 the number

 of

 unique elements

¨ in each of

 the indices in an array with the dimensions of

⍴⍨ the self-reshape (N copies of N) of

 the input (N) [?]


5

Jelly, 9 bytes

ðṗE€ṁ+þ’¡

Try it online!

How it works

Achieving the task directly seems to be difficult (I haven't found a way), but contructing arrays of the same numbers and arrays of the same shape is quite easy.

ð makes the chain dyadic, and the integer input n serves as both left and right argument for the chain. It's possible to use a monadic chain instead, but the parsing rules for dyadic ones save three bytes here (two after accouting for ð).

The Cartesian power atom , with left and right argument equal to n, constructs the array of all vectors of length n that consist of elements of [1, ..., n], sorted lexicographically.

When n = 3, this yields

[[1, 1, 1], [1, 1, 2], [1, 1, 3], [1, 2, 1], [1, 2, 2], [1, 2, 3], [1, 3, 1], [1, 3, 2], [1, 3, 3], [2, 1, 1], [2, 1, 2], [2, 1, 3], [2, 2, 1], [2, 2, 2], [2, 2, 3], [2, 3, 1], [2, 3, 2], [2, 3, 3], [3, 1, 1], [3, 1, 2], [3, 1, 3], [3, 2, 1], [3, 2, 2], [3, 2, 3], [3, 3, 1], [3, 3, 2], [3, 3, 3]]

The equal each quicklink E€ tests the elements of all constructed vectors for equality.

When n = 3, we get

[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]

which are the elements of the 3-dimensional identity matrix, in a flat array.

The dyadic quicklink +þ’¡ is called with left argument and right argument n. The quick ¡ calls the decrement atom , which yields n-1, then calls the add table quicklink n-1 times.

Initially, the arguments of are both n. After each call, the right argument is replaced by the left one, and the left one is replaced by the return value of the call.

The table quick calls the add atom + for each elements of its left argument and each element of its right argument, constructing a table/matrix of the return value. The initial integer arguments n are promoted to the ranges [1, ... n].

When n = 3, after promotion but before the first iteration, both arguments are

[1, 2, 3]

Adding each integer in this array to each integer in this array yields

[[2, 3, 4], [3, 4, 5], [4, 5, 6]]

In the next invocation, we add each of these arrays to the integers in [1, 2, 3]. Addition vectorizes (adding an integer to an array adds it to each element), so we get

[[[3, 4, 5], [4, 5, 6], [5, 6, 7]],
 [[4, 5, 6], [5, 6, 7], [6, 7, 8]],
 [[5, 6, 7], [6, 7, 8], [7, 8, 9]]]

This array has the same shape as the 3-dimensional identity matrix, but not the correct elements.

Finally, the mold atom discards the integer entries of the result to the right and replaces them in order with the elements in the result to the left.


4

Python, 70 bytes

f=lambda n,l=[]:[f(n,l+[i])for i in(len(l)<n)*range(n)]or+(l==l[:1]*n)

Try it online!

A recursive solution. Thinking of the matrix as a list of matrices one dimension smaller, it iterates over that list to go down the tree. It remembers the indices picked in l, and when n indices have been picked, we assign a 1 or 0 depending on whether they are all the same.


Python 2, 73 bytes

n=input();r=0
exec'r=eval(`[r]*n`);'*n+('n-=1;r'+'[n]'*n+'=1;')*n
print r

Try it online!

An improvement on totallyhuman's method of making a matrix of zeroes and then assigning ones to the diagonal.


Python 2, 88 bytes

n=input()
t=tuple(range(n))
print eval('['*n+'+(i0'+'==i%d'*n%t+')'+'for i%d in t]'*n%t)

Try it online!

Some nonsense with eval, generating a nested list, and string-format substitution. The string to be evaluated looks like:

[[[+(i0==i0==i1==i2)for i0 in t]for i1 in t]for i2 in t]

4

Python 2 + NumPy, 80 72 70 bytes

Now tied with the top Python answer!

from numpy import*
n=input()
a=zeros((n,)*n)
a[[range(n)]*n]=1
print a

Try it online

Saved 8 bytes thanks to Andras Deak, and 2 by officialaimm


2
Make use of fancy indexing: a[[range(n)]*n]=1 instead of your exec.
Andras Deak

(There's actually fill_diagonal(a,1) for this purpose, but that's one byte longer)
Andras Deak

1
+1 for the pretty output. And, I don't see the use of the variable i, that should save 2 bytes.
officialaimm

3

Python 2, 99 93 90 bytes

Thanks to Rod for some even more help that got it working and also shaved 6 bytes off! -3 bytes thanks to xnor.

n=input()
r=eval(`eval('['*n+'0'+']*n'*n)`)
for i in range(n):exec'r'+`[i]`*n+'=1'
print r

Try it online!


1
def/return is never better than input/print (in the best scenario it's equal), you can also drop the () in ('[%d]'%i) reducing to 93 bytes
Rod

1
'[%d]'%i happens to be the string rep of [i].
xnor

2

JavaScript (ES6), 67 bytes

f=(n,d=n-1,i)=>[...Array(n)].map((_,j)=>d?f(n,d-1,j-i?n:j):j-i?0:1)

Explanation: i is used to keep track of whether the cell is on the main diagonal or not. Initially it's undefined, so on the first recursive call we always pass the first dimension, while on subsequent recursive calls we only pass it on if the current dimension index is equal to all previous dimensions, otherwise we pass an index of n which indicates that all of the recursive cells should be zero.


2

Brainfuck, 61 bytes

>,[->+>+<<]>[-<<+>>]>-[->+.-<<<<[->+>+<<]>[-<+>]>[->>.<<]>]+.

Ungolfed

The numbers after the angle brackets indicate the cell the head is over.

>,                   read n to 1
[->+>+<<]            move 1 to 2 and 3
>2[-<<+>>]>3         move 2 to 0 
                     (tape: n 0 0 n 0)
-[                   while cell 3 {
    -                  dec 3
    >4+.-<3            print \x1
    <<<0[->+>+<<]      move 0 to 1 and 2
    >1[-<+>]>2         move 1 to 0
                       (tape: 0 0 n rows_left 0)
    [                  while cell 2 {
        -                dec 2
        >>4.<<           print \x0
    ]>3                }
]                    }
+.                   print \x1

Try it online!

Input is a binary number. Output is a matrix stored in row-major order.


There are a few different output formats in the answers already, so I'm assuming that the only requirement is "some standard matrix representation". If it needs to be in a particular format, I'll modify the code accordingly.
Ray

i guess row-major in this particular case would look exactly like column-major
Octopus

@Octopus Actually, the program determines whether it should be in row-major or column-major order based on the language the Brainfuck interpreter is written in. Mine is written in C, so it naturally outputs the matrix in row-major order. However, if you used an interpreter written in Fortran or MATLAB, it would detect that and automatically switch to column major order. (If you used an interpreter written in Brainfuck itself, it resolves the ambiguity based on the language your text editor is written in.) :-)
Ray

2

R, 64 49 bytes

-15 bytes thanks to Jarko Dubbeldam

x=array(0,rep(n<-scan(),n));x[seq(1,n^n,l=n)]=1;x

Reads from stdin and returns an array, printing as matrices. seq generates a sequence evenly spaced from 1 to n^n with length l=n, which does the trick quite nicely to index where the 1s go.

Try it online!

old version:

n=scan();x=rep(0,n^n);x=array(x,rep(n,n));x[matrix(1:n,n,n)]=1;x

Reads n from stdin; returns an array, printing the results as matrices. I struggled with this for a while until I read the docs for [, which indicate that a matrix can be used to index an array, where each row of the matrix represents the set of indices. Neat!

Try it online!


array(0, rep(n,n) works, so you dont have to do rep. You can then also take n through array(0, rep(n<-scan(),n)).
JAD

Also, x[seq(1,n^n,l=n)]=1 is 1 byte shorter.
JAD

@JarkoDubbeldam thank you! Nice one.
Giuseppe


1

Python 3 + numpy, 81 77 bytes

from numpy import*
f=lambda n:all([a==range(n)for a in indices((n,)*n)],0)+0

I'm not entirely sure that the above fits all guidelines: it returns an ndarray with the given properties. I know anonymous functions are usually fine, but an interactive shell will actually print

>>> f(2)
array([[1, 0],
       [0, 1]])

If the array fluff makes the above invalid, I have to throw in a print() for something like 7 additional bytes.

Try it online!


1

Pyth, 14 bytes

ucGQtQms!t{d^U

Test suite

Explanation: ^U, which is implicitly ^UQQ, where Q is the input, calculates all possible Q element lists of the range 0 ... n-1. ms!t{d maps the ones with all elements equal to 1, and the rest to 0. This gives the flattened output

ucGQtQ executes the following, Q - 1 times: Chop the input into lists of size Q.



1

Common Lisp, 147 133 bytes

(defun i(n)(flet((m(x)(fill(make-list n)x)))(let((a(make-array(m n):initial-element 0)))(dotimes(i n)(incf(apply #'aref a(m i))))a)))

Try it online!

The usual super-lengthy lisp. Reduced 12 bytes thank to @ceilingcat!

Explanation:

(defun i (n)
  (flet ((m (x) (fill (make-list n) x)))            ; function to build a list of n values x
    (let ((a (make-array (m n) :initial-element 0))); build the array with all 0
      (dotimes (i n)                                ; for i from 0 to n-1
        (incf (apply #'aref a (m i))))              ; add 1 to a[i]..[i] 
      a)))                                          ; return the array

@ceilingcat, ops, there was a stupid error in the golfed version. Corrected, thanks!
Renzo

0

SOGL V0.12, 22 bytes

.^κ.H/ 0* 1.H≤Οčr.H{.n

Try it Here!
Leaves the output on the stack, viewable in the console. If the numbers in the output were allowed to be strings, then the r could be removed.
Just as a fun test of how SOGL does in challenges it was completely not made for :p

input: x
.^                      push  x^x
  κ                     subtract x    (x^x)-x
   .H/                  divide by x   ((x^x) - x)/x
       0*               get that many zeroes
          1             push "1"
           .H           push x-1
             ≤          pull the first item from the stack to the top
              Ο         encase (x-1 times the zeroes, separated, started and ended with 1s)
               č        chop to a char-array
                r       convert each character to a number
                 .H{    repeat x-1 times:
                    .n    in the top array, for each group of x contents, encase that in an array

โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.