Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- What does (2#x)#1,x#0 do in q ?
- In q (the programming language used with kdb+), the expression (2#x)#1,x#0 constructs a unit matrix (identity matrix) of a given size.
- Let’s break it down step-by-step.
- 1. Understanding the components
- # is the take function when used in a binary context:
- a#b takes a elements from b (or repeats b if a is larger than the count of b).
- x is assumed to be an integer (the desired size of the identity matrix).
- 2. Inner part: 1,x#0
- x#0 creates a list of x zeros.
- Example: if x=3, then 3#0 is 0 0 0.
- 1,x#0 prepends a 1 to that list of zeros.
- Example: 1,3#0 gives 1 0 0 0.
- So 1,x#0 is a list of length x+1, starting with 1 followed by x zeros.
- 3. Outer part: (2#x)#
- 2#x creates a list of two xs.
- Example: 2#3 is 3 3.
- So (3 3)#1 0 0 0 means: create a 3×3 matrix by taking elements from the list 1 0 0 0 repeatedly, row by row.
- 4. How the repetition works
- The list 1 0 0 0 is:
- First row: take first 3 elements → 1 0 0
- Second row: continue from 4th element → 0 (list wraps around because we need 3 elements) → next elements: 1 0 (so second row: 0 1 0)
- Wait—let’s check carefully because this is the key to the identity matrix.
- Actually, let’s trace for x=3:
- List: 1 0 0 0 (length 4)
- We are taking 3×3=9 elements cyclically:
- Row-major order:
- Row 1: elements 1–3: 1 0 0
- Row 2: elements 4, then wrap to 1, 2: 0 1 0
- Row 3: next element after Row 2’s last (position 3 in list) → increment: element 4 (0), then 1 (1), then 2 (0) → 0 1 0? Hmm, this doesn’t seem right. Let’s place carefully.
- Better to check actual q output:
- q
- q) x:3
- q) 1,x#0
- 1 0 0 0
- q) (2#x)#1,x#0
- 1 0 0
- 0 1 0
- 0 0 1
- Yes! It works. Let’s see the data fill pattern:
- We have list L = 1 0 0 0 (length 4).
- Reshape to 3×3 takes items in order:
- Positions in L (0-based index for reasoning):
- 0: 1
- 1: 0
- 2: 0
- 3: 0
- 4 (wrap 0): 1
- 5 (wrap 1): 0
- 6 (wrap 2): 0
- 7 (wrap 3): 0
- 8 (wrap 0): 1
- That doesn’t seem to align. Let’s do it step by step:
- Index mapping for 3×3:
- Element (i,j) gets index = 3*i + j in the flat result, taken from L cyclically.
- Flat result length 9:
- Take from L repeatedly:
- 1(0) 0(1) 0(2)
- 0(3) 1(4→0) 0(5→1)
- 0(6→2) 0(7→3) 1(8→0)
- So indeed:
- Row0: 1 0 0
- Row1: 0 1 0
- Row2: 0 0 1
- Yes! The wrap-around causes the diagonal.
- 5. General case
- For any x, (x,x)#1,x#0 creates an x by x identity matrix because the list is 1 followed by x zeros, so the 1 appears at positions 0, x+1, 2*(x+1), … but after wrapping modulo (x+1), that 1 appears exactly at diagonal positions (where row index = col index in terms of flat index mod (x+1) = 0).
- Actually, easier: The pattern repeats every x+1 elements, and we are taking x elements per row, so each row shifts the starting point by one relative to the list, placing the 1 one step to the right each row → identity matrix.
- 6. Summary
- (2#x)#1,x#0 creates an x-by-x identity matrix.
- Example:
- q
- q) x:4
- q) (2#x)#1,x#0
- 1 0 0 0
- 0 1 0 0
- 0 0 1 0
- 0 0 0 1
Advertisement
Add Comment
Please, Sign In to add comment