Skip to content

Instantly share code, notes, and snippets.

@danielkrizian
Last active July 30, 2020 15:44
Show Gist options
  • Save danielkrizian/78383d3433a86a86b156a98f105a5c00 to your computer and use it in GitHub Desktop.
Save danielkrizian/78383d3433a86a86b156a98f105a5c00 to your computer and use it in GitHub Desktop.
mystery behind ./[matrix; ;+;1] @ i1,i2,i3 ...

mystery behind ./[matrix; ;+;1] @ i1,i2,i3

key concepts involved (layered in that order):

  1. matrix is two dimensional list
  2. indexing at depth: .[;] - see more https://code.kx.com/q4m3/6_Functions/#682-verb-dot
  3. dot apply : .[;;;] - e.g. https://code.kx.com/q4m3/6_Functions/#687-general-apply-for-dyadic-functions
  4. projection : f[x;y;z] is same as f[x;;z] y
  5. iteration : over (/) - e.g. https://code.kx.com/q/ref/adverbs/#over

indexing matrix at depth; as opposed to @ - indexing at first level:

.[10 10#til 100; (2 3; 4 7 9)]
24 27 29

step over list, applying “+” to two arguments previous result and the next element:

(+/)2 3 4
9

amending matrix at given depth indices - step 1

.[10 10#0;;+;1] (1 2 3;1 2 3 4)
0 0 0 0 0 0 0 0 0 0
0 1 1 1 1 0 0 0 0 0
0 1 1 1 1 0 0 0 0 0
0 1 1 1 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

amending matrix at given depth indices - step 2

.[10 10#0;;+;1] (2 3;3 4)
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 1 1 0 0 0 0 0
0 0 0 1 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

over just one step (first one)

./[10 10#0;;+;1] enlist (1 2 3;1 2 3 4)
0 0 0 0 0 0 0 0 0 0
0 1 1 1 1 0 0 0 0 0
0 1 1 1 1 0 0 0 0 0
0 1 1 1 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

list of two steps

((1 2 3;1 2 3 4);(2 3;3 4)) ~ enlist[(1 2 3;1 2 3 4)], enlist (2 3;3 4)
1b

over two steps

./[10 10#0;;+;1] ((1 2 3;1 2 3 4);(2 3;3 4))
0 0 0 0 0 0 0 0 0 0
0 1 1 1 1 0 0 0 0 0
0 1 1 2 2 0 0 0 0 0
0 1 1 2 2 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment