Encode

, also called Represent or Antibase, is a dyadic primitive function which computes the representation of the right argument in the radix system defined by the left argument. Some implementations add monadic usage to this function, which computes the binary representation using as many bits as needed. Encode is the inverse of Decode  with the same left argument X, when X is a vector.

Concept
Encode works by finding the "digits" one by one from the rightmost digit. Let's consider  (convert 12345 minutes to weeks, days, hours, and minutes) as an example.

Collecting all the digits gives the desired result.

If the left argument has high rank, the vectors over the first axis act as independent radix systems.

Examples
A common use case is to convert an integer to base N, usually base 2 or 10. However, Encode does not know how many digits to produce, so it needs to be supplied as the shape of the left argument.

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

Another common usage of Encode is to simulate "divmod": a function which, given two integers, computes the quotient and remainder at once. The construct can also be used to extract the integer part and fractional part of a real number.

Encode has an important property with array index: Given an arbitrary array A with shape S, encoding a raveled index by S gives the original index in A. This can be used to generate all indices of a given array or array shape. Note that index origin 0 is required for this to hold.

Lesson

 * APL Cultivation

Documentation

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