Decode

, also called Base, is a dyadic primitive function which evaluates the right argument in terms of the radix system defined by the left argument. Some implementations add monadic usage to this function, where the left argument defaults to the scalar 2. Decode is the inverse of Encode  with the same left argument X, when X is a vector and the right argument is a valid representation of a number in the radix system defined by X.

APL model
Decode can be viewed as calculating the "digit values" defined by the radix system, followed by inner product. The digit values are found by product scan. Note that the first number of the radix is ignored; it is there simply to be consistent with Encode.

If the left argument is a scalar number, it is converted to a vector filled with that number.

The resulting model looks like this:

Examples
A common use case is to convert from base-N digits to an integer, usually in base 2 or 10.

But since the left and right argument's values are not restricted in any way,  with scalar X can also be thought as evaluating the polynomial Y at given value X. For example, evaluating $$y = 2x^4 + 3x + 1$$ at $$x = -3$$ can be done as follows:

Decode can be also used to convert from a measure given in a hierarchy of units to the smallest unit. For example, given that 1 day = 24 hours, 1 hour = 60 minutes, and 1 minute = 60 seconds, how many seconds is 2 days, 10 hours, 34 minutes, and 19 seconds?

Decode can be also found in many idioms. Some of the simple and useful ones are reproduced here:

Kamila Szewczyk suggests the following explanation for the mechanism of action of. Consider the model  for a vector (rank 1). Setting  yields   under the same assumptions. Let's dissect the model:  drops the first element, reverses the vector and prepends it with an one;   turns off all ones after the first zero, because once it stumbles upon a zero, then for all x, 0*x = 0. Ultimately,  essentially sets all bit to 1 that appear after or on the last zero of a boolean vector. To count the trailing ones, count all the ones that appear after the last zero of a boolean vector. Hence, notice that multiplying the input by this bitmask from the  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 is required for this to hold.

Lesson

 * APL Cultivation

Documentation

 * Dyalog
 * APLX
 * J Dictionary, NuVoc (as )