Guest User

Untitled

a guest
Oct 20th, 2025
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. What does (2#x)#1,x#0 do in q ?
  2.  
  3. 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.
  4.  
  5. Let’s break it down step-by-step.
  6. 1. Understanding the components
  7.  
  8. # is the take function when used in a binary context:
  9. a#b takes a elements from b (or repeats b if a is larger than the count of b).
  10. x is assumed to be an integer (the desired size of the identity matrix).
  11.  
  12. 2. Inner part: 1,x#0
  13.  
  14. x#0 creates a list of x zeros.
  15. Example: if x=3, then 3#0 is 0 0 0.
  16.  
  17. 1,x#0 prepends a 1 to that list of zeros.
  18. Example: 1,3#0 gives 1 0 0 0.
  19.  
  20. So 1,x#0 is a list of length x+1, starting with 1 followed by x zeros.
  21. 3. Outer part: (2#x)#
  22.  
  23. 2#x creates a list of two xs.
  24. Example: 2#3 is 3 3.
  25.  
  26. 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.
  27.  
  28. 4. How the repetition works
  29.  
  30. The list 1 0 0 0 is:
  31. First row: take first 3 elements → 1 0 0
  32. 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)
  33. Wait—let’s check carefully because this is the key to the identity matrix.
  34.  
  35. Actually, let’s trace for x=3:
  36.  
  37. List: 1 0 0 0 (length 4)
  38. We are taking 3×3=9 elements cyclically:
  39.  
  40. Row-major order:
  41.  
  42. Row 1: elements 1–3: 1 0 0
  43. Row 2: elements 4, then wrap to 1, 2: 0 1 0
  44. 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.
  45.  
  46. Better to check actual q output:
  47. q
  48.  
  49. q) x:3
  50. q) 1,x#0
  51. 1 0 0 0
  52. q) (2#x)#1,x#0
  53. 1 0 0
  54. 0 1 0
  55. 0 0 1
  56.  
  57. Yes! It works. Let’s see the data fill pattern:
  58.  
  59. We have list L = 1 0 0 0 (length 4).
  60.  
  61. Reshape to 3×3 takes items in order:
  62.  
  63. Positions in L (0-based index for reasoning):
  64. 0: 1
  65. 1: 0
  66. 2: 0
  67. 3: 0
  68. 4 (wrap 0): 1
  69. 5 (wrap 1): 0
  70. 6 (wrap 2): 0
  71. 7 (wrap 3): 0
  72. 8 (wrap 0): 1
  73.  
  74. That doesn’t seem to align. Let’s do it step by step:
  75.  
  76. Index mapping for 3×3:
  77.  
  78. Element (i,j) gets index = 3*i + j in the flat result, taken from L cyclically.
  79.  
  80. Flat result length 9:
  81. Take from L repeatedly:
  82. 1(0) 0(1) 0(2)
  83. 0(3) 1(4→0) 0(5→1)
  84. 0(6→2) 0(7→3) 1(8→0)
  85.  
  86. So indeed:
  87. Row0: 1 0 0
  88. Row1: 0 1 0
  89. Row2: 0 0 1
  90.  
  91. Yes! The wrap-around causes the diagonal.
  92. 5. General case
  93.  
  94. 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).
  95.  
  96. 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.
  97. 6. Summary
  98.  
  99. (2#x)#1,x#0 creates an x-by-x identity matrix.
  100.  
  101. Example:
  102. q
  103.  
  104. q) x:4
  105. q) (2#x)#1,x#0
  106. 1 0 0 0
  107. 0 1 0 0
  108. 0 0 1 0
  109. 0 0 0 1
  110.  
Advertisement
Add Comment
Please, Sign In to add comment