J's #:
(base)
can be used to compute the offset of an array element as it would appear in
memory. Why so? x #: y
is +/w*y
, where w
is formed as */\.}.x,1
—w
is precisely the strides of an array with dimensions x
.
See that this works:
dims=.2 3
ix=.1 2
(<ix) { (dims$i.6)
5
((|.ix) #. dims) { i.6
5
Recall the examples:
2 2 2 #. 1 0 1
5
24 60 60 #. 23 59 59
86399
The latter tallies seconds from hours-minutes-seconds.
Computing element offsets resembles forming a number from its multibase "digits," with indices corresponding to "digits," and dimensions corresponding to base.