Decode: Difference between revisions

Jump to navigation Jump to search
1,389 bytes added ,  13:40, 3 October 2023
no edit summary
m (Text replacement - "</source>" to "</syntaxhighlight>")
No edit summary
Line 78: Line 78:
3
3
</syntaxhighlight>
</syntaxhighlight>
Kamila Szewczyk suggests the following explanation for the mechanism of action of <syntaxhighlight lang=apl inline>⊥⍨</syntaxhighlight>. Consider the model <syntaxhighlight lang=apl inline>⊥ ≡ {⍵+.×⌽×\1,⌽1↓(≢⍵)⍴⍺}</syntaxhighlight> for a vector (rank 1) <syntaxhighlight lang=apl inline>⍺</syntaxhighlight>. Setting <syntaxhighlight lang=apl inline>⍺≡⍵</syntaxhighlight> yields <syntaxhighlight lang=apl inline>⊥⍨ ≡ {⍵+.×⌽×\1,⌽1↓⍵}</syntaxhighlight> under the same assumptions. Let's dissect the model: <syntaxhighlight lang=apl inline>1,⌽1↓⍵</syntaxhighlight> drops the first element, reverses the vector and prepends it with an one; <syntaxhighlight lang=apl inline>×\</syntaxhighlight> turns off all ones after the first zero, because once you it stumbles upon a zero, then for all x, 0*x = 0. Ultimately, <syntaxhighlight lang=apl inline>⌽×\1,⌽1↓⍵</syntaxhighlight> essentially sets all bit to 1 that appear after or on the last zero of a boolean vector. To count the trailing ones, one counts all the ones that appear after the last zero of a boolean vector. Hence, notice that multiplying the input by this bitmask from the <syntaxhighlight lang=apl inline>⌽×\1,⌽1↓⍵</syntaxhighlight> part will return a vector where ones correspond to the trailing ones, so the sum in the inner product counts them all.


Decode has an important property with array [[index]]: Given an arbitrary array A with [[shape]] S, the index of an [[element]] in A can be decoded with S to obtain the corresponding index in the [[ravel]] of A. Decoding all indices given by [[Index Generator]] shows the ravel order of the elements of an array with the given shape. Note that [[index origin]] 0 (<syntaxhighlight lang=apl inline>⎕IO←0</syntaxhighlight>) is required for this to hold.
Decode has an important property with array [[index]]: Given an arbitrary array A with [[shape]] S, the index of an [[element]] in A can be decoded with S to obtain the corresponding index in the [[ravel]] of A. Decoding all indices given by [[Index Generator]] shows the ravel order of the elements of an array with the given shape. Note that [[index origin]] 0 (<syntaxhighlight lang=apl inline>⎕IO←0</syntaxhighlight>) is required for this to hold.

Navigation menu